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