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