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 | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 29 | #include <private/hwui/DrawGlInfo.h> |
| 30 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 31 | #include <ui/Rect.h> |
| 32 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 33 | #include "OpenGLRenderer.h" |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 34 | #include "DisplayListRenderer.h" |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 35 | #include "Vector.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 38 | namespace uirenderer { |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Defines |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 44 | #define RAD_TO_DEG (180.0f / 3.14159265f) |
| 45 | #define MIN_ANGLE 0.001f |
| 46 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 47 | // TODO: This should be set in properties |
| 48 | #define ALPHA_THRESHOLD (0x7f / PANEL_BIT_DEPTH) |
| 49 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
| 51 | // Globals |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 56 | */ |
| 57 | struct Blender { |
| 58 | SkXfermode::Mode mode; |
| 59 | GLenum src; |
| 60 | GLenum dst; |
| 61 | }; // struct Blender |
| 62 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 63 | // In this array, the index of each Blender equals the value of the first |
| 64 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 65 | static const Blender gBlends[] = { |
Romain Guy | f504a2f | 2011-05-26 16:40:55 -0700 | [diff] [blame] | 66 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
Romain Guy | b146b12 | 2010-12-15 17:06:45 -0800 | [diff] [blame] | 67 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 68 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 69 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 70 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 71 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 72 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 73 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 74 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 75 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 76 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 77 | { 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] | 78 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 79 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 80 | // This array contains the swapped version of each SkXfermode. For instance |
| 81 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 82 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 83 | static const Blender gBlendsSwap[] = { |
Romain Guy | f504a2f | 2011-05-26 16:40:55 -0700 | [diff] [blame] | 84 | { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
Romain Guy | b146b12 | 2010-12-15 17:06:45 -0800 | [diff] [blame] | 85 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 86 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 87 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 88 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 89 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 90 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 91 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 92 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 93 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 94 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 95 | { 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] | 96 | }; |
| 97 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 98 | static const GLenum gTextureUnits[] = { |
Romain Guy | b146b12 | 2010-12-15 17:06:45 -0800 | [diff] [blame] | 99 | GL_TEXTURE0, |
| 100 | GL_TEXTURE1, |
| 101 | GL_TEXTURE2 |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 104 | /////////////////////////////////////////////////////////////////////////////// |
| 105 | // Constructors/destructor |
| 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 108 | OpenGLRenderer::OpenGLRenderer(): mCaches(Caches::getInstance()) { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 109 | mShader = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 110 | mColorFilter = NULL; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 111 | mHasShadow = false; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 112 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 113 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 114 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 115 | mFirstSnapshot = new Snapshot; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 118 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 119 | // The context has already been destroyed at this point, do not call |
| 120 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 123 | /////////////////////////////////////////////////////////////////////////////// |
| 124 | // Setup |
| 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 127 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | f2fc460 | 2011-07-19 15:20:03 -0700 | [diff] [blame] | 128 | glDisable(GL_DITHER); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 129 | glViewport(0, 0, width, height); |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 130 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 131 | |
| 132 | mWidth = width; |
| 133 | mHeight = height; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 134 | |
| 135 | mFirstSnapshot->height = height; |
| 136 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 137 | |
| 138 | mDirtyClip = false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 141 | void OpenGLRenderer::prepare(bool opaque) { |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 142 | prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); |
| 143 | } |
| 144 | |
| 145 | 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] | 146 | mCaches.clearGarbage(); |
| 147 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 148 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 149 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 84962f2 | 2011-03-02 15:43:44 -0800 | [diff] [blame] | 150 | mSnapshot->fbo = getTargetFbo(); |
| 151 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 152 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 153 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 154 | glViewport(0, 0, mWidth, mHeight); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 155 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 156 | glEnable(GL_SCISSOR_TEST); |
| 157 | glScissor(left, mSnapshot->height - bottom, right - left, bottom - top); |
| 158 | mSnapshot->setClip(left, top, right, bottom); |
| 159 | |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 160 | if (!opaque) { |
| 161 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 162 | glClear(GL_COLOR_BUFFER_BIT); |
| 163 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 166 | void OpenGLRenderer::finish() { |
| 167 | #if DEBUG_OPENGL |
| 168 | GLenum status = GL_NO_ERROR; |
| 169 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 170 | LOGD("GL error from OpenGLRenderer: 0x%x", status); |
Romain Guy | a07105b | 2011-01-10 21:14:18 -0800 | [diff] [blame] | 171 | switch (status) { |
| 172 | case GL_OUT_OF_MEMORY: |
| 173 | LOGE(" OpenGLRenderer is out of memory!"); |
| 174 | break; |
| 175 | } |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 176 | } |
| 177 | #endif |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 178 | #if DEBUG_MEMORY_USAGE |
| 179 | mCaches.dumpMemoryUsage(); |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 180 | #else |
| 181 | if (mCaches.getDebugLevel() & kDebugMemory) { |
| 182 | mCaches.dumpMemoryUsage(); |
| 183 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 184 | #endif |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 187 | void OpenGLRenderer::interrupt() { |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 188 | if (mCaches.currentProgram) { |
| 189 | if (mCaches.currentProgram->isInUse()) { |
| 190 | mCaches.currentProgram->remove(); |
| 191 | mCaches.currentProgram = NULL; |
| 192 | } |
| 193 | } |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 194 | mCaches.unbindMeshBuffer(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 197 | void OpenGLRenderer::resume() { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 198 | glViewport(0, 0, mSnapshot->viewport.getWidth(), mSnapshot->viewport.getHeight()); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 199 | |
| 200 | glEnable(GL_SCISSOR_TEST); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 201 | dirtyClip(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 202 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 203 | glDisable(GL_DITHER); |
| 204 | |
Romain Guy | 42f3a4b | 2011-01-19 13:42:26 -0800 | [diff] [blame] | 205 | glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 206 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 207 | |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 208 | mCaches.blend = true; |
| 209 | glEnable(GL_BLEND); |
| 210 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
| 211 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 214 | bool OpenGLRenderer::callDrawGLFunction(Functor *functor, Rect& dirty) { |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 215 | interrupt(); |
Romain Guy | f90f817 | 2011-01-25 22:53:24 -0800 | [diff] [blame] | 216 | if (mDirtyClip) { |
| 217 | setScissorFromClip(); |
| 218 | } |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 219 | |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 220 | Rect clip(*mSnapshot->clipRect); |
| 221 | clip.snapToPixelBoundaries(); |
| 222 | |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 223 | #if RENDER_LAYERS_AS_REGIONS |
| 224 | // Since we don't know what the functor will draw, let's dirty |
| 225 | // tne entire clip region |
| 226 | if (hasLayer()) { |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 227 | dirtyLayerUnchecked(clip, getRegion()); |
| 228 | } |
| 229 | #endif |
| 230 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 231 | DrawGlInfo info; |
| 232 | info.clipLeft = clip.left; |
| 233 | info.clipTop = clip.top; |
| 234 | info.clipRight = clip.right; |
| 235 | info.clipBottom = clip.bottom; |
| 236 | info.isLayer = hasLayer(); |
| 237 | getSnapshot()->transform->copyTo(&info.transform[0]); |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 238 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 239 | status_t result = (*functor)(0, &info); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 240 | |
| 241 | if (result != 0) { |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 242 | Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 243 | dirty.unionWith(localDirty); |
| 244 | } |
| 245 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 246 | resume(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 247 | return result != 0; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 250 | /////////////////////////////////////////////////////////////////////////////// |
| 251 | // State management |
| 252 | /////////////////////////////////////////////////////////////////////////////// |
| 253 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 254 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 255 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 259 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 263 | if (mSaveCount > 1) { |
| 264 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 265 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 269 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 270 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 271 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 272 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 273 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 276 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 277 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 278 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 282 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 283 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 284 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 285 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 286 | sp<Snapshot> current = mSnapshot; |
| 287 | sp<Snapshot> previous = mSnapshot->previous; |
| 288 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 289 | if (restoreOrtho) { |
| 290 | Rect& r = previous->viewport; |
| 291 | glViewport(r.left, r.top, r.right, r.bottom); |
| 292 | mOrthoMatrix.load(current->orthoMatrix); |
| 293 | } |
| 294 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 295 | mSaveCount--; |
| 296 | mSnapshot = previous; |
| 297 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 298 | if (restoreClip) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 299 | dirtyClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 300 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 301 | |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 302 | if (restoreLayer) { |
| 303 | composeLayer(current, previous); |
| 304 | } |
| 305 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 306 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 309 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 310 | // Layers |
| 311 | /////////////////////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 314 | SkPaint* p, int flags) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 315 | const GLuint previousFbo = mSnapshot->fbo; |
| 316 | const int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 317 | |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 318 | if (!mSnapshot->isIgnored()) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 319 | int alpha = 255; |
| 320 | SkXfermode::Mode mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 321 | |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 322 | if (p) { |
| 323 | alpha = p->getAlpha(); |
| 324 | if (!mCaches.extensions.hasFramebufferFetch()) { |
| 325 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 326 | if (!isMode) { |
| 327 | // Assume SRC_OVER |
| 328 | mode = SkXfermode::kSrcOver_Mode; |
| 329 | } |
| 330 | } else { |
| 331 | mode = getXfermode(p->getXfermode()); |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 332 | } |
| 333 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 334 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 335 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 336 | |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 337 | createLayer(mSnapshot, left, top, right, bottom, alpha, mode, flags, previousFbo); |
| 338 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 339 | |
| 340 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | int OpenGLRenderer::saveLayerAlpha(float left, float top, float right, float bottom, |
| 344 | int alpha, int flags) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 345 | if (alpha >= 255 - ALPHA_THRESHOLD) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 346 | return saveLayer(left, top, right, bottom, NULL, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 347 | } else { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 348 | SkPaint paint; |
| 349 | paint.setAlpha(alpha); |
| 350 | return saveLayer(left, top, right, bottom, &paint, flags); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 351 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 352 | } |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 353 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 354 | /** |
| 355 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 356 | * programs (for instance.) When a layer is created, previously created layers |
| 357 | * and the frame buffer still receive every drawing command. For instance, if a |
| 358 | * layer is created and a shape intersecting the bounds of the layers and the |
| 359 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 360 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 361 | * |
| 362 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 363 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 364 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 365 | * means, for every primitive: |
| 366 | * - Switch active frame buffer |
| 367 | * - Change viewport, clip and projection matrix |
| 368 | * - Issue the drawing |
| 369 | * |
| 370 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 371 | * To avoid this, layers are implemented in a different way here, at least in the |
| 372 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 373 | * is set. When this flag is set we can redirect all drawing operations into a |
| 374 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 375 | * |
| 376 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 377 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 378 | * 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] | 379 | * 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] | 380 | * buffer and drawing continues as normal. This technique therefore treats the |
| 381 | * frame buffer as a scratch buffer for the layers. |
| 382 | * |
| 383 | * To compose the layers back onto the frame buffer, each layer texture |
| 384 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 385 | * the frame buffer. The trick is that the quad is set as the composition |
| 386 | * destination in the blending equation, and the frame buffer becomes the source |
| 387 | * of the composition. |
| 388 | * |
| 389 | * Drawing layers with an alpha value requires an extra step before composition. |
| 390 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 391 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 392 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 393 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 394 | * GL_ZERO, GL_SRC_ALPHA. |
| 395 | * |
| 396 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 397 | * be use to draw a single clipped layer. The implementation described above |
| 398 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 399 | * |
| 400 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 401 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 402 | * buffer is left untouched until the first drawing operation. Only when |
| 403 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 404 | */ |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 405 | bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top, |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 406 | float right, float bottom, int alpha, SkXfermode::Mode mode, |
| 407 | int flags, GLuint previousFbo) { |
| 408 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 409 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 410 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 411 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 412 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 413 | // Window coordinates of the layer |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 414 | Rect bounds(left, top, right, bottom); |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 415 | if (!fboLayer) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 416 | mSnapshot->transform->mapRect(bounds); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 417 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 418 | // Layers only make sense if they are in the framebuffer's bounds |
Romain Guy | ad37cd3 | 2011-03-15 11:12:25 -0700 | [diff] [blame] | 419 | if (bounds.intersect(*snapshot->clipRect)) { |
| 420 | // We cannot work with sub-pixels in this case |
| 421 | bounds.snapToPixelBoundaries(); |
Romain Guy | ae51759 | 2010-10-22 10:40:27 -0700 | [diff] [blame] | 422 | |
Romain Guy | ad37cd3 | 2011-03-15 11:12:25 -0700 | [diff] [blame] | 423 | // When the layer is not an FBO, we may use glCopyTexImage so we |
| 424 | // need to make sure the layer does not extend outside the bounds |
| 425 | // of the framebuffer |
| 426 | if (!bounds.intersect(snapshot->previous->viewport)) { |
| 427 | bounds.setEmpty(); |
| 428 | } |
| 429 | } else { |
| 430 | bounds.setEmpty(); |
| 431 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 432 | } |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 433 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 434 | if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || |
| 435 | bounds.getHeight() > mCaches.maxTextureSize) { |
Romain Guy | 32963c3 | 2010-12-10 14:43:41 -0800 | [diff] [blame] | 436 | snapshot->empty = fboLayer; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 437 | } else { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 438 | snapshot->invisible = snapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | // Bail out if we won't draw in this snapshot |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 442 | if (snapshot->invisible || snapshot->empty) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 443 | return false; |
| 444 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 445 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 446 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 447 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 448 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 449 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 452 | layer->setAlpha(alpha, mode); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 453 | layer->layer.set(bounds); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 454 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), |
| 455 | bounds.getWidth() / float(layer->getWidth()), 0.0f); |
| 456 | layer->setColorFilter(mColorFilter); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 457 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 458 | // Save the layer in the snapshot |
| 459 | snapshot->flags |= Snapshot::kFlagIsLayer; |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 460 | snapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 461 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 462 | if (fboLayer) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 463 | return createFboLayer(layer, bounds, snapshot, previousFbo); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 464 | } else { |
| 465 | // Copy the framebuffer into the layer |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 466 | layer->bindTexture(); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 467 | if (!bounds.isEmpty()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 468 | if (layer->isEmpty()) { |
| 469 | glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 470 | bounds.left, snapshot->height - bounds.bottom, |
| 471 | layer->getWidth(), layer->getHeight(), 0); |
| 472 | layer->setEmpty(false); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 473 | } else { |
| 474 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, |
| 475 | snapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); |
| 476 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 477 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 478 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 479 | mLayers.push(new Rect(bounds)); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 480 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 481 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 482 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 483 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 486 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, |
| 487 | GLuint previousFbo) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 488 | layer->setFbo(mCaches.fboCache.get()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 489 | |
| 490 | #if RENDER_LAYERS_AS_REGIONS |
| 491 | snapshot->region = &snapshot->layer->region; |
| 492 | snapshot->flags |= Snapshot::kFlagFboTarget; |
| 493 | #endif |
| 494 | |
| 495 | Rect clip(bounds); |
| 496 | snapshot->transform->mapRect(clip); |
| 497 | clip.intersect(*snapshot->clipRect); |
| 498 | clip.snapToPixelBoundaries(); |
| 499 | clip.intersect(snapshot->previous->viewport); |
| 500 | |
| 501 | mat4 inverse; |
| 502 | inverse.loadInverse(*mSnapshot->transform); |
| 503 | |
| 504 | inverse.mapRect(clip); |
| 505 | clip.snapToPixelBoundaries(); |
| 506 | clip.intersect(bounds); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 507 | clip.translate(-bounds.left, -bounds.top); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 508 | |
| 509 | snapshot->flags |= Snapshot::kFlagIsFboLayer; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 510 | snapshot->fbo = layer->getFbo(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 511 | snapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 512 | snapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 513 | snapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 514 | snapshot->height = bounds.getHeight(); |
| 515 | snapshot->flags |= Snapshot::kFlagDirtyOrtho; |
| 516 | snapshot->orthoMatrix.load(mOrthoMatrix); |
| 517 | |
| 518 | // Bind texture to FBO |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 519 | glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); |
| 520 | layer->bindTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 521 | |
| 522 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 523 | if (layer->isEmpty()) { |
| 524 | layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE); |
| 525 | layer->setEmpty(false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 529 | layer->getTexture(), 0); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 530 | |
| 531 | #if DEBUG_LAYERS_AS_REGIONS |
| 532 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 533 | if (status != GL_FRAMEBUFFER_COMPLETE) { |
| 534 | LOGE("Framebuffer incomplete (GL error code 0x%x)", status); |
| 535 | |
| 536 | glBindFramebuffer(GL_FRAMEBUFFER, previousFbo); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 537 | layer->deleteTexture(); |
| 538 | mCaches.fboCache.put(layer->getFbo()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 539 | |
| 540 | delete layer; |
| 541 | |
| 542 | return false; |
| 543 | } |
| 544 | #endif |
| 545 | |
| 546 | // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering |
| 547 | glScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
| 548 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
| 549 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 550 | glClear(GL_COLOR_BUFFER_BIT); |
| 551 | |
| 552 | dirtyClip(); |
| 553 | |
| 554 | // Change the ortho projection |
| 555 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 556 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 561 | /** |
| 562 | * Read the documentation of createLayer() before doing anything in this method. |
| 563 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 564 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 565 | if (!current->layer) { |
| 566 | LOGE("Attempting to compose a layer that does not exist"); |
| 567 | return; |
| 568 | } |
| 569 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 570 | const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 571 | |
| 572 | if (fboLayer) { |
| 573 | // Unbind current FBO and restore previous one |
| 574 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 575 | } |
| 576 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 577 | Layer* layer = current->layer; |
| 578 | const Rect& rect = layer->layer; |
| 579 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 580 | if (!fboLayer && layer->getAlpha() < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 581 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 582 | layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 583 | // Required below, composeLayerRect() will divide by 255 |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 584 | layer->setAlpha(255); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 587 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 588 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 589 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 590 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 591 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 592 | // drawing only the dirty region |
| 593 | if (fboLayer) { |
| 594 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 595 | if (layer->getColorFilter()) { |
| 596 | setupColorFilter(layer->getColorFilter()); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 597 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 598 | composeLayerRegion(layer, rect); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 599 | if (layer->getColorFilter()) { |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 600 | resetColorFilter(); |
| 601 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 602 | } else if (!rect.isEmpty()) { |
| 603 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
| 604 | composeLayerRect(layer, rect, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 605 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 606 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 607 | if (fboLayer) { |
| 608 | // Detach the texture from the FBO |
| 609 | glBindFramebuffer(GL_FRAMEBUFFER, current->fbo); |
| 610 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 611 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
| 612 | |
| 613 | // Put the FBO name back in the cache, if it doesn't fit, it will be destroyed |
| 614 | mCaches.fboCache.put(current->fbo); |
| 615 | } |
| 616 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 617 | dirtyClip(); |
| 618 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 619 | // 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] | 620 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 621 | LAYER_LOGD("Deleting layer"); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 622 | layer->deleteTexture(); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 623 | delete layer; |
| 624 | } |
| 625 | } |
| 626 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 627 | void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 628 | float alpha = layer->getAlpha() / 255.0f; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 629 | |
| 630 | setupDraw(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 631 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 632 | setupDrawWithTexture(); |
| 633 | } else { |
| 634 | setupDrawWithExternalTexture(); |
| 635 | } |
| 636 | setupDrawTextureTransform(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 637 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 638 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 639 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 640 | setupDrawProgram(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 641 | setupDrawPureColorUniforms(); |
| 642 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 643 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
| 644 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 645 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 646 | setupDrawExternalTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 647 | } |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 648 | if (mSnapshot->transform->isPureTranslate() && |
| 649 | layer->getWidth() == (uint32_t) rect.getWidth() && |
| 650 | layer->getHeight() == (uint32_t) rect.getHeight()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 651 | const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 652 | const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 653 | |
| 654 | layer->setFilter(GL_NEAREST, GL_NEAREST); |
| 655 | setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 656 | } else { |
| 657 | layer->setFilter(GL_LINEAR, GL_LINEAR); |
| 658 | setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); |
| 659 | } |
| 660 | setupDrawTextureTransformUniforms(layer->getTexTransform()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 661 | setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); |
| 662 | |
| 663 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 664 | |
| 665 | finishDrawTexture(); |
| 666 | } |
| 667 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 668 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 669 | if (!layer->isTextureLayer()) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 670 | const Rect& texCoords = layer->texCoords; |
| 671 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, |
| 672 | texCoords.right, texCoords.bottom); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 673 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 674 | float x = rect.left; |
| 675 | float y = rect.top; |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 676 | bool simpleTransform = mSnapshot->transform->isPureTranslate() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 677 | layer->getWidth() == (uint32_t) rect.getWidth() && |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 678 | layer->getHeight() == (uint32_t) rect.getHeight(); |
| 679 | |
| 680 | if (simpleTransform) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 681 | // When we're swapping, the layer is already in screen coordinates |
| 682 | if (!swap) { |
| 683 | x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 684 | y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 685 | } |
| 686 | |
| 687 | layer->setFilter(GL_NEAREST, GL_NEAREST, true); |
| 688 | } else { |
| 689 | layer->setFilter(GL_LINEAR, GL_LINEAR, true); |
| 690 | } |
| 691 | |
| 692 | drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), |
| 693 | layer->getTexture(), layer->getAlpha() / 255.0f, |
| 694 | layer->getMode(), layer->isBlend(), |
| 695 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 696 | GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 697 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 698 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 699 | } else { |
| 700 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
| 701 | drawTextureLayer(layer, rect); |
| 702 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 703 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
| 707 | #if RENDER_LAYERS_AS_REGIONS |
| 708 | if (layer->region.isRect()) { |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 709 | layer->setRegionAsRect(); |
| 710 | |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 711 | composeLayerRect(layer, layer->regionRect); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 712 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 713 | layer->region.clear(); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | if (!layer->region.isEmpty()) { |
| 718 | size_t count; |
| 719 | const android::Rect* rects = layer->region.getArray(&count); |
| 720 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 721 | const float alpha = layer->getAlpha() / 255.0f; |
| 722 | const float texX = 1.0f / float(layer->getWidth()); |
| 723 | const float texY = 1.0f / float(layer->getHeight()); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 724 | const float height = rect.getHeight(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 725 | |
| 726 | TextureVertex* mesh = mCaches.getRegionMesh(); |
| 727 | GLsizei numQuads = 0; |
| 728 | |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 729 | setupDraw(); |
| 730 | setupDrawWithTexture(); |
| 731 | setupDrawColor(alpha, alpha, alpha, alpha); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 732 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 733 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 734 | setupDrawProgram(); |
| 735 | setupDrawDirtyRegionsDisabled(); |
| 736 | setupDrawPureColorUniforms(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 737 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 738 | setupDrawTexture(layer->getTexture()); |
| 739 | if (mSnapshot->transform->isPureTranslate()) { |
| 740 | const float x = (int) floorf(rect.left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 741 | const float y = (int) floorf(rect.top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 742 | |
| 743 | layer->setFilter(GL_NEAREST, GL_NEAREST); |
| 744 | setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 745 | } else { |
| 746 | layer->setFilter(GL_LINEAR, GL_LINEAR); |
| 747 | setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); |
| 748 | } |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 749 | setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 750 | |
| 751 | for (size_t i = 0; i < count; i++) { |
| 752 | const android::Rect* r = &rects[i]; |
| 753 | |
| 754 | const float u1 = r->left * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 755 | const float v1 = (height - r->top) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 756 | const float u2 = r->right * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 757 | const float v2 = (height - r->bottom) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 758 | |
| 759 | // TODO: Reject quads outside of the clip |
| 760 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 761 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 762 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 763 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 764 | |
| 765 | numQuads++; |
| 766 | |
| 767 | if (numQuads >= REGION_MESH_QUAD_COUNT) { |
| 768 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 769 | numQuads = 0; |
| 770 | mesh = mCaches.getRegionMesh(); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | if (numQuads > 0) { |
| 775 | glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL); |
| 776 | } |
| 777 | |
| 778 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 779 | finishDrawTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 780 | |
| 781 | #if DEBUG_LAYERS_AS_REGIONS |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 782 | drawRegionRects(layer->region); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 783 | #endif |
| 784 | |
| 785 | layer->region.clear(); |
| 786 | } |
| 787 | #else |
| 788 | composeLayerRect(layer, rect); |
| 789 | #endif |
| 790 | } |
| 791 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 792 | void OpenGLRenderer::drawRegionRects(const Region& region) { |
| 793 | #if DEBUG_LAYERS_AS_REGIONS |
| 794 | size_t count; |
| 795 | const android::Rect* rects = region.getArray(&count); |
| 796 | |
| 797 | uint32_t colors[] = { |
| 798 | 0x7fff0000, 0x7f00ff00, |
| 799 | 0x7f0000ff, 0x7fff00ff, |
| 800 | }; |
| 801 | |
| 802 | int offset = 0; |
| 803 | int32_t top = rects[0].top; |
| 804 | |
| 805 | for (size_t i = 0; i < count; i++) { |
| 806 | if (top != rects[i].top) { |
| 807 | offset ^= 0x2; |
| 808 | top = rects[i].top; |
| 809 | } |
| 810 | |
| 811 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
| 812 | drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], |
| 813 | SkXfermode::kSrcOver_Mode); |
| 814 | } |
| 815 | #endif |
| 816 | } |
| 817 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 818 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 819 | const float right, const float bottom, const mat4 transform) { |
| 820 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 821 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 822 | Rect bounds(left, top, right, bottom); |
| 823 | transform.mapRect(bounds); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 824 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 825 | } |
| 826 | #endif |
| 827 | } |
| 828 | |
| 829 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 830 | const float right, const float bottom) { |
| 831 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 832 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 833 | Rect bounds(left, top, right, bottom); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 834 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 835 | } |
| 836 | #endif |
| 837 | } |
| 838 | |
| 839 | void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { |
| 840 | #if RENDER_LAYERS_AS_REGIONS |
| 841 | if (bounds.intersect(*mSnapshot->clipRect)) { |
| 842 | bounds.snapToPixelBoundaries(); |
| 843 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 844 | if (!dirty.isEmpty()) { |
| 845 | region->orSelf(dirty); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | #endif |
| 849 | } |
| 850 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 851 | void OpenGLRenderer::clearLayerRegions() { |
| 852 | const size_t count = mLayers.size(); |
| 853 | if (count == 0) return; |
| 854 | |
| 855 | if (!mSnapshot->isIgnored()) { |
| 856 | // Doing several glScissor/glClear here can negatively impact |
| 857 | // GPUs with a tiler architecture, instead we draw quads with |
| 858 | // the Clear blending mode |
| 859 | |
| 860 | // The list contains bounds that have already been clipped |
| 861 | // against their initial clip rect, and the current clip |
| 862 | // is likely different so we need to disable clipping here |
| 863 | glDisable(GL_SCISSOR_TEST); |
| 864 | |
| 865 | Vertex mesh[count * 6]; |
| 866 | Vertex* vertex = mesh; |
| 867 | |
| 868 | for (uint32_t i = 0; i < count; i++) { |
| 869 | Rect* bounds = mLayers.itemAt(i); |
| 870 | |
| 871 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
| 872 | Vertex::set(vertex++, bounds->left, bounds->top); |
| 873 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 874 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
| 875 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 876 | Vertex::set(vertex++, bounds->right, bounds->bottom); |
| 877 | |
| 878 | delete bounds; |
| 879 | } |
| 880 | |
| 881 | setupDraw(false); |
| 882 | setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 883 | setupDrawBlending(true, SkXfermode::kClear_Mode); |
| 884 | setupDrawProgram(); |
| 885 | setupDrawPureColorUniforms(); |
| 886 | setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); |
| 887 | |
| 888 | mCaches.unbindMeshBuffer(); |
| 889 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 890 | gVertexStride, &mesh[0].position[0]); |
| 891 | glDrawArrays(GL_TRIANGLES, 0, count * 6); |
| 892 | |
| 893 | glEnable(GL_SCISSOR_TEST); |
| 894 | } else { |
| 895 | for (uint32_t i = 0; i < count; i++) { |
| 896 | delete mLayers.itemAt(i); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | mLayers.clear(); |
| 901 | } |
| 902 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 903 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 904 | // Transforms |
| 905 | /////////////////////////////////////////////////////////////////////////////// |
| 906 | |
| 907 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 908 | mSnapshot->transform->translate(dx, dy, 0.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 912 | mSnapshot->transform->rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 916 | mSnapshot->transform->scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 919 | void OpenGLRenderer::skew(float sx, float sy) { |
| 920 | mSnapshot->transform->skew(sx, sy); |
| 921 | } |
| 922 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 923 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 924 | mSnapshot->transform->load(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 927 | const float* OpenGLRenderer::getMatrix() const { |
Romain Guy | 99bcdc5 | 2010-10-13 15:17:00 -0700 | [diff] [blame] | 928 | if (mSnapshot->fbo != 0) { |
| 929 | return &mSnapshot->transform->data[0]; |
| 930 | } |
| 931 | return &mIdentity.data[0]; |
Romain Guy | 41030da | 2010-10-13 13:40:37 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 934 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 935 | mSnapshot->transform->copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 939 | SkMatrix transform; |
| 940 | mSnapshot->transform->copyTo(transform); |
| 941 | transform.preConcat(*matrix); |
| 942 | mSnapshot->transform->load(transform); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /////////////////////////////////////////////////////////////////////////////// |
| 946 | // Clipping |
| 947 | /////////////////////////////////////////////////////////////////////////////// |
| 948 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 949 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 950 | Rect clip(*mSnapshot->clipRect); |
| 951 | clip.snapToPixelBoundaries(); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 952 | glScissor(clip.left, mSnapshot->height - clip.bottom, clip.getWidth(), clip.getHeight()); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 953 | mDirtyClip = false; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 957 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 960 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) { |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 961 | if (mSnapshot->isIgnored()) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 962 | return true; |
| 963 | } |
| 964 | |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 965 | Rect r(left, top, right, bottom); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 966 | mSnapshot->transform->mapRect(r); |
Romain Guy | d2a1ff0 | 2010-10-14 14:46:44 -0700 | [diff] [blame] | 967 | r.snapToPixelBoundaries(); |
| 968 | |
| 969 | Rect clipRect(*mSnapshot->clipRect); |
| 970 | clipRect.snapToPixelBoundaries(); |
| 971 | |
| 972 | return !clipRect.intersects(r); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 975 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
| 976 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 977 | if (clipped) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 978 | dirtyClip(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 979 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 980 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 983 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 984 | // Drawing commands |
| 985 | /////////////////////////////////////////////////////////////////////////////// |
| 986 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 987 | void OpenGLRenderer::setupDraw(bool clear) { |
| 988 | if (clear) clearLayerRegions(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 989 | if (mDirtyClip) { |
| 990 | setScissorFromClip(); |
| 991 | } |
| 992 | mDescription.reset(); |
| 993 | mSetShaderColor = false; |
| 994 | mColorSet = false; |
| 995 | mColorA = mColorR = mColorG = mColorB = 0.0f; |
| 996 | mTextureUnit = 0; |
| 997 | mTrackDirtyRegions = true; |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 998 | mTexCoordsSlot = -1; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { |
| 1002 | mDescription.hasTexture = true; |
| 1003 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1004 | } |
| 1005 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1006 | void OpenGLRenderer::setupDrawWithExternalTexture() { |
| 1007 | mDescription.hasExternalTexture = true; |
| 1008 | } |
| 1009 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1010 | void OpenGLRenderer::setupDrawAALine() { |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1011 | mDescription.isAA = true; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1014 | void OpenGLRenderer::setupDrawPoint(float pointSize) { |
| 1015 | mDescription.isPoint = true; |
| 1016 | mDescription.pointSize = pointSize; |
| 1017 | } |
| 1018 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1019 | void OpenGLRenderer::setupDrawColor(int color) { |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1020 | setupDrawColor(color, (color >> 24) & 0xFF); |
| 1021 | } |
| 1022 | |
| 1023 | void OpenGLRenderer::setupDrawColor(int color, int alpha) { |
| 1024 | mColorA = alpha / 255.0f; |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 1025 | // Second divide of a by 255 is an optimization, allowing us to simply multiply |
| 1026 | // the rgb values by a instead of also dividing by 255 |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1027 | const float a = mColorA / 255.0f; |
Romain Guy | fa7952d | 2010-12-14 13:45:54 -0800 | [diff] [blame] | 1028 | mColorR = a * ((color >> 16) & 0xFF); |
| 1029 | mColorG = a * ((color >> 8) & 0xFF); |
| 1030 | mColorB = a * ((color ) & 0xFF); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1031 | mColorSet = true; |
| 1032 | mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); |
| 1033 | } |
| 1034 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1035 | void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { |
| 1036 | mColorA = alpha / 255.0f; |
Chet Haase | 6cfdf45 | 2011-04-22 16:42:10 -0700 | [diff] [blame] | 1037 | // Double-divide of a by 255 is an optimization, allowing us to simply multiply |
| 1038 | // the rgb values by a instead of also dividing by 255 |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1039 | const float a = mColorA / 255.0f; |
| 1040 | mColorR = a * ((color >> 16) & 0xFF); |
| 1041 | mColorG = a * ((color >> 8) & 0xFF); |
| 1042 | mColorB = a * ((color ) & 0xFF); |
| 1043 | mColorSet = true; |
| 1044 | mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); |
| 1045 | } |
| 1046 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1047 | void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { |
| 1048 | mColorA = a; |
| 1049 | mColorR = r; |
| 1050 | mColorG = g; |
| 1051 | mColorB = b; |
| 1052 | mColorSet = true; |
| 1053 | mSetShaderColor = mDescription.setColor(r, g, b, a); |
| 1054 | } |
| 1055 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1056 | void OpenGLRenderer::setupDrawAlpha8Color(float r, float g, float b, float a) { |
| 1057 | mColorA = a; |
| 1058 | mColorR = r; |
| 1059 | mColorG = g; |
| 1060 | mColorB = b; |
| 1061 | mColorSet = true; |
| 1062 | mSetShaderColor = mDescription.setAlpha8Color(r, g, b, a); |
| 1063 | } |
| 1064 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1065 | void OpenGLRenderer::setupDrawShader() { |
| 1066 | if (mShader) { |
| 1067 | mShader->describe(mDescription, mCaches.extensions); |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void OpenGLRenderer::setupDrawColorFilter() { |
| 1072 | if (mColorFilter) { |
| 1073 | mColorFilter->describe(mDescription, mCaches.extensions); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1077 | void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { |
| 1078 | if (mColorSet && mode == SkXfermode::kClear_Mode) { |
| 1079 | mColorA = 1.0f; |
| 1080 | mColorR = mColorG = mColorB = 0.0f; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1081 | mSetShaderColor = mDescription.modulate = true; |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1085 | void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1086 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1087 | // argb=1,0,0,0 |
| 1088 | accountForClear(mode); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1089 | chooseBlending((mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, |
| 1090 | mDescription, swapSrcDst); |
| 1091 | } |
| 1092 | |
| 1093 | void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1094 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1095 | // argb=1,0,0,0 |
| 1096 | accountForClear(mode); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1097 | chooseBlending(blend || (mColorSet && mColorA < 1.0f) || (mShader && mShader->blend()), mode, |
| 1098 | mDescription, swapSrcDst); |
| 1099 | } |
| 1100 | |
| 1101 | void OpenGLRenderer::setupDrawProgram() { |
| 1102 | useProgram(mCaches.programCache.get(mDescription)); |
| 1103 | } |
| 1104 | |
| 1105 | void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { |
| 1106 | mTrackDirtyRegions = false; |
| 1107 | } |
| 1108 | |
| 1109 | void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, |
| 1110 | bool ignoreTransform) { |
| 1111 | mModelView.loadTranslate(left, top, 0.0f); |
| 1112 | if (!ignoreTransform) { |
| 1113 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1114 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1115 | } else { |
| 1116 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); |
| 1117 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1121 | void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { |
| 1122 | mCaches.currentProgram->set(mOrthoMatrix, mIdentity, *mSnapshot->transform, offset); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1125 | void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, |
| 1126 | bool ignoreTransform, bool ignoreModelView) { |
| 1127 | if (!ignoreModelView) { |
| 1128 | mModelView.loadTranslate(left, top, 0.0f); |
| 1129 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1130 | } else { |
| 1131 | mModelView.loadIdentity(); |
| 1132 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1133 | bool dirty = right - left > 0.0f && bottom - top > 0.0f; |
| 1134 | if (!ignoreTransform) { |
| 1135 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, *mSnapshot->transform); |
| 1136 | if (mTrackDirtyRegions && dirty) { |
| 1137 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1138 | } |
| 1139 | } else { |
| 1140 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mIdentity); |
| 1141 | if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); |
| 1142 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1143 | } |
| 1144 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1145 | void OpenGLRenderer::setupDrawPointUniforms() { |
| 1146 | int slot = mCaches.currentProgram->getUniform("pointSize"); |
| 1147 | glUniform1f(slot, mDescription.pointSize); |
| 1148 | } |
| 1149 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1150 | void OpenGLRenderer::setupDrawColorUniforms() { |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1151 | if (mColorSet || (mShader && mSetShaderColor)) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1152 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1156 | void OpenGLRenderer::setupDrawPureColorUniforms() { |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1157 | if (mSetShaderColor) { |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1158 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1162 | void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { |
| 1163 | if (mShader) { |
| 1164 | if (ignoreTransform) { |
| 1165 | mModelView.loadInverse(*mSnapshot->transform); |
| 1166 | } |
| 1167 | mShader->setupProgram(mCaches.currentProgram, mModelView, *mSnapshot, &mTextureUnit); |
| 1168 | } |
| 1169 | } |
| 1170 | |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1171 | void OpenGLRenderer::setupDrawShaderIdentityUniforms() { |
| 1172 | if (mShader) { |
| 1173 | mShader->setupProgram(mCaches.currentProgram, mIdentity, *mSnapshot, &mTextureUnit); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1177 | void OpenGLRenderer::setupDrawColorFilterUniforms() { |
| 1178 | if (mColorFilter) { |
| 1179 | mColorFilter->setupProgram(mCaches.currentProgram); |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | void OpenGLRenderer::setupDrawSimpleMesh() { |
| 1184 | mCaches.bindMeshBuffer(); |
| 1185 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1186 | gMeshStride, 0); |
| 1187 | } |
| 1188 | |
| 1189 | void OpenGLRenderer::setupDrawTexture(GLuint texture) { |
| 1190 | bindTexture(texture); |
| 1191 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); |
| 1192 | |
| 1193 | mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
| 1194 | glEnableVertexAttribArray(mTexCoordsSlot); |
| 1195 | } |
| 1196 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1197 | void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { |
| 1198 | bindExternalTexture(texture); |
| 1199 | glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++); |
| 1200 | |
| 1201 | mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords"); |
| 1202 | glEnableVertexAttribArray(mTexCoordsSlot); |
| 1203 | } |
| 1204 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1205 | void OpenGLRenderer::setupDrawTextureTransform() { |
| 1206 | mDescription.hasTextureTransform = true; |
| 1207 | } |
| 1208 | |
| 1209 | void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1210 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, |
| 1211 | GL_FALSE, &transform.data[0]); |
| 1212 | } |
| 1213 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1214 | void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { |
| 1215 | if (!vertices) { |
| 1216 | mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 1217 | } else { |
| 1218 | mCaches.unbindMeshBuffer(); |
| 1219 | } |
| 1220 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1221 | gMeshStride, vertices); |
David Li | cf28957 | 2011-02-25 12:05:44 -0800 | [diff] [blame] | 1222 | if (mTexCoordsSlot >= 0) { |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1223 | glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords); |
| 1224 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1227 | void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) { |
| 1228 | mCaches.unbindMeshBuffer(); |
| 1229 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
| 1230 | gVertexStride, vertices); |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Sets up the shader to draw an AA line. We draw AA lines with quads, where there is an |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1235 | * outer boundary that fades out to 0. The variables set in the shader define the proportion of |
| 1236 | * the width and length of the primitive occupied by the AA region. The vtxWidth and vtxLength |
| 1237 | * attributes (one per vertex) are values from zero to one that tells the fragment |
| 1238 | * shader where the fragment is in relation to the line width/length overall; these values are |
| 1239 | * then used to compute the proper color, based on whether the fragment lies in the fading AA |
| 1240 | * region of the line. |
| 1241 | * Note that we only pass down the width values in this setup function. The length coordinates |
| 1242 | * are set up for each individual segment. |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1243 | */ |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1244 | void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords, |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1245 | GLvoid* lengthCoords, float boundaryWidthProportion) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1246 | mCaches.unbindMeshBuffer(); |
| 1247 | glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE, |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1248 | gAAVertexStride, vertices); |
| 1249 | int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth"); |
| 1250 | glEnableVertexAttribArray(widthSlot); |
| 1251 | glVertexAttribPointer(widthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, widthCoords); |
| 1252 | int lengthSlot = mCaches.currentProgram->getAttrib("vtxLength"); |
| 1253 | glEnableVertexAttribArray(lengthSlot); |
| 1254 | glVertexAttribPointer(lengthSlot, 1, GL_FLOAT, GL_FALSE, gAAVertexStride, lengthCoords); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1255 | int boundaryWidthSlot = mCaches.currentProgram->getUniform("boundaryWidth"); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1256 | glUniform1f(boundaryWidthSlot, boundaryWidthProportion); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1257 | // Setting the inverse value saves computations per-fragment in the shader |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1258 | int inverseBoundaryWidthSlot = mCaches.currentProgram->getUniform("inverseBoundaryWidth"); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1259 | glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1262 | void OpenGLRenderer::finishDrawTexture() { |
| 1263 | glDisableVertexAttribArray(mTexCoordsSlot); |
| 1264 | } |
| 1265 | |
| 1266 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1267 | // Drawing |
| 1268 | /////////////////////////////////////////////////////////////////////////////// |
| 1269 | |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 1270 | bool OpenGLRenderer::drawDisplayList(DisplayList* displayList, uint32_t width, uint32_t height, |
| 1271 | Rect& dirty, uint32_t level) { |
| 1272 | if (quickReject(0.0f, 0.0f, width, height)) { |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1276 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 1277 | // will be performed by the display list itself |
| 1278 | if (displayList) { |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 1279 | return displayList->replay(*this, dirty, level); |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1280 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 1281 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 1282 | return false; |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 1285 | void OpenGLRenderer::outputDisplayList(DisplayList* displayList, uint32_t level) { |
| 1286 | if (displayList) { |
| 1287 | displayList->output(*this, level); |
| 1288 | } |
| 1289 | } |
| 1290 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1291 | void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { |
| 1292 | int alpha; |
| 1293 | SkXfermode::Mode mode; |
| 1294 | getAlphaAndMode(paint, &alpha, &mode); |
| 1295 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1296 | float x = left; |
| 1297 | float y = top; |
| 1298 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1299 | GLenum filter = GL_LINEAR; |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1300 | bool ignoreTransform = false; |
| 1301 | if (mSnapshot->transform->isPureTranslate()) { |
| 1302 | x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1303 | y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1304 | ignoreTransform = true; |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1305 | filter = GL_NEAREST; |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | setupDraw(); |
| 1309 | setupDrawWithTexture(true); |
Romain Guy | 5b7a315 | 2011-03-23 17:15:38 -0700 | [diff] [blame] | 1310 | if (paint) { |
| 1311 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 1312 | } |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1313 | setupDrawColorFilter(); |
| 1314 | setupDrawShader(); |
| 1315 | setupDrawBlending(true, mode); |
| 1316 | setupDrawProgram(); |
| 1317 | setupDrawModelView(x, y, x + texture->width, y + texture->height, ignoreTransform); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1318 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1319 | setupDrawTexture(texture->id); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1320 | texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); |
| 1321 | texture->setFilter(filter, filter); |
| 1322 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1323 | setupDrawPureColorUniforms(); |
| 1324 | setupDrawColorFilterUniforms(); |
| 1325 | setupDrawShaderUniforms(); |
| 1326 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 1327 | |
| 1328 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 1329 | |
| 1330 | finishDrawTexture(); |
| 1331 | } |
| 1332 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1333 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1334 | const float right = left + bitmap->width(); |
| 1335 | const float bottom = top + bitmap->height(); |
| 1336 | |
| 1337 | if (quickReject(left, top, right, bottom)) { |
| 1338 | return; |
| 1339 | } |
| 1340 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1341 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1342 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1343 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1344 | const AutoTexture autoCleanup(texture); |
| 1345 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 1346 | if (bitmap->getConfig() == SkBitmap::kA8_Config) { |
| 1347 | drawAlphaBitmap(texture, left, top, paint); |
| 1348 | } else { |
| 1349 | drawTextureRect(left, top, right, bottom, texture, paint); |
| 1350 | } |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1353 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 1354 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 1355 | const mat4 transform(*matrix); |
| 1356 | transform.mapRect(r); |
| 1357 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1358 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
| 1359 | return; |
| 1360 | } |
| 1361 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1362 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1363 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1364 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1365 | const AutoTexture autoCleanup(texture); |
| 1366 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1367 | // This could be done in a cheaper way, all we need is pass the matrix |
| 1368 | // to the vertex shader. The save/restore is a bit overkill. |
| 1369 | save(SkCanvas::kMatrix_SaveFlag); |
| 1370 | concatMatrix(matrix); |
| 1371 | drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); |
| 1372 | restore(); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1375 | void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, |
| 1376 | float* vertices, int* colors, SkPaint* paint) { |
| 1377 | // TODO: Do a quickReject |
| 1378 | if (!vertices || mSnapshot->isIgnored()) { |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | glActiveTexture(gTextureUnits[0]); |
| 1383 | Texture* texture = mCaches.textureCache.get(bitmap); |
| 1384 | if (!texture) return; |
| 1385 | const AutoTexture autoCleanup(texture); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1386 | |
| 1387 | texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true); |
| 1388 | texture->setFilter(GL_LINEAR, GL_LINEAR, true); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1389 | |
| 1390 | int alpha; |
| 1391 | SkXfermode::Mode mode; |
| 1392 | getAlphaAndMode(paint, &alpha, &mode); |
| 1393 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1394 | const uint32_t count = meshWidth * meshHeight * 6; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1395 | |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1396 | float left = FLT_MAX; |
| 1397 | float top = FLT_MAX; |
| 1398 | float right = FLT_MIN; |
| 1399 | float bottom = FLT_MIN; |
| 1400 | |
| 1401 | #if RENDER_LAYERS_AS_REGIONS |
| 1402 | bool hasActiveLayer = hasLayer(); |
| 1403 | #else |
| 1404 | bool hasActiveLayer = false; |
| 1405 | #endif |
| 1406 | |
Romain Guy | a566b7c | 2011-01-23 16:36:11 -0800 | [diff] [blame] | 1407 | // TODO: Support the colors array |
| 1408 | TextureVertex mesh[count]; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1409 | TextureVertex* vertex = mesh; |
| 1410 | for (int32_t y = 0; y < meshHeight; y++) { |
| 1411 | for (int32_t x = 0; x < meshWidth; x++) { |
| 1412 | uint32_t i = (y * (meshWidth + 1) + x) * 2; |
| 1413 | |
| 1414 | float u1 = float(x) / meshWidth; |
| 1415 | float u2 = float(x + 1) / meshWidth; |
| 1416 | float v1 = float(y) / meshHeight; |
| 1417 | float v2 = float(y + 1) / meshHeight; |
| 1418 | |
| 1419 | int ax = i + (meshWidth + 1) * 2; |
| 1420 | int ay = ax + 1; |
| 1421 | int bx = i; |
| 1422 | int by = bx + 1; |
| 1423 | int cx = i + 2; |
| 1424 | int cy = cx + 1; |
| 1425 | int dx = i + (meshWidth + 1) * 2 + 2; |
| 1426 | int dy = dx + 1; |
| 1427 | |
| 1428 | TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); |
| 1429 | TextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1); |
| 1430 | TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); |
| 1431 | |
| 1432 | TextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2); |
| 1433 | TextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1); |
| 1434 | TextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1435 | |
| 1436 | #if RENDER_LAYERS_AS_REGIONS |
| 1437 | if (hasActiveLayer) { |
| 1438 | // TODO: This could be optimized to avoid unnecessary ops |
| 1439 | left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); |
| 1440 | top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); |
| 1441 | right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); |
| 1442 | bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); |
| 1443 | } |
| 1444 | #endif |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1448 | #if RENDER_LAYERS_AS_REGIONS |
| 1449 | if (hasActiveLayer) { |
| 1450 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1451 | } |
| 1452 | #endif |
| 1453 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1454 | drawTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, |
| 1455 | mode, texture->blend, &mesh[0].position[0], &mesh[0].texture[0], |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 1456 | GL_TRIANGLES, count, false, false, 0, false, false); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 1457 | } |
| 1458 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1459 | void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
| 1460 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 1461 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 1462 | SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1463 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
| 1464 | return; |
| 1465 | } |
| 1466 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1467 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1468 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1469 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1470 | const AutoTexture autoCleanup(texture); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1471 | texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1472 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1473 | const float width = texture->width; |
| 1474 | const float height = texture->height; |
| 1475 | |
Romain Guy | 1e59f9d | 2011-05-26 18:39:34 -0700 | [diff] [blame] | 1476 | const float u1 = (srcLeft + 0.5f) / width; |
| 1477 | const float v1 = (srcTop + 0.5f) / height; |
| 1478 | const float u2 = (srcRight - 0.5f) / width; |
| 1479 | const float v2 = (srcBottom - 0.5f) / height; |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1480 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1481 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1482 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 1483 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1484 | int alpha; |
| 1485 | SkXfermode::Mode mode; |
| 1486 | getAlphaAndMode(paint, &alpha, &mode); |
| 1487 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1488 | if (mSnapshot->transform->isPureTranslate()) { |
| 1489 | const float x = (int) floorf(dstLeft + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1490 | const float y = (int) floorf(dstTop + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1491 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1492 | texture->setFilter(GL_NEAREST, GL_NEAREST, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1493 | drawTextureMesh(x, y, x + (dstRight - dstLeft), y + (dstBottom - dstTop), |
| 1494 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 1495 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1496 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 1497 | } else { |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1498 | texture->setFilter(GL_LINEAR, GL_LINEAR, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1499 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, texture->id, alpha / 255.0f, |
| 1500 | mode, texture->blend, &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1501 | GL_TRIANGLE_STRIP, gMeshCount); |
| 1502 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 1503 | |
| 1504 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 1505 | } |
| 1506 | |
Romain Guy | 4aa9057 | 2010-09-26 18:40:37 -0700 | [diff] [blame] | 1507 | 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] | 1508 | 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] | 1509 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 1510 | if (quickReject(left, top, right, bottom)) { |
| 1511 | return; |
| 1512 | } |
| 1513 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1514 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 1515 | Texture* texture = mCaches.textureCache.get(bitmap); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 1516 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 1517 | const AutoTexture autoCleanup(texture); |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 1518 | texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true); |
| 1519 | texture->setFilter(GL_LINEAR, GL_LINEAR, true); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1520 | |
| 1521 | int alpha; |
| 1522 | SkXfermode::Mode mode; |
| 1523 | getAlphaAndMode(paint, &alpha, &mode); |
| 1524 | |
Romain Guy | 2728f96 | 2010-10-08 18:36:15 -0700 | [diff] [blame] | 1525 | const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(), |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 1526 | right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1527 | |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 1528 | if (mesh && mesh->verticesCount > 0) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1529 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 1530 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1531 | // 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] | 1532 | if (hasLayer() && mesh->hasEmptyQuads) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1533 | const float offsetX = left + mSnapshot->transform->getTranslateX(); |
| 1534 | const float offsetY = top + mSnapshot->transform->getTranslateY(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1535 | const size_t count = mesh->quads.size(); |
| 1536 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 1537 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1538 | if (pureTranslate) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1539 | const float x = (int) floorf(bounds.left + offsetX + 0.5f); |
| 1540 | const float y = (int) floorf(bounds.top + offsetY + 0.5f); |
| 1541 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1542 | } else { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 1543 | dirtyLayer(left + bounds.left, top + bounds.top, |
| 1544 | left + bounds.right, top + bounds.bottom, *mSnapshot->transform); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1545 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1546 | } |
| 1547 | } |
Romain Guy | a5ef39a | 2010-12-03 16:48:20 -0800 | [diff] [blame] | 1548 | #endif |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1549 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 1550 | if (pureTranslate) { |
| 1551 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 1552 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 1553 | |
| 1554 | drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f, |
| 1555 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 1556 | GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer, |
| 1557 | true, !mesh->hasEmptyQuads); |
| 1558 | } else { |
| 1559 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, |
| 1560 | mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset, |
| 1561 | GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer, |
| 1562 | true, !mesh->hasEmptyQuads); |
| 1563 | } |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 1564 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 1565 | } |
| 1566 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1567 | /** |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1568 | * This function uses a similar approach to that of AA lines in the drawLines() function. |
| 1569 | * We expand the rectangle by a half pixel in screen space on all sides, and use a fragment |
| 1570 | * shader to compute the translucency of the color, determined by whether a given pixel is |
| 1571 | * within that boundary region and how far into the region it is. |
| 1572 | */ |
| 1573 | void OpenGLRenderer::drawAARect(float left, float top, float right, float bottom, |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 1574 | int color, SkXfermode::Mode mode) { |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 1575 | float inverseScaleX = 1.0f; |
| 1576 | float inverseScaleY = 1.0f; |
| 1577 | // The quad that we use needs to account for scaling. |
| 1578 | if (!mSnapshot->transform->isPureTranslate()) { |
| 1579 | Matrix4 *mat = mSnapshot->transform; |
| 1580 | float m00 = mat->data[Matrix4::kScaleX]; |
| 1581 | float m01 = mat->data[Matrix4::kSkewY]; |
| 1582 | float m02 = mat->data[2]; |
| 1583 | float m10 = mat->data[Matrix4::kSkewX]; |
| 1584 | float m11 = mat->data[Matrix4::kScaleX]; |
| 1585 | float m12 = mat->data[6]; |
| 1586 | float scaleX = sqrt(m00 * m00 + m01 * m01); |
| 1587 | float scaleY = sqrt(m10 * m10 + m11 * m11); |
| 1588 | inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; |
| 1589 | inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; |
| 1590 | } |
| 1591 | |
| 1592 | setupDraw(); |
| 1593 | setupDrawAALine(); |
| 1594 | setupDrawColor(color); |
| 1595 | setupDrawColorFilter(); |
| 1596 | setupDrawShader(); |
| 1597 | setupDrawBlending(true, mode); |
| 1598 | setupDrawProgram(); |
| 1599 | setupDrawModelViewIdentity(true); |
| 1600 | setupDrawColorUniforms(); |
| 1601 | setupDrawColorFilterUniforms(); |
| 1602 | setupDrawShaderIdentityUniforms(); |
| 1603 | |
| 1604 | AAVertex rects[4]; |
| 1605 | AAVertex* aaVertices = &rects[0]; |
| 1606 | void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; |
| 1607 | void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; |
| 1608 | |
| 1609 | float boundarySizeX = .5 * inverseScaleX; |
| 1610 | float boundarySizeY = .5 * inverseScaleY; |
| 1611 | |
| 1612 | // Adjust the rect by the AA boundary padding |
| 1613 | left -= boundarySizeX; |
| 1614 | right += boundarySizeX; |
| 1615 | top -= boundarySizeY; |
| 1616 | bottom += boundarySizeY; |
| 1617 | |
| 1618 | float width = right - left; |
| 1619 | float height = bottom - top; |
| 1620 | |
| 1621 | float boundaryWidthProportion = (width != 0) ? (2 * boundarySizeX) / width : 0; |
| 1622 | float boundaryHeightProportion = (height != 0) ? (2 * boundarySizeY) / height : 0; |
| 1623 | setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion); |
| 1624 | int boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); |
| 1625 | int inverseBoundaryLengthSlot = mCaches.currentProgram->getUniform("inverseBoundaryLength"); |
| 1626 | glUniform1f(boundaryLengthSlot, boundaryHeightProportion); |
| 1627 | glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryHeightProportion)); |
| 1628 | |
| 1629 | if (!quickReject(left, top, right, bottom)) { |
| 1630 | AAVertex::set(aaVertices++, left, bottom, 1, 1); |
| 1631 | AAVertex::set(aaVertices++, left, top, 1, 0); |
| 1632 | AAVertex::set(aaVertices++, right, bottom, 0, 1); |
| 1633 | AAVertex::set(aaVertices++, right, top, 0, 0); |
| 1634 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
| 1635 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /** |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1640 | * We draw lines as quads (tristrips). Using GL_LINES can be difficult because the rasterization |
| 1641 | * rules for those lines produces some unexpected results, and may vary between hardware devices. |
| 1642 | * The basics of lines-as-quads is easy; we simply find the normal to the line and position the |
| 1643 | * corners of the quads on either side of each line endpoint, separated by the strokeWidth |
| 1644 | * of the line. Hairlines are more involved because we need to account for transform scaling |
| 1645 | * to end up with a one-pixel-wide line in screen space.. |
| 1646 | * Anti-aliased lines add another factor to the approach. We use a specialized fragment shader |
| 1647 | * in combination with values that we calculate and pass down in this method. The basic approach |
| 1648 | * is that the quad we create contains both the core line area plus a bounding area in which |
| 1649 | * the translucent/AA pixels are drawn. The values we calculate tell the shader what |
| 1650 | * proportion of the width and the length of a given segment is represented by the boundary |
| 1651 | * region. The quad ends up being exactly .5 pixel larger in all directions than the non-AA quad. |
| 1652 | * The bounding region is actually 1 pixel wide on all sides (half pixel on the outside, half pixel |
| 1653 | * on the inside). This ends up giving the result we want, with pixels that are completely |
| 1654 | * 'inside' the line area being filled opaquely and the other pixels being filled according to |
| 1655 | * how far into the boundary region they are, which is determined by shader interpolation. |
| 1656 | */ |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1657 | void OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { |
| 1658 | if (mSnapshot->isIgnored()) return; |
| 1659 | |
| 1660 | const bool isAA = paint->isAntiAlias(); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1661 | // We use half the stroke width here because we're going to position the quad |
| 1662 | // corner vertices half of the width away from the line endpoints |
| 1663 | float halfStrokeWidth = paint->getStrokeWidth() * 0.5f; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1664 | // A stroke width of 0 has a special meaning in Skia: |
| 1665 | // it draws a line 1 px wide regardless of current transform |
| 1666 | bool isHairLine = paint->getStrokeWidth() == 0.0f; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1667 | float inverseScaleX = 1.0f; |
| 1668 | float inverseScaleY = 1.0f; |
| 1669 | bool scaled = false; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1670 | int alpha; |
| 1671 | SkXfermode::Mode mode; |
| 1672 | int generatedVerticesCount = 0; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1673 | int verticesCount = count; |
| 1674 | if (count > 4) { |
Chet Haase | c54ed96 | 2011-05-06 14:13:05 -0700 | [diff] [blame] | 1675 | // Polyline: account for extra vertices needed for continuous tri-strip |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1676 | verticesCount += (count - 4); |
| 1677 | } |
| 1678 | |
| 1679 | if (isHairLine || isAA) { |
| 1680 | // The quad that we use for AA and hairlines needs to account for scaling. For hairlines |
| 1681 | // the line on the screen should always be one pixel wide regardless of scale. For |
| 1682 | // AA lines, we only want one pixel of translucent boundary around the quad. |
| 1683 | if (!mSnapshot->transform->isPureTranslate()) { |
| 1684 | Matrix4 *mat = mSnapshot->transform; |
| 1685 | float m00 = mat->data[Matrix4::kScaleX]; |
| 1686 | float m01 = mat->data[Matrix4::kSkewY]; |
| 1687 | float m02 = mat->data[2]; |
| 1688 | float m10 = mat->data[Matrix4::kSkewX]; |
| 1689 | float m11 = mat->data[Matrix4::kScaleX]; |
| 1690 | float m12 = mat->data[6]; |
| 1691 | float scaleX = sqrt(m00*m00 + m01*m01); |
| 1692 | float scaleY = sqrt(m10*m10 + m11*m11); |
| 1693 | inverseScaleX = (scaleX != 0) ? (inverseScaleX / scaleX) : 0; |
| 1694 | inverseScaleY = (scaleY != 0) ? (inverseScaleY / scaleY) : 0; |
| 1695 | if (inverseScaleX != 1.0f || inverseScaleY != 1.0f) { |
| 1696 | scaled = true; |
| 1697 | } |
| 1698 | } |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1699 | } |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1700 | |
| 1701 | getAlphaAndMode(paint, &alpha, &mode); |
| 1702 | setupDraw(); |
| 1703 | if (isAA) { |
| 1704 | setupDrawAALine(); |
| 1705 | } |
| 1706 | setupDrawColor(paint->getColor(), alpha); |
| 1707 | setupDrawColorFilter(); |
| 1708 | setupDrawShader(); |
| 1709 | if (isAA) { |
| 1710 | setupDrawBlending(true, mode); |
| 1711 | } else { |
| 1712 | setupDrawBlending(mode); |
| 1713 | } |
| 1714 | setupDrawProgram(); |
| 1715 | setupDrawModelViewIdentity(true); |
| 1716 | setupDrawColorUniforms(); |
| 1717 | setupDrawColorFilterUniforms(); |
| 1718 | setupDrawShaderIdentityUniforms(); |
| 1719 | |
| 1720 | if (isHairLine) { |
| 1721 | // Set a real stroke width to be used in quad construction |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1722 | halfStrokeWidth = isAA? 1 : .5; |
| 1723 | } else if (isAA && !scaled) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1724 | // Expand boundary to enable AA calculations on the quad border |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1725 | halfStrokeWidth += .5f; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1726 | } |
| 1727 | Vertex lines[verticesCount]; |
| 1728 | Vertex* vertices = &lines[0]; |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1729 | AAVertex wLines[verticesCount]; |
| 1730 | AAVertex* aaVertices = &wLines[0]; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1731 | if (!isAA) { |
| 1732 | setupDrawVertices(vertices); |
| 1733 | } else { |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1734 | void* widthCoords = ((GLbyte*) aaVertices) + gVertexAAWidthOffset; |
| 1735 | void* lengthCoords = ((GLbyte*) aaVertices) + gVertexAALengthOffset; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1736 | // innerProportion is the ratio of the inner (non-AA) part of the line to the total |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1737 | // AA stroke width (the base stroke width expanded by a half pixel on either side). |
| 1738 | // This value is used in the fragment shader to determine how to fill fragments. |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1739 | // We will need to calculate the actual width proportion on each segment for |
| 1740 | // scaled non-hairlines, since the boundary proportion may differ per-axis when scaled. |
| 1741 | float boundaryWidthProportion = 1 / (2 * halfStrokeWidth); |
| 1742 | setupDrawAALine((void*) aaVertices, widthCoords, lengthCoords, boundaryWidthProportion); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1743 | } |
| 1744 | |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1745 | AAVertex* prevAAVertex = NULL; |
| 1746 | Vertex* prevVertex = NULL; |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 1747 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1748 | int boundaryLengthSlot = -1; |
| 1749 | int inverseBoundaryLengthSlot = -1; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1750 | int boundaryWidthSlot = -1; |
| 1751 | int inverseBoundaryWidthSlot = -1; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1752 | for (int i = 0; i < count; i += 4) { |
| 1753 | // a = start point, b = end point |
| 1754 | vec2 a(points[i], points[i + 1]); |
| 1755 | vec2 b(points[i + 2], points[i + 3]); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1756 | float length = 0; |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1757 | float boundaryLengthProportion = 0; |
| 1758 | float boundaryWidthProportion = 0; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1759 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1760 | // Find the normal to the line |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1761 | vec2 n = (b - a).copyNormalized() * halfStrokeWidth; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1762 | if (isHairLine) { |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1763 | if (isAA) { |
| 1764 | float wideningFactor; |
| 1765 | if (fabs(n.x) >= fabs(n.y)) { |
| 1766 | wideningFactor = fabs(1.0f / n.x); |
| 1767 | } else { |
| 1768 | wideningFactor = fabs(1.0f / n.y); |
| 1769 | } |
| 1770 | n *= wideningFactor; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1771 | } |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1772 | if (scaled) { |
| 1773 | n.x *= inverseScaleX; |
| 1774 | n.y *= inverseScaleY; |
| 1775 | } |
| 1776 | } else if (scaled) { |
| 1777 | // Extend n by .5 pixel on each side, post-transform |
| 1778 | vec2 extendedN = n.copyNormalized(); |
| 1779 | extendedN /= 2; |
| 1780 | extendedN.x *= inverseScaleX; |
| 1781 | extendedN.y *= inverseScaleY; |
| 1782 | float extendedNLength = extendedN.length(); |
| 1783 | // We need to set this value on the shader prior to drawing |
| 1784 | boundaryWidthProportion = extendedNLength / (halfStrokeWidth + extendedNLength); |
| 1785 | n += extendedN; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1786 | } |
| 1787 | float x = n.x; |
| 1788 | n.x = -n.y; |
| 1789 | n.y = x; |
| 1790 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1791 | // aa lines expand the endpoint vertices to encompass the AA boundary |
| 1792 | if (isAA) { |
| 1793 | vec2 abVector = (b - a); |
| 1794 | length = abVector.length(); |
| 1795 | abVector.normalize(); |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1796 | if (scaled) { |
| 1797 | abVector.x *= inverseScaleX; |
| 1798 | abVector.y *= inverseScaleY; |
| 1799 | float abLength = abVector.length(); |
| 1800 | boundaryLengthProportion = abLength / (length + abLength); |
| 1801 | } else { |
| 1802 | boundaryLengthProportion = .5 / (length + 1); |
| 1803 | } |
| 1804 | abVector /= 2; |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1805 | a -= abVector; |
| 1806 | b += abVector; |
| 1807 | } |
| 1808 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1809 | // Four corners of the rectangle defining a thick line |
| 1810 | vec2 p1 = a - n; |
| 1811 | vec2 p2 = a + n; |
| 1812 | vec2 p3 = b + n; |
| 1813 | vec2 p4 = b - n; |
| 1814 | |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1815 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1816 | const float left = fmin(p1.x, fmin(p2.x, fmin(p3.x, p4.x))); |
| 1817 | const float right = fmax(p1.x, fmax(p2.x, fmax(p3.x, p4.x))); |
| 1818 | const float top = fmin(p1.y, fmin(p2.y, fmin(p3.y, p4.y))); |
| 1819 | const float bottom = fmax(p1.y, fmax(p2.y, fmax(p3.y, p4.y))); |
| 1820 | |
| 1821 | if (!quickReject(left, top, right, bottom)) { |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1822 | if (!isAA) { |
| 1823 | if (prevVertex != NULL) { |
| 1824 | // Issue two repeat vertices to create degenerate triangles to bridge |
| 1825 | // between the previous line and the new one. This is necessary because |
| 1826 | // we are creating a single triangle_strip which will contain |
| 1827 | // potentially discontinuous line segments. |
| 1828 | Vertex::set(vertices++, prevVertex->position[0], prevVertex->position[1]); |
| 1829 | Vertex::set(vertices++, p1.x, p1.y); |
| 1830 | generatedVerticesCount += 2; |
| 1831 | } |
| 1832 | Vertex::set(vertices++, p1.x, p1.y); |
| 1833 | Vertex::set(vertices++, p2.x, p2.y); |
| 1834 | Vertex::set(vertices++, p4.x, p4.y); |
| 1835 | Vertex::set(vertices++, p3.x, p3.y); |
| 1836 | prevVertex = vertices - 1; |
| 1837 | generatedVerticesCount += 4; |
| 1838 | } else { |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1839 | if (!isHairLine && scaled) { |
| 1840 | // Must set width proportions per-segment for scaled non-hairlines to use the |
| 1841 | // correct AA boundary dimensions |
| 1842 | if (boundaryWidthSlot < 0) { |
| 1843 | boundaryWidthSlot = |
| 1844 | mCaches.currentProgram->getUniform("boundaryWidth"); |
| 1845 | inverseBoundaryWidthSlot = |
| 1846 | mCaches.currentProgram->getUniform("inverseBoundaryWidth"); |
| 1847 | } |
| 1848 | glUniform1f(boundaryWidthSlot, boundaryWidthProportion); |
| 1849 | glUniform1f(inverseBoundaryWidthSlot, (1 / boundaryWidthProportion)); |
| 1850 | } |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1851 | if (boundaryLengthSlot < 0) { |
| 1852 | boundaryLengthSlot = mCaches.currentProgram->getUniform("boundaryLength"); |
| 1853 | inverseBoundaryLengthSlot = |
| 1854 | mCaches.currentProgram->getUniform("inverseBoundaryLength"); |
| 1855 | } |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 1856 | glUniform1f(boundaryLengthSlot, boundaryLengthProportion); |
| 1857 | glUniform1f(inverseBoundaryLengthSlot, (1 / boundaryLengthProportion)); |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1858 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1859 | if (prevAAVertex != NULL) { |
| 1860 | // Issue two repeat vertices to create degenerate triangles to bridge |
| 1861 | // between the previous line and the new one. This is necessary because |
| 1862 | // we are creating a single triangle_strip which will contain |
| 1863 | // potentially discontinuous line segments. |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1864 | AAVertex::set(aaVertices++,prevAAVertex->position[0], |
| 1865 | prevAAVertex->position[1], prevAAVertex->width, prevAAVertex->length); |
| 1866 | AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1867 | generatedVerticesCount += 2; |
| 1868 | } |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1869 | AAVertex::set(aaVertices++, p4.x, p4.y, 1, 1); |
| 1870 | AAVertex::set(aaVertices++, p1.x, p1.y, 1, 0); |
| 1871 | AAVertex::set(aaVertices++, p3.x, p3.y, 0, 1); |
| 1872 | AAVertex::set(aaVertices++, p2.x, p2.y, 0, 0); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1873 | prevAAVertex = aaVertices - 1; |
| 1874 | generatedVerticesCount += 4; |
| 1875 | } |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1876 | dirtyLayer(a.x == b.x ? left - 1 : left, a.y == b.y ? top - 1 : top, |
| 1877 | a.x == b.x ? right: right, a.y == b.y ? bottom: bottom, |
| 1878 | *mSnapshot->transform); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1879 | } |
| 1880 | } |
| 1881 | if (generatedVerticesCount > 0) { |
| 1882 | glDrawArrays(GL_TRIANGLE_STRIP, 0, generatedVerticesCount); |
| 1883 | } |
| 1884 | } |
| 1885 | |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1886 | void OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { |
| 1887 | if (mSnapshot->isIgnored()) return; |
| 1888 | |
| 1889 | // TODO: The paint's cap style defines whether the points are square or circular |
| 1890 | // TODO: Handle AA for round points |
| 1891 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1892 | // A stroke width of 0 has a special meaning in Skia: |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1893 | // it draws an unscaled 1px point |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1894 | float strokeWidth = paint->getStrokeWidth(); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1895 | const bool isHairLine = paint->getStrokeWidth() == 0.0f; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1896 | if (isHairLine) { |
| 1897 | // Now that we know it's hairline, we can set the effective width, to be used later |
| 1898 | strokeWidth = 1.0f; |
| 1899 | } |
| 1900 | const float halfWidth = strokeWidth / 2; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1901 | int alpha; |
| 1902 | SkXfermode::Mode mode; |
| 1903 | getAlphaAndMode(paint, &alpha, &mode); |
| 1904 | |
| 1905 | int verticesCount = count >> 1; |
| 1906 | int generatedVerticesCount = 0; |
| 1907 | |
| 1908 | TextureVertex pointsData[verticesCount]; |
| 1909 | TextureVertex* vertex = &pointsData[0]; |
| 1910 | |
| 1911 | setupDraw(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1912 | setupDrawPoint(strokeWidth); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1913 | setupDrawColor(paint->getColor(), alpha); |
| 1914 | setupDrawColorFilter(); |
| 1915 | setupDrawShader(); |
| 1916 | setupDrawBlending(mode); |
| 1917 | setupDrawProgram(); |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1918 | setupDrawModelViewIdentity(true); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1919 | setupDrawColorUniforms(); |
| 1920 | setupDrawColorFilterUniforms(); |
| 1921 | setupDrawPointUniforms(); |
| 1922 | setupDrawShaderIdentityUniforms(); |
| 1923 | setupDrawMesh(vertex); |
| 1924 | |
| 1925 | for (int i = 0; i < count; i += 2) { |
| 1926 | TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f); |
| 1927 | generatedVerticesCount++; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1928 | float left = points[i] - halfWidth; |
| 1929 | float right = points[i] + halfWidth; |
| 1930 | float top = points[i + 1] - halfWidth; |
| 1931 | float bottom = points [i + 1] + halfWidth; |
| 1932 | dirtyLayer(left, top, right, bottom, *mSnapshot->transform); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | glDrawArrays(GL_POINTS, 0, generatedVerticesCount); |
| 1936 | } |
| 1937 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 1938 | void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 1939 | // No need to check against the clip, we fill the clip region |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 1940 | if (mSnapshot->isIgnored()) return; |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 1941 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 1942 | Rect& clip(*mSnapshot->clipRect); |
| 1943 | clip.snapToPixelBoundaries(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1944 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 1945 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1946 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1947 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1948 | void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, SkPaint* paint) { |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1949 | if (!texture) return; |
| 1950 | const AutoTexture autoCleanup(texture); |
| 1951 | |
| 1952 | const float x = left + texture->left - texture->offset; |
| 1953 | const float y = top + texture->top - texture->offset; |
| 1954 | |
| 1955 | drawPathTexture(texture, x, y, paint); |
| 1956 | } |
| 1957 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1958 | void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, |
| 1959 | float rx, float ry, SkPaint* paint) { |
| 1960 | if (mSnapshot->isIgnored()) return; |
| 1961 | |
| 1962 | glActiveTexture(gTextureUnits[0]); |
| 1963 | const PathTexture* texture = mCaches.roundRectShapeCache.getRoundRect( |
| 1964 | right - left, bottom - top, rx, ry, paint); |
| 1965 | drawShape(left, top, texture, paint); |
| 1966 | } |
| 1967 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1968 | void OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* paint) { |
| 1969 | if (mSnapshot->isIgnored()) return; |
| 1970 | |
| 1971 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1972 | const PathTexture* texture = mCaches.circleShapeCache.getCircle(radius, paint); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1973 | drawShape(x - radius, y - radius, texture, paint); |
| 1974 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1975 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1976 | void OpenGLRenderer::drawOval(float left, float top, float right, float bottom, SkPaint* paint) { |
| 1977 | if (mSnapshot->isIgnored()) return; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 1978 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1979 | glActiveTexture(gTextureUnits[0]); |
| 1980 | const PathTexture* texture = mCaches.ovalShapeCache.getOval(right - left, bottom - top, paint); |
| 1981 | drawShape(left, top, texture, paint); |
| 1982 | } |
| 1983 | |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 1984 | void OpenGLRenderer::drawArc(float left, float top, float right, float bottom, |
| 1985 | float startAngle, float sweepAngle, bool useCenter, SkPaint* paint) { |
| 1986 | if (mSnapshot->isIgnored()) return; |
| 1987 | |
| 1988 | if (fabs(sweepAngle) >= 360.0f) { |
| 1989 | drawOval(left, top, right, bottom, paint); |
| 1990 | return; |
| 1991 | } |
| 1992 | |
| 1993 | glActiveTexture(gTextureUnits[0]); |
| 1994 | const PathTexture* texture = mCaches.arcShapeCache.getArc(right - left, bottom - top, |
| 1995 | startAngle, sweepAngle, useCenter, paint); |
| 1996 | drawShape(left, top, texture, paint); |
| 1997 | } |
| 1998 | |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 1999 | void OpenGLRenderer::drawRectAsShape(float left, float top, float right, float bottom, |
| 2000 | SkPaint* paint) { |
| 2001 | if (mSnapshot->isIgnored()) return; |
| 2002 | |
| 2003 | glActiveTexture(gTextureUnits[0]); |
| 2004 | const PathTexture* texture = mCaches.rectShapeCache.getRect(right - left, bottom - top, paint); |
| 2005 | drawShape(left, top, texture, paint); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2006 | } |
| 2007 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2008 | 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] | 2009 | if (p->getStyle() != SkPaint::kFill_Style) { |
| 2010 | drawRectAsShape(left, top, right, bottom, p); |
| 2011 | return; |
| 2012 | } |
| 2013 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2014 | if (quickReject(left, top, right, bottom)) { |
| 2015 | return; |
| 2016 | } |
| 2017 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2018 | SkXfermode::Mode mode; |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 2019 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2020 | const bool isMode = SkXfermode::IsMode(p->getXfermode(), &mode); |
| 2021 | if (!isMode) { |
| 2022 | // Assume SRC_OVER |
| 2023 | mode = SkXfermode::kSrcOver_Mode; |
| 2024 | } |
| 2025 | } else { |
| 2026 | mode = getXfermode(p->getXfermode()); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2029 | int color = p->getColor(); |
Romain Guy | 181d0a6 | 2011-06-09 18:52:38 -0700 | [diff] [blame] | 2030 | if (p->isAntiAlias() && !mSnapshot->transform->isSimple()) { |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2031 | drawAARect(left, top, right, bottom, color, mode); |
| 2032 | } else { |
| 2033 | drawColorRect(left, top, right, bottom, color, mode); |
| 2034 | } |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2035 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2036 | |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2037 | void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, |
| 2038 | float x, float y, SkPaint* paint) { |
Romain Guy | b146b12 | 2010-12-15 17:06:45 -0800 | [diff] [blame] | 2039 | if (text == NULL || count == 0) { |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2040 | return; |
| 2041 | } |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 2042 | if (mSnapshot->isIgnored()) return; |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 2043 | |
Romain Guy | 67ffc36 | 2011-06-03 18:50:11 -0700 | [diff] [blame] | 2044 | // TODO: We should probably make a copy of the paint instead of modifying |
| 2045 | // it; modifying the paint will change its generationID the first |
| 2046 | // time, which might impact caches. More investigation needed to |
| 2047 | // see if it matters. |
| 2048 | // If we make a copy, then drawTextDecorations() should *not* make |
| 2049 | // its own copy as it does right now. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2050 | paint->setAntiAlias(true); |
Romain Guy | 67ffc36 | 2011-06-03 18:50:11 -0700 | [diff] [blame] | 2051 | #if RENDER_TEXT_AS_GLYPHS |
| 2052 | paint->setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 2053 | #endif |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2054 | |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 2055 | float length = -1.0f; |
Romain Guy | e8e62a4 | 2010-07-23 18:55:21 -0700 | [diff] [blame] | 2056 | switch (paint->getTextAlign()) { |
| 2057 | case SkPaint::kCenter_Align: |
| 2058 | length = paint->measureText(text, bytesCount); |
| 2059 | x -= length / 2.0f; |
| 2060 | break; |
| 2061 | case SkPaint::kRight_Align: |
| 2062 | length = paint->measureText(text, bytesCount); |
| 2063 | x -= length; |
| 2064 | break; |
| 2065 | default: |
| 2066 | break; |
| 2067 | } |
| 2068 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2069 | const float oldX = x; |
| 2070 | const float oldY = y; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2071 | const bool pureTranslate = mSnapshot->transform->isPureTranslate(); |
| 2072 | if (pureTranslate) { |
| 2073 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2074 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 2075 | } |
| 2076 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 2077 | FontRenderer& fontRenderer = mCaches.fontRenderer.getFontRenderer(paint); |
| 2078 | fontRenderer.setFont(paint, SkTypeface::UniqueID(paint->getTypeface()), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2079 | paint->getTextSize()); |
Romain Guy | e2d345e | 2010-09-24 18:39:22 -0700 | [diff] [blame] | 2080 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2081 | int alpha; |
| 2082 | SkXfermode::Mode mode; |
| 2083 | getAlphaAndMode(paint, &alpha, &mode); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 2084 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2085 | if (mHasShadow) { |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 2086 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
Romain Guy | 67ffc36 | 2011-06-03 18:50:11 -0700 | [diff] [blame] | 2087 | const ShadowTexture* shadow = mCaches.dropShadowCache.get( |
| 2088 | paint, text, bytesCount, count, mShadowRadius); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2089 | const AutoTexture autoCleanup(shadow); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2090 | |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2091 | const float sx = oldX - shadow->left + mShadowDx; |
| 2092 | const float sy = oldY - shadow->top + mShadowDy; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2093 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2094 | const int shadowAlpha = ((mShadowColor >> 24) & 0xFF); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2095 | int shadowColor = mShadowColor; |
| 2096 | if (mShader) { |
| 2097 | shadowColor = 0xffffffff; |
| 2098 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2099 | |
| 2100 | glActiveTexture(gTextureUnits[0]); |
| 2101 | setupDraw(); |
| 2102 | setupDrawWithTexture(true); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2103 | setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); |
| 2104 | setupDrawColorFilter(); |
| 2105 | setupDrawShader(); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2106 | setupDrawBlending(true, mode); |
| 2107 | setupDrawProgram(); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2108 | setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2109 | setupDrawTexture(shadow->id); |
| 2110 | setupDrawPureColorUniforms(); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2111 | setupDrawColorFilterUniforms(); |
| 2112 | setupDrawShaderUniforms(); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2113 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 2114 | |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2115 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | 740bf2b | 2011-04-26 15:33:10 -0700 | [diff] [blame] | 2116 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2117 | finishDrawTexture(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Romain Guy | b146b12 | 2010-12-15 17:06:45 -0800 | [diff] [blame] | 2120 | if (paint->getAlpha() == 0 && paint->getXfermode() == NULL) { |
| 2121 | return; |
| 2122 | } |
| 2123 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2124 | // Pick the appropriate texture filtering |
| 2125 | bool linearFilter = mSnapshot->transform->changesBounds(); |
| 2126 | if (pureTranslate && !linearFilter) { |
| 2127 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 2128 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2129 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2130 | glActiveTexture(gTextureUnits[0]); |
| 2131 | setupDraw(); |
| 2132 | setupDrawDirtyRegionsDisabled(); |
| 2133 | setupDrawWithTexture(true); |
| 2134 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2135 | setupDrawColorFilter(); |
| 2136 | setupDrawShader(); |
| 2137 | setupDrawBlending(true, mode); |
| 2138 | setupDrawProgram(); |
| 2139 | setupDrawModelView(x, y, x, y, pureTranslate, true); |
| 2140 | setupDrawTexture(fontRenderer.getTexture(linearFilter)); |
| 2141 | setupDrawPureColorUniforms(); |
| 2142 | setupDrawColorFilterUniforms(); |
| 2143 | setupDrawShaderUniforms(pureTranslate); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2144 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2145 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2146 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 2147 | |
| 2148 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2149 | bool hasActiveLayer = hasLayer(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2150 | #else |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2151 | bool hasActiveLayer = false; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2152 | #endif |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 2153 | mCaches.unbindMeshBuffer(); |
Alex Sakhartchouk | 894df17 | 2011-02-17 16:45:37 -0800 | [diff] [blame] | 2154 | |
| 2155 | // Tell font renderer the locations of position and texture coord |
| 2156 | // attributes so it can bind its data properly |
| 2157 | int positionSlot = mCaches.currentProgram->position; |
| 2158 | fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2159 | if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2160 | hasActiveLayer ? &bounds : NULL)) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2161 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2162 | if (hasActiveLayer) { |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2163 | if (!pureTranslate) { |
| 2164 | mSnapshot->transform->mapRect(bounds); |
| 2165 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2166 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2167 | } |
| 2168 | #endif |
| 2169 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2170 | |
| 2171 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2172 | glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords")); |
Romain Guy | a674ab7 | 2010-08-10 17:26:42 -0700 | [diff] [blame] | 2173 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2174 | drawTextDecorations(text, bytesCount, length, oldX, oldY, paint); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2177 | void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 2178 | if (mSnapshot->isIgnored()) return; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 2179 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2180 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2181 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2182 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Romain Guy | 9cccc2b | 2010-08-07 23:46:15 -0700 | [diff] [blame] | 2183 | if (!texture) return; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2184 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2185 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 2186 | const float x = texture->left - texture->offset; |
| 2187 | const float y = texture->top - texture->offset; |
| 2188 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2189 | drawPathTexture(texture, x, y, paint); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 2190 | } |
| 2191 | |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 2192 | void OpenGLRenderer::drawLayer(Layer* layer, float x, float y, SkPaint* paint) { |
| 2193 | 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] | 2194 | return; |
| 2195 | } |
| 2196 | |
| 2197 | glActiveTexture(gTextureUnits[0]); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2198 | |
| 2199 | int alpha; |
| 2200 | SkXfermode::Mode mode; |
| 2201 | getAlphaAndMode(paint, &alpha, &mode); |
| 2202 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2203 | layer->setAlpha(alpha, mode); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2204 | |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2205 | #if RENDER_LAYERS_AS_REGIONS |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2206 | if (!layer->region.isEmpty()) { |
| 2207 | if (layer->region.isRect()) { |
Romain Guy | 4066767 | 2011-03-18 14:34:03 -0700 | [diff] [blame] | 2208 | composeLayerRect(layer, layer->regionRect); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2209 | } else if (layer->mesh) { |
Romain Guy | 8168396 | 2011-01-24 20:40:18 -0800 | [diff] [blame] | 2210 | const float a = alpha / 255.0f; |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2211 | const Rect& rect = layer->layer; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2212 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2213 | setupDraw(); |
| 2214 | setupDrawWithTexture(); |
Romain Guy | 8168396 | 2011-01-24 20:40:18 -0800 | [diff] [blame] | 2215 | setupDrawColor(a, a, a, a); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2216 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2217 | setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2218 | setupDrawProgram(); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2219 | setupDrawPureColorUniforms(); |
| 2220 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 2221 | setupDrawTexture(layer->getTexture()); |
| 2222 | if (mSnapshot->transform->isPureTranslate()) { |
| 2223 | x = (int) floorf(x + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2224 | y = (int) floorf(y + mSnapshot->transform->getTranslateY() + 0.5f); |
| 2225 | |
| 2226 | layer->setFilter(GL_NEAREST, GL_NEAREST); |
| 2227 | setupDrawModelViewTranslate(x, y, |
| 2228 | x + layer->layer.getWidth(), y + layer->layer.getHeight(), true); |
| 2229 | } else { |
| 2230 | layer->setFilter(GL_LINEAR, GL_LINEAR); |
| 2231 | setupDrawModelViewTranslate(x, y, |
| 2232 | x + layer->layer.getWidth(), y + layer->layer.getHeight()); |
| 2233 | } |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2234 | setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2235 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2236 | glDrawElements(GL_TRIANGLES, layer->meshElementCount, |
| 2237 | GL_UNSIGNED_SHORT, layer->meshIndices); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2238 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2239 | finishDrawTexture(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 2240 | |
| 2241 | #if DEBUG_LAYERS_AS_REGIONS |
| 2242 | drawRegionRects(layer->region); |
| 2243 | #endif |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 2244 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2245 | } |
| 2246 | #else |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 2247 | const Rect r(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight()); |
| 2248 | composeLayerRect(layer, r); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 2249 | #endif |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 2250 | } |
| 2251 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2252 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2253 | // Shaders |
| 2254 | /////////////////////////////////////////////////////////////////////////////// |
| 2255 | |
| 2256 | void OpenGLRenderer::resetShader() { |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2257 | mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2260 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
| 2261 | mShader = shader; |
| 2262 | if (mShader) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2263 | mShader->set(&mCaches.textureCache, &mCaches.gradientCache); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2264 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 2265 | } |
| 2266 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2267 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 2268 | // Color filters |
| 2269 | /////////////////////////////////////////////////////////////////////////////// |
| 2270 | |
| 2271 | void OpenGLRenderer::resetColorFilter() { |
| 2272 | mColorFilter = NULL; |
| 2273 | } |
| 2274 | |
| 2275 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
| 2276 | mColorFilter = filter; |
| 2277 | } |
| 2278 | |
| 2279 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2280 | // Drop shadow |
| 2281 | /////////////////////////////////////////////////////////////////////////////// |
| 2282 | |
| 2283 | void OpenGLRenderer::resetShadow() { |
| 2284 | mHasShadow = false; |
| 2285 | } |
| 2286 | |
| 2287 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
| 2288 | mHasShadow = true; |
| 2289 | mShadowRadius = radius; |
| 2290 | mShadowDx = dx; |
| 2291 | mShadowDy = dy; |
| 2292 | mShadowColor = color; |
| 2293 | } |
| 2294 | |
| 2295 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2296 | // Drawing implementation |
| 2297 | /////////////////////////////////////////////////////////////////////////////// |
| 2298 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2299 | void OpenGLRenderer::drawPathTexture(const PathTexture* texture, |
| 2300 | float x, float y, SkPaint* paint) { |
| 2301 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 2302 | return; |
| 2303 | } |
| 2304 | |
| 2305 | int alpha; |
| 2306 | SkXfermode::Mode mode; |
| 2307 | getAlphaAndMode(paint, &alpha, &mode); |
| 2308 | |
| 2309 | setupDraw(); |
| 2310 | setupDrawWithTexture(true); |
| 2311 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 2312 | setupDrawColorFilter(); |
| 2313 | setupDrawShader(); |
| 2314 | setupDrawBlending(true, mode); |
| 2315 | setupDrawProgram(); |
| 2316 | setupDrawModelView(x, y, x + texture->width, y + texture->height); |
| 2317 | setupDrawTexture(texture->id); |
| 2318 | setupDrawPureColorUniforms(); |
| 2319 | setupDrawColorFilterUniforms(); |
| 2320 | setupDrawShaderUniforms(); |
| 2321 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 2322 | |
| 2323 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2324 | |
| 2325 | finishDrawTexture(); |
| 2326 | } |
| 2327 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2328 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2329 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 2330 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 2331 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 2332 | |
| 2333 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length, |
| 2334 | float x, float y, SkPaint* paint) { |
| 2335 | // Handle underline and strike-through |
| 2336 | uint32_t flags = paint->getFlags(); |
| 2337 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2338 | SkPaint paintCopy(*paint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2339 | float underlineWidth = length; |
| 2340 | // If length is > 0.0f, we already measured the text for the text alignment |
| 2341 | if (length <= 0.0f) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2342 | underlineWidth = paintCopy.measureText(text, bytesCount); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | float offsetX = 0; |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2346 | switch (paintCopy.getTextAlign()) { |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2347 | case SkPaint::kCenter_Align: |
| 2348 | offsetX = underlineWidth * 0.5f; |
| 2349 | break; |
| 2350 | case SkPaint::kRight_Align: |
| 2351 | offsetX = underlineWidth; |
| 2352 | break; |
| 2353 | default: |
| 2354 | break; |
| 2355 | } |
| 2356 | |
| 2357 | if (underlineWidth > 0.0f) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2358 | const float textSize = paintCopy.getTextSize(); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 2359 | const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2360 | |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2361 | const float left = x - offsetX; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2362 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2363 | |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 2364 | int linesCount = 0; |
| 2365 | if (flags & SkPaint::kUnderlineText_Flag) linesCount++; |
| 2366 | if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; |
| 2367 | |
| 2368 | const int pointsCount = 4 * linesCount; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2369 | float points[pointsCount]; |
| 2370 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2371 | |
| 2372 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 2373 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2374 | points[currentPoint++] = left; |
| 2375 | points[currentPoint++] = top; |
| 2376 | points[currentPoint++] = left + underlineWidth; |
| 2377 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2378 | } |
| 2379 | |
| 2380 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 2381 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2382 | points[currentPoint++] = left; |
| 2383 | points[currentPoint++] = top; |
| 2384 | points[currentPoint++] = left + underlineWidth; |
| 2385 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2386 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2387 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2388 | paintCopy.setStrokeWidth(strokeWidth); |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 2389 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 2390 | drawLines(&points[0], pointsCount, &paintCopy); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2395 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 2396 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2397 | // If a shader is set, preserve only the alpha |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2398 | if (mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2399 | color |= 0x00ffffff; |
| 2400 | } |
| 2401 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2402 | setupDraw(); |
| 2403 | setupDrawColor(color); |
| 2404 | setupDrawShader(); |
| 2405 | setupDrawColorFilter(); |
| 2406 | setupDrawBlending(mode); |
| 2407 | setupDrawProgram(); |
| 2408 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
| 2409 | setupDrawColorUniforms(); |
| 2410 | setupDrawShaderUniforms(ignoreTransform); |
| 2411 | setupDrawColorFilterUniforms(); |
| 2412 | setupDrawSimpleMesh(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 2413 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 2414 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2415 | } |
| 2416 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2417 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 2418 | Texture* texture, SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2419 | int alpha; |
| 2420 | SkXfermode::Mode mode; |
| 2421 | getAlphaAndMode(paint, &alpha, &mode); |
| 2422 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 2423 | texture->setWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE, true); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 2424 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2425 | if (mSnapshot->transform->isPureTranslate()) { |
| 2426 | const float x = (int) floorf(left + mSnapshot->transform->getTranslateX() + 0.5f); |
| 2427 | const float y = (int) floorf(top + mSnapshot->transform->getTranslateY() + 0.5f); |
| 2428 | |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 2429 | texture->setFilter(GL_NEAREST, GL_NEAREST, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2430 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
| 2431 | alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL, |
| 2432 | (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true); |
| 2433 | } else { |
Romain Guy | e3c2685 | 2011-07-25 16:36:01 -0700 | [diff] [blame^] | 2434 | texture->setFilter(GL_LINEAR, GL_LINEAR, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2435 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
| 2436 | texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, |
| 2437 | GL_TRIANGLE_STRIP, gMeshCount); |
| 2438 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 2439 | } |
| 2440 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 2441 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 2442 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 2443 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 2444 | (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 2448 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 2449 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2450 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2451 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 2452 | setupDraw(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2453 | setupDrawWithTexture(); |
| 2454 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 2455 | setupDrawColorFilter(); |
| 2456 | setupDrawBlending(blend, mode, swapSrcDst); |
| 2457 | setupDrawProgram(); |
| 2458 | if (!dirty) { |
| 2459 | setupDrawDirtyRegionsDisabled(); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 2460 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2461 | if (!ignoreScale) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2462 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2463 | } else { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2464 | setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2465 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2466 | setupDrawPureColorUniforms(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2467 | setupDrawColorFilterUniforms(); |
| 2468 | setupDrawTexture(texture); |
| 2469 | setupDrawMesh(vertices, texCoords, vbo); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 2470 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 2471 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2472 | |
| 2473 | finishDrawTexture(); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2476 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2477 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2478 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
| 2479 | if (blend) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2480 | if (mode < SkXfermode::kPlus_Mode) { |
| 2481 | if (!mCaches.blend) { |
| 2482 | glEnable(GL_BLEND); |
| 2483 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2484 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2485 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 2486 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2487 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2488 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 2489 | glBlendFunc(sourceMode, destMode); |
| 2490 | mCaches.lastSrcMode = sourceMode; |
| 2491 | mCaches.lastDstMode = destMode; |
| 2492 | } |
| 2493 | } else { |
| 2494 | // These blend modes are not supported by OpenGL directly and have |
| 2495 | // to be implemented using shaders. Since the shader will perform |
| 2496 | // the blending, turn blending off here |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 2497 | if (mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2498 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 2499 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2500 | } |
| 2501 | |
| 2502 | if (mCaches.blend) { |
| 2503 | glDisable(GL_BLEND); |
| 2504 | } |
| 2505 | blend = false; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2506 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2507 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2508 | glDisable(GL_BLEND); |
| 2509 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2510 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 2511 | } |
| 2512 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 2513 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2514 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2515 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 2516 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 2517 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2518 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 2519 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2520 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2523 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 2524 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 2525 | TextureVertex::setUV(v++, u1, v1); |
| 2526 | TextureVertex::setUV(v++, u2, v1); |
| 2527 | TextureVertex::setUV(v++, u1, v2); |
| 2528 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2531 | void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) { |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2532 | if (paint) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 2533 | if (!mCaches.extensions.hasFramebufferFetch()) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2534 | const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode); |
| 2535 | if (!isMode) { |
| 2536 | // Assume SRC_OVER |
| 2537 | *mode = SkXfermode::kSrcOver_Mode; |
| 2538 | } |
| 2539 | } else { |
| 2540 | *mode = getXfermode(paint->getXfermode()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | // Skia draws using the color's alpha channel if < 255 |
| 2544 | // Otherwise, it uses the paint's alpha |
| 2545 | int color = paint->getColor(); |
| 2546 | *alpha = (color >> 24) & 0xFF; |
| 2547 | if (*alpha == 255) { |
| 2548 | *alpha = paint->getAlpha(); |
| 2549 | } |
| 2550 | } else { |
| 2551 | *mode = SkXfermode::kSrcOver_Mode; |
| 2552 | *alpha = 255; |
| 2553 | } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2554 | } |
| 2555 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2556 | SkXfermode::Mode OpenGLRenderer::getXfermode(SkXfermode* mode) { |
Derek Sollenberger | d39d1af | 2011-05-16 13:09:42 -0400 | [diff] [blame] | 2557 | SkXfermode::Mode resultMode; |
| 2558 | if (!SkXfermode::AsMode(mode, &resultMode)) { |
| 2559 | resultMode = SkXfermode::kSrcOver_Mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2560 | } |
Derek Sollenberger | d39d1af | 2011-05-16 13:09:42 -0400 | [diff] [blame] | 2561 | return resultMode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 2562 | } |
| 2563 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2564 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 2565 | }; // namespace android |