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