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" |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 34 | #include "DeferredDisplayList.h" |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 35 | #include "DisplayListRenderer.h" |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 36 | #include "Fence.h" |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 37 | #include "PathTessellator.h" |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 38 | #include "Properties.h" |
Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 39 | #include "Vector.h" |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 40 | |
| 41 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 42 | namespace uirenderer { |
| 43 | |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | // Defines |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 48 | #define RAD_TO_DEG (180.0f / 3.14159265f) |
| 49 | #define MIN_ANGLE 0.001f |
| 50 | |
Romain Guy | f877308 | 2012-07-12 18:01:00 -0700 | [diff] [blame] | 51 | #define ALPHA_THRESHOLD 0 |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 52 | |
Romain Guy | 713e1bb | 2012-10-16 18:44:09 -0700 | [diff] [blame] | 53 | #define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST) |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 54 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 55 | /////////////////////////////////////////////////////////////////////////////// |
| 56 | // Globals |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 61 | */ |
| 62 | struct Blender { |
| 63 | SkXfermode::Mode mode; |
| 64 | GLenum src; |
| 65 | GLenum dst; |
| 66 | }; // struct Blender |
| 67 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 68 | // In this array, the index of each Blender equals the value of the first |
| 69 | // entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode] |
| 70 | static const Blender gBlends[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 71 | { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 72 | { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO }, |
| 73 | { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE }, |
| 74 | { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 75 | { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 76 | { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 77 | { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 78 | { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 79 | { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 80 | { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 81 | { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 82 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 83 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
Derek Sollenberger | d81ec45 | 2013-02-04 15:42:26 -0500 | [diff] [blame] | 84 | { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR }, |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 85 | { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR } |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 86 | }; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 87 | |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 88 | // This array contains the swapped version of each SkXfermode. For instance |
| 89 | // this array's SrcOver blending mode is actually DstOver. You can refer to |
| 90 | // createLayer() for more information on the purpose of this array. |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 91 | static const Blender gBlendsSwap[] = { |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 92 | { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 93 | { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE }, |
| 94 | { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO }, |
| 95 | { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE }, |
| 96 | { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA }, |
| 97 | { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA }, |
| 98 | { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO }, |
| 99 | { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA }, |
| 100 | { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO }, |
| 101 | { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA }, |
| 102 | { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 103 | { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA }, |
| 104 | { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE }, |
Derek Sollenberger | d81ec45 | 2013-02-04 15:42:26 -0500 | [diff] [blame] | 105 | { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO }, |
Romain Guy | 2ffefd4 | 2011-09-08 15:33:03 -0700 | [diff] [blame] | 106 | { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE } |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 109 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 110 | // Functions |
| 111 | /////////////////////////////////////////////////////////////////////////////// |
| 112 | |
| 113 | template<typename T> |
| 114 | static inline T min(T a, T b) { |
| 115 | return a < b ? a : b; |
| 116 | } |
| 117 | |
| 118 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 119 | // Constructors/destructor |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 122 | OpenGLRenderer::OpenGLRenderer(): |
| 123 | mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 124 | // *set* draw modifiers to be 0 |
| 125 | memset(&mDrawModifiers, 0, sizeof(mDrawModifiers)); |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 126 | mDrawModifiers.mOverrideLayerAlpha = 1.0f; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 127 | |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 128 | memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices)); |
| 129 | |
Romain Guy | ae5575b | 2010-07-29 18:48:04 -0700 | [diff] [blame] | 130 | mFirstSnapshot = new Snapshot; |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 131 | mFrameStarted = false; |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 132 | mCountOverdraw = false; |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 133 | |
| 134 | mScissorOptimizationDisabled = false; |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 137 | OpenGLRenderer::~OpenGLRenderer() { |
Romain Guy | 29d8997 | 2010-09-22 16:10:57 -0700 | [diff] [blame] | 138 | // The context has already been destroyed at this point, do not call |
| 139 | // GL APIs. All GL state should be kept in Caches.h |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 142 | void OpenGLRenderer::initProperties() { |
| 143 | char property[PROPERTY_VALUE_MAX]; |
| 144 | if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) { |
| 145 | mScissorOptimizationDisabled = !strcasecmp(property, "true"); |
| 146 | INIT_LOGD(" Scissor optimization %s", |
| 147 | mScissorOptimizationDisabled ? "disabled" : "enabled"); |
| 148 | } else { |
| 149 | INIT_LOGD(" Scissor optimization enabled"); |
| 150 | } |
Romain Guy | 13631f3 | 2012-01-30 17:41:55 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 154 | // Setup |
| 155 | /////////////////////////////////////////////////////////////////////////////// |
| 156 | |
Romain Guy | ef35927 | 2013-01-31 19:07:29 -0800 | [diff] [blame] | 157 | void OpenGLRenderer::setName(const char* name) { |
| 158 | if (name) { |
| 159 | mName.setTo(name); |
| 160 | } else { |
| 161 | mName.clear(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | const char* OpenGLRenderer::getName() const { |
| 166 | return mName.string(); |
| 167 | } |
| 168 | |
Romain Guy | 49c5fc0 | 2012-05-15 11:10:01 -0700 | [diff] [blame] | 169 | bool OpenGLRenderer::isDeferred() { |
| 170 | return false; |
| 171 | } |
| 172 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 173 | void OpenGLRenderer::setViewport(int width, int height) { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 174 | initViewport(width, height); |
| 175 | |
| 176 | glDisable(GL_DITHER); |
| 177 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 178 | |
| 179 | glEnableVertexAttribArray(Program::kBindingPosition); |
| 180 | } |
| 181 | |
| 182 | void OpenGLRenderer::initViewport(int width, int height) { |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 183 | mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 184 | |
| 185 | mWidth = width; |
| 186 | mHeight = height; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 187 | |
| 188 | mFirstSnapshot->height = height; |
| 189 | mFirstSnapshot->viewport.set(0, 0, width, height); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 192 | void OpenGLRenderer::setupFrameState(float left, float top, |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 193 | float right, float bottom, bool opaque) { |
Romain Guy | fe48f65 | 2010-11-11 15:36:56 -0800 | [diff] [blame] | 194 | mCaches.clearGarbage(); |
| 195 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 196 | mOpaque = opaque; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 197 | mSnapshot = new Snapshot(mFirstSnapshot, |
| 198 | SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag); |
Romain Guy | 84962f2 | 2011-03-02 15:43:44 -0800 | [diff] [blame] | 199 | mSnapshot->fbo = getTargetFbo(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 200 | mSaveCount = 1; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 201 | |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 202 | mSnapshot->setClip(left, top, right, bottom); |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 203 | mTilingClip.set(left, top, right, bottom); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | status_t OpenGLRenderer::startFrame() { |
| 207 | if (mFrameStarted) return DrawGlInfo::kStatusDone; |
| 208 | mFrameStarted = true; |
| 209 | |
Romain Guy | 41308e2 | 2012-10-22 20:02:43 -0700 | [diff] [blame] | 210 | mDirtyClip = true; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 211 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 212 | discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 213 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 214 | glViewport(0, 0, mWidth, mHeight); |
Romain Guy | 7d7b549 | 2011-01-24 16:33:45 -0800 | [diff] [blame] | 215 | |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 216 | // Functors break the tiling extension in pretty spectacular ways |
| 217 | // This ensures we don't use tiling when a functor is going to be |
| 218 | // invoked during the frame |
| 219 | mSuppressTiling = mCaches.hasRegisteredFunctors(); |
| 220 | |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 221 | startTiling(mSnapshot, true); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 222 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 223 | debugOverdraw(true, true); |
| 224 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 225 | return clear(mTilingClip.left, mTilingClip.top, |
| 226 | mTilingClip.right, mTilingClip.bottom, mOpaque); |
| 227 | } |
| 228 | |
| 229 | status_t OpenGLRenderer::prepare(bool opaque) { |
| 230 | return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque); |
| 231 | } |
| 232 | |
| 233 | status_t OpenGLRenderer::prepareDirty(float left, float top, |
| 234 | float right, float bottom, bool opaque) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 235 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 236 | setupFrameState(left, top, right, bottom, opaque); |
| 237 | |
| 238 | // Layer renderers will start the frame immediately |
| 239 | // The framebuffer renderer will first defer the display list |
| 240 | // for each layer and wait until the first drawing command |
| 241 | // to start the frame |
| 242 | if (mSnapshot->fbo == 0) { |
| 243 | syncState(); |
| 244 | updateLayers(); |
| 245 | } else { |
| 246 | return startFrame(); |
| 247 | } |
| 248 | |
| 249 | return DrawGlInfo::kStatusDone; |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Romain Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 252 | void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) { |
| 253 | // If we know that we are going to redraw the entire framebuffer, |
| 254 | // perform a discard to let the driver know we don't need to preserve |
| 255 | // the back buffer for this frame. |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 256 | if (mExtensions.hasDiscardFramebuffer() && |
Romain Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 257 | left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) { |
Romain Guy | f158198 | 2013-01-31 17:20:30 -0800 | [diff] [blame] | 258 | const bool isFbo = getTargetFbo() == 0; |
| 259 | const GLenum attachments[] = { |
| 260 | isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0, |
| 261 | isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT }; |
Romain Guy | dcfc836 | 2013-01-03 13:08:57 -0800 | [diff] [blame] | 262 | glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments); |
| 263 | } |
| 264 | } |
| 265 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 266 | status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 267 | if (!opaque || mCountOverdraw) { |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 268 | mCaches.enableScissor(); |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 269 | mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 270 | glClear(GL_COLOR_BUFFER_BIT); |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 271 | return DrawGlInfo::kStatusDrew; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 272 | } |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 273 | |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 274 | mCaches.resetScissor(); |
Chet Haase | 44b2fe3 | 2012-06-06 19:03:58 -0700 | [diff] [blame] | 275 | return DrawGlInfo::kStatusDone; |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | void OpenGLRenderer::syncState() { |
Romain Guy | ddf7437 | 2012-05-22 14:07:07 -0700 | [diff] [blame] | 279 | if (mCaches.blend) { |
| 280 | glEnable(GL_BLEND); |
| 281 | } else { |
| 282 | glDisable(GL_BLEND); |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 283 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Romain Guy | 57b5268 | 2012-09-20 17:38:46 -0700 | [diff] [blame] | 286 | void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 287 | if (!mSuppressTiling) { |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 288 | Rect* clip = &mTilingClip; |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 289 | if (s->flags & Snapshot::kFlagFboTarget) { |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 290 | clip = &(s->layer->clipRect); |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 291 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 292 | |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 293 | startTiling(*clip, s->height, opaque); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) { |
| 298 | if (!mSuppressTiling) { |
| 299 | mCaches.startTiling(clip.left, windowHeight - clip.bottom, |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 300 | clip.right - clip.left, clip.bottom - clip.top, opaque); |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 301 | } |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void OpenGLRenderer::endTiling() { |
Romain Guy | 54c1a64 | 2012-09-27 17:55:46 -0700 | [diff] [blame] | 305 | if (!mSuppressTiling) mCaches.endTiling(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 308 | void OpenGLRenderer::finish() { |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 309 | renderOverdraw(); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 310 | endTiling(); |
| 311 | |
Romain Guy | ca89e2a | 2013-03-08 17:44:20 -0800 | [diff] [blame] | 312 | // When finish() is invoked on FBO 0 we've reached the end |
| 313 | // of the current frame |
| 314 | if (getTargetFbo() == 0) { |
| 315 | mCaches.pathCache.trim(); |
| 316 | } |
| 317 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 318 | if (!suppressErrorChecks()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 319 | #if DEBUG_OPENGL |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 320 | GLenum status = GL_NO_ERROR; |
| 321 | while ((status = glGetError()) != GL_NO_ERROR) { |
| 322 | ALOGD("GL error from OpenGLRenderer: 0x%x", status); |
| 323 | switch (status) { |
| 324 | case GL_INVALID_ENUM: |
| 325 | ALOGE(" GL_INVALID_ENUM"); |
| 326 | break; |
| 327 | case GL_INVALID_VALUE: |
| 328 | ALOGE(" GL_INVALID_VALUE"); |
| 329 | break; |
| 330 | case GL_INVALID_OPERATION: |
| 331 | ALOGE(" GL_INVALID_OPERATION"); |
| 332 | break; |
| 333 | case GL_OUT_OF_MEMORY: |
| 334 | ALOGE(" Out of memory!"); |
| 335 | break; |
| 336 | } |
Romain Guy | a07105b | 2011-01-10 21:14:18 -0800 | [diff] [blame] | 337 | } |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 338 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 339 | |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 340 | #if DEBUG_MEMORY_USAGE |
Romain Guy | e190aa6 | 2010-11-10 19:01:29 -0800 | [diff] [blame] | 341 | mCaches.dumpMemoryUsage(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 342 | #else |
| 343 | if (mCaches.getDebugLevel() & kDebugMemory) { |
| 344 | mCaches.dumpMemoryUsage(); |
| 345 | } |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 346 | #endif |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 347 | } |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 348 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 349 | if (mCountOverdraw) { |
| 350 | countOverdraw(); |
| 351 | } |
| 352 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 353 | mFrameStarted = false; |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 356 | void OpenGLRenderer::interrupt() { |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 357 | if (mCaches.currentProgram) { |
| 358 | if (mCaches.currentProgram->isInUse()) { |
| 359 | mCaches.currentProgram->remove(); |
| 360 | mCaches.currentProgram = NULL; |
| 361 | } |
| 362 | } |
Chris Craik | 9ab2d18 | 2013-07-22 16:16:06 -0700 | [diff] [blame] | 363 | mCaches.resetActiveTexture(); |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 364 | mCaches.unbindMeshBuffer(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 365 | mCaches.unbindIndicesBuffer(); |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 366 | mCaches.resetVertexPointers(); |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 367 | mCaches.disableTexCoordsVertexArray(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 368 | debugOverdraw(false, false); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 371 | void OpenGLRenderer::resume() { |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 372 | sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; |
Chet Haase | 08837c2 | 2011-11-28 11:53:21 -0800 | [diff] [blame] | 373 | glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 374 | glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 375 | debugOverdraw(true, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 376 | |
Romain Guy | 3e263fa | 2011-12-12 16:47:48 -0800 | [diff] [blame] | 377 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 378 | |
Chet Haase | 8025061 | 2012-08-15 13:46:54 -0700 | [diff] [blame] | 379 | mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 380 | mCaches.enableScissor(); |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 381 | mCaches.resetScissor(); |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 382 | dirtyClip(); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 383 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 384 | mCaches.activeTexture(0); |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 385 | mCaches.resetBoundTextures(); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 386 | |
Romain Guy | 50c0f09 | 2010-10-19 11:42:22 -0700 | [diff] [blame] | 387 | mCaches.blend = true; |
| 388 | glEnable(GL_BLEND); |
| 389 | glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode); |
| 390 | glBlendEquation(GL_FUNC_ADD); |
Romain Guy | da8532c | 2010-08-31 11:50:35 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 393 | void OpenGLRenderer::resumeAfterLayer() { |
| 394 | sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot; |
| 395 | glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight()); |
| 396 | glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 397 | debugOverdraw(true, false); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 398 | |
| 399 | mCaches.resetScissor(); |
| 400 | dirtyClip(); |
| 401 | } |
| 402 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 403 | void OpenGLRenderer::detachFunctor(Functor* functor) { |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 404 | mFunctors.remove(functor); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void OpenGLRenderer::attachFunctor(Functor* functor) { |
| 408 | mFunctors.add(functor); |
| 409 | } |
| 410 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 411 | status_t OpenGLRenderer::invokeFunctors(Rect& dirty) { |
| 412 | status_t result = DrawGlInfo::kStatusDone; |
Romain Guy | 3d745c0 | 2012-04-23 20:36:17 -0700 | [diff] [blame] | 413 | size_t count = mFunctors.size(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 414 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 415 | if (count > 0) { |
Chris Craik | d15321b | 2012-11-28 14:45:04 -0800 | [diff] [blame] | 416 | interrupt(); |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 417 | SortedVector<Functor*> functors(mFunctors); |
| 418 | mFunctors.clear(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 419 | |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 420 | DrawGlInfo info; |
| 421 | info.clipLeft = 0; |
| 422 | info.clipTop = 0; |
| 423 | info.clipRight = 0; |
| 424 | info.clipBottom = 0; |
| 425 | info.isLayer = false; |
| 426 | info.width = 0; |
| 427 | info.height = 0; |
| 428 | memset(info.transform, 0, sizeof(float) * 16); |
| 429 | |
| 430 | for (size_t i = 0; i < count; i++) { |
| 431 | Functor* f = functors.itemAt(i); |
| 432 | result |= (*f)(DrawGlInfo::kModeProcess, &info); |
| 433 | |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 434 | if (result & DrawGlInfo::kStatusDraw) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 435 | Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); |
| 436 | dirty.unionWith(localDirty); |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 437 | } |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 438 | |
Chris Craik | c2c9543 | 2012-04-25 15:13:52 -0700 | [diff] [blame] | 439 | if (result & DrawGlInfo::kStatusInvoke) { |
| 440 | mFunctors.add(f); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
Chris Craik | d15321b | 2012-11-28 14:45:04 -0800 | [diff] [blame] | 443 | resume(); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | return result; |
| 447 | } |
| 448 | |
| 449 | status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) { |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 450 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
| 451 | |
Chris Craik | 932b7f6 | 2012-06-06 13:59:33 -0700 | [diff] [blame] | 452 | detachFunctor(functor); |
Chris Craik | c8538ade | 2012-05-22 11:54:06 -0700 | [diff] [blame] | 453 | |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 454 | |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 455 | Rect clip(*mSnapshot->clipRect); |
| 456 | clip.snapToPixelBoundaries(); |
| 457 | |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 458 | // Since we don't know what the functor will draw, let's dirty |
| 459 | // tne entire clip region |
| 460 | if (hasLayer()) { |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 461 | dirtyLayerUnchecked(clip, getRegion()); |
| 462 | } |
Romain Guy | d643bb5 | 2011-03-01 14:55:21 -0800 | [diff] [blame] | 463 | |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 464 | DrawGlInfo info; |
| 465 | info.clipLeft = clip.left; |
| 466 | info.clipTop = clip.top; |
| 467 | info.clipRight = clip.right; |
| 468 | info.clipBottom = clip.bottom; |
| 469 | info.isLayer = hasLayer(); |
Chet Haase | 7b6a758 | 2012-04-11 14:32:02 -0700 | [diff] [blame] | 470 | info.width = getSnapshot()->viewport.getWidth(); |
| 471 | info.height = getSnapshot()->height; |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 472 | getSnapshot()->transform->copyTo(&info.transform[0]); |
Romain Guy | 80911b8 | 2011-03-16 15:30:12 -0700 | [diff] [blame] | 473 | |
John Reck | 25d2f7b | 2013-09-10 13:12:09 -0700 | [diff] [blame] | 474 | bool dirtyClip = mDirtyClip; |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 475 | // setup GL state for functor |
| 476 | if (mDirtyClip) { |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 477 | setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt() |
| 478 | } |
John Reck | 25d2f7b | 2013-09-10 13:12:09 -0700 | [diff] [blame] | 479 | if (mCaches.enableScissor() || dirtyClip) { |
| 480 | setScissorFromClip(); |
| 481 | } |
Chris Craik | 54f574a | 2013-08-26 11:23:46 -0700 | [diff] [blame] | 482 | interrupt(); |
| 483 | |
| 484 | // call functor immediately after GL state setup |
Chris Craik | 4a2bff7 | 2013-04-16 13:50:16 -0700 | [diff] [blame] | 485 | status_t result = (*functor)(DrawGlInfo::kModeDraw, &info); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 486 | |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 487 | if (result != DrawGlInfo::kStatusDone) { |
Romain Guy | 08aa2cb | 2011-03-17 11:06:57 -0700 | [diff] [blame] | 488 | Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 489 | dirty.unionWith(localDirty); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 490 | |
Chris Craik | 65924a3 | 2012-04-05 17:52:11 -0700 | [diff] [blame] | 491 | if (result & DrawGlInfo::kStatusInvoke) { |
Romain Guy | ba6be8a | 2012-04-23 18:22:09 -0700 | [diff] [blame] | 492 | mFunctors.add(functor); |
Romain Guy | 8f3b8e3 | 2012-03-27 16:33:45 -0700 | [diff] [blame] | 493 | } |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 496 | resume(); |
Chris Craik | 4a2bff7 | 2013-04-16 13:50:16 -0700 | [diff] [blame] | 497 | return result | DrawGlInfo::kStatusDrew; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 500 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 501 | // Debug |
| 502 | /////////////////////////////////////////////////////////////////////////////// |
| 503 | |
Romain Guy | 0f66753 | 2013-03-01 14:31:04 -0800 | [diff] [blame] | 504 | void OpenGLRenderer::eventMark(const char* name) const { |
| 505 | mCaches.eventMark(0, name); |
| 506 | } |
| 507 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 508 | void OpenGLRenderer::startMark(const char* name) const { |
| 509 | mCaches.startMark(0, name); |
| 510 | } |
| 511 | |
| 512 | void OpenGLRenderer::endMark() const { |
| 513 | mCaches.endMark(); |
| 514 | } |
| 515 | |
| 516 | void OpenGLRenderer::debugOverdraw(bool enable, bool clear) { |
| 517 | if (mCaches.debugOverdraw && getTargetFbo() == 0) { |
| 518 | if (clear) { |
| 519 | mCaches.disableScissor(); |
| 520 | mCaches.stencil.clear(); |
| 521 | } |
| 522 | if (enable) { |
| 523 | mCaches.stencil.enableDebugWrite(); |
| 524 | } else { |
| 525 | mCaches.stencil.disable(); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | void OpenGLRenderer::renderOverdraw() { |
| 531 | if (mCaches.debugOverdraw && getTargetFbo() == 0) { |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 532 | const Rect* clip = &mTilingClip; |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 533 | |
| 534 | mCaches.enableScissor(); |
Chris Craik | 5f80362 | 2013-03-21 14:39:04 -0700 | [diff] [blame] | 535 | mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom, |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 536 | clip->right - clip->left, clip->bottom - clip->top); |
| 537 | |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 538 | // 1x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 539 | mCaches.stencil.enableDebugTest(2); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 540 | drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode); |
| 541 | |
| 542 | // 2x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 543 | mCaches.stencil.enableDebugTest(3); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 544 | drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode); |
| 545 | |
| 546 | // 3x overdraw |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 547 | mCaches.stencil.enableDebugTest(4); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 548 | drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode); |
| 549 | |
| 550 | // 4x overdraw and higher |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 551 | mCaches.stencil.enableDebugTest(4, true); |
Romain Guy | 627c6fd | 2013-08-21 11:53:18 -0700 | [diff] [blame] | 552 | drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode); |
| 553 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 554 | mCaches.stencil.disable(); |
| 555 | } |
| 556 | } |
| 557 | |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 558 | void OpenGLRenderer::countOverdraw() { |
| 559 | size_t count = mWidth * mHeight; |
| 560 | uint32_t* buffer = new uint32_t[count]; |
| 561 | glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]); |
| 562 | |
| 563 | size_t total = 0; |
| 564 | for (size_t i = 0; i < count; i++) { |
| 565 | total += buffer[i] & 0xff; |
| 566 | } |
| 567 | |
| 568 | mOverdraw = total / float(count); |
| 569 | |
| 570 | delete[] buffer; |
| 571 | } |
| 572 | |
Romain Guy | 87e2f757 | 2012-09-24 11:37:12 -0700 | [diff] [blame] | 573 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 574 | // Layers |
| 575 | /////////////////////////////////////////////////////////////////////////////// |
| 576 | |
| 577 | bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 578 | if (layer->deferredUpdateScheduled && layer->renderer && |
| 579 | layer->displayList && layer->displayList->isRenderable()) { |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 580 | ATRACE_CALL(); |
| 581 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 582 | Rect& dirty = layer->dirtyRect; |
| 583 | |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 584 | if (inFrame) { |
| 585 | endTiling(); |
| 586 | debugOverdraw(false, false); |
| 587 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 588 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 589 | if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) { |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 590 | layer->render(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 591 | } else { |
| 592 | layer->defer(); |
| 593 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 594 | |
| 595 | if (inFrame) { |
| 596 | resumeAfterLayer(); |
| 597 | startTiling(mSnapshot); |
| 598 | } |
| 599 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 600 | layer->debugDrawUpdate = mCaches.debugLayersUpdates; |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 601 | layer->hasDrawnSinceUpdate = false; |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
| 606 | return false; |
| 607 | } |
| 608 | |
| 609 | void OpenGLRenderer::updateLayers() { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 610 | // If draw deferring is enabled this method will simply defer |
| 611 | // the display list of each individual layer. The layers remain |
| 612 | // in the layer updates list which will be cleared by flushLayers(). |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 613 | int count = mLayerUpdates.size(); |
| 614 | if (count > 0) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 615 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
| 616 | startMark("Layer Updates"); |
| 617 | } else { |
| 618 | startMark("Defer Layer Updates"); |
| 619 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 620 | |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 621 | // Note: it is very important to update the layers in order |
| 622 | for (int i = 0; i < count; i++) { |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 623 | Layer* layer = mLayerUpdates.itemAt(i); |
| 624 | updateLayer(layer, false); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 625 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
| 626 | mCaches.resourceCache.decrementRefcount(layer); |
| 627 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 628 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 629 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 630 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
| 631 | mLayerUpdates.clear(); |
| 632 | glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); |
| 633 | } |
| 634 | endMark(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | void OpenGLRenderer::flushLayers() { |
| 639 | int count = mLayerUpdates.size(); |
| 640 | if (count > 0) { |
| 641 | startMark("Apply Layer Updates"); |
| 642 | char layerName[12]; |
| 643 | |
Chris Craik | 1206b9b | 2013-04-04 14:46:24 -0700 | [diff] [blame] | 644 | // Note: it is very important to update the layers in order |
| 645 | for (int i = 0; i < count; i++) { |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 646 | sprintf(layerName, "Layer #%d", i); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 647 | startMark(layerName); |
| 648 | |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 649 | ATRACE_BEGIN("flushLayer"); |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 650 | Layer* layer = mLayerUpdates.itemAt(i); |
| 651 | layer->flush(); |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 652 | ATRACE_END(); |
| 653 | |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 654 | mCaches.resourceCache.decrementRefcount(layer); |
| 655 | |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 656 | endMark(); |
| 657 | } |
| 658 | |
| 659 | mLayerUpdates.clear(); |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 660 | glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo()); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 661 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 662 | endMark(); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void OpenGLRenderer::pushLayerUpdate(Layer* layer) { |
| 667 | if (layer) { |
Romain Guy | 02b49b7 | 2013-03-29 12:37:16 -0700 | [diff] [blame] | 668 | // Make sure we don't introduce duplicates. |
| 669 | // SortedVector would do this automatically but we need to respect |
| 670 | // the insertion order. The linear search is not an issue since |
| 671 | // this list is usually very short (typically one item, at most a few) |
| 672 | for (int i = mLayerUpdates.size() - 1; i >= 0; i--) { |
| 673 | if (mLayerUpdates.itemAt(i) == layer) { |
| 674 | return; |
| 675 | } |
| 676 | } |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 677 | mLayerUpdates.push_back(layer); |
| 678 | mCaches.resourceCache.incrementRefcount(layer); |
| 679 | } |
| 680 | } |
| 681 | |
Romain Guy | e93482f | 2013-06-17 13:14:51 -0700 | [diff] [blame] | 682 | void OpenGLRenderer::cancelLayerUpdate(Layer* layer) { |
| 683 | if (layer) { |
| 684 | for (int i = mLayerUpdates.size() - 1; i >= 0; i--) { |
| 685 | if (mLayerUpdates.itemAt(i) == layer) { |
| 686 | mLayerUpdates.removeAt(i); |
| 687 | mCaches.resourceCache.decrementRefcount(layer); |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 694 | void OpenGLRenderer::clearLayerUpdates() { |
| 695 | size_t count = mLayerUpdates.size(); |
| 696 | if (count > 0) { |
| 697 | mCaches.resourceCache.lock(); |
| 698 | for (size_t i = 0; i < count; i++) { |
| 699 | mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i)); |
| 700 | } |
| 701 | mCaches.resourceCache.unlock(); |
| 702 | mLayerUpdates.clear(); |
| 703 | } |
| 704 | } |
| 705 | |
Romain Guy | 4054360 | 2013-06-12 15:31:28 -0700 | [diff] [blame] | 706 | void OpenGLRenderer::flushLayerUpdates() { |
| 707 | syncState(); |
| 708 | updateLayers(); |
| 709 | flushLayers(); |
| 710 | // Wait for all the layer updates to be executed |
| 711 | AutoFence fence; |
| 712 | } |
| 713 | |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 714 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 715 | // State management |
| 716 | /////////////////////////////////////////////////////////////////////////////// |
| 717 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 718 | int OpenGLRenderer::getSaveCount() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 719 | return mSaveCount; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | int OpenGLRenderer::save(int flags) { |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 723 | return saveSnapshot(flags); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | void OpenGLRenderer::restore() { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 727 | if (mSaveCount > 1) { |
| 728 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 729 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | void OpenGLRenderer::restoreToCount(int saveCount) { |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 733 | if (saveCount < 1) saveCount = 1; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 734 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 735 | while (mSaveCount > saveCount) { |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 736 | restoreSnapshot(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 737 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 740 | int OpenGLRenderer::saveSnapshot(int flags) { |
| 741 | mSnapshot = new Snapshot(mSnapshot, flags); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 742 | return mSaveCount++; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | bool OpenGLRenderer::restoreSnapshot() { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 746 | bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 747 | bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 748 | bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 749 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 750 | sp<Snapshot> current = mSnapshot; |
| 751 | sp<Snapshot> previous = mSnapshot->previous; |
| 752 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 753 | if (restoreOrtho) { |
| 754 | Rect& r = previous->viewport; |
| 755 | glViewport(r.left, r.top, r.right, r.bottom); |
| 756 | mOrthoMatrix.load(current->orthoMatrix); |
| 757 | } |
| 758 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 759 | mSaveCount--; |
| 760 | mSnapshot = previous; |
| 761 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 762 | if (restoreClip) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 763 | dirtyClip(); |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 764 | } |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 765 | |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 766 | if (restoreLayer) { |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 767 | endMark(); // Savelayer |
| 768 | startMark("ComposeLayer"); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 769 | composeLayer(current, previous); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 770 | endMark(); |
Romain Guy | 5ec9924 | 2010-11-03 16:19:08 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Romain Guy | 2542d19 | 2010-08-18 11:47:12 -0700 | [diff] [blame] | 773 | return restoreClip; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 776 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 777 | // Layers |
| 778 | /////////////////////////////////////////////////////////////////////////////// |
| 779 | |
| 780 | int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom, |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 781 | int alpha, SkXfermode::Mode mode, int flags) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 782 | const GLuint previousFbo = mSnapshot->fbo; |
| 783 | const int count = saveSnapshot(flags); |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 784 | |
Romain Guy | af636eb | 2010-12-09 17:47:21 -0800 | [diff] [blame] | 785 | if (!mSnapshot->isIgnored()) { |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 786 | createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 787 | } |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 788 | |
| 789 | return count; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 792 | void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) { |
| 793 | const Rect untransformedBounds(bounds); |
| 794 | |
| 795 | currentTransform().mapRect(bounds); |
| 796 | |
| 797 | // Layers only make sense if they are in the framebuffer's bounds |
| 798 | if (bounds.intersect(*mSnapshot->clipRect)) { |
| 799 | // We cannot work with sub-pixels in this case |
| 800 | bounds.snapToPixelBoundaries(); |
| 801 | |
| 802 | // When the layer is not an FBO, we may use glCopyTexImage so we |
| 803 | // need to make sure the layer does not extend outside the bounds |
| 804 | // of the framebuffer |
| 805 | if (!bounds.intersect(mSnapshot->previous->viewport)) { |
| 806 | bounds.setEmpty(); |
| 807 | } else if (fboLayer) { |
| 808 | clip.set(bounds); |
| 809 | mat4 inverse; |
| 810 | inverse.loadInverse(currentTransform()); |
| 811 | inverse.mapRect(clip); |
| 812 | clip.snapToPixelBoundaries(); |
| 813 | if (clip.intersect(untransformedBounds)) { |
| 814 | clip.translate(-untransformedBounds.left, -untransformedBounds.top); |
| 815 | bounds.set(untransformedBounds); |
| 816 | } else { |
| 817 | clip.setEmpty(); |
| 818 | } |
| 819 | } |
| 820 | } else { |
| 821 | bounds.setEmpty(); |
| 822 | } |
| 823 | } |
| 824 | |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 825 | void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip, |
| 826 | bool fboLayer, int alpha) { |
| 827 | if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize || |
| 828 | bounds.getHeight() > mCaches.maxTextureSize || |
| 829 | (fboLayer && clip.isEmpty())) { |
| 830 | mSnapshot->empty = fboLayer; |
| 831 | } else { |
| 832 | mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer); |
| 833 | } |
| 834 | } |
| 835 | |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 836 | int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom, |
| 837 | int alpha, SkXfermode::Mode mode, int flags) { |
| 838 | const GLuint previousFbo = mSnapshot->fbo; |
| 839 | const int count = saveSnapshot(flags); |
| 840 | |
| 841 | if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) { |
| 842 | // initialize the snapshot as though it almost represents an FBO layer so deferred draw |
| 843 | // operations will be able to store and restore the current clip and transform info, and |
| 844 | // quick rejection will be correct (for display lists) |
| 845 | |
| 846 | Rect bounds(left, top, right, bottom); |
| 847 | Rect clip; |
| 848 | calculateLayerBoundsAndClip(bounds, clip, true); |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 849 | updateSnapshotIgnoreForLayer(bounds, clip, true, alpha); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 850 | |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 851 | if (!mSnapshot->isIgnored()) { |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 852 | mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 853 | mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
Chris Craik | 0e87f00 | 2013-06-19 16:54:59 -0700 | [diff] [blame] | 854 | mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
| 858 | return count; |
| 859 | } |
| 860 | |
| 861 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 862 | /** |
| 863 | * Layers are viewed by Skia are slightly different than layers in image editing |
| 864 | * programs (for instance.) When a layer is created, previously created layers |
| 865 | * and the frame buffer still receive every drawing command. For instance, if a |
| 866 | * layer is created and a shape intersecting the bounds of the layers and the |
| 867 | * framebuffer is draw, the shape will be drawn on both (unless the layer was |
| 868 | * created with the SkCanvas::kClipToLayer_SaveFlag flag.) |
| 869 | * |
| 870 | * A way to implement layers is to create an FBO for each layer, backed by an RGBA |
| 871 | * texture. Unfortunately, this is inefficient as it requires every primitive to |
| 872 | * be drawn n + 1 times, where n is the number of active layers. In practice this |
| 873 | * means, for every primitive: |
| 874 | * - Switch active frame buffer |
| 875 | * - Change viewport, clip and projection matrix |
| 876 | * - Issue the drawing |
| 877 | * |
| 878 | * Switching rendering target n + 1 times per drawn primitive is extremely costly. |
Romain Guy | 6b7bd24 | 2010-10-06 19:49:23 -0700 | [diff] [blame] | 879 | * To avoid this, layers are implemented in a different way here, at least in the |
| 880 | * general case. FBOs are used, as an optimization, when the "clip to layer" flag |
| 881 | * is set. When this flag is set we can redirect all drawing operations into a |
| 882 | * single FBO. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 883 | * |
| 884 | * This implementation relies on the frame buffer being at least RGBA 8888. When |
| 885 | * a layer is created, only a texture is created, not an FBO. The content of the |
| 886 | * 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] | 887 | * 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] | 888 | * buffer and drawing continues as normal. This technique therefore treats the |
| 889 | * frame buffer as a scratch buffer for the layers. |
| 890 | * |
| 891 | * To compose the layers back onto the frame buffer, each layer texture |
| 892 | * (containing the original frame buffer data) is drawn as a simple quad over |
| 893 | * the frame buffer. The trick is that the quad is set as the composition |
| 894 | * destination in the blending equation, and the frame buffer becomes the source |
| 895 | * of the composition. |
| 896 | * |
| 897 | * Drawing layers with an alpha value requires an extra step before composition. |
| 898 | * An empty quad is drawn over the layer's region in the frame buffer. This quad |
| 899 | * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the |
| 900 | * quad is used to multiply the colors in the frame buffer. This is achieved by |
| 901 | * changing the GL blend functions for the GL_FUNC_ADD blend equation to |
| 902 | * GL_ZERO, GL_SRC_ALPHA. |
| 903 | * |
| 904 | * Because glCopyTexImage2D() can be slow, an alternative implementation might |
| 905 | * be use to draw a single clipped layer. The implementation described above |
| 906 | * is correct in every case. |
Romain Guy | 87a7657 | 2010-09-13 18:11:21 -0700 | [diff] [blame] | 907 | * |
| 908 | * (1) The frame buffer is actually not cleared right away. To allow the GPU |
| 909 | * to potentially optimize series of calls to glCopyTexImage2D, the frame |
| 910 | * buffer is left untouched until the first drawing operation. Only when |
| 911 | * something actually gets drawn are the layers regions cleared. |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 912 | */ |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 913 | bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom, |
| 914 | int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) { |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 915 | LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 916 | LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize()); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 917 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 918 | const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag; |
| 919 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 920 | // Window coordinates of the layer |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 921 | Rect clip; |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 922 | Rect bounds(left, top, right, bottom); |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 923 | calculateLayerBoundsAndClip(bounds, clip, fboLayer); |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 924 | updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha); |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 925 | |
| 926 | // Bail out if we won't draw in this snapshot |
Chris Craik | 408eb12 | 2013-03-26 18:55:15 -0700 | [diff] [blame] | 927 | if (mSnapshot->isIgnored()) { |
Romain Guy | b025b9c | 2010-09-16 14:16:48 -0700 | [diff] [blame] | 928 | return false; |
| 929 | } |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 930 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 931 | mCaches.activeTexture(0); |
Romain Guy | 8550c4c | 2010-10-08 15:49:53 -0700 | [diff] [blame] | 932 | Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight()); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 933 | if (!layer) { |
Romain Guy | f18fd99 | 2010-07-08 11:45:51 -0700 | [diff] [blame] | 934 | return false; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 937 | layer->setAlpha(alpha, mode); |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 938 | layer->layer.set(bounds); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 939 | layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()), |
| 940 | bounds.getWidth() / float(layer->getWidth()), 0.0f); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 941 | layer->setColorFilter(mDrawModifiers.mColorFilter); |
Chet Haase | a23eed8 | 2012-04-12 15:19:04 -0700 | [diff] [blame] | 942 | layer->setBlend(true); |
Romain Guy | 7c25aab | 2012-10-18 15:05:02 -0700 | [diff] [blame] | 943 | layer->setDirty(false); |
Romain Guy | dda5702 | 2010-07-06 11:39:32 -0700 | [diff] [blame] | 944 | |
Romain Guy | 8fb9542 | 2010-08-17 18:38:51 -0700 | [diff] [blame] | 945 | // Save the layer in the snapshot |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 946 | mSnapshot->flags |= Snapshot::kFlagIsLayer; |
| 947 | mSnapshot->layer = layer; |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 948 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 949 | startMark("SaveLayer"); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 950 | if (fboLayer) { |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 951 | return createFboLayer(layer, bounds, clip, previousFbo); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 952 | } else { |
| 953 | // Copy the framebuffer into the layer |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 954 | layer->bindTexture(); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 955 | if (!bounds.isEmpty()) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 956 | if (layer->isEmpty()) { |
Romain Guy | b254c24 | 2013-06-27 17:15:24 -0700 | [diff] [blame] | 957 | // Workaround for some GL drivers. When reading pixels lying outside |
| 958 | // of the window we should get undefined values for those pixels. |
| 959 | // Unfortunately some drivers will turn the entire target texture black |
| 960 | // when reading outside of the window. |
| 961 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(), |
| 962 | 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 963 | layer->setEmpty(false); |
Romain Guy | 514fb18 | 2011-01-19 14:38:29 -0800 | [diff] [blame] | 964 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 965 | |
Romain Guy | b254c24 | 2013-06-27 17:15:24 -0700 | [diff] [blame] | 966 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left, |
| 967 | mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight()); |
| 968 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 969 | // Enqueue the buffer coordinates to clear the corresponding region later |
| 970 | mLayers.push(new Rect(bounds)); |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 971 | } |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 972 | } |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 973 | |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 974 | return true; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 977 | bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) { |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 978 | layer->clipRect.set(clip); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 979 | layer->setFbo(mCaches.fboCache.get()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 980 | |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 981 | mSnapshot->region = &mSnapshot->layer->region; |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 982 | mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer | |
| 983 | Snapshot::kFlagDirtyOrtho; |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 984 | mSnapshot->fbo = layer->getFbo(); |
| 985 | mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f); |
| 986 | mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom); |
| 987 | mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight()); |
| 988 | mSnapshot->height = bounds.getHeight(); |
Chet Haase | d48885a | 2012-08-28 17:43:28 -0700 | [diff] [blame] | 989 | mSnapshot->orthoMatrix.load(mOrthoMatrix); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 990 | |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 991 | endTiling(); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 992 | debugOverdraw(false, false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 993 | // Bind texture to FBO |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 994 | glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo()); |
| 995 | layer->bindTexture(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 996 | |
| 997 | // Initialize the texture if needed |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 998 | if (layer->isEmpty()) { |
Romain Guy | 0908764 | 2013-04-04 12:27:54 -0700 | [diff] [blame] | 999 | layer->allocateTexture(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1000 | layer->setEmpty(false); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1004 | layer->getTexture(), 0); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1005 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 1006 | startTiling(mSnapshot, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1007 | |
| 1008 | // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 1009 | mCaches.enableScissor(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 1010 | mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1011 | clip.getWidth() + 2.0f, clip.getHeight() + 2.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1012 | glClear(GL_COLOR_BUFFER_BIT); |
| 1013 | |
| 1014 | dirtyClip(); |
| 1015 | |
| 1016 | // Change the ortho projection |
| 1017 | glViewport(0, 0, bounds.getWidth(), bounds.getHeight()); |
| 1018 | mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f); |
| 1019 | |
| 1020 | return true; |
| 1021 | } |
| 1022 | |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 1023 | /** |
| 1024 | * Read the documentation of createLayer() before doing anything in this method. |
| 1025 | */ |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1026 | void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) { |
| 1027 | if (!current->layer) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1028 | ALOGE("Attempting to compose a layer that does not exist"); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1032 | Layer* layer = current->layer; |
| 1033 | const Rect& rect = layer->layer; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1034 | const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer; |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 1035 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1036 | bool clipRequired = false; |
| 1037 | quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected |
| 1038 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
| 1039 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 1040 | if (fboLayer) { |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1041 | endTiling(); |
| 1042 | |
Romain Guy | e0aa84b | 2012-04-03 19:30:26 -0700 | [diff] [blame] | 1043 | // Detach the texture from the FBO |
| 1044 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1045 | |
| 1046 | layer->removeFbo(false); |
| 1047 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 1048 | // Unbind current FBO and restore previous one |
| 1049 | glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo); |
Romain Guy | 7c450aa | 2012-09-21 19:15:00 -0700 | [diff] [blame] | 1050 | debugOverdraw(true, false); |
Romain Guy | 2b7028e | 2012-09-19 17:25:38 -0700 | [diff] [blame] | 1051 | |
| 1052 | startTiling(previous); |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1055 | if (!fboLayer && layer->getAlpha() < 255) { |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1056 | drawColorRect(rect.left, rect.top, rect.right, rect.bottom, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1057 | layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1058 | // Required below, composeLayerRect() will divide by 255 |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1059 | layer->setAlpha(255); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 1062 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 1063 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 1064 | mCaches.activeTexture(0); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1065 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1066 | // When the layer is stored in an FBO, we can save a bit of fillrate by |
| 1067 | // drawing only the dirty region |
| 1068 | if (fboLayer) { |
| 1069 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1070 | if (layer->getColorFilter()) { |
| 1071 | setupColorFilter(layer->getColorFilter()); |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 1072 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1073 | composeLayerRegion(layer, rect); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1074 | if (layer->getColorFilter()) { |
Romain Guy | 171c592 | 2011-01-06 10:04:23 -0800 | [diff] [blame] | 1075 | resetColorFilter(); |
| 1076 | } |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1077 | } else if (!rect.isEmpty()) { |
| 1078 | dirtyLayer(rect.left, rect.top, rect.right, rect.bottom); |
Digish Pandya | a5ff739 | 2013-11-04 06:30:25 +0530 | [diff] [blame] | 1079 | |
| 1080 | save(0); |
| 1081 | // the layer contains screen buffer content that shouldn't be alpha modulated |
| 1082 | // (and any necessary alpha modulation was handled drawing into the layer) |
| 1083 | mSnapshot->alpha = 1.0f; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1084 | composeLayerRect(layer, rect, true); |
Digish Pandya | a5ff739 | 2013-11-04 06:30:25 +0530 | [diff] [blame] | 1085 | restore(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1086 | } |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 1087 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1088 | dirtyClip(); |
| 1089 | |
Romain Guy | eb99356 | 2010-10-05 18:14:38 -0700 | [diff] [blame] | 1090 | // 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] | 1091 | if (!mCaches.layerCache.put(layer)) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1092 | LAYER_LOGD("Deleting layer"); |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 1093 | Caches::getInstance().resourceCache.decrementRefcount(layer); |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1097 | void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) { |
Romain Guy | 2458939 | 2013-06-19 12:17:01 -0700 | [diff] [blame] | 1098 | float alpha = getLayerAlpha(layer); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1099 | |
| 1100 | setupDraw(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1101 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1102 | setupDrawWithTexture(); |
| 1103 | } else { |
| 1104 | setupDrawWithExternalTexture(); |
| 1105 | } |
| 1106 | setupDrawTextureTransform(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1107 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 1108 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1109 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1110 | setupDrawProgram(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1111 | setupDrawPureColorUniforms(); |
| 1112 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1113 | if (layer->getRenderTarget() == GL_TEXTURE_2D) { |
| 1114 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1115 | } else { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1116 | setupDrawExternalTexture(layer->getTexture()); |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1117 | } |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1118 | if (currentTransform().isPureTranslate() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 1119 | layer->getWidth() == (uint32_t) rect.getWidth() && |
| 1120 | layer->getHeight() == (uint32_t) rect.getHeight()) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1121 | const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); |
| 1122 | const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1123 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1124 | layer->setFilter(GL_NEAREST); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1125 | setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 1126 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1127 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1128 | setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom); |
| 1129 | } |
| 1130 | setupDrawTextureTransformUniforms(layer->getTexTransform()); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1131 | setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]); |
| 1132 | |
| 1133 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1136 | void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1137 | if (!layer->isTextureLayer()) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1138 | const Rect& texCoords = layer->texCoords; |
| 1139 | resetDrawTextureTexCoords(texCoords.left, texCoords.top, |
| 1140 | texCoords.right, texCoords.bottom); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1141 | |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1142 | float x = rect.left; |
| 1143 | float y = rect.top; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1144 | bool simpleTransform = currentTransform().isPureTranslate() && |
Romain Guy | ec19b4a | 2011-07-07 21:05:04 -0700 | [diff] [blame] | 1145 | layer->getWidth() == (uint32_t) rect.getWidth() && |
Romain Guy | b247915 | 2011-07-08 11:57:29 -0700 | [diff] [blame] | 1146 | layer->getHeight() == (uint32_t) rect.getHeight(); |
| 1147 | |
| 1148 | if (simpleTransform) { |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1149 | // When we're swapping, the layer is already in screen coordinates |
| 1150 | if (!swap) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1151 | x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); |
| 1152 | y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1155 | layer->setFilter(GL_NEAREST, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1156 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1157 | layer->setFilter(GL_LINEAR, true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1158 | } |
| 1159 | |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 1160 | float alpha = getLayerAlpha(layer); |
Chris Craik | e83569c | 2013-03-20 16:57:09 -0700 | [diff] [blame] | 1161 | bool blend = layer->isBlend() || alpha < 1.0f; |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1162 | drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(), |
Chris Craik | e83569c | 2013-03-20 16:57:09 -0700 | [diff] [blame] | 1163 | layer->getTexture(), alpha, layer->getMode(), blend, |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1164 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 1165 | GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1166 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1167 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 1168 | } else { |
| 1169 | resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f); |
| 1170 | drawTextureLayer(layer, rect); |
| 1171 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
| 1172 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1175 | /** |
| 1176 | * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated |
| 1177 | * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw |
| 1178 | * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used |
| 1179 | * by saveLayer's restore |
| 1180 | */ |
| 1181 | #define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \ |
| 1182 | DRAW_COMMAND; \ |
| 1183 | if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \ |
| 1184 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \ |
| 1185 | DRAW_COMMAND; \ |
| 1186 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \ |
| 1187 | } \ |
| 1188 | } |
| 1189 | |
| 1190 | #define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND) |
| 1191 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1192 | void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1193 | if (layer->region.isRect()) { |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 1194 | layer->setRegionAsRect(); |
| 1195 | |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1196 | DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect)); |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 1197 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1198 | layer->region.clear(); |
| 1199 | return; |
| 1200 | } |
| 1201 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 1202 | if (CC_LIKELY(!layer->region.isEmpty())) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1203 | size_t count; |
Chris Craik | 6c5b9be | 2013-02-27 14:03:19 -0800 | [diff] [blame] | 1204 | const android::Rect* rects; |
| 1205 | Region safeRegion; |
| 1206 | if (CC_LIKELY(hasRectToRectTransform())) { |
| 1207 | rects = layer->region.getArray(&count); |
| 1208 | } else { |
| 1209 | safeRegion = Region::createTJunctionFreeRegion(layer->region); |
| 1210 | rects = safeRegion.getArray(&count); |
| 1211 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1212 | |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 1213 | const float alpha = getLayerAlpha(layer); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1214 | const float texX = 1.0f / float(layer->getWidth()); |
| 1215 | const float texY = 1.0f / float(layer->getHeight()); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1216 | const float height = rect.getHeight(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1217 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1218 | setupDraw(); |
| 1219 | |
| 1220 | // We must get (and therefore bind) the region mesh buffer |
| 1221 | // after we setup drawing in case we need to mess with the |
| 1222 | // stencil buffer in setupDraw() |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1223 | TextureVertex* mesh = mCaches.getRegionMesh(); |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 1224 | uint32_t numQuads = 0; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1225 | |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 1226 | setupDrawWithTexture(); |
| 1227 | setupDrawColor(alpha, alpha, alpha, alpha); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 1228 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1229 | setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false); |
Romain Guy | 7230a74 | 2011-01-10 22:26:16 -0800 | [diff] [blame] | 1230 | setupDrawProgram(); |
| 1231 | setupDrawDirtyRegionsDisabled(); |
| 1232 | setupDrawPureColorUniforms(); |
Romain Guy | ada830f | 2011-01-13 12:13:20 -0800 | [diff] [blame] | 1233 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1234 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1235 | if (currentTransform().isPureTranslate()) { |
| 1236 | const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f); |
| 1237 | const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1238 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1239 | layer->setFilter(GL_NEAREST); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1240 | setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true); |
| 1241 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 1242 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 1243 | setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom); |
| 1244 | } |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1245 | setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1246 | |
| 1247 | for (size_t i = 0; i < count; i++) { |
| 1248 | const android::Rect* r = &rects[i]; |
| 1249 | |
| 1250 | const float u1 = r->left * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1251 | const float v1 = (height - r->top) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1252 | const float u2 = r->right * texX; |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1253 | const float v2 = (height - r->bottom) * texY; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1254 | |
| 1255 | // TODO: Reject quads outside of the clip |
| 1256 | TextureVertex::set(mesh++, r->left, r->top, u1, v1); |
| 1257 | TextureVertex::set(mesh++, r->right, r->top, u2, v1); |
| 1258 | TextureVertex::set(mesh++, r->left, r->bottom, u1, v2); |
| 1259 | TextureVertex::set(mesh++, r->right, r->bottom, u2, v2); |
| 1260 | |
| 1261 | numQuads++; |
| 1262 | |
Romain Guy | 31e08e9 | 2013-06-18 15:53:53 -0700 | [diff] [blame] | 1263 | if (numQuads >= gMaxNumberOfQuads) { |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1264 | DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6, |
| 1265 | GL_UNSIGNED_SHORT, NULL)); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1266 | numQuads = 0; |
| 1267 | mesh = mCaches.getRegionMesh(); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | if (numQuads > 0) { |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 1272 | DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6, |
| 1273 | GL_UNSIGNED_SHORT, NULL)); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1276 | #if DEBUG_LAYERS_AS_REGIONS |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1277 | drawRegionRects(layer->region); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1278 | #endif |
| 1279 | |
| 1280 | layer->region.clear(); |
| 1281 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 1284 | void OpenGLRenderer::drawRegionRects(const Region& region) { |
| 1285 | #if DEBUG_LAYERS_AS_REGIONS |
| 1286 | size_t count; |
| 1287 | const android::Rect* rects = region.getArray(&count); |
| 1288 | |
| 1289 | uint32_t colors[] = { |
| 1290 | 0x7fff0000, 0x7f00ff00, |
| 1291 | 0x7f0000ff, 0x7fff00ff, |
| 1292 | }; |
| 1293 | |
| 1294 | int offset = 0; |
| 1295 | int32_t top = rects[0].top; |
| 1296 | |
| 1297 | for (size_t i = 0; i < count; i++) { |
| 1298 | if (top != rects[i].top) { |
| 1299 | offset ^= 0x2; |
| 1300 | top = rects[i].top; |
| 1301 | } |
| 1302 | |
| 1303 | Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); |
| 1304 | drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)], |
| 1305 | SkXfermode::kSrcOver_Mode); |
| 1306 | } |
| 1307 | #endif |
| 1308 | } |
| 1309 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1310 | void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color, |
| 1311 | SkXfermode::Mode mode, bool dirty) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1312 | Vector<float> rects; |
| 1313 | |
| 1314 | SkRegion::Iterator it(region); |
| 1315 | while (!it.done()) { |
| 1316 | const SkIRect& r = it.rect(); |
| 1317 | rects.push(r.fLeft); |
| 1318 | rects.push(r.fTop); |
| 1319 | rects.push(r.fRight); |
| 1320 | rects.push(r.fBottom); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1321 | it.next(); |
| 1322 | } |
| 1323 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1324 | drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1325 | } |
| 1326 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1327 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1328 | const float right, const float bottom, const mat4 transform) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1329 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1330 | Rect bounds(left, top, right, bottom); |
| 1331 | transform.mapRect(bounds); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1332 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1333 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | void OpenGLRenderer::dirtyLayer(const float left, const float top, |
| 1337 | const float right, const float bottom) { |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1338 | if (hasLayer()) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1339 | Rect bounds(left, top, right, bottom); |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 1340 | dirtyLayerUnchecked(bounds, getRegion()); |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1341 | } |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) { |
Romain Guy | 1bd1bad | 2011-01-14 20:07:20 -0800 | [diff] [blame] | 1345 | if (bounds.intersect(*mSnapshot->clipRect)) { |
| 1346 | bounds.snapToPixelBoundaries(); |
| 1347 | android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom); |
| 1348 | if (!dirty.isEmpty()) { |
| 1349 | region->orSelf(dirty); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1354 | void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) { |
| 1355 | GLsizei elementsCount = quadsCount * 6; |
| 1356 | while (elementsCount > 0) { |
| 1357 | GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6); |
| 1358 | |
| 1359 | setupDrawIndexedVertices(&mesh[0].position[0]); |
| 1360 | glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL); |
| 1361 | |
| 1362 | elementsCount -= drawCount; |
| 1363 | // Though there are 4 vertices in a quad, we use 6 indices per |
| 1364 | // quad to draw with GL_TRIANGLES |
| 1365 | mesh += (drawCount / 6) * 4; |
| 1366 | } |
| 1367 | } |
| 1368 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1369 | void OpenGLRenderer::clearLayerRegions() { |
| 1370 | const size_t count = mLayers.size(); |
| 1371 | if (count == 0) return; |
| 1372 | |
| 1373 | if (!mSnapshot->isIgnored()) { |
| 1374 | // Doing several glScissor/glClear here can negatively impact |
| 1375 | // GPUs with a tiler architecture, instead we draw quads with |
| 1376 | // the Clear blending mode |
| 1377 | |
| 1378 | // The list contains bounds that have already been clipped |
| 1379 | // against their initial clip rect, and the current clip |
| 1380 | // is likely different so we need to disable clipping here |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1381 | bool scissorChanged = mCaches.disableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1382 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1383 | Vertex mesh[count * 4]; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1384 | Vertex* vertex = mesh; |
| 1385 | |
| 1386 | for (uint32_t i = 0; i < count; i++) { |
| 1387 | Rect* bounds = mLayers.itemAt(i); |
| 1388 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1389 | Vertex::set(vertex++, bounds->left, bounds->top); |
| 1390 | Vertex::set(vertex++, bounds->right, bounds->top); |
| 1391 | Vertex::set(vertex++, bounds->left, bounds->bottom); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1392 | Vertex::set(vertex++, bounds->right, bounds->bottom); |
| 1393 | |
| 1394 | delete bounds; |
| 1395 | } |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 1396 | // We must clear the list of dirty rects before we |
| 1397 | // call setupDraw() to prevent stencil setup to do |
| 1398 | // the same thing again |
| 1399 | mLayers.clear(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1400 | |
| 1401 | setupDraw(false); |
| 1402 | setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f); |
| 1403 | setupDrawBlending(true, SkXfermode::kClear_Mode); |
| 1404 | setupDrawProgram(); |
| 1405 | setupDrawPureColorUniforms(); |
| 1406 | setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true); |
| 1407 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 1408 | drawIndexedQuads(&mesh[0], count); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1409 | |
| 1410 | if (scissorChanged) mCaches.enableScissor(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1411 | } else { |
| 1412 | for (uint32_t i = 0; i < count; i++) { |
| 1413 | delete mLayers.itemAt(i); |
| 1414 | } |
Romain Guy | e67307c | 2013-02-11 18:01:20 -0800 | [diff] [blame] | 1415 | mLayers.clear(); |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1416 | } |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 1419 | /////////////////////////////////////////////////////////////////////////////// |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1420 | // State Deferral |
| 1421 | /////////////////////////////////////////////////////////////////////////////// |
| 1422 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1423 | bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1424 | const Rect& currentClip = *(mSnapshot->clipRect); |
| 1425 | const mat4& currentMatrix = *(mSnapshot->transform); |
| 1426 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1427 | if (stateDeferFlags & kStateDeferFlag_Draw) { |
| 1428 | // state has bounds initialized in local coordinates |
| 1429 | if (!state.mBounds.isEmpty()) { |
| 1430 | currentMatrix.mapRect(state.mBounds); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1431 | Rect clippedBounds(state.mBounds); |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1432 | // NOTE: if we ever want to use this clipping info to drive whether the scissor |
| 1433 | // is used, it should more closely duplicate the quickReject logic (in how it uses |
| 1434 | // snapToPixelBoundaries) |
| 1435 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1436 | if(!clippedBounds.intersect(currentClip)) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1437 | // quick rejected |
| 1438 | return true; |
| 1439 | } |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1440 | |
Chris Craik | a02c4ed | 2013-06-14 13:43:58 -0700 | [diff] [blame] | 1441 | state.mClipSideFlags = kClipSide_None; |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1442 | if (!currentClip.contains(state.mBounds)) { |
| 1443 | int& flags = state.mClipSideFlags; |
| 1444 | // op partially clipped, so record which sides are clipped for clip-aware merging |
| 1445 | if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left; |
| 1446 | if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top; |
| 1447 | if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right; |
| 1448 | if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom; |
| 1449 | } |
| 1450 | state.mBounds.set(clippedBounds); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1451 | } else { |
Chris Craik | d72b73c | 2013-06-17 13:52:06 -0700 | [diff] [blame] | 1452 | // Empty bounds implies size unknown. Label op as conservatively clipped to disable |
| 1453 | // overdraw avoidance (since we don't know what it overlaps) |
| 1454 | state.mClipSideFlags = kClipSide_ConservativeFull; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1455 | state.mBounds.set(currentClip); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1456 | } |
| 1457 | } |
| 1458 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1459 | state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip); |
| 1460 | if (state.mClipValid) { |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1461 | state.mClip.set(currentClip); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1462 | } |
| 1463 | |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 1464 | // Transform, drawModifiers, and alpha always deferred, since they are used by state operations |
| 1465 | // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything) |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1466 | state.mMatrix.load(currentMatrix); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 1467 | state.mDrawModifiers = mDrawModifiers; |
| 1468 | state.mAlpha = mSnapshot->alpha; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1469 | return false; |
| 1470 | } |
| 1471 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1472 | void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1473 | currentTransform().load(state.mMatrix); |
Chris Craik | 7273daa | 2013-03-28 11:25:24 -0700 | [diff] [blame] | 1474 | mDrawModifiers = state.mDrawModifiers; |
| 1475 | mSnapshot->alpha = state.mAlpha; |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1476 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1477 | if (state.mClipValid && !skipClipRestore) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1478 | mSnapshot->setClip(state.mClip.left, state.mClip.top, |
| 1479 | state.mClip.right, state.mClip.bottom); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 1480 | dirtyClip(); |
| 1481 | } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1484 | /** |
| 1485 | * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done |
| 1486 | * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at |
| 1487 | * least one op is clipped), or disabled entirely (because no merged op is clipped) |
| 1488 | * |
| 1489 | * This method should be called when restoreDisplayState() won't be restoring the clip |
| 1490 | */ |
| 1491 | void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) { |
| 1492 | if (clipRect != NULL) { |
| 1493 | mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom); |
| 1494 | } else { |
| 1495 | mSnapshot->setClip(0, 0, mWidth, mHeight); |
| 1496 | } |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1497 | dirtyClip(); |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 1498 | mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1501 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1502 | // Transforms |
| 1503 | /////////////////////////////////////////////////////////////////////////////// |
| 1504 | |
| 1505 | void OpenGLRenderer::translate(float dx, float dy) { |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 1506 | currentTransform().translate(dx, dy); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | void OpenGLRenderer::rotate(float degrees) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1510 | currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | void OpenGLRenderer::scale(float sx, float sy) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1514 | currentTransform().scale(sx, sy, 1.0f); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 1517 | void OpenGLRenderer::skew(float sx, float sy) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1518 | currentTransform().skew(sx, sy); |
Romain Guy | 807daf7 | 2011-01-18 11:19:19 -0800 | [diff] [blame] | 1519 | } |
| 1520 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1521 | void OpenGLRenderer::setMatrix(SkMatrix* matrix) { |
Romain Guy | e707859 | 2011-10-28 14:32:20 -0700 | [diff] [blame] | 1522 | if (matrix) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1523 | currentTransform().load(*matrix); |
Romain Guy | e707859 | 2011-10-28 14:32:20 -0700 | [diff] [blame] | 1524 | } else { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1525 | currentTransform().loadIdentity(); |
Romain Guy | e707859 | 2011-10-28 14:32:20 -0700 | [diff] [blame] | 1526 | } |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Chris Craik | b98a016 | 2013-02-21 11:30:22 -0800 | [diff] [blame] | 1529 | bool OpenGLRenderer::hasRectToRectTransform() { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1530 | return CC_LIKELY(currentTransform().rectToRect()); |
Chris Craik | b98a016 | 2013-02-21 11:30:22 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1533 | void OpenGLRenderer::getMatrix(SkMatrix* matrix) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1534 | currentTransform().copyTo(*matrix); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | void OpenGLRenderer::concatMatrix(SkMatrix* matrix) { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1538 | SkMatrix transform; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1539 | currentTransform().copyTo(transform); |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1540 | transform.preConcat(*matrix); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1541 | currentTransform().load(transform); |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /////////////////////////////////////////////////////////////////////////////// |
| 1545 | // Clipping |
| 1546 | /////////////////////////////////////////////////////////////////////////////// |
| 1547 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1548 | void OpenGLRenderer::setScissorFromClip() { |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1549 | Rect clip(*mSnapshot->clipRect); |
| 1550 | clip.snapToPixelBoundaries(); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 1551 | |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1552 | if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom, |
| 1553 | clip.getWidth(), clip.getHeight())) { |
| 1554 | mDirtyClip = false; |
| 1555 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1558 | void OpenGLRenderer::ensureStencilBuffer() { |
| 1559 | // Thanks to the mismatch between EGL and OpenGL ES FBO we |
| 1560 | // cannot attach a stencil buffer to fbo0 dynamically. Let's |
| 1561 | // just hope we have one when hasLayer() returns false. |
| 1562 | if (hasLayer()) { |
| 1563 | attachStencilBufferToLayer(mSnapshot->layer); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) { |
| 1568 | // The layer's FBO is already bound when we reach this stage |
| 1569 | if (!layer->getStencilRenderBuffer()) { |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 1570 | // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer |
| 1571 | // is attached after we initiated tiling. We must turn it off, |
| 1572 | // attach the new render buffer then turn tiling back on |
| 1573 | endTiling(); |
| 1574 | |
Romain Guy | 8d4aeb7 | 2013-02-12 16:08:55 -0800 | [diff] [blame] | 1575 | RenderBuffer* buffer = mCaches.renderBufferCache.get( |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 1576 | Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight()); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1577 | layer->setStencilRenderBuffer(buffer); |
Romain Guy | c3fedaf | 2013-01-29 17:26:25 -0800 | [diff] [blame] | 1578 | |
Romain Guy | f735c8e | 2013-01-31 17:45:55 -0800 | [diff] [blame] | 1579 | startTiling(layer->clipRect, layer->layer.getHeight()); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | void OpenGLRenderer::setStencilFromClip() { |
| 1584 | if (!mCaches.debugOverdraw) { |
| 1585 | if (!mSnapshot->clipRegion->isEmpty()) { |
| 1586 | // NOTE: The order here is important, we must set dirtyClip to false |
| 1587 | // before any draw call to avoid calling back into this method |
| 1588 | mDirtyClip = false; |
| 1589 | |
| 1590 | ensureStencilBuffer(); |
| 1591 | |
| 1592 | mCaches.stencil.enableWrite(); |
| 1593 | |
| 1594 | // Clear the stencil but first make sure we restrict drawing |
| 1595 | // to the region's bounds |
| 1596 | bool resetScissor = mCaches.enableScissor(); |
| 1597 | if (resetScissor) { |
| 1598 | // The scissor was not set so we now need to update it |
| 1599 | setScissorFromClip(); |
| 1600 | } |
| 1601 | mCaches.stencil.clear(); |
| 1602 | if (resetScissor) mCaches.disableScissor(); |
| 1603 | |
| 1604 | // NOTE: We could use the region contour path to generate a smaller mesh |
| 1605 | // Since we are using the stencil we could use the red book path |
| 1606 | // drawing technique. It might increase bandwidth usage though. |
| 1607 | |
| 1608 | // The last parameter is important: we are not drawing in the color buffer |
| 1609 | // so we don't want to dirty the current layer, if any |
| 1610 | drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false); |
| 1611 | |
| 1612 | mCaches.stencil.enableTest(); |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1613 | |
| 1614 | // Draw the region used to generate the stencil if the appropriate debug |
| 1615 | // mode is enabled |
| 1616 | if (mCaches.debugStencilClip == Caches::kStencilShowRegion) { |
| 1617 | drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode); |
| 1618 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1619 | } else { |
| 1620 | mCaches.stencil.disable(); |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 1625 | const Rect& OpenGLRenderer::getClipBounds() { |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 1626 | return mSnapshot->getLocalClip(); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1629 | bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom, |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1630 | bool snapOut, bool* clipRequired) { |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1631 | if (mSnapshot->isIgnored() || bottom <= top || right <= left) { |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1632 | return true; |
| 1633 | } |
| 1634 | |
| 1635 | Rect r(left, top, right, bottom); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1636 | currentTransform().mapRect(r); |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 1637 | r.snapGeometryToPixelBoundaries(snapOut); |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1638 | |
| 1639 | Rect clipRect(*mSnapshot->clipRect); |
| 1640 | clipRect.snapToPixelBoundaries(); |
| 1641 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1642 | if (!clipRect.intersects(r)) return true; |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1643 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1644 | if (clipRequired) *clipRequired = !clipRect.contains(r); |
| 1645 | return false; |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 1646 | } |
| 1647 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 1648 | bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom, |
| 1649 | SkPaint* paint) { |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1650 | // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out |
| 1651 | // the final mapped rect to ensure correct clipping behavior for the ramp. |
| 1652 | bool snapOut = paint->isAntiAlias(); |
| 1653 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1654 | if (paint->getStyle() != SkPaint::kFill_Style) { |
| 1655 | float outset = paint->getStrokeWidth() * 0.5f; |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1656 | return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1657 | } else { |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1658 | return quickReject(left, top, right, bottom, snapOut); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1662 | bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) { |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1663 | bool clipRequired = false; |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 1664 | if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) { |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 1665 | return true; |
| 1666 | } |
| 1667 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1668 | if (!isDeferred()) { |
| 1669 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
Romain Guy | 586cae3 | 2012-07-13 15:28:31 -0700 | [diff] [blame] | 1670 | } |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 1671 | return false; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1674 | void OpenGLRenderer::debugClip() { |
| 1675 | #if DEBUG_CLIP_REGIONS |
| 1676 | if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) { |
| 1677 | drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode); |
| 1678 | } |
| 1679 | #endif |
| 1680 | } |
| 1681 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 1682 | bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1683 | if (CC_LIKELY(currentTransform().rectToRect())) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1684 | bool clipped = mSnapshot->clip(left, top, right, bottom, op); |
| 1685 | if (clipped) { |
| 1686 | dirtyClip(); |
| 1687 | } |
| 1688 | return !mSnapshot->clipRect->isEmpty(); |
| 1689 | } |
| 1690 | |
| 1691 | SkPath path; |
| 1692 | path.addRect(left, top, right, bottom); |
| 1693 | |
Romain Guy | b7b93e0 | 2013-08-01 15:29:25 -0700 | [diff] [blame] | 1694 | return OpenGLRenderer::clipPath(&path, op); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) { |
| 1698 | SkMatrix transform; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1699 | currentTransform().copyTo(transform); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1700 | |
| 1701 | SkPath transformed; |
| 1702 | path->transform(transform, &transformed); |
| 1703 | |
| 1704 | SkRegion clip; |
Romain Guy | b7b93e0 | 2013-08-01 15:29:25 -0700 | [diff] [blame] | 1705 | if (!mSnapshot->previous->clipRegion->isEmpty()) { |
| 1706 | clip.setRegion(*mSnapshot->previous->clipRegion); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1707 | } else { |
Romain Guy | b7b93e0 | 2013-08-01 15:29:25 -0700 | [diff] [blame] | 1708 | if (mSnapshot->previous == mFirstSnapshot) { |
| 1709 | clip.setRect(0, 0, mWidth, mHeight); |
| 1710 | } else { |
| 1711 | Rect* bounds = mSnapshot->previous->clipRect; |
| 1712 | clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom); |
| 1713 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | SkRegion region; |
| 1717 | region.setPath(transformed, clip); |
| 1718 | |
| 1719 | bool clipped = mSnapshot->clipRegionTransformed(region, op); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 1720 | if (clipped) { |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 1721 | dirtyClip(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 1722 | } |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 1723 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1724 | } |
| 1725 | |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 1726 | bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) { |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1727 | bool clipped = mSnapshot->clipRegionTransformed(*region, op); |
| 1728 | if (clipped) { |
| 1729 | dirtyClip(); |
| 1730 | } |
| 1731 | return !mSnapshot->clipRect->isEmpty(); |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 1732 | } |
| 1733 | |
Chet Haase | a23eed8 | 2012-04-12 15:19:04 -0700 | [diff] [blame] | 1734 | Rect* OpenGLRenderer::getClipRect() { |
| 1735 | return mSnapshot->clipRect; |
| 1736 | } |
| 1737 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 1738 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1739 | // Drawing commands |
| 1740 | /////////////////////////////////////////////////////////////////////////////// |
| 1741 | |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1742 | void OpenGLRenderer::setupDraw(bool clear) { |
Romain Guy | 8a4ac61 | 2012-07-17 17:32:48 -0700 | [diff] [blame] | 1743 | // TODO: It would be best if we could do this before quickReject() |
| 1744 | // changes the scissor test state |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1745 | if (clear) clearLayerRegions(); |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1746 | // Make sure setScissor & setStencil happen at the beginning of |
| 1747 | // this method |
Chris Craik | b98a016 | 2013-02-21 11:30:22 -0800 | [diff] [blame] | 1748 | if (mDirtyClip) { |
| 1749 | if (mCaches.scissorEnabled) { |
| 1750 | setScissorFromClip(); |
| 1751 | } |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 1752 | setStencilFromClip(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1753 | } |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1754 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1755 | mDescription.reset(); |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1756 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1757 | mSetShaderColor = false; |
| 1758 | mColorSet = false; |
| 1759 | mColorA = mColorR = mColorG = mColorB = 0.0f; |
| 1760 | mTextureUnit = 0; |
| 1761 | mTrackDirtyRegions = true; |
Romain Guy | 3ff0bfd | 2013-02-25 14:15:37 -0800 | [diff] [blame] | 1762 | |
| 1763 | // Enable debug highlight when what we're about to draw is tested against |
| 1764 | // the stencil buffer and if stencil highlight debugging is on |
| 1765 | mDescription.hasDebugHighlight = !mCaches.debugOverdraw && |
| 1766 | mCaches.debugStencilClip == Caches::kStencilShowHighlight && |
| 1767 | mCaches.stencil.isTestEnabled(); |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 1768 | |
| 1769 | mDescription.emulateStencil = mCountOverdraw; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) { |
| 1773 | mDescription.hasTexture = true; |
| 1774 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1775 | } |
| 1776 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1777 | void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) { |
| 1778 | mDescription.hasTexture = true; |
| 1779 | mDescription.hasColors = true; |
| 1780 | mDescription.hasAlpha8Texture = isAlpha8; |
| 1781 | } |
| 1782 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1783 | void OpenGLRenderer::setupDrawWithExternalTexture() { |
| 1784 | mDescription.hasExternalTexture = true; |
| 1785 | } |
| 1786 | |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1787 | void OpenGLRenderer::setupDrawNoTexture() { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1788 | mCaches.disableTexCoordsVertexArray(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1789 | } |
| 1790 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 1791 | void OpenGLRenderer::setupDrawAA() { |
Chet Haase | 99585ad | 2011-05-02 15:00:16 -0700 | [diff] [blame] | 1792 | mDescription.isAA = true; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1795 | void OpenGLRenderer::setupDrawColor(int color, int alpha) { |
| 1796 | mColorA = alpha / 255.0f; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1797 | mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; |
| 1798 | mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; |
| 1799 | mColorB = mColorA * ((color ) & 0xFF) / 255.0f; |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1800 | mColorSet = true; |
| 1801 | mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA); |
| 1802 | } |
| 1803 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1804 | void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) { |
| 1805 | mColorA = alpha / 255.0f; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 1806 | mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f; |
| 1807 | mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f; |
| 1808 | mColorB = mColorA * ((color ) & 0xFF) / 255.0f; |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1809 | mColorSet = true; |
| 1810 | mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA); |
| 1811 | } |
| 1812 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1813 | void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) { |
| 1814 | mCaches.fontRenderer->describe(mDescription, paint); |
| 1815 | } |
| 1816 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1817 | void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) { |
| 1818 | mColorA = a; |
| 1819 | mColorR = r; |
| 1820 | mColorG = g; |
| 1821 | mColorB = b; |
| 1822 | mColorSet = true; |
| 1823 | mSetShaderColor = mDescription.setColor(r, g, b, a); |
| 1824 | } |
| 1825 | |
| 1826 | void OpenGLRenderer::setupDrawShader() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1827 | if (mDrawModifiers.mShader) { |
| 1828 | mDrawModifiers.mShader->describe(mDescription, mExtensions); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | void OpenGLRenderer::setupDrawColorFilter() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1833 | if (mDrawModifiers.mColorFilter) { |
| 1834 | mDrawModifiers.mColorFilter->describe(mDescription, mExtensions); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1838 | void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) { |
| 1839 | if (mColorSet && mode == SkXfermode::kClear_Mode) { |
| 1840 | mColorA = 1.0f; |
| 1841 | mColorR = mColorG = mColorB = 0.0f; |
Romain Guy | 54be1cd | 2011-06-13 19:04:27 -0700 | [diff] [blame] | 1842 | mSetShaderColor = mDescription.modulate = true; |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1846 | void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1847 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1848 | // argb=1,0,0,0 |
| 1849 | accountForClear(mode); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1850 | bool blend = (mColorSet && mColorA < 1.0f) || |
| 1851 | (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()); |
| 1852 | chooseBlending(blend, mode, mDescription, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) { |
Romain Guy | f09ef51 | 2011-05-27 11:43:46 -0700 | [diff] [blame] | 1856 | // When the blending mode is kClear_Mode, we need to use a modulate color |
| 1857 | // argb=1,0,0,0 |
| 1858 | accountForClear(mode); |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1859 | blend |= (mColorSet && mColorA < 1.0f) || |
| 1860 | (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) || |
| 1861 | (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend()); |
| 1862 | chooseBlending(blend, mode, mDescription, swapSrcDst); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | void OpenGLRenderer::setupDrawProgram() { |
| 1866 | useProgram(mCaches.programCache.get(mDescription)); |
| 1867 | } |
| 1868 | |
| 1869 | void OpenGLRenderer::setupDrawDirtyRegionsDisabled() { |
| 1870 | mTrackDirtyRegions = false; |
| 1871 | } |
| 1872 | |
| 1873 | void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom, |
| 1874 | bool ignoreTransform) { |
| 1875 | mModelView.loadTranslate(left, top, 0.0f); |
| 1876 | if (!ignoreTransform) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1877 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform()); |
| 1878 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform()); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1879 | } else { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 1880 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity()); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1881 | if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom); |
| 1882 | } |
| 1883 | } |
| 1884 | |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 1885 | void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1886 | mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1889 | void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom, |
| 1890 | bool ignoreTransform, bool ignoreModelView) { |
| 1891 | if (!ignoreModelView) { |
| 1892 | mModelView.loadTranslate(left, top, 0.0f); |
| 1893 | mModelView.scale(right - left, bottom - top, 1.0f); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1894 | } else { |
| 1895 | mModelView.loadIdentity(); |
| 1896 | } |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1897 | bool dirty = right - left > 0.0f && bottom - top > 0.0f; |
| 1898 | if (!ignoreTransform) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1899 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform()); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1900 | if (mTrackDirtyRegions && dirty) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1901 | dirtyLayer(left, top, right, bottom, currentTransform()); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1902 | } |
| 1903 | } else { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 1904 | mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity()); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1905 | if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom); |
| 1906 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | void OpenGLRenderer::setupDrawColorUniforms() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1910 | if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1911 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1915 | void OpenGLRenderer::setupDrawPureColorUniforms() { |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1916 | if (mSetShaderColor) { |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 1917 | mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA); |
Romain Guy | 5536841 | 2010-12-14 10:59:41 -0800 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1921 | void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1922 | if (mDrawModifiers.mShader) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1923 | if (ignoreTransform) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 1924 | mModelView.loadInverse(currentTransform()); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1925 | } |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1926 | mDrawModifiers.mShader->setupProgram(mCaches.currentProgram, |
| 1927 | mModelView, *mSnapshot, &mTextureUnit); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1931 | void OpenGLRenderer::setupDrawShaderIdentityUniforms() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1932 | if (mDrawModifiers.mShader) { |
| 1933 | mDrawModifiers.mShader->setupProgram(mCaches.currentProgram, |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 1934 | mat4::identity(), *mSnapshot, &mTextureUnit); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 1935 | } |
| 1936 | } |
| 1937 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1938 | void OpenGLRenderer::setupDrawColorFilterUniforms() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 1939 | if (mDrawModifiers.mColorFilter) { |
| 1940 | mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
Romain Guy | 4121063 | 2012-07-16 17:04:24 -0700 | [diff] [blame] | 1944 | void OpenGLRenderer::setupDrawTextGammaUniforms() { |
| 1945 | mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram); |
| 1946 | } |
| 1947 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1948 | void OpenGLRenderer::setupDrawSimpleMesh() { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1949 | bool force = mCaches.bindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1950 | mCaches.bindPositionVertexPointer(force, 0); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1951 | mCaches.unbindIndicesBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | void OpenGLRenderer::setupDrawTexture(GLuint texture) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 1955 | if (texture) bindTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1956 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1957 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1960 | void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) { |
| 1961 | bindExternalTexture(texture); |
Romain Guy | 2d4fd36 | 2011-12-13 22:00:19 -0800 | [diff] [blame] | 1962 | mTextureUnit++; |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1963 | mCaches.enableTexCoordsVertexArray(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
Romain Guy | 8f0095c | 2011-05-02 17:24:22 -0700 | [diff] [blame] | 1966 | void OpenGLRenderer::setupDrawTextureTransform() { |
| 1967 | mDescription.hasTextureTransform = true; |
| 1968 | } |
| 1969 | |
| 1970 | void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 1971 | glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1, |
| 1972 | GL_FALSE, &transform.data[0]); |
| 1973 | } |
| 1974 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1975 | void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1976 | bool force = false; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 1977 | if (!vertices || vbo) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1978 | force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1979 | } else { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 1980 | force = mCaches.unbindMeshBuffer(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 1981 | } |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 1982 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1983 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1984 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 1985 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | mCaches.unbindIndicesBuffer(); |
| 1989 | } |
| 1990 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 1991 | void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) { |
| 1992 | bool force = mCaches.unbindMeshBuffer(); |
| 1993 | GLsizei stride = sizeof(ColorTextureVertex); |
| 1994 | |
| 1995 | mCaches.bindPositionVertexPointer(force, vertices, stride); |
| 1996 | if (mCaches.currentProgram->texCoords >= 0) { |
| 1997 | mCaches.bindTexCoordsVertexPointer(force, texCoords, stride); |
| 1998 | } |
| 1999 | int slot = mCaches.currentProgram->getAttrib("colors"); |
| 2000 | if (slot >= 0) { |
| 2001 | glEnableVertexAttribArray(slot); |
| 2002 | glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors); |
| 2003 | } |
| 2004 | |
| 2005 | mCaches.unbindIndicesBuffer(); |
| 2006 | } |
| 2007 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2008 | void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) { |
| 2009 | bool force = false; |
| 2010 | // If vbo is != 0 we want to treat the vertices parameter as an offset inside |
| 2011 | // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to |
| 2012 | // use the default VBO found in Caches |
| 2013 | if (!vertices || vbo) { |
| 2014 | force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo); |
| 2015 | } else { |
| 2016 | force = mCaches.unbindMeshBuffer(); |
| 2017 | } |
| 2018 | mCaches.bindIndicesBuffer(); |
| 2019 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2020 | mCaches.bindPositionVertexPointer(force, vertices); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 2021 | if (mCaches.currentProgram->texCoords >= 0) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2022 | mCaches.bindTexCoordsVertexPointer(force, texCoords); |
Romain Guy | 8d0d478 | 2010-12-14 20:13:35 -0800 | [diff] [blame] | 2023 | } |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2024 | } |
| 2025 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 2026 | void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) { |
Romain Guy | f3a910b4 | 2011-12-12 20:35:21 -0800 | [diff] [blame] | 2027 | bool force = mCaches.unbindMeshBuffer(); |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 2028 | mCaches.bindIndicesBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2029 | mCaches.bindPositionVertexPointer(force, vertices, gVertexStride); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2032 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 2033 | // Drawing |
| 2034 | /////////////////////////////////////////////////////////////////////////////// |
| 2035 | |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 2036 | status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty, |
| 2037 | int32_t replayFlags) { |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 2038 | status_t status; |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 2039 | // All the usual checks and setup operations (quickReject, setupDraw, etc.) |
| 2040 | // will be performed by the display list itself |
Romain Guy | 04c9d8c | 2011-08-25 14:01:48 -0700 | [diff] [blame] | 2041 | if (displayList && displayList->isRenderable()) { |
Chris Craik | d90144d | 2013-03-19 15:03:48 -0700 | [diff] [blame] | 2042 | if (CC_UNLIKELY(mCaches.drawDeferDisabled)) { |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 2043 | status = startFrame(); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 2044 | ReplayStateStruct replayStruct(*this, dirty, replayFlags); |
| 2045 | displayList->replay(replayStruct, 0); |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 2046 | return status | replayStruct.mDrawGlStatus; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2047 | } |
| 2048 | |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 2049 | bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs! |
| 2050 | DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw); |
Chris Craik | ff78583 | 2013-03-08 13:12:16 -0800 | [diff] [blame] | 2051 | DeferStateStruct deferStruct(deferredList, *this, replayFlags); |
| 2052 | displayList->defer(deferStruct, 0); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 2053 | |
| 2054 | flushLayers(); |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 2055 | status = startFrame(); |
Romain Guy | 96885eb | 2013-03-26 15:05:58 -0700 | [diff] [blame] | 2056 | |
Chet Haase | 58d110a | 2013-04-10 07:43:29 -0700 | [diff] [blame] | 2057 | return status | deferredList.flush(*this, dirty); |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 2058 | } |
Romain Guy | 7b5b6ab | 2011-03-14 18:05:08 -0700 | [diff] [blame] | 2059 | |
Romain Guy | 6554943 | 2012-03-26 16:45:05 -0700 | [diff] [blame] | 2060 | return DrawGlInfo::kStatusDone; |
Romain Guy | 0fe478e | 2010-11-08 12:08:41 -0800 | [diff] [blame] | 2061 | } |
| 2062 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2063 | void OpenGLRenderer::outputDisplayList(DisplayList* displayList) { |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 2064 | if (displayList) { |
Romain Guy | 7031ff6 | 2013-02-22 11:48:16 -0800 | [diff] [blame] | 2065 | displayList->output(1); |
Chet Haase | ed30fd8 | 2011-04-22 16:18:45 -0700 | [diff] [blame] | 2066 | } |
| 2067 | } |
| 2068 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2069 | void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) { |
| 2070 | int alpha; |
| 2071 | SkXfermode::Mode mode; |
| 2072 | getAlphaAndMode(paint, &alpha, &mode); |
| 2073 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2074 | int color = paint != NULL ? paint->getColor() : 0; |
| 2075 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2076 | float x = left; |
| 2077 | float y = top; |
| 2078 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2079 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2080 | |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2081 | bool ignoreTransform = false; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2082 | if (currentTransform().isPureTranslate()) { |
| 2083 | x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); |
| 2084 | y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2085 | ignoreTransform = true; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2086 | |
| 2087 | texture->setFilter(GL_NEAREST, true); |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2088 | } else { |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2089 | texture->setFilter(FILTER(paint), true); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2090 | } |
| 2091 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2092 | // No need to check for a UV mapper on the texture object, only ARGB_8888 |
| 2093 | // bitmaps get packed in the atlas |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2094 | drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2095 | paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, |
| 2096 | GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2099 | /** |
| 2100 | * Important note: this method is intended to draw batches of bitmaps and |
| 2101 | * will not set the scissor enable or dirty the current layer, if any. |
| 2102 | * The caller is responsible for properly dirtying the current layer. |
| 2103 | */ |
Romain Guy | 55b6f95 | 2013-06-27 15:27:09 -0700 | [diff] [blame] | 2104 | status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount, |
Chris Craik | 996fe65 | 2013-09-20 17:13:18 -0700 | [diff] [blame] | 2105 | TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2106 | mCaches.activeTexture(0); |
Romain Guy | 55b6f95 | 2013-06-27 15:27:09 -0700 | [diff] [blame] | 2107 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2108 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2109 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2110 | const AutoTexture autoCleanup(texture); |
| 2111 | |
| 2112 | int alpha; |
| 2113 | SkXfermode::Mode mode; |
| 2114 | getAlphaAndMode(paint, &alpha, &mode); |
| 2115 | |
| 2116 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Chris Craik | 996fe65 | 2013-09-20 17:13:18 -0700 | [diff] [blame] | 2117 | texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2118 | |
| 2119 | const float x = (int) floorf(bounds.left + 0.5f); |
| 2120 | const float y = (int) floorf(bounds.top + 0.5f); |
| 2121 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
| 2122 | int color = paint != NULL ? paint->getColor() : 0; |
| 2123 | drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
| 2124 | texture->id, paint != NULL, color, alpha, mode, |
| 2125 | &vertices[0].position[0], &vertices[0].texture[0], |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2126 | GL_TRIANGLES, bitmapCount * 6, true, true, false); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2127 | } else { |
| 2128 | drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(), |
| 2129 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 2130 | &vertices[0].position[0], &vertices[0].texture[0], |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2131 | GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | return DrawGlInfo::kStatusDrew; |
| 2135 | } |
| 2136 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2137 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2138 | const float right = left + bitmap->width(); |
| 2139 | const float bottom = top + bitmap->height(); |
| 2140 | |
| 2141 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2142 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2145 | mCaches.activeTexture(0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2146 | Texture* texture = getTexture(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2147 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2148 | const AutoTexture autoCleanup(texture); |
| 2149 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2150 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
Romain Guy | a168d73 | 2011-03-18 16:50:13 -0700 | [diff] [blame] | 2151 | drawAlphaBitmap(texture, left, top, paint); |
| 2152 | } else { |
| 2153 | drawTextureRect(left, top, right, bottom, texture, paint); |
| 2154 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2155 | |
| 2156 | return DrawGlInfo::kStatusDrew; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2159 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) { |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 2160 | Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height()); |
| 2161 | const mat4 transform(*matrix); |
| 2162 | transform.mapRect(r); |
| 2163 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2164 | if (quickReject(r.left, r.top, r.right, r.bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2165 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2168 | mCaches.activeTexture(0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2169 | Texture* texture = getTexture(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2170 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2171 | const AutoTexture autoCleanup(texture); |
| 2172 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2173 | // This could be done in a cheaper way, all we need is pass the matrix |
| 2174 | // to the vertex shader. The save/restore is a bit overkill. |
| 2175 | save(SkCanvas::kMatrix_SaveFlag); |
| 2176 | concatMatrix(matrix); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2177 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
| 2178 | drawAlphaBitmap(texture, 0.0f, 0.0f, paint); |
| 2179 | } else { |
| 2180 | drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint); |
| 2181 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2182 | restore(); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2183 | |
| 2184 | return DrawGlInfo::kStatusDrew; |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2187 | status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) { |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 2188 | const float right = left + bitmap->width(); |
| 2189 | const float bottom = top + bitmap->height(); |
| 2190 | |
| 2191 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2192 | return DrawGlInfo::kStatusDone; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | mCaches.activeTexture(0); |
| 2196 | Texture* texture = mCaches.textureCache.getTransient(bitmap); |
| 2197 | const AutoTexture autoCleanup(texture); |
| 2198 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2199 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
| 2200 | drawAlphaBitmap(texture, left, top, paint); |
| 2201 | } else { |
| 2202 | drawTextureRect(left, top, right, bottom, texture, paint); |
| 2203 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2204 | |
| 2205 | return DrawGlInfo::kStatusDrew; |
Romain Guy | e651cc6 | 2012-05-14 19:44:40 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2208 | status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight, |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2209 | float* vertices, int* colors, SkPaint* paint) { |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2210 | if (!vertices || mSnapshot->isIgnored()) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2211 | return DrawGlInfo::kStatusDone; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2212 | } |
| 2213 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2214 | // TODO: use quickReject on bounds from vertices |
| 2215 | mCaches.enableScissor(); |
| 2216 | |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2217 | float left = FLT_MAX; |
| 2218 | float top = FLT_MAX; |
| 2219 | float right = FLT_MIN; |
| 2220 | float bottom = FLT_MIN; |
| 2221 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2222 | const uint32_t count = meshWidth * meshHeight * 6; |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2223 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2224 | ColorTextureVertex mesh[count]; |
| 2225 | ColorTextureVertex* vertex = mesh; |
| 2226 | |
| 2227 | bool cleanupColors = false; |
| 2228 | if (!colors) { |
| 2229 | uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1); |
| 2230 | colors = new int[colorsCount]; |
| 2231 | memset(colors, 0xff, colorsCount * sizeof(int)); |
| 2232 | cleanupColors = true; |
| 2233 | } |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2234 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2235 | mCaches.activeTexture(0); |
| 2236 | Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap); |
| 2237 | const UvMapper& mapper(getMapper(texture)); |
| 2238 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2239 | for (int32_t y = 0; y < meshHeight; y++) { |
| 2240 | for (int32_t x = 0; x < meshWidth; x++) { |
| 2241 | uint32_t i = (y * (meshWidth + 1) + x) * 2; |
| 2242 | |
| 2243 | float u1 = float(x) / meshWidth; |
| 2244 | float u2 = float(x + 1) / meshWidth; |
| 2245 | float v1 = float(y) / meshHeight; |
| 2246 | float v2 = float(y + 1) / meshHeight; |
| 2247 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2248 | mapper.map(u1, v1, u2, v2); |
| 2249 | |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2250 | int ax = i + (meshWidth + 1) * 2; |
| 2251 | int ay = ax + 1; |
| 2252 | int bx = i; |
| 2253 | int by = bx + 1; |
| 2254 | int cx = i + 2; |
| 2255 | int cy = cx + 1; |
| 2256 | int dx = i + (meshWidth + 1) * 2 + 2; |
| 2257 | int dy = dx + 1; |
| 2258 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2259 | ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); |
| 2260 | ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]); |
| 2261 | ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2262 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2263 | ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]); |
| 2264 | ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]); |
| 2265 | ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2266 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2267 | left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx]))); |
| 2268 | top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy]))); |
| 2269 | right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx]))); |
| 2270 | bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy]))); |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2271 | } |
| 2272 | } |
| 2273 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2274 | if (quickReject(left, top, right, bottom)) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2275 | if (cleanupColors) delete[] colors; |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2276 | return DrawGlInfo::kStatusDone; |
| 2277 | } |
| 2278 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2279 | if (!texture) { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2280 | texture = mCaches.textureCache.get(bitmap); |
| 2281 | if (!texture) { |
| 2282 | if (cleanupColors) delete[] colors; |
| 2283 | return DrawGlInfo::kStatusDone; |
| 2284 | } |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2285 | } |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2286 | const AutoTexture autoCleanup(texture); |
| 2287 | |
| 2288 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2289 | texture->setFilter(FILTER(paint), true); |
| 2290 | |
| 2291 | int alpha; |
| 2292 | SkXfermode::Mode mode; |
| 2293 | getAlphaAndMode(paint, &alpha, &mode); |
| 2294 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2295 | float a = alpha / 255.0f; |
| 2296 | |
Romain Guy | a92bb4d | 2012-10-16 11:08:44 -0700 | [diff] [blame] | 2297 | if (hasLayer()) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2298 | dirtyLayer(left, top, right, bottom, currentTransform()); |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2299 | } |
Romain Guy | b18d2d0 | 2011-02-10 15:52:54 -0800 | [diff] [blame] | 2300 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2301 | setupDraw(); |
| 2302 | setupDrawWithTextureAndColor(); |
| 2303 | setupDrawColor(a, a, a, a); |
| 2304 | setupDrawColorFilter(); |
| 2305 | setupDrawBlending(true, mode, false); |
| 2306 | setupDrawProgram(); |
| 2307 | setupDrawDirtyRegionsDisabled(); |
| 2308 | setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false); |
| 2309 | setupDrawTexture(texture->id); |
| 2310 | setupDrawPureColorUniforms(); |
| 2311 | setupDrawColorFilterUniforms(); |
| 2312 | setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]); |
| 2313 | |
| 2314 | glDrawArrays(GL_TRIANGLES, 0, count); |
| 2315 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 2316 | int slot = mCaches.currentProgram->getAttrib("colors"); |
| 2317 | if (slot >= 0) { |
| 2318 | glDisableVertexAttribArray(slot); |
| 2319 | } |
| 2320 | |
| 2321 | if (cleanupColors) delete[] colors; |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2322 | |
| 2323 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 5a7b466 | 2011-01-20 19:09:30 -0800 | [diff] [blame] | 2324 | } |
| 2325 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2326 | status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2327 | float srcLeft, float srcTop, float srcRight, float srcBottom, |
| 2328 | float dstLeft, float dstTop, float dstRight, float dstBottom, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2329 | SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2330 | if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2331 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 2334 | mCaches.activeTexture(0); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2335 | Texture* texture = getTexture(bitmap); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2336 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 2337 | const AutoTexture autoCleanup(texture); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2338 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2339 | const float width = texture->width; |
| 2340 | const float height = texture->height; |
| 2341 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2342 | float u1 = fmax(0.0f, srcLeft / width); |
| 2343 | float v1 = fmax(0.0f, srcTop / height); |
| 2344 | float u2 = fmin(1.0f, srcRight / width); |
| 2345 | float v2 = fmin(1.0f, srcBottom / height); |
| 2346 | |
| 2347 | getMapper(texture).map(u1, v1, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2348 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 2349 | mCaches.unbindMeshBuffer(); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2350 | resetDrawTextureTexCoords(u1, v1, u2, v2); |
| 2351 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 2352 | int alpha; |
| 2353 | SkXfermode::Mode mode; |
| 2354 | getAlphaAndMode(paint, &alpha, &mode); |
| 2355 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2356 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2357 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2358 | float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft); |
| 2359 | float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2360 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2361 | bool scaled = scaleX != 1.0f || scaleY != 1.0f; |
| 2362 | // Apply a scale transform on the canvas only when a shader is in use |
| 2363 | // Skia handles the ratio between the dst and src rects as a scale factor |
| 2364 | // when a shader is set |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2365 | bool useScaleTransform = mDrawModifiers.mShader && scaled; |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2366 | bool ignoreTransform = false; |
Romain Guy | b501498 | 2011-07-28 15:39:12 -0700 | [diff] [blame] | 2367 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2368 | if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) { |
| 2369 | float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f); |
| 2370 | float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2371 | |
| 2372 | dstRight = x + (dstRight - dstLeft); |
| 2373 | dstBottom = y + (dstBottom - dstTop); |
| 2374 | |
| 2375 | dstLeft = x; |
| 2376 | dstTop = y; |
| 2377 | |
| 2378 | texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true); |
| 2379 | ignoreTransform = true; |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2380 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 2381 | texture->setFilter(FILTER(paint), true); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | if (CC_UNLIKELY(useScaleTransform)) { |
| 2385 | save(SkCanvas::kMatrix_SaveFlag); |
| 2386 | translate(dstLeft, dstTop); |
| 2387 | scale(scaleX, scaleY); |
| 2388 | |
| 2389 | dstLeft = 0.0f; |
| 2390 | dstTop = 0.0f; |
| 2391 | |
| 2392 | dstRight = srcRight - srcLeft; |
| 2393 | dstBottom = srcBottom - srcTop; |
| 2394 | } |
| 2395 | |
| 2396 | if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) { |
| 2397 | int color = paint ? paint->getColor() : 0; |
| 2398 | drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom, |
| 2399 | texture->id, paint != NULL, color, alpha, mode, |
| 2400 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 2401 | GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform); |
| 2402 | } else { |
| 2403 | drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom, |
| 2404 | texture->id, alpha / 255.0f, mode, texture->blend, |
| 2405 | &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0], |
| 2406 | GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform); |
| 2407 | } |
| 2408 | |
| 2409 | if (CC_UNLIKELY(useScaleTransform)) { |
| 2410 | restore(); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2411 | } |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2412 | |
| 2413 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2414 | |
| 2415 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2418 | status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2419 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2420 | if (quickReject(left, top, right, bottom)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2421 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2422 | } |
| 2423 | |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 2424 | AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2425 | const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(), |
| 2426 | right - left, bottom - top, patch); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 2427 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2428 | return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint); |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2431 | status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry, |
| 2432 | float left, float top, float right, float bottom, SkPaint* paint) { |
Romain Guy | 4c2547f | 2013-06-11 16:19:24 -0700 | [diff] [blame] | 2433 | if (quickReject(left, top, right, bottom)) { |
| 2434 | return DrawGlInfo::kStatusDone; |
| 2435 | } |
| 2436 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2437 | if (CC_LIKELY(mesh && mesh->verticesCount > 0)) { |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2438 | mCaches.activeTexture(0); |
Romain Guy | a404e16 | 2013-05-24 16:19:19 -0700 | [diff] [blame] | 2439 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2440 | if (!texture) return DrawGlInfo::kStatusDone; |
| 2441 | const AutoTexture autoCleanup(texture); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2442 | |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2443 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2444 | texture->setFilter(GL_LINEAR, true); |
| 2445 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2446 | int alpha; |
| 2447 | SkXfermode::Mode mode; |
| 2448 | getAlphaAndMode(paint, &alpha, &mode); |
| 2449 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2450 | const bool pureTranslate = currentTransform().isPureTranslate(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2451 | // 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] | 2452 | if (hasLayer() && mesh->hasEmptyQuads) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2453 | const float offsetX = left + currentTransform().getTranslateX(); |
| 2454 | const float offsetY = top + currentTransform().getTranslateY(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2455 | const size_t count = mesh->quads.size(); |
| 2456 | for (size_t i = 0; i < count; i++) { |
Romain Guy | 8ab4079 | 2010-12-07 13:30:10 -0800 | [diff] [blame] | 2457 | const Rect& bounds = mesh->quads.itemAt(i); |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2458 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 2459 | const float x = (int) floorf(bounds.left + offsetX + 0.5f); |
| 2460 | const float y = (int) floorf(bounds.top + offsetY + 0.5f); |
| 2461 | dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2462 | } else { |
Romain Guy | c78b5d5 | 2011-02-04 14:00:42 -0800 | [diff] [blame] | 2463 | dirtyLayer(left + bounds.left, top + bounds.top, |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2464 | left + bounds.right, top + bounds.bottom, currentTransform()); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2465 | } |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2466 | } |
| 2467 | } |
| 2468 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2469 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2470 | const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); |
| 2471 | const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2472 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2473 | right = x + right - left; |
| 2474 | bottom = y + bottom - top; |
| 2475 | drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f, |
| 2476 | mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset, |
| 2477 | GL_TRIANGLES, mesh->indexCount, false, true, |
| 2478 | mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2479 | } else { |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 2480 | drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, |
| 2481 | mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset, |
| 2482 | GL_TRIANGLES, mesh->indexCount, false, false, |
| 2483 | mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2484 | } |
Romain Guy | 054dc18 | 2010-10-15 17:55:25 -0700 | [diff] [blame] | 2485 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2486 | |
| 2487 | return DrawGlInfo::kStatusDrew; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Romain Guy | 03c00b5 | 2013-06-20 18:30:28 -0700 | [diff] [blame] | 2490 | /** |
| 2491 | * Important note: this method is intended to draw batches of 9-patch objects and |
| 2492 | * will not set the scissor enable or dirty the current layer, if any. |
| 2493 | * The caller is responsible for properly dirtying the current layer. |
| 2494 | */ |
| 2495 | status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry, |
| 2496 | TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) { |
| 2497 | mCaches.activeTexture(0); |
| 2498 | Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap); |
| 2499 | if (!texture) return DrawGlInfo::kStatusDone; |
| 2500 | const AutoTexture autoCleanup(texture); |
| 2501 | |
| 2502 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
| 2503 | texture->setFilter(GL_LINEAR, true); |
| 2504 | |
| 2505 | int alpha; |
| 2506 | SkXfermode::Mode mode; |
| 2507 | getAlphaAndMode(paint, &alpha, &mode); |
| 2508 | |
| 2509 | drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f, |
| 2510 | mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0], |
| 2511 | GL_TRIANGLES, indexCount, false, true, 0, true, false); |
| 2512 | |
| 2513 | return DrawGlInfo::kStatusDrew; |
| 2514 | } |
| 2515 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2516 | status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint, |
| 2517 | bool useOffset) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2518 | if (!vertexBuffer.getVertexCount()) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2519 | // no vertices to draw |
| 2520 | return DrawGlInfo::kStatusDone; |
| 2521 | } |
| 2522 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2523 | int color = paint->getColor(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2524 | SkXfermode::Mode mode = getXfermode(paint->getXfermode()); |
| 2525 | bool isAA = paint->isAntiAlias(); |
| 2526 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2527 | setupDraw(); |
| 2528 | setupDrawNoTexture(); |
| 2529 | if (isAA) setupDrawAA(); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2530 | setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); |
| 2531 | setupDrawColorFilter(); |
| 2532 | setupDrawShader(); |
| 2533 | setupDrawBlending(isAA, mode); |
| 2534 | setupDrawProgram(); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2535 | setupDrawModelViewIdentity(useOffset); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2536 | setupDrawColorUniforms(); |
| 2537 | setupDrawColorFilterUniforms(); |
| 2538 | setupDrawShaderIdentityUniforms(); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2539 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2540 | void* vertices = vertexBuffer.getBuffer(); |
| 2541 | bool force = mCaches.unbindMeshBuffer(); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2542 | mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2543 | mCaches.resetTexCoordsVertexPointer(); |
| 2544 | mCaches.unbindIndicesBuffer(); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2545 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2546 | int alphaSlot = -1; |
| 2547 | if (isAA) { |
| 2548 | void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset; |
| 2549 | alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha"); |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 2550 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2551 | // TODO: avoid enable/disable in back to back uses of the alpha attribute |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 2552 | glEnableVertexAttribArray(alphaSlot); |
| 2553 | glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2554 | } |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2555 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2556 | glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount()); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2557 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2558 | if (isAA) { |
Chris Craik | 6ebdc11 | 2012-08-31 18:24:33 -0700 | [diff] [blame] | 2559 | glDisableVertexAttribArray(alphaSlot); |
Romain Guy | 0429938 | 2012-07-18 17:15:41 -0700 | [diff] [blame] | 2560 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2561 | |
| 2562 | return DrawGlInfo::kStatusDrew; |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | /** |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2566 | * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to |
| 2567 | * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in |
| 2568 | * screen space in all directions. However, instead of using a fragment shader to compute the |
| 2569 | * translucency of the color from its position, we simply use a varying parameter to define how far |
| 2570 | * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used. |
| 2571 | * |
| 2572 | * Doesn't yet support joins, caps, or path effects. |
| 2573 | */ |
| 2574 | status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) { |
| 2575 | VertexBuffer vertexBuffer; |
| 2576 | // TODO: try clipping large paths to viewport |
| 2577 | PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer); |
| 2578 | |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2579 | if (hasLayer()) { |
| 2580 | SkRect bounds = path.getBounds(); |
| 2581 | PathTessellator::expandBoundsForStroke(bounds, paint, false); |
| 2582 | dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform()); |
| 2583 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2584 | |
| 2585 | return drawVertexBuffer(vertexBuffer, paint); |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha |
| 2590 | * and additional geometry for defining an alpha slope perimeter. |
| 2591 | * |
| 2592 | * Using GL_LINES can be difficult because the rasterization rules for those lines produces some |
| 2593 | * unexpected results, and may vary between hardware devices. Previously we used a varying-base |
| 2594 | * in-shader alpha region, but found it to be taxing on some GPUs. |
| 2595 | * |
| 2596 | * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce |
| 2597 | * memory transfer by removing need for degenerate vertices. |
Chet Haase | 99ecdc4 | 2011-05-06 12:06:34 -0700 | [diff] [blame] | 2598 | */ |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2599 | status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) { |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2600 | if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone; |
Chet Haase | 8a5cc92 | 2011-04-26 07:28:09 -0700 | [diff] [blame] | 2601 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2602 | count &= ~0x3; // round down to nearest four |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2603 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2604 | VertexBuffer buffer; |
| 2605 | SkRect bounds; |
| 2606 | PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer); |
Romain Guy | d71ff91d | 2013-02-08 13:46:40 -0800 | [diff] [blame] | 2607 | |
| 2608 | if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) { |
| 2609 | return DrawGlInfo::kStatusDone; |
| 2610 | } |
| 2611 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2612 | dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform()); |
Romain Guy | 7b63142 | 2012-04-04 11:38:54 -0700 | [diff] [blame] | 2613 | |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2614 | bool useOffset = !paint->isAntiAlias(); |
| 2615 | return drawVertexBuffer(buffer, paint, useOffset); |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 2616 | } |
| 2617 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2618 | status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2619 | if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2620 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2621 | count &= ~0x1; // round down to nearest two |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2622 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2623 | VertexBuffer buffer; |
| 2624 | SkRect bounds; |
| 2625 | PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer); |
Romain Guy | d71ff91d | 2013-02-08 13:46:40 -0800 | [diff] [blame] | 2626 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2627 | if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) { |
| 2628 | return DrawGlInfo::kStatusDone; |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2629 | } |
| 2630 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2631 | dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform()); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2632 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 2633 | bool useOffset = !paint->isAntiAlias(); |
| 2634 | return drawVertexBuffer(buffer, paint, useOffset); |
Romain Guy | ed6fcb0 | 2011-03-21 13:11:28 -0700 | [diff] [blame] | 2635 | } |
| 2636 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2637 | status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) { |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2638 | // No need to check against the clip, we fill the clip region |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2639 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Romain Guy | e45362c | 2010-11-03 19:58:32 -0700 | [diff] [blame] | 2640 | |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 2641 | Rect& clip(*mSnapshot->clipRect); |
| 2642 | clip.snapToPixelBoundaries(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 2643 | |
Romain Guy | 3d58c03 | 2010-07-14 16:34:53 -0700 | [diff] [blame] | 2644 | drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2645 | |
| 2646 | return DrawGlInfo::kStatusDrew; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2647 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2648 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2649 | status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture, |
| 2650 | SkPaint* paint) { |
| 2651 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2652 | const AutoTexture autoCleanup(texture); |
| 2653 | |
| 2654 | const float x = left + texture->left - texture->offset; |
| 2655 | const float y = top + texture->top - texture->offset; |
| 2656 | |
| 2657 | drawPathTexture(texture, x, y, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2658 | |
| 2659 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2660 | } |
| 2661 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2662 | status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom, |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2663 | float rx, float ry, SkPaint* p) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2664 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || |
| 2665 | (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2666 | return DrawGlInfo::kStatusDone; |
| 2667 | } |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2668 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2669 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2670 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2671 | const PathTexture* texture = mCaches.pathCache.getRoundRect( |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2672 | right - left, bottom - top, rx, ry, p); |
| 2673 | return drawShape(left, top, texture, p); |
| 2674 | } |
| 2675 | |
| 2676 | SkPath path; |
| 2677 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2678 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2679 | float outset = p->getStrokeWidth() / 2; |
| 2680 | rect.outset(outset, outset); |
| 2681 | rx += outset; |
| 2682 | ry += outset; |
| 2683 | } |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2684 | path.addRoundRect(rect, rx, ry); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2685 | return drawConvexPath(path, p); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2688 | status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) { |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2689 | if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius, |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2690 | x + radius, y + radius, p) || |
| 2691 | (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2692 | return DrawGlInfo::kStatusDone; |
| 2693 | } |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2694 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2695 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2696 | const PathTexture* texture = mCaches.pathCache.getCircle(radius, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2697 | return drawShape(x - radius, y - radius, texture, p); |
| 2698 | } |
| 2699 | |
| 2700 | SkPath path; |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2701 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2702 | path.addCircle(x, y, radius + p->getStrokeWidth() / 2); |
| 2703 | } else { |
| 2704 | path.addCircle(x, y, radius); |
| 2705 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2706 | return drawConvexPath(path, p); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2707 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2708 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2709 | status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom, |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2710 | SkPaint* p) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2711 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || |
| 2712 | (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2713 | return DrawGlInfo::kStatusDone; |
| 2714 | } |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 2715 | |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2716 | if (p->getPathEffect() != 0) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2717 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2718 | const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p); |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2719 | return drawShape(left, top, texture, p); |
| 2720 | } |
| 2721 | |
| 2722 | SkPath path; |
| 2723 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
Chris Craik | cb4d600 | 2012-09-25 12:00:29 -0700 | [diff] [blame] | 2724 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2725 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2726 | } |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2727 | path.addOval(rect); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2728 | return drawConvexPath(path, p); |
Romain Guy | c1cd9ba3 | 2011-01-23 14:18:41 -0800 | [diff] [blame] | 2729 | } |
| 2730 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2731 | status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom, |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2732 | float startAngle, float sweepAngle, bool useCenter, SkPaint* p) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2733 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || |
| 2734 | (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2735 | return DrawGlInfo::kStatusDone; |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2736 | } |
| 2737 | |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2738 | if (fabs(sweepAngle) >= 360.0f) { |
| 2739 | return drawOval(left, top, right, bottom, p); |
| 2740 | } |
| 2741 | |
| 2742 | // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180) |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2743 | if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) { |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2744 | mCaches.activeTexture(0); |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2745 | const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top, |
Chris Craik | 780c128 | 2012-10-04 14:10:49 -0700 | [diff] [blame] | 2746 | startAngle, sweepAngle, useCenter, p); |
| 2747 | return drawShape(left, top, texture, p); |
| 2748 | } |
| 2749 | |
| 2750 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2751 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2752 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2753 | } |
| 2754 | |
| 2755 | SkPath path; |
| 2756 | if (useCenter) { |
| 2757 | path.moveTo(rect.centerX(), rect.centerY()); |
| 2758 | } |
| 2759 | path.arcTo(rect, startAngle, sweepAngle, !useCenter); |
| 2760 | if (useCenter) { |
| 2761 | path.close(); |
| 2762 | } |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2763 | return drawConvexPath(path, p); |
Romain Guy | 8b2f526 | 2011-01-23 16:15:02 -0800 | [diff] [blame] | 2764 | } |
| 2765 | |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2766 | // See SkPaintDefaults.h |
| 2767 | #define SkPaintDefaults_MiterLimit SkIntToScalar(4) |
| 2768 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2769 | status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) { |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2770 | if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) || |
| 2771 | (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2772 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 2773 | } |
| 2774 | |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2775 | if (p->getStyle() != SkPaint::kFill_Style) { |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2776 | // only fill style is supported by drawConvexPath, since others have to handle joins |
| 2777 | if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join || |
| 2778 | p->getStrokeMiter() != SkPaintDefaults_MiterLimit) { |
| 2779 | mCaches.activeTexture(0); |
| 2780 | const PathTexture* texture = |
Romain Guy | c46d07a | 2013-03-15 19:06:39 -0700 | [diff] [blame] | 2781 | mCaches.pathCache.getRect(right - left, bottom - top, p); |
Romain Guy | cf8675e | 2012-10-02 12:32:25 -0700 | [diff] [blame] | 2782 | return drawShape(left, top, texture, p); |
| 2783 | } |
| 2784 | |
| 2785 | SkPath path; |
| 2786 | SkRect rect = SkRect::MakeLTRB(left, top, right, bottom); |
| 2787 | if (p->getStyle() == SkPaint::kStrokeAndFill_Style) { |
| 2788 | rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2); |
| 2789 | } |
| 2790 | path.addRect(rect); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2791 | return drawConvexPath(path, p); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2794 | if (p->isAntiAlias() && !currentTransform().isSimple()) { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2795 | SkPath path; |
| 2796 | path.addRect(left, top, right, bottom); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2797 | return drawConvexPath(path, p); |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2798 | } else { |
Chris Craik | 710f46d | 2012-09-17 17:25:49 -0700 | [diff] [blame] | 2799 | drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode())); |
Chris Craik | 65cd612 | 2012-12-10 17:56:27 -0800 | [diff] [blame] | 2800 | return DrawGlInfo::kStatusDrew; |
Chet Haase | 858aa93 | 2011-05-12 09:06:00 -0700 | [diff] [blame] | 2801 | } |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 2802 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 2803 | |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2804 | void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count, |
| 2805 | const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode, |
| 2806 | float x, float y) { |
| 2807 | mCaches.activeTexture(0); |
| 2808 | |
| 2809 | // NOTE: The drop shadow will not perform gamma correction |
| 2810 | // if shader-based correction is enabled |
| 2811 | mCaches.dropShadowCache.setFontRenderer(fontRenderer); |
| 2812 | const ShadowTexture* shadow = mCaches.dropShadowCache.get( |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2813 | paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 2814 | // If the drop shadow exceeds the max texture size or couldn't be |
| 2815 | // allocated, skip drawing |
| 2816 | if (!shadow) return; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2817 | const AutoTexture autoCleanup(shadow); |
| 2818 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2819 | const float sx = x - shadow->left + mDrawModifiers.mShadowDx; |
| 2820 | const float sy = y - shadow->top + mDrawModifiers.mShadowDy; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2821 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2822 | const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha; |
| 2823 | int shadowColor = mDrawModifiers.mShadowColor; |
| 2824 | if (mDrawModifiers.mShader) { |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2825 | shadowColor = 0xffffffff; |
| 2826 | } |
| 2827 | |
| 2828 | setupDraw(); |
| 2829 | setupDrawWithTexture(true); |
| 2830 | setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha); |
| 2831 | setupDrawColorFilter(); |
| 2832 | setupDrawShader(); |
| 2833 | setupDrawBlending(true, mode); |
| 2834 | setupDrawProgram(); |
| 2835 | setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height); |
| 2836 | setupDrawTexture(shadow->id); |
| 2837 | setupDrawPureColorUniforms(); |
| 2838 | setupDrawColorFilterUniforms(); |
| 2839 | setupDrawShaderUniforms(); |
| 2840 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 2841 | |
| 2842 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 2843 | } |
| 2844 | |
Romain Guy | 768bffc | 2013-02-27 13:50:45 -0800 | [diff] [blame] | 2845 | bool OpenGLRenderer::canSkipText(const SkPaint* paint) const { |
| 2846 | float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha; |
| 2847 | return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode; |
| 2848 | } |
| 2849 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2850 | status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count, |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2851 | const float* positions, SkPaint* paint) { |
Romain Guy | 768bffc | 2013-02-27 13:50:45 -0800 | [diff] [blame] | 2852 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2853 | return DrawGlInfo::kStatusDone; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2854 | } |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2855 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2856 | // NOTE: Skia does not support perspective transform on drawPosText yet |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2857 | if (!currentTransform().isSimple()) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2858 | return DrawGlInfo::kStatusDone; |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2859 | } |
| 2860 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 2861 | mCaches.enableScissor(); |
| 2862 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2863 | float x = 0.0f; |
| 2864 | float y = 0.0f; |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2865 | const bool pureTranslate = currentTransform().isPureTranslate(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2866 | if (pureTranslate) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2867 | x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f); |
| 2868 | y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2869 | } |
| 2870 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 2871 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2872 | fontRenderer.setFont(paint, mat4::identity()); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2873 | |
| 2874 | int alpha; |
| 2875 | SkXfermode::Mode mode; |
| 2876 | getAlphaAndMode(paint, &alpha, &mode); |
| 2877 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2878 | if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) { |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 2879 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, |
| 2880 | alpha, mode, 0.0f, 0.0f); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 2881 | } |
| 2882 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2883 | // Pick the appropriate texture filtering |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2884 | bool linearFilter = currentTransform().changesBounds(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2885 | if (pureTranslate && !linearFilter) { |
| 2886 | linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
| 2887 | } |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2888 | fontRenderer.setTextureFiltering(linearFilter); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2889 | |
| 2890 | const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip(); |
| 2891 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
| 2892 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2893 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2894 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 2895 | TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2896 | if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2897 | positions, hasActiveLayer ? &bounds : NULL, &functor)) { |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2898 | if (hasActiveLayer) { |
| 2899 | if (!pureTranslate) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2900 | currentTransform().mapRect(bounds); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2901 | } |
| 2902 | dirtyLayerUnchecked(bounds, getRegion()); |
| 2903 | } |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 2904 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 2905 | |
| 2906 | return DrawGlInfo::kStatusDrew; |
Romain Guy | eb9a536 | 2012-01-17 17:39:26 -0800 | [diff] [blame] | 2907 | } |
| 2908 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2909 | mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const { |
| 2910 | mat4 fontTransform; |
| 2911 | if (CC_LIKELY(transform.isPureTranslate())) { |
| 2912 | fontTransform = mat4::identity(); |
| 2913 | } else { |
| 2914 | if (CC_UNLIKELY(transform.isPerspective())) { |
Romain Guy | b09f147 | 2013-03-07 17:01:05 -0800 | [diff] [blame] | 2915 | fontTransform = mat4::identity(); |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2916 | } else { |
| 2917 | float sx, sy; |
| 2918 | currentTransform().decomposeScale(sx, sy); |
| 2919 | fontTransform.loadScale(sx, sy, 1.0f); |
| 2920 | } |
| 2921 | } |
| 2922 | return fontTransform; |
| 2923 | } |
| 2924 | |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2925 | status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y, |
| 2926 | const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds, |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2927 | DrawOpMode drawOpMode) { |
| 2928 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2929 | if (drawOpMode == kDrawOpMode_Immediate) { |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 2930 | // The checks for corner-case ignorable text and quick rejection is only done for immediate |
| 2931 | // drawing as ops from DeferredDisplayList are already filtered for these |
| 2932 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) || |
| 2933 | quickReject(bounds)) { |
| 2934 | return DrawGlInfo::kStatusDone; |
| 2935 | } |
Romain Guy | cac5fd3 | 2011-12-01 20:08:50 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Romain Guy | 3a3fa1b | 2010-12-06 18:47:50 -0800 | [diff] [blame] | 2938 | const float oldX = x; |
| 2939 | const float oldY = y; |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2940 | |
| 2941 | const mat4& transform = currentTransform(); |
| 2942 | const bool pureTranslate = transform.isPureTranslate(); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2943 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 2944 | if (CC_LIKELY(pureTranslate)) { |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2945 | x = (int) floorf(x + transform.getTranslateX() + 0.5f); |
| 2946 | y = (int) floorf(y + transform.getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2947 | } |
| 2948 | |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 2949 | int alpha; |
| 2950 | SkXfermode::Mode mode; |
| 2951 | getAlphaAndMode(paint, &alpha, &mode); |
Romain Guy | 9d13fe25 | 2010-10-15 16:06:03 -0700 | [diff] [blame] | 2952 | |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2953 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
| 2954 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 2955 | if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) { |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2956 | fontRenderer.setFont(paint, mat4::identity()); |
| 2957 | drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer, |
| 2958 | alpha, mode, oldX, oldY); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Romain Guy | 19d4dd8 | 2013-03-04 11:14:26 -0800 | [diff] [blame] | 2961 | const bool hasActiveLayer = hasLayer(); |
| 2962 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2963 | // We only pass a partial transform to the font renderer. That partial |
| 2964 | // matrix defines how glyphs are rasterized. Typically we want glyphs |
| 2965 | // to be rasterized at their final size on screen, which means the partial |
| 2966 | // matrix needs to take the scale factor into account. |
| 2967 | // When a partial matrix is used to transform glyphs during rasterization, |
| 2968 | // the mesh is generated with the inverse transform (in the case of scale, |
| 2969 | // the mesh is generated at 1.0 / scale for instance.) This allows us to |
| 2970 | // apply the full transform matrix at draw time in the vertex shader. |
| 2971 | // Applying the full matrix in the shader is the easiest way to handle |
| 2972 | // rotation and perspective and allows us to always generated quads in the |
| 2973 | // font renderer which greatly simplifies the code, clipping in particular. |
| 2974 | mat4 fontTransform = findBestFontTransform(transform); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 2975 | fontRenderer.setFont(paint, fontTransform); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 2976 | |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 2977 | // Pick the appropriate texture filtering |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 2978 | bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f; |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 2979 | fontRenderer.setTextureFiltering(linearFilter); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 2980 | |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 2981 | // TODO: Implement better clipping for scaled/rotated text |
| 2982 | const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect; |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2983 | Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 2984 | |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2985 | bool status; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 2986 | TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 2987 | |
| 2988 | // don't call issuedrawcommand, do it at end of batch |
| 2989 | bool forceFinish = (drawOpMode != kDrawOpMode_Defer); |
Romain Guy | a3dc55f | 2012-09-28 13:55:44 -0700 | [diff] [blame] | 2990 | if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2991 | SkPaint paintCopy(*paint); |
| 2992 | paintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 2993 | status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2994 | positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2995 | } else { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 2996 | status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y, |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 2997 | positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish); |
Raph Levien | 996e57c | 2012-07-23 15:22:52 -0700 | [diff] [blame] | 2998 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 2999 | |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 3000 | if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) { |
Romain Guy | 624234f | 2013-03-05 16:43:31 -0800 | [diff] [blame] | 3001 | if (!pureTranslate) { |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 3002 | transform.mapRect(layerBounds); |
Romain Guy | a4adcf0 | 2013-02-28 12:15:35 -0800 | [diff] [blame] | 3003 | } |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 3004 | dirtyLayerUnchecked(layerBounds, getRegion()); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 3005 | } |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 3006 | |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 3007 | drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3008 | |
| 3009 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3012 | status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path, |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 3013 | float hOffset, float vOffset, SkPaint* paint) { |
Romain Guy | 768bffc | 2013-02-27 13:50:45 -0800 | [diff] [blame] | 3014 | if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) { |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3015 | return DrawGlInfo::kStatusDone; |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 3016 | } |
| 3017 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 3018 | // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics |
| 3019 | mCaches.enableScissor(); |
| 3020 | |
Romain Guy | b1d0a4e | 2012-07-13 18:25:35 -0700 | [diff] [blame] | 3021 | FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint); |
Romain Guy | c74f45a | 2013-02-26 19:10:14 -0800 | [diff] [blame] | 3022 | fontRenderer.setFont(paint, mat4::identity()); |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 3023 | fontRenderer.setTextureFiltering(true); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 3024 | |
| 3025 | int alpha; |
| 3026 | SkXfermode::Mode mode; |
| 3027 | getAlphaAndMode(paint, &alpha, &mode); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 3028 | TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 3029 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3030 | const Rect* clip = &mSnapshot->getLocalClip(); |
| 3031 | Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f); |
Romain Guy | 03d5852 | 2012-02-24 17:54:07 -0800 | [diff] [blame] | 3032 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3033 | const bool hasActiveLayer = hasLayer(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3034 | |
| 3035 | if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path, |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 3036 | hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) { |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3037 | if (hasActiveLayer) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3038 | currentTransform().mapRect(bounds); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3039 | dirtyLayerUnchecked(bounds, getRegion()); |
| 3040 | } |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 3041 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3042 | |
| 3043 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 325740f | 2012-02-24 16:48:34 -0800 | [diff] [blame] | 3044 | } |
| 3045 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3046 | status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) { |
| 3047 | if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone; |
Romain Guy | dbc26d2 | 2010-10-11 17:58:29 -0700 | [diff] [blame] | 3048 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 3049 | mCaches.activeTexture(0); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 3050 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3051 | const PathTexture* texture = mCaches.pathCache.get(path, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3052 | if (!texture) return DrawGlInfo::kStatusDone; |
Romain Guy | 22158e1 | 2010-08-06 11:18:34 -0700 | [diff] [blame] | 3053 | const AutoTexture autoCleanup(texture); |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 3054 | |
Romain Guy | 8b55f37 | 2010-08-18 17:10:07 -0700 | [diff] [blame] | 3055 | const float x = texture->left - texture->offset; |
| 3056 | const float y = texture->top - texture->offset; |
| 3057 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3058 | drawPathTexture(texture, x, y, paint); |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3059 | |
| 3060 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 7fbcc04 | 2010-08-04 15:40:07 -0700 | [diff] [blame] | 3061 | } |
| 3062 | |
Chris Craik | a08f95c | 2013-03-15 17:24:33 -0700 | [diff] [blame] | 3063 | status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) { |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 3064 | if (!layer) { |
| 3065 | return DrawGlInfo::kStatusDone; |
| 3066 | } |
| 3067 | |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 3068 | mat4* transform = NULL; |
| 3069 | if (layer->isTextureLayer()) { |
| 3070 | transform = &layer->getTransform(); |
| 3071 | if (!transform->isIdentity()) { |
| 3072 | save(0); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3073 | currentTransform().multiply(*transform); |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 3074 | } |
| 3075 | } |
| 3076 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 3077 | bool clipRequired = false; |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 3078 | const bool rejected = quickRejectNoScissor(x, y, |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 3079 | x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired); |
Romain Guy | 35643dd | 2012-09-18 15:40:58 -0700 | [diff] [blame] | 3080 | |
| 3081 | if (rejected) { |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 3082 | if (transform && !transform->isIdentity()) { |
| 3083 | restore(); |
| 3084 | } |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3085 | return DrawGlInfo::kStatusDone; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 3086 | } |
| 3087 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 3088 | updateLayer(layer, true); |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 3089 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 3090 | mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired); |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 3091 | mCaches.activeTexture(0); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 3092 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 3093 | if (CC_LIKELY(!layer->region.isEmpty())) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3094 | SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter; |
| 3095 | mDrawModifiers.mColorFilter = layer->getColorFilter(); |
Romain Guy | e529ece | 2012-09-26 11:23:17 -0700 | [diff] [blame] | 3096 | |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3097 | if (layer->region.isRect()) { |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 3098 | DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, |
| 3099 | composeLayerRect(layer, layer->regionRect)); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3100 | } else if (layer->mesh) { |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3101 | const float a = getLayerAlpha(layer); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3102 | setupDraw(); |
| 3103 | setupDrawWithTexture(); |
Romain Guy | 8168396 | 2011-01-24 20:40:18 -0800 | [diff] [blame] | 3104 | setupDrawColor(a, a, a, a); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3105 | setupDrawColorFilter(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3106 | setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3107 | setupDrawProgram(); |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3108 | setupDrawPureColorUniforms(); |
| 3109 | setupDrawColorFilterUniforms(); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3110 | setupDrawTexture(layer->getTexture()); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3111 | if (CC_LIKELY(currentTransform().isPureTranslate())) { |
| 3112 | int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f); |
| 3113 | int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3114 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3115 | layer->setFilter(GL_NEAREST); |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3116 | setupDrawModelViewTranslate(tx, ty, |
| 3117 | tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3118 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3119 | layer->setFilter(GL_LINEAR); |
Romain Guy | 9ace8f5 | 2011-07-07 20:50:11 -0700 | [diff] [blame] | 3120 | setupDrawModelViewTranslate(x, y, |
| 3121 | x + layer->layer.getWidth(), y + layer->layer.getHeight()); |
| 3122 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3123 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3124 | TextureVertex* mesh = &layer->mesh[0]; |
| 3125 | GLsizei elementsCount = layer->meshElementCount; |
| 3126 | |
| 3127 | while (elementsCount > 0) { |
| 3128 | GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6); |
| 3129 | |
| 3130 | setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]); |
| 3131 | DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate, |
| 3132 | glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL)); |
| 3133 | |
| 3134 | elementsCount -= drawCount; |
| 3135 | // Though there are 4 vertices in a quad, we use 6 indices per |
| 3136 | // quad to draw with GL_TRIANGLES |
| 3137 | mesh += (drawCount / 6) * 4; |
| 3138 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3139 | |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 3140 | #if DEBUG_LAYERS_AS_REGIONS |
| 3141 | drawRegionRects(layer->region); |
| 3142 | #endif |
Romain Guy | c88e357 | 2011-01-22 00:32:12 -0800 | [diff] [blame] | 3143 | } |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3144 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3145 | mDrawModifiers.mColorFilter = oldFilter; |
Romain Guy | e529ece | 2012-09-26 11:23:17 -0700 | [diff] [blame] | 3146 | |
Romain Guy | 5bb3c73 | 2012-11-29 17:52:58 -0800 | [diff] [blame] | 3147 | if (layer->debugDrawUpdate) { |
| 3148 | layer->debugDrawUpdate = false; |
Romain Guy | 4ff0cf4 | 2012-08-06 14:51:10 -0700 | [diff] [blame] | 3149 | drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), |
| 3150 | 0x7f00ff00, SkXfermode::kSrcOver_Mode); |
| 3151 | } |
Romain Guy | f219da5 | 2011-01-16 12:54:25 -0800 | [diff] [blame] | 3152 | } |
Chris Craik | 34416ea | 2013-04-15 16:08:28 -0700 | [diff] [blame] | 3153 | layer->hasDrawnSinceUpdate = true; |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3154 | |
Romain Guy | b2e2f24 | 2012-10-17 18:18:35 -0700 | [diff] [blame] | 3155 | if (transform && !transform->isIdentity()) { |
| 3156 | restore(); |
| 3157 | } |
| 3158 | |
Chet Haase | 4865909 | 2012-05-31 15:21:51 -0700 | [diff] [blame] | 3159 | return DrawGlInfo::kStatusDrew; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3162 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3163 | // Shaders |
| 3164 | /////////////////////////////////////////////////////////////////////////////// |
| 3165 | |
| 3166 | void OpenGLRenderer::resetShader() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3167 | mDrawModifiers.mShader = NULL; |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3168 | } |
| 3169 | |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 3170 | void OpenGLRenderer::setupShader(SkiaShader* shader) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3171 | mDrawModifiers.mShader = shader; |
| 3172 | if (mDrawModifiers.mShader) { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 3173 | mDrawModifiers.mShader->setCaches(mCaches); |
Romain Guy | 06f96e2 | 2010-07-30 19:18:16 -0700 | [diff] [blame] | 3174 | } |
Romain Guy | 7fac2e1 | 2010-07-16 17:10:13 -0700 | [diff] [blame] | 3175 | } |
| 3176 | |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3177 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3178 | // Color filters |
| 3179 | /////////////////////////////////////////////////////////////////////////////// |
| 3180 | |
| 3181 | void OpenGLRenderer::resetColorFilter() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3182 | mDrawModifiers.mColorFilter = NULL; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3186 | mDrawModifiers.mColorFilter = filter; |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 3190 | // Drop shadow |
| 3191 | /////////////////////////////////////////////////////////////////////////////// |
| 3192 | |
| 3193 | void OpenGLRenderer::resetShadow() { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3194 | mDrawModifiers.mHasShadow = false; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3198 | mDrawModifiers.mHasShadow = true; |
| 3199 | mDrawModifiers.mShadowRadius = radius; |
| 3200 | mDrawModifiers.mShadowDx = dx; |
| 3201 | mDrawModifiers.mShadowDy = dy; |
| 3202 | mDrawModifiers.mShadowColor = color; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3206 | // Draw filters |
| 3207 | /////////////////////////////////////////////////////////////////////////////// |
| 3208 | |
| 3209 | void OpenGLRenderer::resetPaintFilter() { |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 3210 | // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier |
| 3211 | // comparison, see MergingDrawBatch::canMergeWith |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3212 | mDrawModifiers.mHasDrawFilter = false; |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 3213 | mDrawModifiers.mPaintFilterClearBits = 0; |
| 3214 | mDrawModifiers.mPaintFilterSetBits = 0; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) { |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3218 | mDrawModifiers.mHasDrawFilter = true; |
| 3219 | mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags; |
| 3220 | mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags; |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3221 | } |
| 3222 | |
Chris Craik | a08f95c | 2013-03-15 17:24:33 -0700 | [diff] [blame] | 3223 | SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) { |
Romain Guy | 758724f | 2013-02-27 11:53:12 -0800 | [diff] [blame] | 3224 | if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) { |
Romain Guy | 758724f | 2013-02-27 11:53:12 -0800 | [diff] [blame] | 3225 | return paint; |
| 3226 | } |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3227 | |
| 3228 | uint32_t flags = paint->getFlags(); |
| 3229 | |
| 3230 | mFilteredPaint = *paint; |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3231 | mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) | |
| 3232 | mDrawModifiers.mPaintFilterSetBits); |
Romain Guy | 5ff9df6 | 2012-01-23 17:09:05 -0800 | [diff] [blame] | 3233 | |
| 3234 | return &mFilteredPaint; |
| 3235 | } |
| 3236 | |
| 3237 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3238 | // Drawing implementation |
| 3239 | /////////////////////////////////////////////////////////////////////////////// |
| 3240 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3241 | Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) { |
| 3242 | Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap); |
| 3243 | if (!texture) { |
| 3244 | return mCaches.textureCache.get(bitmap); |
| 3245 | } |
| 3246 | return texture; |
| 3247 | } |
| 3248 | |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3249 | void OpenGLRenderer::drawPathTexture(const PathTexture* texture, |
| 3250 | float x, float y, SkPaint* paint) { |
| 3251 | if (quickReject(x, y, x + texture->width, y + texture->height)) { |
| 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | int alpha; |
| 3256 | SkXfermode::Mode mode; |
| 3257 | getAlphaAndMode(paint, &alpha, &mode); |
| 3258 | |
| 3259 | setupDraw(); |
| 3260 | setupDrawWithTexture(true); |
| 3261 | setupDrawAlpha8Color(paint->getColor(), alpha); |
| 3262 | setupDrawColorFilter(); |
| 3263 | setupDrawShader(); |
| 3264 | setupDrawBlending(true, mode); |
| 3265 | setupDrawProgram(); |
| 3266 | setupDrawModelView(x, y, x + texture->width, y + texture->height); |
| 3267 | setupDrawTexture(texture->id); |
| 3268 | setupDrawPureColorUniforms(); |
| 3269 | setupDrawColorFilterUniforms(); |
| 3270 | setupDrawShaderUniforms(); |
| 3271 | setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset); |
| 3272 | |
| 3273 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
Romain Guy | 01d58e4 | 2011-01-19 21:54:02 -0800 | [diff] [blame] | 3274 | } |
| 3275 | |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3276 | // Same values used by Skia |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3277 | #define kStdStrikeThru_Offset (-6.0f / 21.0f) |
| 3278 | #define kStdUnderline_Offset (1.0f / 9.0f) |
| 3279 | #define kStdUnderline_Thickness (1.0f / 18.0f) |
| 3280 | |
Chris Craik | 4154182 | 2013-05-03 16:35:54 -0700 | [diff] [blame] | 3281 | void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth, |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3282 | float x, float y, SkPaint* paint) { |
| 3283 | // Handle underline and strike-through |
| 3284 | uint32_t flags = paint->getFlags(); |
| 3285 | if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3286 | SkPaint paintCopy(*paint); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3287 | |
Romain Guy | 211370f | 2012-02-01 16:10:55 -0800 | [diff] [blame] | 3288 | if (CC_LIKELY(underlineWidth > 0.0f)) { |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3289 | const float textSize = paintCopy.getTextSize(); |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 3290 | const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3291 | |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 3292 | const float left = x; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3293 | float top = 0.0f; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3294 | |
Romain Guy | f683447 | 2011-01-23 13:32:12 -0800 | [diff] [blame] | 3295 | int linesCount = 0; |
| 3296 | if (flags & SkPaint::kUnderlineText_Flag) linesCount++; |
| 3297 | if (flags & SkPaint::kStrikeThruText_Flag) linesCount++; |
| 3298 | |
| 3299 | const int pointsCount = 4 * linesCount; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3300 | float points[pointsCount]; |
| 3301 | int currentPoint = 0; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3302 | |
| 3303 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 3304 | top = y + textSize * kStdUnderline_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3305 | points[currentPoint++] = left; |
| 3306 | points[currentPoint++] = top; |
| 3307 | points[currentPoint++] = left + underlineWidth; |
| 3308 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 3312 | top = y + textSize * kStdStrikeThru_Offset; |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3313 | points[currentPoint++] = left; |
| 3314 | points[currentPoint++] = top; |
| 3315 | points[currentPoint++] = left + underlineWidth; |
| 3316 | points[currentPoint++] = top; |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3317 | } |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3318 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3319 | paintCopy.setStrokeWidth(strokeWidth); |
Romain Guy | e20ecbd | 2010-09-22 19:49:04 -0700 | [diff] [blame] | 3320 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 3321 | drawLines(&points[0], pointsCount, &paintCopy); |
Romain Guy | 0a41749 | 2010-08-16 20:26:20 -0700 | [diff] [blame] | 3322 | } |
| 3323 | } |
| 3324 | } |
| 3325 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3326 | status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) { |
| 3327 | if (mSnapshot->isIgnored()) { |
| 3328 | return DrawGlInfo::kStatusDone; |
| 3329 | } |
| 3330 | |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3331 | int color = paint->getColor(); |
| 3332 | // If a shader is set, preserve only the alpha |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3333 | if (mDrawModifiers.mShader) { |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3334 | color |= 0x00ffffff; |
| 3335 | } |
| 3336 | SkXfermode::Mode mode = getXfermode(paint->getXfermode()); |
| 3337 | |
| 3338 | return drawColorRects(rects, count, color, mode); |
| 3339 | } |
| 3340 | |
| 3341 | status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color, |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 3342 | SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3343 | if (count == 0) { |
| 3344 | return DrawGlInfo::kStatusDone; |
| 3345 | } |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3346 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3347 | float left = FLT_MAX; |
| 3348 | float top = FLT_MAX; |
| 3349 | float right = FLT_MIN; |
| 3350 | float bottom = FLT_MIN; |
| 3351 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3352 | Vertex mesh[count]; |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3353 | Vertex* vertex = mesh; |
| 3354 | |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 3355 | for (int index = 0; index < count; index += 4) { |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3356 | float l = rects[index + 0]; |
| 3357 | float t = rects[index + 1]; |
| 3358 | float r = rects[index + 2]; |
| 3359 | float b = rects[index + 3]; |
| 3360 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3361 | Vertex::set(vertex++, l, t); |
| 3362 | Vertex::set(vertex++, r, t); |
| 3363 | Vertex::set(vertex++, l, b); |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3364 | Vertex::set(vertex++, r, b); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3365 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3366 | left = fminf(left, l); |
| 3367 | top = fminf(top, t); |
| 3368 | right = fmaxf(right, r); |
| 3369 | bottom = fmaxf(bottom, b); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3370 | } |
| 3371 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3372 | if (clip && quickReject(left, top, right, bottom)) { |
Romain Guy | a362c69 | 2013-02-04 13:50:16 -0800 | [diff] [blame] | 3373 | return DrawGlInfo::kStatusDone; |
| 3374 | } |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3375 | |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3376 | setupDraw(); |
| 3377 | setupDrawNoTexture(); |
| 3378 | setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); |
| 3379 | setupDrawShader(); |
| 3380 | setupDrawColorFilter(); |
| 3381 | setupDrawBlending(mode); |
| 3382 | setupDrawProgram(); |
| 3383 | setupDrawDirtyRegionsDisabled(); |
Romain Guy | 735738c | 2012-12-03 12:34:51 -0800 | [diff] [blame] | 3384 | setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3385 | setupDrawColorUniforms(); |
| 3386 | setupDrawShaderUniforms(); |
| 3387 | setupDrawColorFilterUniforms(); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3388 | |
Romain Guy | 8ce0030 | 2013-01-15 18:51:42 -0800 | [diff] [blame] | 3389 | if (dirty && hasLayer()) { |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3390 | dirtyLayer(left, top, right, bottom, currentTransform()); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3391 | } |
| 3392 | |
Romain Guy | 448455f | 2013-07-22 13:57:50 -0700 | [diff] [blame] | 3393 | drawIndexedQuads(&mesh[0], count / 4); |
Romain Guy | 672433d | 2013-01-04 19:05:13 -0800 | [diff] [blame] | 3394 | |
| 3395 | return DrawGlInfo::kStatusDrew; |
| 3396 | } |
| 3397 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3398 | void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom, |
Romain Guy | 1c740bc | 2010-09-13 18:00:09 -0700 | [diff] [blame] | 3399 | int color, SkXfermode::Mode mode, bool ignoreTransform) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3400 | // If a shader is set, preserve only the alpha |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 3401 | if (mDrawModifiers.mShader) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3402 | color |= 0x00ffffff; |
| 3403 | } |
| 3404 | |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3405 | setupDraw(); |
Romain Guy | 15bc643 | 2011-12-13 13:11:32 -0800 | [diff] [blame] | 3406 | setupDrawNoTexture(); |
Romain Guy | 9c0b188 | 2012-07-13 12:13:07 -0700 | [diff] [blame] | 3407 | setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3408 | setupDrawShader(); |
| 3409 | setupDrawColorFilter(); |
| 3410 | setupDrawBlending(mode); |
| 3411 | setupDrawProgram(); |
| 3412 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
| 3413 | setupDrawColorUniforms(); |
| 3414 | setupDrawShaderUniforms(ignoreTransform); |
| 3415 | setupDrawColorFilterUniforms(); |
| 3416 | setupDrawSimpleMesh(); |
Romain Guy | c0ac193 | 2010-07-19 18:43:02 -0700 | [diff] [blame] | 3417 | |
Romain Guy | c95c8d6 | 2010-09-17 15:31:32 -0700 | [diff] [blame] | 3418 | glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount); |
| 3419 | } |
| 3420 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3421 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 3422 | Texture* texture, SkPaint* paint) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3423 | int alpha; |
| 3424 | SkXfermode::Mode mode; |
| 3425 | getAlphaAndMode(paint, &alpha, &mode); |
| 3426 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3427 | texture->setWrap(GL_CLAMP_TO_EDGE, true); |
Romain Guy | 8164c2d | 2010-10-25 18:03:28 -0700 | [diff] [blame] | 3428 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3429 | GLvoid* vertices = (GLvoid*) NULL; |
| 3430 | GLvoid* texCoords = (GLvoid*) gMeshTextureOffset; |
| 3431 | |
| 3432 | if (texture->uvMapper) { |
| 3433 | vertices = &mMeshVertices[0].position[0]; |
| 3434 | texCoords = &mMeshVertices[0].texture[0]; |
| 3435 | |
| 3436 | Rect uvs(0.0f, 0.0f, 1.0f, 1.0f); |
| 3437 | texture->uvMapper->map(uvs); |
| 3438 | |
| 3439 | resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom); |
| 3440 | } |
| 3441 | |
Romain Guy | 3b75382 | 2013-03-05 10:27:35 -0800 | [diff] [blame] | 3442 | if (CC_LIKELY(currentTransform().isPureTranslate())) { |
| 3443 | const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f); |
| 3444 | const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3445 | |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3446 | texture->setFilter(GL_NEAREST, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3447 | drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3448 | alpha / 255.0f, mode, texture->blend, vertices, texCoords, |
| 3449 | GL_TRIANGLE_STRIP, gMeshCount, false, true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3450 | } else { |
Romain Guy | d21b6e1 | 2011-11-30 20:21:23 -0800 | [diff] [blame] | 3451 | texture->setFilter(FILTER(paint), true); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3452 | drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode, |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3453 | texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount); |
| 3454 | } |
| 3455 | |
| 3456 | if (texture->uvMapper) { |
| 3457 | resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Romain Guy | 6620c6d | 2010-12-06 18:07:02 -0800 | [diff] [blame] | 3458 | } |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 3459 | } |
| 3460 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 3461 | void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 3462 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) { |
| 3463 | drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend, |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 3464 | (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount); |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom, |
Romain Guy | a979474 | 2010-07-13 11:37:54 -0700 | [diff] [blame] | 3468 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3469 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 3470 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3471 | |
Romain Guy | 746b740 | 2010-10-26 16:27:31 -0700 | [diff] [blame] | 3472 | setupDraw(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3473 | setupDrawWithTexture(); |
| 3474 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 3475 | setupDrawColorFilter(); |
| 3476 | setupDrawBlending(blend, mode, swapSrcDst); |
| 3477 | setupDrawProgram(); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3478 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 3479 | if (!ignoreScale) { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3480 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3481 | } else { |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3482 | setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3483 | } |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3484 | setupDrawTexture(texture); |
Romain Guy | 8656819 | 2010-12-14 15:55:39 -0800 | [diff] [blame] | 3485 | setupDrawPureColorUniforms(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3486 | setupDrawColorFilterUniforms(); |
Romain Guy | 70ca14e | 2010-12-13 18:24:33 -0800 | [diff] [blame] | 3487 | setupDrawMesh(vertices, texCoords, vbo); |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 3488 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 3489 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3492 | void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom, |
| 3493 | GLuint texture, float alpha, SkXfermode::Mode mode, bool blend, |
| 3494 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
| 3495 | bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) { |
| 3496 | |
| 3497 | setupDraw(); |
| 3498 | setupDrawWithTexture(); |
| 3499 | setupDrawColor(alpha, alpha, alpha, alpha); |
| 3500 | setupDrawColorFilter(); |
| 3501 | setupDrawBlending(blend, mode, swapSrcDst); |
| 3502 | setupDrawProgram(); |
| 3503 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
| 3504 | if (!ignoreScale) { |
| 3505 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
| 3506 | } else { |
| 3507 | setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); |
| 3508 | } |
| 3509 | setupDrawTexture(texture); |
| 3510 | setupDrawPureColorUniforms(); |
| 3511 | setupDrawColorFilterUniforms(); |
| 3512 | setupDrawMeshIndices(vertices, texCoords, vbo); |
| 3513 | |
| 3514 | glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL); |
Romain Guy | 3b748a4 | 2013-04-17 18:54:38 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3517 | void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom, |
| 3518 | GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode, |
| 3519 | GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 3520 | bool ignoreTransform, bool ignoreScale, bool dirty) { |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3521 | |
| 3522 | setupDraw(); |
| 3523 | setupDrawWithTexture(true); |
| 3524 | if (hasColor) { |
| 3525 | setupDrawAlpha8Color(color, alpha); |
| 3526 | } |
| 3527 | setupDrawColorFilter(); |
| 3528 | setupDrawShader(); |
| 3529 | setupDrawBlending(true, mode); |
| 3530 | setupDrawProgram(); |
| 3531 | if (!dirty) setupDrawDirtyRegionsDisabled(); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 3532 | if (!ignoreScale) { |
| 3533 | setupDrawModelView(left, top, right, bottom, ignoreTransform); |
| 3534 | } else { |
| 3535 | setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform); |
| 3536 | } |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3537 | setupDrawTexture(texture); |
| 3538 | setupDrawPureColorUniforms(); |
| 3539 | setupDrawColorFilterUniforms(); |
| 3540 | setupDrawShaderUniforms(); |
| 3541 | setupDrawMesh(vertices, texCoords); |
| 3542 | |
| 3543 | glDrawArrays(drawMode, 0, elementsCount); |
Romain Guy | 886b275 | 2013-01-04 12:26:18 -0800 | [diff] [blame] | 3544 | } |
| 3545 | |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3546 | void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode, |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3547 | ProgramDescription& description, bool swapSrcDst) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 3548 | if (mCountOverdraw) { |
| 3549 | if (!mCaches.blend) glEnable(GL_BLEND); |
| 3550 | if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) { |
| 3551 | glBlendFunc(GL_ONE, GL_ONE); |
| 3552 | } |
| 3553 | |
| 3554 | mCaches.blend = true; |
| 3555 | mCaches.lastSrcMode = GL_ONE; |
| 3556 | mCaches.lastDstMode = GL_ONE; |
| 3557 | |
| 3558 | return; |
| 3559 | } |
| 3560 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3561 | blend = blend || mode != SkXfermode::kSrcOver_Mode; |
Romain Guy | c189ef5 | 2012-04-25 20:02:53 -0700 | [diff] [blame] | 3562 | |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3563 | if (blend) { |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3564 | // These blend modes are not supported by OpenGL directly and have |
| 3565 | // to be implemented using shaders. Since the shader will perform |
| 3566 | // the blending, turn blending off here |
| 3567 | // If the blend mode cannot be implemented using shaders, fall |
| 3568 | // back to the default SrcOver blend mode instead |
Romain Guy | 33fa1f7 | 2012-08-07 19:09:57 -0700 | [diff] [blame] | 3569 | if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) { |
Romain Guy | 3bbacf2 | 2013-02-06 16:51:04 -0800 | [diff] [blame] | 3570 | if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) { |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3571 | description.framebufferMode = mode; |
Romain Guy | f607bdc | 2010-09-10 19:20:06 -0700 | [diff] [blame] | 3572 | description.swapSrcDst = swapSrcDst; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3573 | |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3574 | if (mCaches.blend) { |
| 3575 | glDisable(GL_BLEND); |
| 3576 | mCaches.blend = false; |
| 3577 | } |
| 3578 | |
| 3579 | return; |
| 3580 | } else { |
| 3581 | mode = SkXfermode::kSrcOver_Mode; |
Romain Guy | a5aed0d | 2010-09-09 14:42:43 -0700 | [diff] [blame] | 3582 | } |
Romain Guy | 82bc7a7 | 2012-01-03 14:13:39 -0800 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | if (!mCaches.blend) { |
| 3586 | glEnable(GL_BLEND); |
| 3587 | } |
| 3588 | |
| 3589 | GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src; |
| 3590 | GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst; |
| 3591 | |
| 3592 | if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) { |
| 3593 | glBlendFunc(sourceMode, destMode); |
| 3594 | mCaches.lastSrcMode = sourceMode; |
| 3595 | mCaches.lastDstMode = destMode; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3596 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3597 | } else if (mCaches.blend) { |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3598 | glDisable(GL_BLEND); |
| 3599 | } |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3600 | mCaches.blend = blend; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 3601 | } |
| 3602 | |
Romain Guy | 889f8d1 | 2010-07-29 14:37:42 -0700 | [diff] [blame] | 3603 | bool OpenGLRenderer::useProgram(Program* program) { |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3604 | if (!program->isInUse()) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3605 | if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove(); |
Romain Guy | d27977d | 2010-07-14 19:18:51 -0700 | [diff] [blame] | 3606 | program->use(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 3607 | mCaches.currentProgram = program; |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3608 | return false; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3609 | } |
Romain Guy | 6926c72 | 2010-07-12 20:20:03 -0700 | [diff] [blame] | 3610 | return true; |
Romain Guy | 260e102 | 2010-07-12 14:41:06 -0700 | [diff] [blame] | 3611 | } |
| 3612 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3613 | void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) { |
Romain Guy | ac670c0 | 2010-07-27 17:39:27 -0700 | [diff] [blame] | 3614 | TextureVertex* v = &mMeshVertices[0]; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 3615 | TextureVertex::setUV(v++, u1, v1); |
| 3616 | TextureVertex::setUV(v++, u2, v1); |
| 3617 | TextureVertex::setUV(v++, u1, v2); |
| 3618 | TextureVertex::setUV(v++, u2, v2); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3621 | void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const { |
Romain Guy | be6f9dc | 2012-07-16 12:41:17 -0700 | [diff] [blame] | 3622 | getAlphaAndModeDirect(paint, alpha, mode); |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3623 | if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { |
| 3624 | // if drawing a layer, ignore the paint's alpha |
Romain Guy | 87b515c | 2013-05-03 17:42:27 -0700 | [diff] [blame] | 3625 | *alpha = mDrawModifiers.mOverrideLayerAlpha * 255; |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3626 | } |
Chet Haase | db8c9a6 | 2012-03-21 18:54:18 -0700 | [diff] [blame] | 3627 | *alpha *= mSnapshot->alpha; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 3628 | } |
| 3629 | |
Chris Craik | 16ecda5 | 2013-03-29 10:59:59 -0700 | [diff] [blame] | 3630 | float OpenGLRenderer::getLayerAlpha(Layer* layer) const { |
| 3631 | float alpha; |
| 3632 | if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) { |
| 3633 | alpha = mDrawModifiers.mOverrideLayerAlpha; |
| 3634 | } else { |
| 3635 | alpha = layer->getAlpha() / 255.0f; |
| 3636 | } |
| 3637 | return alpha * mSnapshot->alpha; |
| 3638 | } |
| 3639 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 3640 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 3641 | }; // namespace android |