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