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