Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 17 | #define LOG_TAG "OpenGLRenderer" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 23 | #include <SkCanvas.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 24 | #include <SkTypeface.h> |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 25 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 27 | #include <utils/StopWatch.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 28 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 29 | #include "OpenGLRenderer.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 32 | namespace uirenderer { |
| 33 | |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | // Defines |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 38 | #define REQUIRED_TEXTURE_UNITS_COUNT 3 |
| 39 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 40 | // Generates simple and textured vertices |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 41 | #define FV(x, y, u, v) { { x, y }, { u, v } } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 42 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 43 | #define RAD_TO_DEG (180.0f / 3.14159265f) |
| 44 | #define MIN_ANGLE 0.001f |
| 45 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 46 | // TODO: This should be set in properties |
| 47 | #define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) |
| 48 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | // Globals |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 53 | // This array is never used directly but used as a memcpy source in the |
| 54 | // OpenGLRenderer constructor |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 55 | static const TextureVertex gMeshVertices[] = { |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 56 | FV(0.0f, 0.0f, 0.0f, 0.0f), |
| 57 | FV(1.0f, 0.0f, 1.0f, 0.0f), |
| 58 | FV(0.0f, 1.0f, 0.0f, 1.0f), |
| 59 | FV(1.0f, 1.0f, 1.0f, 1.0f) |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 60 | }; |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 61 | static const GLsizei gMeshStride = sizeof(TextureVertex); |
| 62 | static const GLsizei gMeshCount = 4; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 63 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 66 | */ |
| 67 | struct Blender { |
| 68 | SkXfermode::Mode mode; |
| 69 | GLenum src; |
| 70 | GLenum dst; |
| 71 | }; // struct Blender |
| 72 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 73 | // In this array, the index of each Blender equals the value of the first |
| 74 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 75 | static const Blender gBlends[] = { |
| 76 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 77 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 78 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 79 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 80 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 81 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 82 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 83 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 84 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 85 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 86 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 87 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 88 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 89 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 90 | // This array contains the swapped version of each SkXfermode. For instance |
| 91 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 92 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 93 | static const Blender gBlendsSwap[] = { |
| 94 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ZERO }, |
| 95 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 96 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 97 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 98 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 99 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 100 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 101 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 102 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 103 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 104 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 105 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA } |
| 106 | }; |
| 107 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 108 | static const GLenum gTextureUnits[] = { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 109 | GL_TEXTURE0, |
| 110 | GL_TEXTURE1, |
| 111 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 114 | /////////////////////////////////////////////////////////////////////////////// |
| 115 | // Constructors/destructor |
| 116 | /////////////////////////////////////////////////////////////////////////////// |
| 117 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 118 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 119 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 120 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 121 | mHasShadow = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 122 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 123 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 124 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 125 | mFirstSnapshot = new Snapshot; |
| 126 | |
| 127 | GLint maxTextureUnits; |
| 128 | glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits); |
| 129 | if (maxTextureUnits < REQUIRED_TEXTURE_UNITS_COUNT) { |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 130 | LOGW("At least %d texture units are required!", REQUIRED_TEXTURE_UNITS_COUNT); |
| 131 | } |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 132 | |
| 133 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 136 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 137 | // The context has already been destroyed at this point, do not call |
| 138 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 141 | /////////////////////////////////////////////////////////////////////////////// |
| 142 | // Setup |
| 143 | /////////////////////////////////////////////////////////////////////////////// |
| 144 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 145 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 146 | glViewport(0, 0, width, height); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 147 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 148 | |
| 149 | mWidth = width; |
| 150 | mHeight = height; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 151 | |
| 152 | mFirstSnapshot->height = height; |
| 153 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 156 | void OpenGLRenderer::prepare(bool opaque) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 157 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 158 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 159 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 160 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 161 | glViewport(0, 0, mWidth, mHeight); |
| 162 | |
Romain Guy | d90f23e | 2010-09-09 11:47:54 -0700 | [diff] [blame] | 163 | glDisable(GL_DITHER); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 164 | glDisable(GL_SCISSOR_TEST); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 165 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 166 | if (!opaque) { |
| 167 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 168 | glClear(GL_COLOR_BUFFER_BIT); |
| 169 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 170 | |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 171 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 172 | glScissor(0, 0, mWidth, mHeight); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 173 | |
Romain Guy | b82da65 | 2010-07-30 11:36:12 -0700 | [diff] [blame] | 174 | mSnapshot->setClip(0.0f, 0.0f, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 177 | void OpenGLRenderer::finish() { |
| 178 | #if DEBUG_OPENGL |
| 179 | GLenum status = GL_NO_ERROR; |
| 180 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 181 | LOGD("GL error from OpenGLRenderer: 0x%x", status); |
| 182 | } |
| 183 | #endif |
| 184 | } |
| 185 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 186 | void OpenGLRenderer::acquireContext() { |
| 187 | if (mCaches.currentProgram) { |
| 188 | if (mCaches.currentProgram->isInUse()) { |
| 189 | mCaches.currentProgram->remove(); |
| 190 | mCaches.currentProgram = NULL; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void OpenGLRenderer::releaseContext() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 196 | glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 197 | |
| 198 | glEnable(GL_SCISSOR_TEST); |
| 199 | setScissorFromClip(); |
| 200 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 201 | glDisable(GL_DITHER); |
| 202 | |
| 203 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 204 | |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 205 | if (mCaches.blend) { |
| 206 | glEnable(GL_BLEND); |
| 207 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 208 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 209 | } else { |
| 210 | glDisable(GL_BLEND); |
| 211 | } |
| 212 | } |
| 213 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 214 | /////////////////////////////////////////////////////////////////////////////// |
| 215 | // State management |
| 216 | /////////////////////////////////////////////////////////////////////////////// |
| 217 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 218 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 219 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 223 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 227 | if (mSaveCount > 1) { |
| 228 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 229 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 233 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 234 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 235 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 236 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 237 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 240 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 241 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 242 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 246 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 247 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 248 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 249 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 250 | sp<Snapshot> current = mSnapshot; |
| 251 | sp<Snapshot> previous = mSnapshot->previous; |
| 252 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 253 | if (restoreOrtho) { |
| 254 | Rect& r = previous->viewport; |
| 255 | glViewport(r.left, r.top, r.right, r.bottom); |
| 256 | mOrthoMatrix.load(current->orthoMatrix); |
| 257 | } |
| 258 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 259 | mSaveCount--; |
| 260 | mSnapshot = previous; |
| 261 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 262 | if (restoreLayer) { |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 263 | composeLayer(current, previous); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 266 | if (restoreClip) { |
| 267 | setScissorFromClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 268 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 269 | |
| 270 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 273 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 274 | // Layers |
| 275 | /////////////////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
| 278 | const SkPaint* p, int flags) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 279 | const GLuint previousFbo = mSnapshot->fbo; |
| 280 | const int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 281 | |
| 282 | int alpha = 255; |
| 283 | SkXfermode::Mode mode; |
| 284 | |
| 285 | if (p) { |
| 286 | alpha = p->getAlpha(); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 287 | if (!mExtensions.hasFramebufferFetch()) { |
| 288 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 289 | if (!isMode) { |
| 290 | // Assume SRC_OVER |
| 291 | mode = SkXfermode::kSrcOver_Mode; |
| 292 | } |
| 293 | } else { |
| 294 | mode = getXfermode(p->getXfermode()); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 295 | } |
| 296 | } else { |
| 297 | mode = SkXfermode::kSrcOver_Mode; |
| 298 | } |
| 299 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 300 | if (!mSnapshot->previous->invisible) { |
| 301 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); |
| 302 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 303 | |
| 304 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 308 | int alpha, int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 309 | if (alpha == 0xff) { |
| 310 | return saveLayer(left, top, right, bottom, NULL, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 311 | } else { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 312 | SkPaint paint; |
| 313 | paint.setAlpha(alpha); |
| 314 | return saveLayer(left, top, right, bottom, &paint, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 315 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 316 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 317 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 320 | * programs (for instance.) When a layer is created, previously created layers |
| 321 | * and the frame buffer still receive every drawing command. For instance, if a |
| 322 | * layer is created and a shape intersecting the bounds of the layers and the |
| 323 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 324 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 325 | * |
| 326 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 327 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 328 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 329 | * means, for every primitive: |
| 330 | * - Switch active frame buffer |
| 331 | * - Change viewport, clip and projection matrix |
| 332 | * - Issue the drawing |
| 333 | * |
| 334 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 335 | * To avoid this, layers are implemented in a different way here, at least in the |
| 336 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 337 | * is set. When this flag is set we can redirect all drawing operations into a |
| 338 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 339 | * |
| 340 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 341 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 342 | * frame buffer contained within the layer's bounds is copied into this texture |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 343 | * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 344 | * buffer and drawing continues as normal. This technique therefore treats the |
| 345 | * frame buffer as a scratch buffer for the layers. |
| 346 | * |
| 347 | * To compose the layers back onto the frame buffer, each layer texture |
| 348 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 349 | * the frame buffer. The trick is that the quad is set as the composition |
| 350 | * destination in the blending equation, and the frame buffer becomes the source |
| 351 | * of the composition. |
| 352 | * |
| 353 | * Drawing layers with an alpha value requires an extra step before composition. |
| 354 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 355 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 356 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 357 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 358 | * GL_ZERO, GL_SRC_ALPHA. |
| 359 | * |
| 360 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 361 | * be use to draw a single clipped layer. The implementation described above |
| 362 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 363 | * |
| 364 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 365 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 366 | * buffer is left untouched until the first drawing operation. Only when |
| 367 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 368 | */ |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 369 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 370 | float right, float bottom, int alpha, SkXfermode::Mode mode, |
| 371 | int flags, GLuint previousFbo) { |
| 372 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 373 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 374 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 375 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 376 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 377 | // Window coordinates of the layer |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 378 | Rect bounds(left, top, right, bottom); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 379 | if (!fboLayer) { |
| 380 | mSnapshot->transform->mapRect(bounds); |
| 381 | // Layers only make sense if they are in the framebuffer's bounds |
| 382 | bounds.intersect(*mSnapshot->clipRect); |
| 383 | bounds.snapToPixelBoundaries(); |
| 384 | } |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 385 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 386 | if (bounds.isEmpty() || bounds.getWidth() > mMaxTextureSize || |
| 387 | bounds.getHeight() > mMaxTextureSize) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 388 | snapshot->invisible = true; |
| 389 | } else { |
| 390 | // TODO: Should take the mode into account |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame^] | 391 | snapshot->invisible = snapshot->previous->invisible || |
| 392 | (alpha <= ALPHA_THRESHOLD && fboLayer); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Bail out if we won't draw in this snapshot |
| 396 | if (snapshot->invisible) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 397 | return false; |
| 398 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 399 | |
Romain Guy | 0bb5667 | 2010-10-01 00:25:02 -0700 | [diff] [blame] | 400 | glActiveTexture(GL_TEXTURE0); |
| 401 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 402 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 403 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 404 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 407 | layer->mode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 408 | layer->alpha = alpha; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 409 | layer->layer.set(bounds); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 410 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), |
| 411 | bounds.getWidth() / float(layer->width), 0.0f); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 412 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 413 | // Save the layer in the snapshot |
| 414 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 415 | snapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 416 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 417 | if (fboLayer) { |
| 418 | layer->fbo = mCaches.fboCache.get(); |
Romain Guy | 38c85b9 | 2010-09-22 22:48:20 -0700 | [diff] [blame] | 419 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 420 | snapshot->flags |= Snapshot::kFlagIsFboLayer; |
| 421 | snapshot->fbo = layer->fbo; |
| 422 | snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 423 | snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 424 | snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 425 | snapshot->height = bounds.getHeight(); |
| 426 | snapshot->flags |= Snapshot::kFlagDirtyOrtho; |
| 427 | snapshot->orthoMatrix.load(mOrthoMatrix); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 428 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 429 | // Bind texture to FBO |
| 430 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
| 431 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 432 | |
| 433 | // Initialize the texture if needed |
| 434 | if (layer->empty) { |
| 435 | layer->empty = false; |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 436 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 437 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 438 | } |
| 439 | |
| 440 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 441 | layer->texture, 0); |
| 442 | |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 443 | #if DEBUG_LAYERS |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 444 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 445 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 446 | LOGE("Framebuffer incomplete (GL error code 0x%x)", status); |
| 447 | |
| 448 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 449 | glDeleteTextures(1, &layer->texture); |
| 450 | mCaches.fboCache.put(layer->fbo); |
| 451 | |
| 452 | delete layer; |
| 453 | |
| 454 | return false; |
| 455 | } |
Romain Guy | e910805 | 2010-10-12 18:15:42 -0700 | [diff] [blame] | 456 | #endif |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 457 | |
| 458 | // Clear the FBO |
Romain Guy | 93d2361 | 2010-10-13 18:26:36 -0700 | [diff] [blame] | 459 | glScissor(0.0f, 0.0f, bounds.getWidth() + 1.0f, bounds.getHeight() + 1.0f); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 460 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 461 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | eb07af6 | 2010-10-12 18:40:36 -0700 | [diff] [blame] | 462 | |
| 463 | setScissorFromClip(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 464 | |
| 465 | // Change the ortho projection |
| 466 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 467 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 468 | } else { |
| 469 | // Copy the framebuffer into the layer |
| 470 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 471 | |
Romain Guy | c00972b | 2010-10-12 11:31:07 -0700 | [diff] [blame] | 472 | if (layer->empty) { |
| 473 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, mHeight - bounds.bottom, |
| 474 | layer->width, layer->height, 0); |
| 475 | layer->empty = false; |
| 476 | } else { |
| 477 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, mHeight - bounds.bottom, |
| 478 | bounds.getWidth(), bounds.getHeight()); |
| 479 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 480 | |
| 481 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 482 | mLayers.push(new Rect(bounds)); |
| 483 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 484 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 485 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Read the documentation of createLayer() before doing anything in this method. |
| 490 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 491 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 492 | if (!current->layer) { |
| 493 | LOGE("Attempting to compose a layer that does not exist"); |
| 494 | return; |
| 495 | } |
| 496 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 497 | const bool fboLayer = current->flags & SkCanvas::kClipToLayer_SaveFlag; |
| 498 | |
| 499 | if (fboLayer) { |
| 500 | // Unbind current FBO and restore previous one |
| 501 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 502 | } |
| 503 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 504 | // Restore the clip from the previous snapshot |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 505 | const Rect& clip = *previous->clipRect; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 506 | glScissor(clip.left, previous->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 507 | |
| 508 | Layer* layer = current->layer; |
| 509 | const Rect& rect = layer->layer; |
| 510 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 511 | if (!fboLayer && layer->alpha < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 512 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 513 | layer->alpha << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 516 | const Rect& texCoords = layer->texCoords; |
| 517 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 518 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 519 | if (fboLayer) { |
| 520 | drawTextureRect(rect.left, rect.top, rect.right, rect.bottom, |
| 521 | layer->texture, layer->alpha / 255.0f, layer->mode, layer->blend); |
| 522 | } else { |
| 523 | drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, |
| 524 | 1.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], |
| 525 | &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, true, true); |
| 526 | } |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 527 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 528 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 529 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 530 | if (fboLayer) { |
| 531 | // Detach the texture from the FBO |
| 532 | glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); |
| 533 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 534 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 535 | |
| 536 | // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed |
| 537 | mCaches.fboCache.put(current->fbo); |
| 538 | } |
| 539 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 540 | // Failing to add the layer to the cache should happen only if the layer is too large |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 541 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 542 | LAYER_LOGD("Deleting layer"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 543 | glDeleteTextures(1, &layer->texture); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 544 | delete layer; |
| 545 | } |
| 546 | } |
| 547 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 548 | void OpenGLRenderer::clearLayerRegions() { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 549 | if (mLayers.size() == 0 || mSnapshot->invisible) return; |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 550 | |
| 551 | for (uint32_t i = 0; i < mLayers.size(); i++) { |
| 552 | Rect* bounds = mLayers.itemAt(i); |
| 553 | |
| 554 | // Clear the framebuffer where the layer will draw |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame^] | 555 | glScissor(bounds->left, mSnapshot->height - bounds->bottom, |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 556 | bounds->getWidth(), bounds->getHeight()); |
| 557 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 558 | glClear(GL_COLOR_BUFFER_BIT); |
| 559 | |
| 560 | delete bounds; |
| 561 | } |
| 562 | mLayers.clear(); |
| 563 | |
| 564 | // Restore the clip |
| 565 | setScissorFromClip(); |
| 566 | } |
| 567 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 568 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 569 | // Transforms |
| 570 | /////////////////////////////////////////////////////////////////////////////// |
| 571 | |
| 572 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 573 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 577 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 581 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 585 | mSnapshot->transform->load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 588 | const float* OpenGLRenderer::getMatrix() const { |
Romain Guy | 99bcdc5 | 2010-10-13 15:17:00 -0700 | [diff] [blame] | 589 | if (mSnapshot->fbo != 0) { |
| 590 | return &mSnapshot->transform->data[0]; |
| 591 | } |
| 592 | return &mIdentity.data[0]; |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 595 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 596 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 600 | mat4 m(*matrix); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 601 | mSnapshot->transform->multiply(m); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /////////////////////////////////////////////////////////////////////////////// |
| 605 | // Clipping |
| 606 | /////////////////////////////////////////////////////////////////////////////// |
| 607 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 608 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 609 | const Rect& clip = *mSnapshot->clipRect; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 610 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 614 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 617 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 618 | if (mSnapshot->invisible) { |
| 619 | return true; |
| 620 | } |
| 621 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 622 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 623 | mSnapshot->transform->mapRect(r); |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame^] | 624 | r.snapToPixelBoundaries(); |
| 625 | |
| 626 | Rect clipRect(*mSnapshot->clipRect); |
| 627 | clipRect.snapToPixelBoundaries(); |
| 628 | |
| 629 | return !clipRect.intersects(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 632 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 633 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 634 | if (clipped) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 635 | setScissorFromClip(); |
| 636 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 637 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 640 | /////////////////////////////////////////////////////////////////////////////// |
| 641 | // Drawing |
| 642 | /////////////////////////////////////////////////////////////////////////////// |
| 643 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 644 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 645 | const float right = left + bitmap->width(); |
| 646 | const float bottom = top + bitmap->height(); |
| 647 | |
| 648 | if (quickReject(left, top, right, bottom)) { |
| 649 | return; |
| 650 | } |
| 651 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 652 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 653 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 654 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 655 | const AutoTexture autoCleanup(texture); |
| 656 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 657 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 660 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint) { |
| 661 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 662 | const mat4 transform(*matrix); |
| 663 | transform.mapRect(r); |
| 664 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 665 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 666 | return; |
| 667 | } |
| 668 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 669 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 670 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 671 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 672 | const AutoTexture autoCleanup(texture); |
| 673 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 674 | drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 677 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 678 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 679 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 680 | const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 681 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 682 | return; |
| 683 | } |
| 684 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 685 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 686 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 687 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 688 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 689 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 690 | const float width = texture->width; |
| 691 | const float height = texture->height; |
| 692 | |
| 693 | const float u1 = srcLeft / width; |
| 694 | const float v1 = srcTop / height; |
| 695 | const float u2 = srcRight / width; |
| 696 | const float v2 = srcBottom / height; |
| 697 | |
| 698 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 699 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 700 | drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture, paint); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 701 | |
| 702 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 703 | } |
| 704 | |
Romain Guy | 4aa9057 | 2010-09-26 18:40:37 -0700 | [diff] [blame] | 705 | void OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 706 | const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, |
| 707 | float left, float top, float right, float bottom, const SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 708 | if (quickReject(left, top, right, bottom)) { |
| 709 | return; |
| 710 | } |
| 711 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 712 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 713 | const Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 714 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 715 | const AutoTexture autoCleanup(texture); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 716 | |
| 717 | int alpha; |
| 718 | SkXfermode::Mode mode; |
| 719 | getAlphaAndMode(paint, &alpha, &mode); |
| 720 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 721 | const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 722 | right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 723 | |
| 724 | // Specify right and bottom as +1.0f from left/top to prevent scaling since the |
| 725 | // patch mesh already defines the final size |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 726 | drawTextureMesh(left, top, left + 1.0f, top + 1.0f, texture->id, alpha / 255.0f, |
| 727 | mode, texture->blend, &mesh->vertices[0].position[0], |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 728 | &mesh->vertices[0].texture[0], GL_TRIANGLES, mesh->verticesCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 731 | void OpenGLRenderer::drawLines(float* points, int count, const SkPaint* paint) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 732 | if (mSnapshot->invisible) return; |
| 733 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 734 | int alpha; |
| 735 | SkXfermode::Mode mode; |
| 736 | getAlphaAndMode(paint, &alpha, &mode); |
| 737 | |
| 738 | uint32_t color = paint->getColor(); |
| 739 | const GLfloat a = alpha / 255.0f; |
| 740 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 741 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 742 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 743 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 744 | const bool isAA = paint->isAntiAlias(); |
| 745 | if (isAA) { |
| 746 | GLuint textureUnit = 0; |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 747 | setupTextureAlpha8(mCaches.line.getTexture(), 0, 0, textureUnit, 0.0f, 0.0f, r, g, b, a, |
| 748 | mode, false, true, mCaches.line.getVertices(), mCaches.line.getTexCoords()); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 749 | } else { |
| 750 | setupColorRect(0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a, mode, false); |
| 751 | } |
| 752 | |
| 753 | const float strokeWidth = paint->getStrokeWidth(); |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 754 | const GLsizei elementsCount = isAA ? mCaches.line.getElementsCount() : gMeshCount; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 755 | const GLenum drawMode = isAA ? GL_TRIANGLES : GL_TRIANGLE_STRIP; |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 756 | |
| 757 | for (int i = 0; i < count; i += 4) { |
| 758 | float tx = 0.0f; |
| 759 | float ty = 0.0f; |
| 760 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 761 | if (isAA) { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 762 | mCaches.line.update(points[i], points[i + 1], points[i + 2], points[i + 3], |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 763 | strokeWidth, tx, ty); |
| 764 | } else { |
Romain Guy | b5ab417 | 2010-09-17 15:36:56 -0700 | [diff] [blame] | 765 | ty = strokeWidth <= 1.0f ? 0.0f : -strokeWidth * 0.5f; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 766 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 767 | |
| 768 | const float dx = points[i + 2] - points[i]; |
| 769 | const float dy = points[i + 3] - points[i + 1]; |
| 770 | const float mag = sqrtf(dx * dx + dy * dy); |
| 771 | const float angle = acos(dx / mag); |
| 772 | |
| 773 | mModelView.loadTranslate(points[i], points[i + 1], 0.0f); |
| 774 | if (angle > MIN_ANGLE || angle < -MIN_ANGLE) { |
| 775 | mModelView.rotate(angle * RAD_TO_DEG, 0.0f, 0.0f, 1.0f); |
| 776 | } |
| 777 | mModelView.translate(tx, ty, 0.0f); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 778 | if (!isAA) { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 779 | float length = mCaches.line.getLength(points[i], points[i + 1], |
| 780 | points[i + 2], points[i + 3]); |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 781 | mModelView.scale(length, strokeWidth, 1.0f); |
| 782 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 783 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 784 | |
| 785 | if (mShader) { |
| 786 | mShader->updateTransforms(mCaches.currentProgram, mModelView, *mSnapshot); |
| 787 | } |
| 788 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 789 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 792 | if (isAA) { |
| 793 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
| 794 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 797 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 798 | const Rect& clip = *mSnapshot->clipRect; |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 799 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 800 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 801 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 802 | void OpenGLRenderer::drawRect(float left, float top, float right, float bottom, const SkPaint* p) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 803 | if (quickReject(left, top, right, bottom)) { |
| 804 | return; |
| 805 | } |
| 806 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 807 | SkXfermode::Mode mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 808 | if (!mExtensions.hasFramebufferFetch()) { |
| 809 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 810 | if (!isMode) { |
| 811 | // Assume SRC_OVER |
| 812 | mode = SkXfermode::kSrcOver_Mode; |
| 813 | } |
| 814 | } else { |
| 815 | mode = getXfermode(p->getXfermode()); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | // Skia draws using the color's alpha channel if < 255 |
| 819 | // Otherwise, it uses the paint's alpha |
| 820 | int color = p->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 821 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 822 | color |= p->getAlpha() << 24; |
| 823 | } |
| 824 | |
| 825 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 826 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 827 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 828 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 829 | float x, float y, SkPaint* paint) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 830 | if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 831 | return; |
| 832 | } |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 833 | if (mSnapshot->invisible) return; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 834 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 835 | paint->setAntiAlias(true); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 836 | |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 837 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 838 | switch (paint->getTextAlign()) { |
| 839 | case SkPaint::kCenter_Align: |
| 840 | length = paint->measureText(text, bytesCount); |
| 841 | x -= length / 2.0f; |
| 842 | break; |
| 843 | case SkPaint::kRight_Align: |
| 844 | length = paint->measureText(text, bytesCount); |
| 845 | x -= length; |
| 846 | break; |
| 847 | default: |
| 848 | break; |
| 849 | } |
| 850 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 851 | int alpha; |
| 852 | SkXfermode::Mode mode; |
| 853 | getAlphaAndMode(paint, &alpha, &mode); |
| 854 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 855 | uint32_t color = paint->getColor(); |
| 856 | const GLfloat a = alpha / 255.0f; |
| 857 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 858 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 859 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 860 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 861 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 862 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 863 | paint->getTextSize()); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 864 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 865 | if (mHasShadow) { |
| 866 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 867 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 868 | const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 869 | count, mShadowRadius); |
| 870 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 871 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 872 | setupShadow(shadow, x, y, mode, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 873 | |
| 874 | // Draw the mesh |
| 875 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 876 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 879 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 880 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 881 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 882 | // Assume that the modelView matrix does not force scales, rotates, etc. |
| 883 | const bool linearFilter = mSnapshot->transform->changesBounds(); |
| 884 | setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit, |
| 885 | x, y, r, g, b, a, mode, false, true); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 886 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 887 | const Rect& clip = mSnapshot->getLocalClip(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 888 | clearLayerRegions(); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 889 | fontRenderer.renderText(paint, &clip, text, 0, bytesCount, count, x, y); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 890 | |
| 891 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 892 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 893 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 894 | drawTextDecorations(text, bytesCount, length, x, y, paint); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 897 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 898 | if (mSnapshot->invisible) return; |
| 899 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 900 | GLuint textureUnit = 0; |
| 901 | glActiveTexture(gTextureUnits[textureUnit]); |
| 902 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 903 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 904 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 905 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 906 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 907 | const float x = texture->left - texture->offset; |
| 908 | const float y = texture->top - texture->offset; |
| 909 | |
| 910 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 911 | return; |
| 912 | } |
| 913 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 914 | int alpha; |
| 915 | SkXfermode::Mode mode; |
| 916 | getAlphaAndMode(paint, &alpha, &mode); |
| 917 | |
| 918 | uint32_t color = paint->getColor(); |
| 919 | const GLfloat a = alpha / 255.0f; |
| 920 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 921 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 922 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 923 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 924 | setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); |
| 925 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 926 | clearLayerRegions(); |
| 927 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 928 | // Draw the mesh |
| 929 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 930 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 933 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 934 | // Shaders |
| 935 | /////////////////////////////////////////////////////////////////////////////// |
| 936 | |
| 937 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 938 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 941 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 942 | mShader = shader; |
| 943 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 944 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 945 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 948 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 949 | // Color filters |
| 950 | /////////////////////////////////////////////////////////////////////////////// |
| 951 | |
| 952 | void OpenGLRenderer::resetColorFilter() { |
| 953 | mColorFilter = NULL; |
| 954 | } |
| 955 | |
| 956 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 957 | mColorFilter = filter; |
| 958 | } |
| 959 | |
| 960 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 961 | // Drop shadow |
| 962 | /////////////////////////////////////////////////////////////////////////////// |
| 963 | |
| 964 | void OpenGLRenderer::resetShadow() { |
| 965 | mHasShadow = false; |
| 966 | } |
| 967 | |
| 968 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 969 | mHasShadow = true; |
| 970 | mShadowRadius = radius; |
| 971 | mShadowDx = dx; |
| 972 | mShadowDy = dy; |
| 973 | mShadowColor = color; |
| 974 | } |
| 975 | |
| 976 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 977 | // Drawing implementation |
| 978 | /////////////////////////////////////////////////////////////////////////////// |
| 979 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 980 | void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 981 | SkXfermode::Mode mode, float alpha) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 982 | const float sx = x - texture->left + mShadowDx; |
| 983 | const float sy = y - texture->top + mShadowDy; |
| 984 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 985 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
| 986 | const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 987 | const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; |
| 988 | const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; |
| 989 | const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; |
| 990 | |
| 991 | GLuint textureUnit = 0; |
| 992 | setupTextureAlpha8(texture, textureUnit, sx, sy, r, g, b, a, mode, true, false); |
| 993 | } |
| 994 | |
| 995 | void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, |
| 996 | float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, |
| 997 | bool transforms, bool applyFilters) { |
| 998 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 999 | x, y, r, g, b, a, mode, transforms, applyFilters, |
| 1000 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1004 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1005 | SkXfermode::Mode mode, bool transforms, bool applyFilters) { |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1006 | setupTextureAlpha8(texture, width, height, textureUnit, |
| 1007 | x, y, r, g, b, a, mode, transforms, applyFilters, |
| 1008 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); |
| 1009 | } |
| 1010 | |
| 1011 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1012 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1013 | SkXfermode::Mode mode, bool transforms, bool applyFilters, |
| 1014 | GLvoid* vertices, GLvoid* texCoords) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1015 | // Describe the required shaders |
| 1016 | ProgramDescription description; |
| 1017 | description.hasTexture = true; |
| 1018 | description.hasAlpha8Texture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1019 | const bool setColor = description.setAlpha8Color(r, g, b, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (applyFilters) { |
| 1022 | if (mShader) { |
| 1023 | mShader->describe(description, mExtensions); |
| 1024 | } |
| 1025 | if (mColorFilter) { |
| 1026 | mColorFilter->describe(description, mExtensions); |
| 1027 | } |
| 1028 | } |
| 1029 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1030 | // Setup the blending mode |
| 1031 | chooseBlending(true, mode, description); |
| 1032 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1033 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1034 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1035 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1036 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, textureUnit); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1037 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1038 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1039 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1040 | glEnableVertexAttribArray(texCoordsSlot); |
| 1041 | |
| 1042 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1043 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1044 | gMeshStride, vertices); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1045 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1046 | gMeshStride, texCoords); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1047 | |
| 1048 | // Setup uniforms |
| 1049 | if (transforms) { |
| 1050 | mModelView.loadTranslate(x, y, 0.0f); |
| 1051 | mModelView.scale(width, height, 1.0f); |
| 1052 | } else { |
| 1053 | mModelView.loadIdentity(); |
| 1054 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1055 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1056 | if (setColor) { |
| 1057 | mCaches.currentProgram->setColor(r, g, b, a); |
| 1058 | } |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1059 | |
| 1060 | textureUnit++; |
| 1061 | if (applyFilters) { |
| 1062 | // Setup attributes and uniforms required by the shaders |
| 1063 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1064 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1065 | } |
| 1066 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1067 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1072 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1073 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 1074 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 1075 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 1076 | |
| 1077 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 1078 | float x, float y, SkPaint* paint) { |
| 1079 | // Handle underline and strike-through |
| 1080 | uint32_t flags = paint->getFlags(); |
| 1081 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
| 1082 | float underlineWidth = length; |
| 1083 | // If length is > 0.0f, we already measured the text for the text alignment |
| 1084 | if (length <= 0.0f) { |
| 1085 | underlineWidth = paint->measureText(text, bytesCount); |
| 1086 | } |
| 1087 | |
| 1088 | float offsetX = 0; |
| 1089 | switch (paint->getTextAlign()) { |
| 1090 | case SkPaint::kCenter_Align: |
| 1091 | offsetX = underlineWidth * 0.5f; |
| 1092 | break; |
| 1093 | case SkPaint::kRight_Align: |
| 1094 | offsetX = underlineWidth; |
| 1095 | break; |
| 1096 | default: |
| 1097 | break; |
| 1098 | } |
| 1099 | |
| 1100 | if (underlineWidth > 0.0f) { |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1101 | const float textSize = paint->getTextSize(); |
| 1102 | const float strokeWidth = textSize * kStdUnderline_Thickness; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1103 | |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1104 | const float left = x - offsetX; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1105 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1106 | |
| 1107 | const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1); |
| 1108 | float points[pointsCount]; |
| 1109 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1110 | |
| 1111 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 1112 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1113 | points[currentPoint++] = left; |
| 1114 | points[currentPoint++] = top; |
| 1115 | points[currentPoint++] = left + underlineWidth; |
| 1116 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 1120 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1121 | points[currentPoint++] = left; |
| 1122 | points[currentPoint++] = top; |
| 1123 | points[currentPoint++] = left + underlineWidth; |
| 1124 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1125 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1126 | |
| 1127 | SkPaint linesPaint(*paint); |
| 1128 | linesPaint.setStrokeWidth(strokeWidth); |
| 1129 | |
| 1130 | drawLines(&points[0], pointsCount, &linesPaint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1135 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1136 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1137 | clearLayerRegions(); |
| 1138 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1139 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1140 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1141 | color |= 0x00ffffff; |
| 1142 | } |
| 1143 | |
| 1144 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1145 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1146 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1147 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1148 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1149 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1150 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1151 | setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform); |
| 1152 | |
| 1153 | // Draw the mesh |
| 1154 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 1155 | } |
| 1156 | |
| 1157 | void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom, |
| 1158 | float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1159 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1160 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1161 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1162 | ProgramDescription description; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1163 | const bool setColor = description.setColor(r, g, b, a); |
| 1164 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1165 | if (mShader) { |
| 1166 | mShader->describe(description, mExtensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1167 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1168 | if (mColorFilter) { |
| 1169 | mColorFilter->describe(description, mExtensions); |
| 1170 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1171 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1172 | // Setup the blending mode |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1173 | chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1174 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1175 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1176 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1177 | |
| 1178 | // Setup attributes |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1179 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1180 | gMeshStride, &mMeshVertices[0].position[0]); |
| 1181 | |
| 1182 | // Setup uniforms |
| 1183 | mModelView.loadTranslate(left, top, 0.0f); |
| 1184 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1185 | if (!ignoreTransform) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1186 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | mat4 identity; |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1189 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1190 | } |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1191 | mCaches.currentProgram->setColor(r, g, b, a); |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1192 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1193 | // Setup attributes and uniforms required by the shaders |
| 1194 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1195 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1196 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1197 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1198 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1199 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1202 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1203 | const Texture* texture, const SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1204 | int alpha; |
| 1205 | SkXfermode::Mode mode; |
| 1206 | getAlphaAndMode(paint, &alpha, &mode); |
| 1207 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1208 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
| 1209 | texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1210 | GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1213 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1214 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 1215 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1216 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1217 | GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1221 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1222 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1223 | bool swapSrcDst, bool ignoreTransform) { |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1224 | clearLayerRegions(); |
| 1225 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1226 | ProgramDescription description; |
| 1227 | description.hasTexture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1228 | const bool setColor = description.setColor(alpha, alpha, alpha, alpha); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1229 | if (mColorFilter) { |
| 1230 | mColorFilter->describe(description, mExtensions); |
| 1231 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1232 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1233 | mModelView.loadTranslate(left, top, 0.0f); |
| 1234 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 1235 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1236 | chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1237 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1238 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1239 | if (!ignoreTransform) { |
| 1240 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1241 | } else { |
| 1242 | mat4 m; |
| 1243 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, m); |
| 1244 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1245 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1246 | // Texture |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1247 | bindTexture(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1248 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 1249 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1250 | // Always premultiplied |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1251 | if (setColor) { |
| 1252 | mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); |
| 1253 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1254 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1255 | // Mesh |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1256 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1257 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1258 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1259 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1260 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1261 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1262 | // Color filter |
| 1263 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1264 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1267 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1268 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1271 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1272 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1273 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 1274 | if (blend) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1275 | if (mode < SkXfermode::kPlus_Mode) { |
| 1276 | if (!mCaches.blend) { |
| 1277 | glEnable(GL_BLEND); |
| 1278 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1279 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1280 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 1281 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1282 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1283 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 1284 | glBlendFunc(sourceMode, destMode); |
| 1285 | mCaches.lastSrcMode = sourceMode; |
| 1286 | mCaches.lastDstMode = destMode; |
| 1287 | } |
| 1288 | } else { |
| 1289 | // These blend modes are not supported by OpenGL directly and have |
| 1290 | // to be implemented using shaders. Since the shader will perform |
| 1291 | // the blending, turn blending off here |
| 1292 | if (mExtensions.hasFramebufferFetch()) { |
| 1293 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1294 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | if (mCaches.blend) { |
| 1298 | glDisable(GL_BLEND); |
| 1299 | } |
| 1300 | blend = false; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1301 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1302 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1303 | glDisable(GL_BLEND); |
| 1304 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1305 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1308 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1309 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1310 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1311 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1312 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1313 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1314 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1315 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1318 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1319 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1320 | TextureVertex::setUV(v++, u1, v1); |
| 1321 | TextureVertex::setUV(v++, u2, v1); |
| 1322 | TextureVertex::setUV(v++, u1, v2); |
| 1323 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
| 1327 | if (paint) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1328 | if (!mExtensions.hasFramebufferFetch()) { |
| 1329 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 1330 | if (!isMode) { |
| 1331 | // Assume SRC_OVER |
| 1332 | *mode = SkXfermode::kSrcOver_Mode; |
| 1333 | } |
| 1334 | } else { |
| 1335 | *mode = getXfermode(paint->getXfermode()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | // Skia draws using the color's alpha channel if < 255 |
| 1339 | // Otherwise, it uses the paint's alpha |
| 1340 | int color = paint->getColor(); |
| 1341 | *alpha = (color >> 24) & 0xFF; |
| 1342 | if (*alpha == 255) { |
| 1343 | *alpha = paint->getAlpha(); |
| 1344 | } |
| 1345 | } else { |
| 1346 | *mode = SkXfermode::kSrcOver_Mode; |
| 1347 | *alpha = 255; |
| 1348 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1351 | SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { |
| 1352 | if (mode == NULL) { |
| 1353 | return SkXfermode::kSrcOver_Mode; |
| 1354 | } |
| 1355 | return mode->fMode; |
| 1356 | } |
| 1357 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1358 | void OpenGLRenderer::bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit) { |
| 1359 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 1360 | glBindTexture(GL_TEXTURE_2D, texture); |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1361 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 1362 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 1363 | } |
| 1364 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1365 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1366 | }; // namespace android |