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