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