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 | 32963c3 | 2010-12-10 14:43:41 -0800 | [diff] [blame^] | 388 | snapshot->empty = fboLayer; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 389 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 390 | snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // Bail out if we won't draw in this snapshot |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 394 | if (snapshot->invisible || snapshot->empty) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 397 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 398 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 399 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 400 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 401 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 404 | layer->mode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 405 | layer->alpha = alpha; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 406 | layer->layer.set(bounds); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 407 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->height), |
| 408 | bounds.getWidth() / float(layer->width), 0.0f); |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 409 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 410 | // Save the layer in the snapshot |
| 411 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 412 | snapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 413 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 414 | if (fboLayer) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 415 | return createFboLayer(layer, bounds, snapshot, previousFbo); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 416 | } else { |
| 417 | // Copy the framebuffer into the layer |
| 418 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 419 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 420 | if (layer->empty) { |
| 421 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bounds.left, |
| 422 | snapshot->height - bounds.bottom, layer->width, layer->height, 0); |
| 423 | layer->empty = false; |
| 424 | } else { |
| 425 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, |
| 426 | snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); |
| 427 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 428 | |
| 429 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 430 | mLayers.push(new Rect(bounds)); |
| 431 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 432 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 433 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 436 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, |
| 437 | GLuint previousFbo) { |
| 438 | layer->fbo = mCaches.fboCache.get(); |
| 439 | |
| 440 | #if RENDER_LAYERS_AS_REGIONS |
| 441 | snapshot->region = &snapshot->layer->region; |
| 442 | snapshot->flags |= Snapshot::kFlagFboTarget; |
| 443 | #endif |
| 444 | |
| 445 | Rect clip(bounds); |
| 446 | snapshot->transform->mapRect(clip); |
| 447 | clip.intersect(*snapshot->clipRect); |
| 448 | clip.snapToPixelBoundaries(); |
| 449 | clip.intersect(snapshot->previous->viewport); |
| 450 | |
| 451 | mat4 inverse; |
| 452 | inverse.loadInverse(*mSnapshot->transform); |
| 453 | |
| 454 | inverse.mapRect(clip); |
| 455 | clip.snapToPixelBoundaries(); |
| 456 | clip.intersect(bounds); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 457 | clip.translate(-bounds.left, -bounds.top); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 458 | |
| 459 | snapshot->flags |= Snapshot::kFlagIsFboLayer; |
| 460 | snapshot->fbo = layer->fbo; |
| 461 | snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 462 | //snapshot->resetClip(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 463 | snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 464 | snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 465 | snapshot->height = bounds.getHeight(); |
| 466 | snapshot->flags |= Snapshot::kFlagDirtyOrtho; |
| 467 | snapshot->orthoMatrix.load(mOrthoMatrix); |
| 468 | |
| 469 | // Bind texture to FBO |
| 470 | glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo); |
| 471 | glBindTexture(GL_TEXTURE_2D, layer->texture); |
| 472 | |
| 473 | // Initialize the texture if needed |
| 474 | if (layer->empty) { |
| 475 | layer->empty = false; |
| 476 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->width, layer->height, 0, |
| 477 | GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 478 | } |
| 479 | |
| 480 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 481 | layer->texture, 0); |
| 482 | |
| 483 | #if DEBUG_LAYERS_AS_REGIONS |
| 484 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 485 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 486 | LOGE("Framebuffer incomplete (GL error code 0x%x)", status); |
| 487 | |
| 488 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
| 489 | glDeleteTextures(1, &layer->texture); |
| 490 | mCaches.fboCache.put(layer->fbo); |
| 491 | |
| 492 | delete layer; |
| 493 | |
| 494 | return false; |
| 495 | } |
| 496 | #endif |
| 497 | |
| 498 | // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering |
| 499 | glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
| 500 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
| 501 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 502 | glClear(GL_COLOR_BUFFER_BIT); |
| 503 | |
| 504 | dirtyClip(); |
| 505 | |
| 506 | // Change the ortho projection |
| 507 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 508 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 509 | |
| 510 | return true; |
| 511 | } |
| 512 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 513 | /** |
| 514 | * Read the documentation of createLayer() before doing anything in this method. |
| 515 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 516 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 517 | if (!current->layer) { |
| 518 | LOGE("Attempting to compose a layer that does not exist"); |
| 519 | return; |
| 520 | } |
| 521 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 522 | const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 523 | |
| 524 | if (fboLayer) { |
| 525 | // Unbind current FBO and restore previous one |
| 526 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 527 | } |
| 528 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 529 | Layer* layer = current->layer; |
| 530 | const Rect& rect = layer->layer; |
| 531 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 532 | if (!fboLayer && layer->alpha < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 533 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 534 | layer->alpha << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 535 | // Required below, composeLayerRect() will divide by 255 |
| 536 | layer->alpha = 255; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 539 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 540 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 541 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 542 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 543 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 544 | // drawing only the dirty region |
| 545 | if (fboLayer) { |
| 546 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); |
| 547 | composeLayerRegion(layer, rect); |
| 548 | } else { |
| 549 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
| 550 | composeLayerRect(layer, rect, true); |
| 551 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 552 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 553 | if (fboLayer) { |
| 554 | // Detach the texture from the FBO |
| 555 | glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); |
| 556 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 557 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 558 | |
| 559 | // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed |
| 560 | mCaches.fboCache.put(current->fbo); |
| 561 | } |
| 562 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 563 | dirtyClip(); |
| 564 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 565 | // 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] | 566 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 567 | LAYER_LOGD("Deleting layer"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 568 | glDeleteTextures(1, &layer->texture); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 569 | delete layer; |
| 570 | } |
| 571 | } |
| 572 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 573 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
| 574 | const Rect& texCoords = layer->texCoords; |
| 575 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, texCoords.right, texCoords.bottom); |
| 576 | |
| 577 | drawTextureMesh(rect.left, rect.top, rect.right, rect.bottom, layer->texture, |
| 578 | layer->alpha / 255.0f, layer->mode, layer->blend, &mMeshVertices[0].position[0], |
| 579 | &mMeshVertices[0].texture[0], GL_TRIANGLE_STRIP, gMeshCount, swap, swap); |
| 580 | |
| 581 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 582 | } |
| 583 | |
| 584 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
| 585 | #if RENDER_LAYERS_AS_REGIONS |
| 586 | if (layer->region.isRect()) { |
| 587 | composeLayerRect(layer, rect); |
| 588 | layer->region.clear(); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | if (!layer->region.isEmpty()) { |
| 593 | size_t count; |
| 594 | const android::Rect* rects = layer->region.getArray(&count); |
| 595 | |
| 596 | setupDraw(); |
| 597 | |
| 598 | ProgramDescription description; |
| 599 | description.hasTexture = true; |
| 600 | |
| 601 | const float alpha = layer->alpha / 255.0f; |
| 602 | const bool setColor = description.setColor(alpha, alpha, alpha, alpha); |
| 603 | chooseBlending(layer->blend || layer->alpha < 255, layer->mode, description, false); |
| 604 | |
| 605 | useProgram(mCaches.programCache.get(description)); |
| 606 | |
| 607 | // Texture |
| 608 | bindTexture(layer->texture); |
| 609 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
| 610 | |
| 611 | // Always premultiplied |
| 612 | if (setColor) { |
| 613 | mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); |
| 614 | } |
| 615 | |
| 616 | // Mesh |
| 617 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
| 618 | glEnableVertexAttribArray(texCoordsSlot); |
| 619 | |
| 620 | mModelView.loadIdentity(); |
| 621 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 622 | |
| 623 | const float texX = 1.0f / float(layer->width); |
| 624 | const float texY = 1.0f / float(layer->height); |
| 625 | |
| 626 | TextureVertex* mesh = mCaches.getRegionMesh(); |
| 627 | GLsizei numQuads = 0; |
| 628 | |
| 629 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 630 | gMeshStride, &mesh[0].position[0]); |
| 631 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, |
| 632 | gMeshStride, &mesh[0].texture[0]); |
| 633 | |
| 634 | for (size_t i = 0; i < count; i++) { |
| 635 | const android::Rect* r = &rects[i]; |
| 636 | |
| 637 | const float u1 = r->left * texX; |
| 638 | const float v1 = (rect.getHeight() - r->top) * texY; |
| 639 | const float u2 = r->right * texX; |
| 640 | const float v2 = (rect.getHeight() - r->bottom) * texY; |
| 641 | |
| 642 | // TODO: Reject quads outside of the clip |
| 643 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 644 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 645 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 646 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 647 | |
| 648 | numQuads++; |
| 649 | |
| 650 | if (numQuads >= REGION_MESH_QUAD_COUNT) { |
| 651 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 652 | numQuads = 0; |
| 653 | mesh = mCaches.getRegionMesh(); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (numQuads > 0) { |
| 658 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 659 | } |
| 660 | |
| 661 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 662 | glDisableVertexAttribArray(texCoordsSlot); |
| 663 | |
| 664 | #if DEBUG_LAYERS_AS_REGIONS |
| 665 | uint32_t colors[] = { |
| 666 | 0x7fff0000, 0x7f00ff00, |
| 667 | 0x7f0000ff, 0x7fff00ff, |
| 668 | }; |
| 669 | |
| 670 | int offset = 0; |
| 671 | int32_t top = rects[0].top; |
| 672 | int i = 0; |
| 673 | |
| 674 | for (size_t i = 0; i < count; i++) { |
| 675 | if (top != rects[i].top) { |
| 676 | offset ^= 0x2; |
| 677 | top = rects[i].top; |
| 678 | } |
| 679 | |
| 680 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
| 681 | drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], |
| 682 | SkXfermode::kSrcOver_Mode); |
| 683 | } |
| 684 | #endif |
| 685 | |
| 686 | layer->region.clear(); |
| 687 | } |
| 688 | #else |
| 689 | composeLayerRect(layer, rect); |
| 690 | #endif |
| 691 | } |
| 692 | |
| 693 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 694 | const float right, const float bottom, const mat4 transform) { |
| 695 | #if RENDER_LAYERS_AS_REGIONS |
| 696 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) { |
| 697 | Rect bounds(left, top, right, bottom); |
| 698 | transform.mapRect(bounds); |
| 699 | bounds.intersect(*mSnapshot->clipRect); |
| 700 | bounds.snapToPixelBoundaries(); |
| 701 | |
| 702 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 703 | if (!dirty.isEmpty()) { |
| 704 | mSnapshot->region->orSelf(dirty); |
| 705 | } |
| 706 | } |
| 707 | #endif |
| 708 | } |
| 709 | |
| 710 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 711 | const float right, const float bottom) { |
| 712 | #if RENDER_LAYERS_AS_REGIONS |
| 713 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region) { |
| 714 | Rect bounds(left, top, right, bottom); |
| 715 | bounds.intersect(*mSnapshot->clipRect); |
| 716 | bounds.snapToPixelBoundaries(); |
| 717 | |
| 718 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 719 | if (!dirty.isEmpty()) { |
| 720 | mSnapshot->region->orSelf(dirty); |
| 721 | } |
| 722 | } |
| 723 | #endif |
| 724 | } |
| 725 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 726 | void OpenGLRenderer::setupDraw() { |
| 727 | clearLayerRegions(); |
| 728 | if (mDirtyClip) { |
| 729 | setScissorFromClip(); |
| 730 | } |
| 731 | } |
| 732 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 733 | void OpenGLRenderer::clearLayerRegions() { |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 734 | if (mLayers.size() == 0 || mSnapshot->isIgnored()) return; |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 735 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 736 | Rect clipRect(*mSnapshot->clipRect); |
| 737 | clipRect.snapToPixelBoundaries(); |
| 738 | |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 739 | for (uint32_t i = 0; i < mLayers.size(); i++) { |
| 740 | Rect* bounds = mLayers.itemAt(i); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 741 | if (clipRect.intersects(*bounds)) { |
| 742 | // Clear the framebuffer where the layer will draw |
| 743 | glScissor(bounds->left, mSnapshot->height - bounds->bottom, |
| 744 | bounds->getWidth(), bounds->getHeight()); |
| 745 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 746 | glClear(GL_COLOR_BUFFER_BIT); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 747 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 748 | // Restore the clip |
| 749 | dirtyClip(); |
| 750 | } |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 751 | |
| 752 | delete bounds; |
| 753 | } |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 754 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 755 | mLayers.clear(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 758 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 759 | // Transforms |
| 760 | /////////////////////////////////////////////////////////////////////////////// |
| 761 | |
| 762 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 763 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 767 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 771 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 775 | mSnapshot->transform->load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 778 | const float* OpenGLRenderer::getMatrix() const { |
Romain Guy | 99bcdc5 | 2010-10-13 15:17:00 -0700 | [diff] [blame] | 779 | if (mSnapshot->fbo != 0) { |
| 780 | return &mSnapshot->transform->data[0]; |
| 781 | } |
| 782 | return &mIdentity.data[0]; |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 785 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 786 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 790 | SkMatrix transform; |
| 791 | mSnapshot->transform->copyTo(transform); |
| 792 | transform.preConcat(*matrix); |
| 793 | mSnapshot->transform->load(transform); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /////////////////////////////////////////////////////////////////////////////// |
| 797 | // Clipping |
| 798 | /////////////////////////////////////////////////////////////////////////////// |
| 799 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 800 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 801 | Rect clip(*mSnapshot->clipRect); |
| 802 | clip.snapToPixelBoundaries(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 803 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 804 | mDirtyClip = false; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 808 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 811 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 812 | if (mSnapshot->isIgnored()) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 813 | return true; |
| 814 | } |
| 815 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 816 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 817 | mSnapshot->transform->mapRect(r); |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame] | 818 | r.snapToPixelBoundaries(); |
| 819 | |
| 820 | Rect clipRect(*mSnapshot->clipRect); |
| 821 | clipRect.snapToPixelBoundaries(); |
| 822 | |
| 823 | return !clipRect.intersects(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 826 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 827 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 828 | if (clipped) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 829 | dirtyClip(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 830 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 831 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 834 | /////////////////////////////////////////////////////////////////////////////// |
| 835 | // Drawing |
| 836 | /////////////////////////////////////////////////////////////////////////////// |
| 837 | |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 838 | void OpenGLRenderer::drawDisplayList(DisplayList* displayList) { |
| 839 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 840 | // will be performed by the display list itself |
| 841 | if (displayList) { |
| 842 | displayList->replay(*this); |
| 843 | } |
| 844 | } |
| 845 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 846 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 847 | const float right = left + bitmap->width(); |
| 848 | const float bottom = top + bitmap->height(); |
| 849 | |
| 850 | if (quickReject(left, top, right, bottom)) { |
| 851 | return; |
| 852 | } |
| 853 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 854 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 855 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 856 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 857 | const AutoTexture autoCleanup(texture); |
| 858 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 859 | drawTextureRect(left, top, right, bottom, texture, paint); |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 860 | } |
| 861 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 862 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 863 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 864 | const mat4 transform(*matrix); |
| 865 | transform.mapRect(r); |
| 866 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 867 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 868 | return; |
| 869 | } |
| 870 | |
Romain Guy | 61c8c9c | 2010-08-09 20:48:09 -0700 | [diff] [blame] | 871 | glActiveTexture(GL_TEXTURE0); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 872 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 873 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 874 | const AutoTexture autoCleanup(texture); |
| 875 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 876 | // This could be done in a cheaper way, all we need is pass the matrix |
| 877 | // to the vertex shader. The save/restore is a bit overkill. |
| 878 | save(SkCanvas::kMatrix_SaveFlag); |
| 879 | concatMatrix(matrix); |
| 880 | drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); |
| 881 | restore(); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 882 | } |
| 883 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 884 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 885 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 886 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 887 | SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 888 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 889 | return; |
| 890 | } |
| 891 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 892 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 893 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 894 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 895 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 896 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 897 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 898 | const float width = texture->width; |
| 899 | const float height = texture->height; |
| 900 | |
| 901 | const float u1 = srcLeft / width; |
| 902 | const float v1 = srcTop / height; |
| 903 | const float u2 = srcRight / width; |
| 904 | const float v2 = srcBottom / height; |
| 905 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 906 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 907 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 908 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 909 | int alpha; |
| 910 | SkXfermode::Mode mode; |
| 911 | getAlphaAndMode(paint, &alpha, &mode); |
| 912 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 913 | if (mSnapshot->transform->isPureTranslate()) { |
| 914 | const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); |
| 915 | const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); |
| 916 | |
| 917 | drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), |
| 918 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 919 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 920 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 921 | } else { |
| 922 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, |
| 923 | mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 924 | GL_TRIANGLE_STRIP, gMeshCount); |
| 925 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 926 | |
| 927 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 928 | } |
| 929 | |
Romain Guy | 4aa9057 | 2010-09-26 18:40:37 -0700 | [diff] [blame] | 930 | 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] | 931 | 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] | 932 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 933 | if (quickReject(left, top, right, bottom)) { |
| 934 | return; |
| 935 | } |
| 936 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 937 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 938 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 939 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 940 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 941 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 942 | |
| 943 | int alpha; |
| 944 | SkXfermode::Mode mode; |
| 945 | getAlphaAndMode(paint, &alpha, &mode); |
| 946 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 947 | const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 948 | right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 949 | |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 950 | if (mesh && mesh->verticesCount > 0) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 951 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 952 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 953 | // Mark the current layer dirty where we are going to draw the patch |
| 954 | if ((mSnapshot->flags & Snapshot::kFlagFboTarget) && |
| 955 | mSnapshot->region && mesh->hasEmptyQuads) { |
| 956 | const size_t count = mesh->quads.size(); |
| 957 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 958 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 959 | if (pureTranslate) { |
| 960 | const float x = (int) floorf(bounds.left + 0.5f); |
| 961 | const float y = (int) floorf(bounds.top + 0.5f); |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 962 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 963 | *mSnapshot->transform); |
| 964 | } else { |
| 965 | dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, |
| 966 | *mSnapshot->transform); |
| 967 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 968 | } |
| 969 | } |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 970 | #endif |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 971 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 972 | if (pureTranslate) { |
| 973 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 974 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 975 | |
| 976 | drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, |
| 977 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 978 | GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, |
| 979 | true, !mesh->hasEmptyQuads); |
| 980 | } else { |
| 981 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, |
| 982 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 983 | GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, |
| 984 | true, !mesh->hasEmptyQuads); |
| 985 | } |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 986 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 989 | void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 990 | // TODO: Should do quickReject for each line |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 991 | if (mSnapshot->isIgnored()) return; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 992 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 993 | const bool isAA = paint->isAntiAlias(); |
| 994 | const float strokeWidth = paint->getStrokeWidth() * 0.5f; |
| 995 | // A stroke width of 0 has a special meaningin Skia: |
| 996 | // it draws an unscaled 1px wide line |
| 997 | const bool isHairLine = paint->getStrokeWidth() == 0.0f; |
| 998 | |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 999 | setupDraw(); |
| 1000 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1001 | int alpha; |
| 1002 | SkXfermode::Mode mode; |
| 1003 | getAlphaAndMode(paint, &alpha, &mode); |
| 1004 | |
| 1005 | uint32_t color = paint->getColor(); |
| 1006 | const GLfloat a = alpha / 255.0f; |
| 1007 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1008 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1009 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1010 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1011 | // Used only with AA lines |
| 1012 | GLuint textureUnit = 0; |
| 1013 | |
| 1014 | // Describe the required shaders |
| 1015 | ProgramDescription description; |
| 1016 | const bool setColor = description.setColor(r, g, b, a); |
| 1017 | |
| 1018 | if (mShader) { |
| 1019 | mShader->describe(description, mCaches.extensions); |
| 1020 | } |
| 1021 | if (mColorFilter) { |
| 1022 | mColorFilter->describe(description, mCaches.extensions); |
| 1023 | } |
| 1024 | |
| 1025 | // Setup the blending mode |
| 1026 | chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); |
| 1027 | |
| 1028 | // We're not drawing with VBOs here |
| 1029 | mCaches.unbindMeshBuffer(); |
| 1030 | |
| 1031 | int verticesCount = count >> 2; |
| 1032 | if (!isHairLine) { |
| 1033 | // TODO: AA needs more vertices |
| 1034 | verticesCount *= 6; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1035 | } else { |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1036 | // TODO: AA will be different |
| 1037 | verticesCount *= 2; |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1040 | TextureVertex lines[verticesCount]; |
| 1041 | TextureVertex* vertex = &lines[0]; |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1042 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1043 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1044 | gMeshStride, vertex); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1045 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1046 | mModelView.loadIdentity(); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1047 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1048 | // Build and use the appropriate shader |
| 1049 | useProgram(mCaches.programCache.get(description)); |
| 1050 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
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 | if (!mShader || (mShader && setColor)) { |
| 1053 | mCaches.currentProgram->setColor(r, g, b, a); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1056 | if (mShader) { |
| 1057 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
| 1058 | } |
| 1059 | if (mColorFilter) { |
| 1060 | mColorFilter->setupProgram(mCaches.currentProgram); |
| 1061 | } |
| 1062 | |
| 1063 | if (!isHairLine) { |
| 1064 | // TODO: Handle the AA case |
| 1065 | for (int i = 0; i < count; i += 4) { |
| 1066 | // a = start point, b = end point |
| 1067 | vec2 a(points[i], points[i + 1]); |
| 1068 | vec2 b(points[i + 2], points[i + 3]); |
| 1069 | |
| 1070 | // Bias to snap to the same pixels as Skia |
| 1071 | a += 0.375; |
| 1072 | b += 0.375; |
| 1073 | |
| 1074 | // Find the normal to the line |
| 1075 | vec2 n = (b - a).copyNormalized() * strokeWidth; |
| 1076 | float x = n.x; |
| 1077 | n.x = -n.y; |
| 1078 | n.y = x; |
| 1079 | |
| 1080 | // Four corners of the rectangle defining a thick line |
| 1081 | vec2 p1 = a - n; |
| 1082 | vec2 p2 = a + n; |
| 1083 | vec2 p3 = b + n; |
| 1084 | vec2 p4 = b - n; |
| 1085 | |
| 1086 | // Draw the line as 2 triangles, could be optimized |
| 1087 | // by using only 4 vertices and the correct indices |
| 1088 | // Also we should probably used non textured vertices |
| 1089 | // when line AA is disabled to save on bandwidth |
| 1090 | TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); |
| 1091 | TextureVertex::set(vertex++, p2.x, p2.y, 0.0f, 0.0f); |
| 1092 | TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); |
| 1093 | TextureVertex::set(vertex++, p1.x, p1.y, 0.0f, 0.0f); |
| 1094 | TextureVertex::set(vertex++, p3.x, p3.y, 0.0f, 0.0f); |
| 1095 | TextureVertex::set(vertex++, p4.x, p4.y, 0.0f, 0.0f); |
| 1096 | |
| 1097 | // TODO: Mark the dirty regions when RENDER_LAYERS_AS_REGIONS is set |
| 1098 | } |
| 1099 | |
| 1100 | // GL_LINE does not give the result we want to match Skia |
| 1101 | glDrawArrays(GL_TRIANGLES, 0, verticesCount); |
| 1102 | } else { |
| 1103 | // TODO: Handle the AA case |
| 1104 | for (int i = 0; i < count; i += 4) { |
| 1105 | TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); |
| 1106 | TextureVertex::set(vertex++, points[i + 2], points[i + 3], 0.0f, 0.0f); |
| 1107 | } |
| 1108 | glLineWidth(1.0f); |
| 1109 | glDrawArrays(GL_LINES, 0, verticesCount); |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 1110 | } |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1113 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 1114 | // No need to check against the clip, we fill the clip region |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 1115 | if (mSnapshot->isIgnored()) return; |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 1116 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 1117 | Rect& clip(*mSnapshot->clipRect); |
| 1118 | clip.snapToPixelBoundaries(); |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 1119 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1120 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1121 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1122 | 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] | 1123 | if (quickReject(left, top, right, bottom)) { |
| 1124 | return; |
| 1125 | } |
| 1126 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1127 | SkXfermode::Mode mode; |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1128 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1129 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 1130 | if (!isMode) { |
| 1131 | // Assume SRC_OVER |
| 1132 | mode = SkXfermode::kSrcOver_Mode; |
| 1133 | } |
| 1134 | } else { |
| 1135 | mode = getXfermode(p->getXfermode()); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | // Skia draws using the color's alpha channel if < 255 |
| 1139 | // Otherwise, it uses the paint's alpha |
| 1140 | int color = p->getColor(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1141 | if (((color >> 24) & 0xff) == 255) { |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1142 | color |= p->getAlpha() << 24; |
| 1143 | } |
| 1144 | |
| 1145 | drawColorRect(left, top, right, bottom, color, mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1146 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1147 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1148 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 1149 | float x, float y, SkPaint* paint) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1150 | if (text == NULL || count == 0 || (paint->getAlpha() == 0 && paint->getXfermode() == NULL)) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1151 | return; |
| 1152 | } |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 1153 | if (mSnapshot->isIgnored()) return; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 1154 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1155 | paint->setAntiAlias(true); |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1156 | |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 1157 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 1158 | switch (paint->getTextAlign()) { |
| 1159 | case SkPaint::kCenter_Align: |
| 1160 | length = paint->measureText(text, bytesCount); |
| 1161 | x -= length / 2.0f; |
| 1162 | break; |
| 1163 | case SkPaint::kRight_Align: |
| 1164 | length = paint->measureText(text, bytesCount); |
| 1165 | x -= length; |
| 1166 | break; |
| 1167 | default: |
| 1168 | break; |
| 1169 | } |
| 1170 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1171 | // TODO: Handle paint->getTextScaleX() |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 1172 | const float oldX = x; |
| 1173 | const float oldY = y; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1174 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
| 1175 | if (pureTranslate) { |
| 1176 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1177 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1178 | } |
| 1179 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1180 | int alpha; |
| 1181 | SkXfermode::Mode mode; |
| 1182 | getAlphaAndMode(paint, &alpha, &mode); |
| 1183 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 1184 | uint32_t color = paint->getColor(); |
| 1185 | const GLfloat a = alpha / 255.0f; |
| 1186 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1187 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1188 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1189 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 1190 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 1191 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1192 | paint->getTextSize()); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 1193 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1194 | setupDraw(); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 1195 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1196 | if (mHasShadow) { |
| 1197 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 1198 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1199 | const ShadowTexture* shadow = mCaches.dropShadowCache.get(paint, text, bytesCount, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1200 | count, mShadowRadius); |
| 1201 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1202 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1203 | setupShadow(shadow, x, y, mode, a, pureTranslate); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1204 | |
| 1205 | // Draw the mesh |
| 1206 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1207 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1210 | GLuint textureUnit = 0; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1211 | glActiveTexture(gTextureUnits[textureUnit]); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1212 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1213 | // Pick the appropriate texture filtering |
| 1214 | bool linearFilter = mSnapshot->transform->changesBounds(); |
| 1215 | if (pureTranslate && !linearFilter) { |
| 1216 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 1217 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1218 | |
| 1219 | // 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] | 1220 | setupTextureAlpha8(fontRenderer.getTexture(linearFilter), 0, 0, textureUnit, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1221 | 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] | 1222 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1223 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1224 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 1225 | |
| 1226 | #if RENDER_LAYERS_AS_REGIONS |
| 1227 | bool hasLayer = (mSnapshot->flags & Snapshot::kFlagFboTarget) && mSnapshot->region; |
| 1228 | #else |
| 1229 | bool hasLayer = false; |
| 1230 | #endif |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 1231 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1232 | mCaches.unbindMeshBuffer(); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1233 | if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1234 | hasLayer ? &bounds : NULL)) { |
| 1235 | #if RENDER_LAYERS_AS_REGIONS |
| 1236 | if (hasLayer) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1237 | if (!pureTranslate) { |
| 1238 | mSnapshot->transform->mapRect(bounds); |
| 1239 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1240 | bounds.intersect(*mSnapshot->clipRect); |
| 1241 | bounds.snapToPixelBoundaries(); |
| 1242 | |
| 1243 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 1244 | mSnapshot->region->orSelf(dirty); |
| 1245 | } |
| 1246 | #endif |
| 1247 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1248 | |
| 1249 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1250 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 1251 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 1252 | drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1255 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 1256 | if (mSnapshot->isIgnored()) return; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1257 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1258 | GLuint textureUnit = 0; |
| 1259 | glActiveTexture(gTextureUnits[textureUnit]); |
| 1260 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1261 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b9 | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1262 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1263 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1264 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 1265 | const float x = texture->left - texture->offset; |
| 1266 | const float y = texture->top - texture->offset; |
| 1267 | |
| 1268 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 1269 | return; |
| 1270 | } |
| 1271 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1272 | int alpha; |
| 1273 | SkXfermode::Mode mode; |
| 1274 | getAlphaAndMode(paint, &alpha, &mode); |
| 1275 | |
| 1276 | uint32_t color = paint->getColor(); |
| 1277 | const GLfloat a = alpha / 255.0f; |
| 1278 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1279 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1280 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1281 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1282 | setupTextureAlpha8(texture, textureUnit, x, y, r, g, b, a, mode, true, true); |
| 1283 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1284 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1285 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1286 | // Draw the mesh |
| 1287 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1288 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1291 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1292 | // Shaders |
| 1293 | /////////////////////////////////////////////////////////////////////////////// |
| 1294 | |
| 1295 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1296 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1299 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 1300 | mShader = shader; |
| 1301 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1302 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1303 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 1304 | } |
| 1305 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1306 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1307 | // Color filters |
| 1308 | /////////////////////////////////////////////////////////////////////////////// |
| 1309 | |
| 1310 | void OpenGLRenderer::resetColorFilter() { |
| 1311 | mColorFilter = NULL; |
| 1312 | } |
| 1313 | |
| 1314 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 1315 | mColorFilter = filter; |
| 1316 | } |
| 1317 | |
| 1318 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1319 | // Drop shadow |
| 1320 | /////////////////////////////////////////////////////////////////////////////// |
| 1321 | |
| 1322 | void OpenGLRenderer::resetShadow() { |
| 1323 | mHasShadow = false; |
| 1324 | } |
| 1325 | |
| 1326 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 1327 | mHasShadow = true; |
| 1328 | mShadowRadius = radius; |
| 1329 | mShadowDx = dx; |
| 1330 | mShadowDy = dy; |
| 1331 | mShadowColor = color; |
| 1332 | } |
| 1333 | |
| 1334 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1335 | // Drawing implementation |
| 1336 | /////////////////////////////////////////////////////////////////////////////// |
| 1337 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1338 | void OpenGLRenderer::setupShadow(const ShadowTexture* texture, float x, float y, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1339 | SkXfermode::Mode mode, float alpha, bool ignoreTransforms) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1340 | const float sx = x - texture->left + mShadowDx; |
| 1341 | const float sy = y - texture->top + mShadowDy; |
| 1342 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 1343 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
| 1344 | const GLfloat a = shadowAlpha < 255 ? shadowAlpha / 255.0f : alpha; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1345 | const GLfloat r = a * ((mShadowColor >> 16) & 0xFF) / 255.0f; |
| 1346 | const GLfloat g = a * ((mShadowColor >> 8) & 0xFF) / 255.0f; |
| 1347 | const GLfloat b = a * ((mShadowColor ) & 0xFF) / 255.0f; |
| 1348 | |
| 1349 | GLuint textureUnit = 0; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1350 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
| 1351 | sx, sy, r, g, b, a, mode, true, false, |
| 1352 | (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, 0, ignoreTransforms); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | void OpenGLRenderer::setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, |
| 1356 | float x, float y, float r, float g, float b, float a, SkXfermode::Mode mode, |
| 1357 | bool transforms, bool applyFilters) { |
| 1358 | setupTextureAlpha8(texture->id, texture->width, texture->height, textureUnit, |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1359 | x, y, r, g, b, a, mode, transforms, applyFilters, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1360 | (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1364 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1365 | SkXfermode::Mode mode, bool transforms, bool applyFilters) { |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1366 | setupTextureAlpha8(texture, width, height, textureUnit, x, y, r, g, b, a, mode, |
| 1367 | transforms, applyFilters, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset); |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | void OpenGLRenderer::setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, |
| 1371 | GLuint& textureUnit, float x, float y, float r, float g, float b, float a, |
| 1372 | SkXfermode::Mode mode, bool transforms, bool applyFilters, |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1373 | GLvoid* vertices, GLvoid* texCoords, GLuint vbo, bool ignoreTransform) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1374 | // Describe the required shaders |
| 1375 | ProgramDescription description; |
| 1376 | description.hasTexture = true; |
| 1377 | description.hasAlpha8Texture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1378 | const bool setColor = description.setAlpha8Color(r, g, b, a); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1379 | |
| 1380 | if (applyFilters) { |
| 1381 | if (mShader) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1382 | mShader->describe(description, mCaches.extensions); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1383 | } |
| 1384 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1385 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1389 | // Setup the blending mode |
| 1390 | chooseBlending(true, mode, description); |
| 1391 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1392 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1393 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1394 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1395 | bindTexture(texture); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1396 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1397 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1398 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1399 | glEnableVertexAttribArray(texCoordsSlot); |
| 1400 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1401 | if (texCoords) { |
| 1402 | // Setup attributes |
| 1403 | if (!vertices) { |
| 1404 | mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1405 | } else { |
| 1406 | mCaches.unbindMeshBuffer(); |
| 1407 | } |
| 1408 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1409 | gMeshStride, vertices); |
| 1410 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
| 1411 | } |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1412 | |
| 1413 | // Setup uniforms |
| 1414 | if (transforms) { |
| 1415 | mModelView.loadTranslate(x, y, 0.0f); |
| 1416 | mModelView.scale(width, height, 1.0f); |
| 1417 | } else { |
| 1418 | mModelView.loadIdentity(); |
| 1419 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1420 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1421 | mat4 t; |
| 1422 | if (!ignoreTransform) { |
| 1423 | t.load(*mSnapshot->transform); |
| 1424 | } |
| 1425 | |
| 1426 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, t); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1427 | if (width > 0 && height > 0) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1428 | dirtyLayer(x, y, x + width, y + height, t); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1431 | if (setColor) { |
| 1432 | mCaches.currentProgram->setColor(r, g, b, a); |
| 1433 | } |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1434 | |
| 1435 | textureUnit++; |
| 1436 | if (applyFilters) { |
| 1437 | // Setup attributes and uniforms required by the shaders |
| 1438 | if (mShader) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1439 | if (ignoreTransform) { |
| 1440 | mModelView.loadInverse(*mSnapshot->transform); |
| 1441 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1442 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1443 | } |
| 1444 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1445 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1450 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1451 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 1452 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 1453 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 1454 | |
| 1455 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 1456 | float x, float y, SkPaint* paint) { |
| 1457 | // Handle underline and strike-through |
| 1458 | uint32_t flags = paint->getFlags(); |
| 1459 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
| 1460 | float underlineWidth = length; |
| 1461 | // If length is > 0.0f, we already measured the text for the text alignment |
| 1462 | if (length <= 0.0f) { |
| 1463 | underlineWidth = paint->measureText(text, bytesCount); |
| 1464 | } |
| 1465 | |
| 1466 | float offsetX = 0; |
| 1467 | switch (paint->getTextAlign()) { |
| 1468 | case SkPaint::kCenter_Align: |
| 1469 | offsetX = underlineWidth * 0.5f; |
| 1470 | break; |
| 1471 | case SkPaint::kRight_Align: |
| 1472 | offsetX = underlineWidth; |
| 1473 | break; |
| 1474 | default: |
| 1475 | break; |
| 1476 | } |
| 1477 | |
| 1478 | if (underlineWidth > 0.0f) { |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1479 | const float textSize = paint->getTextSize(); |
| 1480 | const float strokeWidth = textSize * kStdUnderline_Thickness; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1481 | |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1482 | const float left = x - offsetX; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1483 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1484 | |
| 1485 | const int pointsCount = 4 * (flags & SkPaint::kStrikeThruText_Flag ? 2 : 1); |
| 1486 | float points[pointsCount]; |
| 1487 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1488 | |
| 1489 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 1490 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1491 | points[currentPoint++] = left; |
| 1492 | points[currentPoint++] = top; |
| 1493 | points[currentPoint++] = left + underlineWidth; |
| 1494 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 1498 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1499 | points[currentPoint++] = left; |
| 1500 | points[currentPoint++] = top; |
| 1501 | points[currentPoint++] = left + underlineWidth; |
| 1502 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1503 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 1504 | |
| 1505 | SkPaint linesPaint(*paint); |
| 1506 | linesPaint.setStrokeWidth(strokeWidth); |
| 1507 | |
| 1508 | drawLines(&points[0], pointsCount, &linesPaint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1513 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1514 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1515 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1516 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1517 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1518 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1519 | color |= 0x00ffffff; |
| 1520 | } |
| 1521 | |
| 1522 | // Render using pre-multiplied alpha |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1523 | const int alpha = (color >> 24) & 0xFF; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1524 | const GLfloat a = alpha / 255.0f; |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1525 | const GLfloat r = a * ((color >> 16) & 0xFF) / 255.0f; |
| 1526 | const GLfloat g = a * ((color >> 8) & 0xFF) / 255.0f; |
| 1527 | const GLfloat b = a * ((color ) & 0xFF) / 255.0f; |
| 1528 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1529 | setupColorRect(left, top, right, bottom, r, g, b, a, mode, ignoreTransform); |
| 1530 | |
| 1531 | // Draw the mesh |
| 1532 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 1533 | } |
| 1534 | |
| 1535 | void OpenGLRenderer::setupColorRect(float left, float top, float right, float bottom, |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 1536 | float r, float g, float b, float a, SkXfermode::Mode mode, |
| 1537 | bool ignoreTransform, bool ignoreMatrix) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1538 | GLuint textureUnit = 0; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1539 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1540 | // Describe the required shaders |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1541 | ProgramDescription description; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1542 | const bool setColor = description.setColor(r, g, b, a); |
| 1543 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1544 | if (mShader) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1545 | mShader->describe(description, mCaches.extensions); |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1546 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1547 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1548 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1549 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1550 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1551 | // Setup the blending mode |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 1552 | chooseBlending(a < 1.0f || (mShader && mShader->blend()), mode, description); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1553 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1554 | // Build and use the appropriate shader |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1555 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1556 | |
| 1557 | // Setup attributes |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1558 | mCaches.bindMeshBuffer(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1559 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1560 | gMeshStride, 0); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1561 | |
Romain Guy | 7f78b0c | 2010-11-04 14:36:26 -0700 | [diff] [blame] | 1562 | if (!ignoreMatrix) { |
| 1563 | // Setup uniforms |
| 1564 | mModelView.loadTranslate(left, top, 0.0f); |
| 1565 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 1566 | if (!ignoreTransform) { |
| 1567 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1568 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1569 | } else { |
| 1570 | mat4 identity; |
| 1571 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
| 1572 | dirtyLayer(left, top, right, bottom); |
| 1573 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1574 | } |
Romain Guy | 01d0657 | 2010-11-11 12:06:27 -0800 | [diff] [blame] | 1575 | if (!mShader || (mShader && setColor)) { |
| 1576 | mCaches.currentProgram->setColor(r, g, b, a); |
| 1577 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1578 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 1579 | // Setup attributes and uniforms required by the shaders |
| 1580 | if (mShader) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1581 | if (ignoreMatrix) { |
| 1582 | mModelView.loadIdentity(); |
| 1583 | } else if (ignoreTransform) { |
| 1584 | mModelView.loadInverse(*mSnapshot->transform); |
| 1585 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1586 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &textureUnit); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 1587 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1588 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1589 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1590 | } |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1593 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1594 | Texture* texture, SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1595 | int alpha; |
| 1596 | SkXfermode::Mode mode; |
| 1597 | getAlphaAndMode(paint, &alpha, &mode); |
| 1598 | |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1599 | setTextureWrapModes(texture, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
| 1600 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1601 | if (mSnapshot->transform->isPureTranslate()) { |
| 1602 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1603 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1604 | |
| 1605 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
| 1606 | alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, |
| 1607 | (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 1608 | } else { |
| 1609 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
| 1610 | texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, |
| 1611 | GL_TRIANGLE_STRIP, gMeshCount); |
| 1612 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1615 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1616 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 1617 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1618 | (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1622 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1623 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1624 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1625 | setupDraw(); |
Romain Guy | 8694230 | 2010-09-12 13:02:16 -0700 | [diff] [blame] | 1626 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1627 | ProgramDescription description; |
| 1628 | description.hasTexture = true; |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1629 | const bool setColor = description.setColor(alpha, alpha, alpha, alpha); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1630 | if (mColorFilter) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1631 | mColorFilter->describe(description, mCaches.extensions); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1632 | } |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1633 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1634 | mModelView.loadTranslate(left, top, 0.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1635 | if (!ignoreScale) { |
| 1636 | mModelView.scale(right - left, bottom - top, 1.0f); |
| 1637 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1638 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1639 | chooseBlending(blend || alpha < 1.0f, mode, description, swapSrcDst); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1640 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1641 | useProgram(mCaches.programCache.get(description)); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1642 | if (!ignoreTransform) { |
| 1643 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1644 | if (dirty) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1645 | } else { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1646 | mat4 identity; |
| 1647 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, identity); |
| 1648 | if (dirty) dirtyLayer(left, top, right, bottom); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1649 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1650 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1651 | // Texture |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1652 | bindTexture(texture); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1653 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), 0); |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 1654 | |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 1655 | // Always premultiplied |
Romain Guy | 707b2f7 | 2010-10-11 16:34:59 -0700 | [diff] [blame] | 1656 | if (setColor) { |
| 1657 | mCaches.currentProgram->setColor(alpha, alpha, alpha, alpha); |
| 1658 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1659 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1660 | // Mesh |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1661 | int texCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1662 | glEnableVertexAttribArray(texCoordsSlot); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1663 | |
| 1664 | if (!vertices) { |
| 1665 | mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1666 | } else { |
| 1667 | mCaches.unbindMeshBuffer(); |
| 1668 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1669 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1670 | gMeshStride, vertices); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1671 | glVertexAttribPointer(texCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1672 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1673 | // Color filter |
| 1674 | if (mColorFilter) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1675 | mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 1678 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1679 | glDisableVertexAttribArray(texCoordsSlot); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1682 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1683 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1684 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 1685 | if (blend) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1686 | if (mode < SkXfermode::kPlus_Mode) { |
| 1687 | if (!mCaches.blend) { |
| 1688 | glEnable(GL_BLEND); |
| 1689 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1690 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1691 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 1692 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1693 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1694 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 1695 | glBlendFunc(sourceMode, destMode); |
| 1696 | mCaches.lastSrcMode = sourceMode; |
| 1697 | mCaches.lastDstMode = destMode; |
| 1698 | } |
| 1699 | } else { |
| 1700 | // These blend modes are not supported by OpenGL directly and have |
| 1701 | // to be implemented using shaders. Since the shader will perform |
| 1702 | // the blending, turn blending off here |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1703 | if (mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1704 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1705 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | if (mCaches.blend) { |
| 1709 | glDisable(GL_BLEND); |
| 1710 | } |
| 1711 | blend = false; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1712 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1713 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1714 | glDisable(GL_BLEND); |
| 1715 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1716 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 1719 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1720 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1721 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 1722 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 1723 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1724 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1725 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1726 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 1727 | } |
| 1728 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1729 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 1730 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 1731 | TextureVertex::setUV(v++, u1, v1); |
| 1732 | TextureVertex::setUV(v++, u2, v1); |
| 1733 | TextureVertex::setUV(v++, u1, v2); |
| 1734 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1735 | } |
| 1736 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1737 | void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1738 | if (paint) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1739 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1740 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 1741 | if (!isMode) { |
| 1742 | // Assume SRC_OVER |
| 1743 | *mode = SkXfermode::kSrcOver_Mode; |
| 1744 | } |
| 1745 | } else { |
| 1746 | *mode = getXfermode(paint->getXfermode()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | // Skia draws using the color's alpha channel if < 255 |
| 1750 | // Otherwise, it uses the paint's alpha |
| 1751 | int color = paint->getColor(); |
| 1752 | *alpha = (color >> 24) & 0xFF; |
| 1753 | if (*alpha == 255) { |
| 1754 | *alpha = paint->getAlpha(); |
| 1755 | } |
| 1756 | } else { |
| 1757 | *mode = SkXfermode::kSrcOver_Mode; |
| 1758 | *alpha = 255; |
| 1759 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 1762 | SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { |
| 1763 | if (mode == NULL) { |
| 1764 | return SkXfermode::kSrcOver_Mode; |
| 1765 | } |
| 1766 | return mode->fMode; |
| 1767 | } |
| 1768 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1769 | void OpenGLRenderer::setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT) { |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1770 | bool bound = false; |
| 1771 | if (wrapS != texture->wrapS) { |
| 1772 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 1773 | bound = true; |
| 1774 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS); |
| 1775 | texture->wrapS = wrapS; |
| 1776 | } |
| 1777 | if (wrapT != texture->wrapT) { |
Romain Guy | 3e3ba15 | 2010-10-25 18:33:16 -0700 | [diff] [blame] | 1778 | if (!bound) { |
Romain Guy | 3e3ba15 | 2010-10-25 18:33:16 -0700 | [diff] [blame] | 1779 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 1780 | } |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1781 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT); |
| 1782 | texture->wrapT = wrapT; |
| 1783 | } |
Romain Guy | a1db574 | 2010-07-20 13:09:13 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1786 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1787 | }; // namespace android |