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