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