blob: 0844cb618e3b3df6ea6356f96138bb781c784f2f [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
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 Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
Chris Craik65fe5ee2015-01-26 18:06:29 -080019#include "OpenGLRenderer.h"
20
21#include "DeferredDisplayList.h"
22#include "DisplayListRenderer.h"
23#include "Fence.h"
24#include "GammaFontRenderer.h"
25#include "Patch.h"
26#include "PathTessellator.h"
27#include "Properties.h"
28#include "RenderNode.h"
29#include "renderstate/RenderState.h"
30#include "ShadowTessellator.h"
31#include "SkiaShader.h"
32#include "Vector.h"
33#include "VertexBuffer.h"
34#include "utils/GLUtils.h"
35#include "utils/PaintUtils.h"
36#include "utils/TraceUtils.h"
37
Romain Guye4d01122010-06-16 18:44:05 -070038#include <stdlib.h>
39#include <stdint.h>
40#include <sys/types.h>
41
Romain Guy5cbbce52010-06-27 22:59:20 -070042#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070043#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040044#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070045#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070046
Romain Guye4d01122010-06-16 18:44:05 -070047#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070048#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070049
Romain Guy08aa2cb2011-03-17 11:06:57 -070050#include <private/hwui/DrawGlInfo.h>
51
Romain Guy5b3b3522010-10-27 18:57:51 -070052#include <ui/Rect.h>
53
Chris Craik62d307c2014-07-29 10:35:13 -070054#if DEBUG_DETAILED_EVENTS
55 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
56#else
57 #define EVENT_LOGD(...)
58#endif
59
Romain Guye4d01122010-06-16 18:44:05 -070060namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070061namespace uirenderer {
62
Chris Craik678625242014-02-28 12:26:34 -080063static GLenum getFilter(const SkPaint* paint) {
64 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
65 return GL_LINEAR;
66 }
67 return GL_NEAREST;
68}
Romain Guyd21b6e12011-11-30 20:21:23 -080069
Romain Guy9d5316e2010-06-24 19:30:36 -070070///////////////////////////////////////////////////////////////////////////////
71// Globals
72///////////////////////////////////////////////////////////////////////////////
73
Romain Guy889f8d12010-07-29 14:37:42 -070074/**
75 * Structure mapping Skia xfermodes to OpenGL blending factors.
76 */
77struct Blender {
78 SkXfermode::Mode mode;
79 GLenum src;
80 GLenum dst;
81}; // struct Blender
82
Romain Guy026c5e162010-06-28 17:12:22 -070083// In this array, the index of each Blender equals the value of the first
84// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
85static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070086 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
88 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
89 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
91 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
93 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
94 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
97 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
98 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050099 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -0700100 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700101};
Romain Guye4d01122010-06-16 18:44:05 -0700102
Romain Guy87a76572010-09-13 18:11:21 -0700103// This array contains the swapped version of each SkXfermode. For instance
104// this array's SrcOver blending mode is actually DstOver. You can refer to
105// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700106static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700107 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
108 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
109 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
110 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
111 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
112 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
113 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
114 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
115 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
116 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
117 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
118 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
119 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500120 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700121 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700122};
123
Romain Guyf6a11b82010-06-23 17:47:49 -0700124///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700125// Functions
126///////////////////////////////////////////////////////////////////////////////
127
128template<typename T>
129static inline T min(T a, T b) {
130 return a < b ? a : b;
131}
132
133///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700134// Constructors/destructor
135///////////////////////////////////////////////////////////////////////////////
136
John Reck3b202512014-06-23 13:13:08 -0700137OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400138 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -0700139 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700140 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700141 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -0800142 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700143 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700144 , mSuppressTiling(false)
145 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400146 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700147 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700148 , mLightRadius(FLT_MIN)
149 , mAmbientShadowAlpha(0)
150 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800151 // *set* draw modifiers to be 0
152 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700153 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700154
Romain Guyac670c02010-07-27 17:39:27 -0700155 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700156}
157
Romain Guy85bf02f2010-06-22 13:11:24 -0700158OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700159 // The context has already been destroyed at this point, do not call
160 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700161}
162
Romain Guy87e2f7572012-09-24 11:37:12 -0700163void OpenGLRenderer::initProperties() {
164 char property[PROPERTY_VALUE_MAX];
165 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
166 mScissorOptimizationDisabled = !strcasecmp(property, "true");
167 INIT_LOGD(" Scissor optimization %s",
168 mScissorOptimizationDisabled ? "disabled" : "enabled");
169 } else {
170 INIT_LOGD(" Scissor optimization enabled");
171 }
Romain Guy13631f32012-01-30 17:41:55 -0800172}
173
Chris Craik058fc642014-07-23 18:19:28 -0700174void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
175 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
176 mLightCenter = lightCenter;
177 mLightRadius = lightRadius;
178 mAmbientShadowAlpha = ambientShadowAlpha;
179 mSpotShadowAlpha = spotShadowAlpha;
180}
181
Romain Guy13631f32012-01-30 17:41:55 -0800182///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700183// Setup
184///////////////////////////////////////////////////////////////////////////////
185
Chris Craik797b95b2014-05-20 18:10:25 -0700186void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700187 glDisable(GL_DITHER);
188 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
189
190 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700191 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700192}
193
Romain Guy96885eb2013-03-26 15:05:58 -0700194void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800195 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800196 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400197 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700198 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700199 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700200}
201
Tom Hudson107843d2014-09-08 11:26:26 -0400202void OpenGLRenderer::startFrame() {
203 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700204 mFrameStarted = true;
205
Tom Hudson984162f2014-10-10 13:38:16 -0400206 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700207
Romain Guy96885eb2013-03-26 15:05:58 -0700208 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700209
Tom Hudson984162f2014-10-10 13:38:16 -0400210 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800211
Romain Guy54c1a642012-09-27 17:55:46 -0700212 // Functors break the tiling extension in pretty spectacular ways
213 // This ensures we don't use tiling when a functor is going to be
214 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700215 mSuppressTiling = mCaches.hasRegisteredFunctors()
216 || mFirstFrameAfterResize;
217 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700218
Chris Craikd6b65f62014-01-01 14:45:21 -0800219 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700220
Romain Guy7c450aa2012-09-21 19:15:00 -0700221 debugOverdraw(true, true);
222
Tom Hudson107843d2014-09-08 11:26:26 -0400223 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700224 mTilingClip.right, mTilingClip.bottom, mOpaque);
225}
226
Tom Hudson107843d2014-09-08 11:26:26 -0400227void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700228 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700229
Romain Guy96885eb2013-03-26 15:05:58 -0700230 setupFrameState(left, top, right, bottom, opaque);
231
232 // Layer renderers will start the frame immediately
233 // The framebuffer renderer will first defer the display list
234 // for each layer and wait until the first drawing command
235 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800236 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700237 syncState();
238 updateLayers();
239 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400240 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700241 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700242}
243
Romain Guydcfc8362013-01-03 13:08:57 -0800244void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
245 // If we know that we are going to redraw the entire framebuffer,
246 // perform a discard to let the driver know we don't need to preserve
247 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800248 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400249 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
250 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800251 const GLenum attachments[] = {
252 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
253 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800254 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
255 }
256}
257
Tom Hudson107843d2014-09-08 11:26:26 -0400258void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700259 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800260 mRenderState.scissor().setEnabled(true);
261 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700262 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400263 mDirty = true;
264 return;
Romain Guyddf74372012-05-22 14:07:07 -0700265 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700266
Chris Craik65fe5ee2015-01-26 18:06:29 -0800267 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700268}
269
270void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700271 if (mCaches.blend) {
272 glEnable(GL_BLEND);
273 } else {
274 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700275 }
Romain Guybb9524b2010-06-22 18:56:38 -0700276}
277
henry.uh_chen33f5a592014-07-02 19:36:56 +0800278void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700279 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800280 const Snapshot* snapshot = currentSnapshot();
281
Chris Craik14e51302013-12-30 15:32:54 -0800282 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800283 if (snapshot->flags & Snapshot::kFlagFboTarget) {
284 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700285 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700286
henry.uh_chen33f5a592014-07-02 19:36:56 +0800287 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800288 }
289}
290
henry.uh_chen33f5a592014-07-02 19:36:56 +0800291void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800292 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800293 if(expand) {
294 // Expand the startTiling region by 1
295 int leftNotZero = (clip.left > 0) ? 1 : 0;
296 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
297
298 mCaches.startTiling(
299 clip.left - leftNotZero,
300 windowHeight - clip.bottom - topNotZero,
301 clip.right - clip.left + leftNotZero + 1,
302 clip.bottom - clip.top + topNotZero + 1,
303 opaque);
304 } else {
305 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800306 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800307 }
Romain Guy54c1a642012-09-27 17:55:46 -0700308 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700309}
310
311void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700312 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700313}
314
Tom Hudson107843d2014-09-08 11:26:26 -0400315bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700316 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700317 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800318 mTempPaths.clear();
319
Romain Guyca89e2a2013-03-08 17:44:20 -0800320 // When finish() is invoked on FBO 0 we've reached the end
321 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400322 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800323 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700324 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800325 }
326
Romain Guy11cb6422012-09-21 00:39:43 -0700327 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700328#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700329 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700330#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700331
Romain Guyc15008e2010-11-10 11:59:15 -0800332#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800333 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700334#else
335 if (mCaches.getDebugLevel() & kDebugMemory) {
336 mCaches.dumpMemoryUsage();
337 }
Romain Guyc15008e2010-11-10 11:59:15 -0800338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339 }
Romain Guy96885eb2013-03-26 15:05:58 -0700340
341 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400342
343 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700344}
345
Romain Guy35643dd2012-09-18 15:40:58 -0700346void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700347 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
348 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700349 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700350
Chris Craik65fe5ee2015-01-26 18:06:29 -0800351 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700352 dirtyClip();
353}
354
Andreas Gampe64bb4132014-11-22 00:35:09 +0000355void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400356 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700357
Rob Tsuk487a92c2015-01-06 13:22:54 -0800358 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700359 clip.snapToPixelBoundaries();
360
Romain Guyd643bb52011-03-01 14:55:21 -0800361 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800362 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800363 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800364 dirtyLayerUnchecked(clip, getRegion());
365 }
Romain Guyd643bb52011-03-01 14:55:21 -0800366
Romain Guy08aa2cb2011-03-17 11:06:57 -0700367 DrawGlInfo info;
368 info.clipLeft = clip.left;
369 info.clipTop = clip.top;
370 info.clipRight = clip.right;
371 info.clipBottom = clip.bottom;
372 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700373 info.width = getViewportWidth();
374 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800375 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700376
Tom Hudson984162f2014-10-10 13:38:16 -0400377 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700378 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400379 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700380 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
381 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800382 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700383 setScissorFromClip();
384 }
Chris Craik54f574a2013-08-26 11:23:46 -0700385
John Reck3b202512014-06-23 13:13:08 -0700386 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
387 // Scissor may have been modified, reset dirty clip
388 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800389
Tom Hudson107843d2014-09-08 11:26:26 -0400390 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800391}
392
Romain Guyf6a11b82010-06-23 17:47:49 -0700393///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700394// Debug
395///////////////////////////////////////////////////////////////////////////////
396
Chris Craik62d307c2014-07-29 10:35:13 -0700397void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
398#if DEBUG_DETAILED_EVENTS
399 const int BUFFER_SIZE = 256;
400 va_list ap;
401 char buf[BUFFER_SIZE];
402
403 va_start(ap, fmt);
404 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
405 va_end(ap);
406
407 eventMark(buf);
408#endif
409}
410
411
Romain Guy0f667532013-03-01 14:31:04 -0800412void OpenGLRenderer::eventMark(const char* name) const {
413 mCaches.eventMark(0, name);
414}
415
Romain Guy87e2f7572012-09-24 11:37:12 -0700416void OpenGLRenderer::startMark(const char* name) const {
417 mCaches.startMark(0, name);
418}
419
420void OpenGLRenderer::endMark() const {
421 mCaches.endMark();
422}
423
424void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700425 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700426}
427
428void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400429 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700430 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700431
Chris Craik65fe5ee2015-01-26 18:06:29 -0800432 mRenderState.scissor().setEnabled(true);
433 mRenderState.scissor().set(clip->left,
434 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
435 clip->right - clip->left,
436 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700437
Romain Guy627c6fd2013-08-21 11:53:18 -0700438 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700439 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700440 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
441
442 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700443 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700444 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
445
446 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700447 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700448 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
449
450 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700452 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
453
Romain Guy87e2f7572012-09-24 11:37:12 -0700454 mCaches.stencil.disable();
455 }
456}
457
458///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700459// Layers
460///////////////////////////////////////////////////////////////////////////////
461
462bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700463 if (layer->deferredUpdateScheduled && layer->renderer
464 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700465
Romain Guy7c450aa2012-09-21 19:15:00 -0700466 if (inFrame) {
467 endTiling();
468 debugOverdraw(false, false);
469 }
Romain Guy11cb6422012-09-21 00:39:43 -0700470
Romain Guy96885eb2013-03-26 15:05:58 -0700471 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700472 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700473 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700474 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700475 }
Romain Guy11cb6422012-09-21 00:39:43 -0700476
477 if (inFrame) {
478 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800479 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700480 }
481
Romain Guy5bb3c732012-11-29 17:52:58 -0800482 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700483 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700484
485 return true;
486 }
487
488 return false;
489}
490
491void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700492 // If draw deferring is enabled this method will simply defer
493 // the display list of each individual layer. The layers remain
494 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700495 int count = mLayerUpdates.size();
496 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700497 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
498 startMark("Layer Updates");
499 } else {
500 startMark("Defer Layer Updates");
501 }
Romain Guy11cb6422012-09-21 00:39:43 -0700502
Chris Craik1206b9b2013-04-04 14:46:24 -0700503 // Note: it is very important to update the layers in order
504 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700505 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700506 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700507 }
Romain Guy11cb6422012-09-21 00:39:43 -0700508
Romain Guy96885eb2013-03-26 15:05:58 -0700509 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
510 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400511 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700512 }
513 endMark();
514 }
515}
516
517void OpenGLRenderer::flushLayers() {
518 int count = mLayerUpdates.size();
519 if (count > 0) {
520 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700521
Chris Craik1206b9b2013-04-04 14:46:24 -0700522 // Note: it is very important to update the layers in order
523 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800524 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700525 }
526
527 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400528 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700529
Romain Guy11cb6422012-09-21 00:39:43 -0700530 endMark();
531 }
532}
533
534void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
535 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700536 // Make sure we don't introduce duplicates.
537 // SortedVector would do this automatically but we need to respect
538 // the insertion order. The linear search is not an issue since
539 // this list is usually very short (typically one item, at most a few)
540 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
541 if (mLayerUpdates.itemAt(i) == layer) {
542 return;
543 }
544 }
Romain Guy11cb6422012-09-21 00:39:43 -0700545 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700546 }
547}
548
Romain Guye93482f2013-06-17 13:14:51 -0700549void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
550 if (layer) {
551 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
552 if (mLayerUpdates.itemAt(i) == layer) {
553 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700554 break;
555 }
556 }
557 }
558}
559
Romain Guy40543602013-06-12 15:31:28 -0700560void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800561 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700562 syncState();
563 updateLayers();
564 flushLayers();
565 // Wait for all the layer updates to be executed
566 AutoFence fence;
567}
568
John Reck443a7142014-09-04 17:40:05 -0700569void OpenGLRenderer::markLayersAsBuildLayers() {
570 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
571 mLayerUpdates[i]->wasBuildLayered = true;
572 }
573}
574
Romain Guy11cb6422012-09-21 00:39:43 -0700575///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700576// State management
577///////////////////////////////////////////////////////////////////////////////
578
Chris Craik14e51302013-12-30 15:32:54 -0800579void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700580 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800581 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
582 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700583
Chris Craika64a2be2014-05-14 14:17:01 -0700584 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700585 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700586 }
587
Romain Guy2542d192010-08-18 11:47:12 -0700588 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700589 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700590 }
Romain Guy2542d192010-08-18 11:47:12 -0700591
Romain Guy5ec99242010-11-03 16:19:08 -0700592 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700593 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700594 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700595 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800596 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700597 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700598 }
Romain Guybb9524b2010-06-22 18:56:38 -0700599}
600
Romain Guyf6a11b82010-06-23 17:47:49 -0700601///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700602// Layers
603///////////////////////////////////////////////////////////////////////////////
604
605int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700606 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700607 // force matrix/clip isolation for layer
608 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
609
Tom Hudson984162f2014-10-10 13:38:16 -0400610 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700611
Tom Hudson984162f2014-10-10 13:38:16 -0400612 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700613 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700614 }
Romain Guyd55a8612010-06-28 17:42:46 -0700615
616 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700617}
618
Chris Craikd90144d2013-03-19 15:03:48 -0700619void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
620 const Rect untransformedBounds(bounds);
621
Chris Craikd6b65f62014-01-01 14:45:21 -0800622 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700623
624 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800625 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700626 // We cannot work with sub-pixels in this case
627 bounds.snapToPixelBoundaries();
628
629 // When the layer is not an FBO, we may use glCopyTexImage so we
630 // need to make sure the layer does not extend outside the bounds
631 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700632 const Snapshot& previous = *(currentSnapshot()->previous);
633 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
634 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700635 bounds.setEmpty();
636 } else if (fboLayer) {
637 clip.set(bounds);
638 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800639 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700640 inverse.mapRect(clip);
641 clip.snapToPixelBoundaries();
642 if (clip.intersect(untransformedBounds)) {
643 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
644 bounds.set(untransformedBounds);
645 } else {
646 clip.setEmpty();
647 }
648 }
649 } else {
650 bounds.setEmpty();
651 }
652}
653
Chris Craik408eb122013-03-26 18:55:15 -0700654void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
655 bool fboLayer, int alpha) {
656 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
657 bounds.getHeight() > mCaches.maxTextureSize ||
658 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400659 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700660 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400661 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700662 }
663}
664
Chris Craikd90144d2013-03-19 15:03:48 -0700665int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500666 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400667 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700668
Tom Hudson984162f2014-10-10 13:38:16 -0400669 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700670 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
671 // operations will be able to store and restore the current clip and transform info, and
672 // quick rejection will be correct (for display lists)
673
674 Rect bounds(left, top, right, bottom);
675 Rect clip;
676 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500677 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700678
Tom Hudson984162f2014-10-10 13:38:16 -0400679 if (!mState.currentlyIgnored()) {
680 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
681 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
682 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800683 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700684 }
685 }
686
687 return count;
688}
689
Romain Guy1c740bc2010-09-13 18:00:09 -0700690/**
691 * Layers are viewed by Skia are slightly different than layers in image editing
692 * programs (for instance.) When a layer is created, previously created layers
693 * and the frame buffer still receive every drawing command. For instance, if a
694 * layer is created and a shape intersecting the bounds of the layers and the
695 * framebuffer is draw, the shape will be drawn on both (unless the layer was
696 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
697 *
698 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
699 * texture. Unfortunately, this is inefficient as it requires every primitive to
700 * be drawn n + 1 times, where n is the number of active layers. In practice this
701 * means, for every primitive:
702 * - Switch active frame buffer
703 * - Change viewport, clip and projection matrix
704 * - Issue the drawing
705 *
706 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700707 * To avoid this, layers are implemented in a different way here, at least in the
708 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
709 * is set. When this flag is set we can redirect all drawing operations into a
710 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700711 *
712 * This implementation relies on the frame buffer being at least RGBA 8888. When
713 * a layer is created, only a texture is created, not an FBO. The content of the
714 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700715 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700716 * buffer and drawing continues as normal. This technique therefore treats the
717 * frame buffer as a scratch buffer for the layers.
718 *
719 * To compose the layers back onto the frame buffer, each layer texture
720 * (containing the original frame buffer data) is drawn as a simple quad over
721 * the frame buffer. The trick is that the quad is set as the composition
722 * destination in the blending equation, and the frame buffer becomes the source
723 * of the composition.
724 *
725 * Drawing layers with an alpha value requires an extra step before composition.
726 * An empty quad is drawn over the layer's region in the frame buffer. This quad
727 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
728 * quad is used to multiply the colors in the frame buffer. This is achieved by
729 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
730 * GL_ZERO, GL_SRC_ALPHA.
731 *
732 * Because glCopyTexImage2D() can be slow, an alternative implementation might
733 * be use to draw a single clipped layer. The implementation described above
734 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700735 *
736 * (1) The frame buffer is actually not cleared right away. To allow the GPU
737 * to potentially optimize series of calls to glCopyTexImage2D, the frame
738 * buffer is left untouched until the first drawing operation. Only when
739 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700740 */
Chet Haased48885a2012-08-28 17:43:28 -0700741bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700742 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800743 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
744 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700745
Romain Guyeb993562010-10-05 18:14:38 -0700746 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
747
Romain Guyf607bdc2010-09-10 19:20:06 -0700748 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700749 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700750 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700751 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000752 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700753
754 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400755 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700756 return false;
757 }
Romain Guyf18fd992010-07-08 11:45:51 -0700758
Romain Guya1d3c912011-12-13 14:55:06 -0800759 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700760 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700761 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700762 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700763 }
764
Derek Sollenberger674554f2014-02-19 16:47:32 +0000765 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700766 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700767 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
768 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500769
Chet Haasea23eed82012-04-12 15:19:04 -0700770 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700771 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700772 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700773
Romain Guy8fb95422010-08-17 18:38:51 -0700774 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400775 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
776 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700777
Chris Craika8bea8e2014-09-24 11:29:43 -0700778 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
779 fboLayer ? "" : "unclipped ",
780 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700781 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700782 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700783 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700784 } else {
785 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700786 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800787 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700789 // Workaround for some GL drivers. When reading pixels lying outside
790 // of the window we should get undefined values for those pixels.
791 // Unfortunately some drivers will turn the entire target texture black
792 // when reading outside of the window.
793 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800794 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700795 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800796 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700797
Chris Craika64a2be2014-05-14 14:17:01 -0700798 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
799 bounds.left, getViewportHeight() - bounds.bottom,
800 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700801
Romain Guy54be1cd2011-06-13 19:04:27 -0700802 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800803 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700804 }
Romain Guyeb993562010-10-05 18:14:38 -0700805 }
Romain Guyf86ef572010-07-01 11:05:42 -0700806
Romain Guyd55a8612010-06-28 17:42:46 -0700807 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700808}
809
Chris Craike63f7c622013-10-17 10:30:55 -0700810bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800811 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700812 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700813
Tom Hudson984162f2014-10-10 13:38:16 -0400814 writableSnapshot()->region = &writableSnapshot()->layer->region;
815 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
816 writableSnapshot()->fbo = layer->getFbo();
817 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
818 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
819 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800820 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700821
Romain Guy2b7028e2012-09-19 17:25:38 -0700822 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700823 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700824 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700825 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700827
828 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700829 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700830 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832 }
833
834 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700835 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
henry.uh_chen33f5a592014-07-02 19:36:56 +0800837 // Expand the startTiling region by 1
838 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700839
840 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800841 mRenderState.scissor().setEnabled(true);
842 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700844 glClear(GL_COLOR_BUFFER_BIT);
845
846 dirtyClip();
847
848 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700849 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700850 return true;
851}
852
Romain Guy1c740bc2010-09-13 18:00:09 -0700853/**
854 * Read the documentation of createLayer() before doing anything in this method.
855 */
Chris Craik14e51302013-12-30 15:32:54 -0800856void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
857 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000858 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700859 return;
860 }
861
Chris Craik14e51302013-12-30 15:32:54 -0800862 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800863 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800864 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700865
Chris Craik39a908c2013-06-13 14:39:01 -0700866 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400867 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800868 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800869 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700870
Romain Guyeb993562010-10-05 18:14:38 -0700871 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700872 endTiling();
873
Romain Guye0aa84b2012-04-03 19:30:26 -0700874 // Detach the texture from the FBO
875 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800876
877 layer->removeFbo(false);
878
Romain Guyeb993562010-10-05 18:14:38 -0700879 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700880 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700881 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700882
Chris Craikd6b65f62014-01-01 14:45:21 -0800883 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700884 }
885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500887 SkPaint layerPaint;
888 layerPaint.setAlpha(layer->getAlpha());
889 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
890 layerPaint.setColorFilter(layer->getColorFilter());
891
892 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700893 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700894 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700895 }
896
Romain Guy03750a02010-10-18 14:06:08 -0700897 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700898
Romain Guya1d3c912011-12-13 14:55:06 -0800899 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700900
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 // When the layer is stored in an FBO, we can save a bit of fillrate by
902 // drawing only the dirty region
903 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700905 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700906 } else if (!rect.isEmpty()) {
907 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530908
909 save(0);
910 // the layer contains screen buffer content that shouldn't be alpha modulated
911 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400912 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700913 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530914 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 }
Romain Guy8b55f372010-08-18 17:10:07 -0700916
Romain Guy746b7402010-10-26 16:27:31 -0700917 dirtyClip();
918
Romain Guyeb993562010-10-05 18:14:38 -0700919 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800920 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700921 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800922 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800923 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700924 }
925}
926
Romain Guyaa6c24c2011-04-28 18:40:04 -0700927void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700928 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700929
930 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700931 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700932 setupDrawWithTexture();
933 } else {
934 setupDrawWithExternalTexture();
935 }
936 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500938 setupDrawColorFilter(layer->getColorFilter());
939 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700941 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500942 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
944 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700945 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700947 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800948 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800949 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700950 layer->getWidth() == (uint32_t) rect.getWidth() &&
951 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800952 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
953 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954
Romain Guyd21b6e12011-11-30 20:21:23 -0800955 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800956 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
957 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800959 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800960 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
961 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 }
963 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700964 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700965
966 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700967}
968
Romain Guy5b3b3522010-10-27 18:57:51 -0700969void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700970 if (layer->isTextureLayer()) {
971 EVENT_LOGD("composeTextureLayerRect");
972 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
973 drawTextureLayer(layer, rect);
974 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
975 } else {
976 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700977 const Rect& texCoords = layer->texCoords;
978 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
979 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700980
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 float x = rect.left;
982 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800983 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700984 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700985 layer->getHeight() == (uint32_t) rect.getHeight();
986
987 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700988 // When we're swapping, the layer is already in screen coordinates
989 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800990 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
991 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 }
993
Romain Guyd21b6e12011-11-30 20:21:23 -0800994 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800996 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 }
998
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500999 SkPaint layerPaint;
1000 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1001 layerPaint.setXfermodeMode(layer->getMode());
1002 layerPaint.setColorFilter(layer->getColorFilter());
1003
1004 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001006 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001007 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001008 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001009
Romain Guyaa6c24c2011-04-28 18:40:04 -07001010 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001011 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001012}
1013
Chris Craik34416ea2013-04-15 16:08:28 -07001014/**
1015 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1016 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1017 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1018 * by saveLayer's restore
1019 */
Tom Hudson984162f2014-10-10 13:38:16 -04001020#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1021 DRAW_COMMAND; \
1022 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1023 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1024 DRAW_COMMAND; \
1025 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1026 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001027 }
1028
1029#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1030
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001031// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1032// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1033class LayerShader : public SkShader {
1034public:
1035 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1036 : INHERITED(localMatrix)
1037 , mLayer(layer) {
1038 }
1039
Chris Craike84a2082014-12-22 14:28:49 -08001040 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001041 if (data) {
1042 *data = static_cast<void*>(mLayer);
1043 }
1044 return true;
1045 }
1046
Chris Craike84a2082014-12-22 14:28:49 -08001047 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001048 return !mLayer->isBlend();
1049 }
1050
1051protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001052 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001053 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1054 }
1055
Chris Craike84a2082014-12-22 14:28:49 -08001056 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001057 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1058 }
1059
Chris Craike84a2082014-12-22 14:28:49 -08001060 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001061 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001062 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001063 }
1064private:
1065 // Unowned.
1066 Layer* mLayer;
1067 typedef SkShader INHERITED;
1068};
1069
Romain Guy5b3b3522010-10-27 18:57:51 -07001070void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001071 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1072
1073 if (layer->getConvexMask()) {
1074 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1075
1076 // clip to the area of the layer the mask can be larger
1077 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1078
1079 SkPaint paint;
1080 paint.setAntiAlias(true);
1081 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1082
Chris Craik3f0854292014-04-15 16:18:08 -07001083 // create LayerShader to map SaveLayer content into subsequent draw
1084 SkMatrix shaderMatrix;
1085 shaderMatrix.setTranslate(rect.left, rect.bottom);
1086 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001087 LayerShader layerShader(layer, &shaderMatrix);
1088 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001089
1090 // Since the drawing primitive is defined in local drawing space,
1091 // we don't need to modify the draw matrix
1092 const SkPath* maskPath = layer->getConvexMask();
1093 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1094
Chris Craike84a2082014-12-22 14:28:49 -08001095 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001096 restore();
1097
1098 return;
1099 }
1100
Romain Guy5b3b3522010-10-27 18:57:51 -07001101 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001102 layer->setRegionAsRect();
1103
Chris Craik34416ea2013-04-15 16:08:28 -07001104 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001105
Romain Guy5b3b3522010-10-27 18:57:51 -07001106 layer->region.clear();
1107 return;
1108 }
1109
Chris Craik62d307c2014-07-29 10:35:13 -07001110 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001111 // standard Region based draw
1112 size_t count;
1113 const android::Rect* rects;
1114 Region safeRegion;
1115 if (CC_LIKELY(hasRectToRectTransform())) {
1116 rects = layer->region.getArray(&count);
1117 } else {
1118 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1119 rects = safeRegion.getArray(&count);
1120 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
Chris Craik3f0854292014-04-15 16:18:08 -07001122 const float alpha = getLayerAlpha(layer);
1123 const float texX = 1.0f / float(layer->getWidth());
1124 const float texY = 1.0f / float(layer->getHeight());
1125 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001126
Chris Craik3f0854292014-04-15 16:18:08 -07001127 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001128
Chris Craik3f0854292014-04-15 16:18:08 -07001129 // We must get (and therefore bind) the region mesh buffer
1130 // after we setup drawing in case we need to mess with the
1131 // stencil buffer in setupDraw()
1132 TextureVertex* mesh = mCaches.getRegionMesh();
1133 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001134
Chris Craik3f0854292014-04-15 16:18:08 -07001135 setupDrawWithTexture();
1136 setupDrawColor(alpha, alpha, alpha, alpha);
1137 setupDrawColorFilter(layer->getColorFilter());
1138 setupDrawBlending(layer);
1139 setupDrawProgram();
1140 setupDrawDirtyRegionsDisabled();
1141 setupDrawPureColorUniforms();
1142 setupDrawColorFilterUniforms(layer->getColorFilter());
1143 setupDrawTexture(layer->getTexture());
1144 if (currentTransform()->isPureTranslate()) {
1145 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1146 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001147
Chris Craik3f0854292014-04-15 16:18:08 -07001148 layer->setFilter(GL_NEAREST);
1149 setupDrawModelView(kModelViewMode_Translate, false,
1150 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1151 } else {
1152 layer->setFilter(GL_LINEAR);
1153 setupDrawModelView(kModelViewMode_Translate, false,
1154 rect.left, rect.top, rect.right, rect.bottom);
1155 }
1156 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Chris Craik3f0854292014-04-15 16:18:08 -07001158 for (size_t i = 0; i < count; i++) {
1159 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Chris Craik3f0854292014-04-15 16:18:08 -07001161 const float u1 = r->left * texX;
1162 const float v1 = (height - r->top) * texY;
1163 const float u2 = r->right * texX;
1164 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001165
Chris Craik3f0854292014-04-15 16:18:08 -07001166 // TODO: Reject quads outside of the clip
1167 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1168 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1169 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1170 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001171
Chris Craik3f0854292014-04-15 16:18:08 -07001172 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001173
Chris Craik3f0854292014-04-15 16:18:08 -07001174 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001175 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001176 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001177 numQuads = 0;
1178 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001179 }
Chris Craik3f0854292014-04-15 16:18:08 -07001180 }
1181
1182 if (numQuads > 0) {
1183 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001184 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001185 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001186
Romain Guy5b3b3522010-10-27 18:57:51 -07001187#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001188 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001189#endif
1190
Chris Craik3f0854292014-04-15 16:18:08 -07001191 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001192}
1193
Romain Guy3a3133d2011-02-01 22:59:58 -08001194#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001195void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001196 size_t count;
1197 const android::Rect* rects = region.getArray(&count);
1198
1199 uint32_t colors[] = {
1200 0x7fff0000, 0x7f00ff00,
1201 0x7f0000ff, 0x7fff00ff,
1202 };
1203
1204 int offset = 0;
1205 int32_t top = rects[0].top;
1206
1207 for (size_t i = 0; i < count; i++) {
1208 if (top != rects[i].top) {
1209 offset ^= 0x2;
1210 top = rects[i].top;
1211 }
1212
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001213 SkPaint paint;
1214 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001215 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001216 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001217 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001218}
Chris Craike63f7c622013-10-17 10:30:55 -07001219#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001220
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001221void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001222 Vector<float> rects;
1223
1224 SkRegion::Iterator it(region);
1225 while (!it.done()) {
1226 const SkIRect& r = it.rect();
1227 rects.push(r.fLeft);
1228 rects.push(r.fTop);
1229 rects.push(r.fRight);
1230 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001231 it.next();
1232 }
1233
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001234 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001235}
1236
Romain Guy5b3b3522010-10-27 18:57:51 -07001237void OpenGLRenderer::dirtyLayer(const float left, const float top,
1238 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001239 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 Rect bounds(left, top, right, bottom);
1241 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001242 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001243 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001244}
1245
1246void OpenGLRenderer::dirtyLayer(const float left, const float top,
1247 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001248 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001249 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001250 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001251 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001252}
1253
1254void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001255 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001256 bounds.snapToPixelBoundaries();
1257 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1258 if (!dirty.isEmpty()) {
1259 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001260 }
1261 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001262}
1263
Chris Craik4063a0e2013-11-15 16:06:56 -08001264void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001265 GLsizei elementsCount = quadsCount * 6;
1266 while (elementsCount > 0) {
1267 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1268
Romain Guy3380cfd2013-08-15 16:57:57 -07001269 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001270 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001271
1272 elementsCount -= drawCount;
1273 // Though there are 4 vertices in a quad, we use 6 indices per
1274 // quad to draw with GL_TRIANGLES
1275 mesh += (drawCount / 6) * 4;
1276 }
1277}
1278
Romain Guy54be1cd2011-06-13 19:04:27 -07001279void OpenGLRenderer::clearLayerRegions() {
1280 const size_t count = mLayers.size();
1281 if (count == 0) return;
1282
Tom Hudson984162f2014-10-10 13:38:16 -04001283 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001284 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001285 // Doing several glScissor/glClear here can negatively impact
1286 // GPUs with a tiler architecture, instead we draw quads with
1287 // the Clear blending mode
1288
1289 // The list contains bounds that have already been clipped
1290 // against their initial clip rect, and the current clip
1291 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001292 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001293
Romain Guy448455f2013-07-22 13:57:50 -07001294 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001295 Vertex* vertex = mesh;
1296
1297 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001298 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001299
Chris Craik51d6a3d2014-12-22 17:16:56 -08001300 Vertex::set(vertex++, bounds.left, bounds.top);
1301 Vertex::set(vertex++, bounds.right, bounds.top);
1302 Vertex::set(vertex++, bounds.left, bounds.bottom);
1303 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001304 }
Romain Guye67307c2013-02-11 18:01:20 -08001305 // We must clear the list of dirty rects before we
1306 // call setupDraw() to prevent stencil setup to do
1307 // the same thing again
1308 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001309
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001310 SkPaint clearPaint;
1311 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1312
Romain Guy54be1cd2011-06-13 19:04:27 -07001313 setupDraw(false);
1314 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001315 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001316 setupDrawProgram();
1317 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001318 setupDrawModelView(kModelViewMode_Translate, false,
1319 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001320
Chris Craik4063a0e2013-11-15 16:06:56 -08001321 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001322
Chris Craik65fe5ee2015-01-26 18:06:29 -08001323 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001324 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001325 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001326 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001327}
1328
Romain Guybd6b79b2010-06-26 00:13:53 -07001329///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001330// State Deferral
1331///////////////////////////////////////////////////////////////////////////////
1332
Chris Craikff785832013-03-08 13:12:16 -08001333bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001334 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001335 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001336
Chris Craikff785832013-03-08 13:12:16 -08001337 if (stateDeferFlags & kStateDeferFlag_Draw) {
1338 // state has bounds initialized in local coordinates
1339 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001340 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001341 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001342 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1343 // is used, it should more closely duplicate the quickReject logic (in how it uses
1344 // snapToPixelBoundaries)
1345
Rob Tsuk487a92c2015-01-06 13:22:54 -08001346 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001347 // quick rejected
1348 return true;
1349 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001350
Chris Craika02c4ed2013-06-14 13:43:58 -07001351 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001352 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001353 int& flags = state.mClipSideFlags;
1354 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001355 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1356 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1357 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1358 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001359 }
1360 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001361 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001362 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1363 // overdraw avoidance (since we don't know what it overlaps)
1364 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001365 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001366 }
1367 }
1368
Chris Craik527a3aa2013-03-04 10:19:31 -08001369 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1370 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001371 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001372 }
1373
Chris Craik7273daa2013-03-28 11:25:24 -07001374 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1375 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001376 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001377 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001378 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001379
1380 // always store/restore, since it's just a pointer
1381 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001382 return false;
1383}
1384
Chris Craik527a3aa2013-03-04 10:19:31 -08001385void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001386 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001387 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001388 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001389 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001390
Chris Craik527a3aa2013-03-04 10:19:31 -08001391 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001392 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001393 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001394 dirtyClip();
1395 }
Chris Craikc3566d02013-02-04 16:16:33 -08001396}
1397
Chris Craik28ce94a2013-05-31 11:38:03 -07001398/**
1399 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1400 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1401 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1402 *
1403 * This method should be called when restoreDisplayState() won't be restoring the clip
1404 */
1405void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001406 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001407 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001408 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001409 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001410 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001411 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001412 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1413 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001414}
1415
Chris Craikc3566d02013-02-04 16:16:33 -08001416///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001417// Clipping
1418///////////////////////////////////////////////////////////////////////////////
1419
Romain Guybb9524b2010-06-22 18:56:38 -07001420void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001421 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001422 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001423
Chris Craik65fe5ee2015-01-26 18:06:29 -08001424 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001425 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001426 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001427 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001428}
1429
Romain Guy8ce00302013-01-15 18:51:42 -08001430void OpenGLRenderer::ensureStencilBuffer() {
1431 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1432 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1433 // just hope we have one when hasLayer() returns false.
1434 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001435 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001436 }
1437}
1438
1439void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1440 // The layer's FBO is already bound when we reach this stage
1441 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001442 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1443 // is attached after we initiated tiling. We must turn it off,
1444 // attach the new render buffer then turn tiling back on
1445 endTiling();
1446
Romain Guy8d4aeb72013-02-12 16:08:55 -08001447 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001448 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001449 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001450
Romain Guyf735c8e2013-01-31 17:45:55 -08001451 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001452 }
1453}
1454
Rob Tsuk487a92c2015-01-06 13:22:54 -08001455static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1456 float x, float y) {
1457 Vertex v;
1458 v.x = x;
1459 v.y = y;
1460 transform.mapPoint(v.x, v.y);
1461 rectangleVertices.push_back(v);
1462}
1463
1464static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1465 Vertex v;
1466 v.x = x;
1467 v.y = y;
1468 rectangleVertices.push_back(v);
1469}
1470
1471void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1472 int count = rectangleList.getTransformedRectanglesCount();
1473 std::vector<Vertex> rectangleVertices(count * 4);
1474 Rect scissorBox = rectangleList.calculateBounds();
1475 scissorBox.snapToPixelBoundaries();
1476 for (int i = 0; i < count; ++i) {
1477 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1478 const Matrix4& transform = tr.getTransform();
1479 Rect bounds = tr.getBounds();
1480 if (transform.rectToRect()) {
1481 transform.mapRect(bounds);
1482 if (!bounds.intersect(scissorBox)) {
1483 bounds.setEmpty();
1484 } else {
1485 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1486 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1487 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1488 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1489 }
1490 } else {
1491 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1492 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1493 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1494 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1495 }
1496 }
1497
Chris Craik65fe5ee2015-01-26 18:06:29 -08001498 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001499 scissorBox.getWidth(), scissorBox.getHeight());
1500
1501 const SkPaint* paint = nullptr;
1502 setupDraw();
1503 setupDrawNoTexture();
1504 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1505 setupDrawShader(getShader(paint));
1506 setupDrawColorFilter(getColorFilter(paint));
1507 setupDrawBlending(paint);
1508 setupDrawProgram();
1509 setupDrawDirtyRegionsDisabled();
1510 setupDrawModelView(kModelViewMode_Translate, false,
1511 0.0f, 0.0f, 0.0f, 0.0f, true);
1512 setupDrawColorUniforms(getShader(paint));
1513 setupDrawShaderUniforms(getShader(paint));
1514 setupDrawColorFilterUniforms(getColorFilter(paint));
1515
1516 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1517}
1518
Romain Guy8ce00302013-01-15 18:51:42 -08001519void OpenGLRenderer::setStencilFromClip() {
1520 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001521 if (!currentSnapshot()->clipIsSimple()) {
1522 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001523 EVENT_LOGD("setStencilFromClip - enabling");
1524
Romain Guy8ce00302013-01-15 18:51:42 -08001525 // NOTE: The order here is important, we must set dirtyClip to false
1526 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001527 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001528
1529 ensureStencilBuffer();
1530
Rob Tsuk487a92c2015-01-06 13:22:54 -08001531 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001532
Rob Tsuk487a92c2015-01-06 13:22:54 -08001533 bool isRectangleList = clipArea.isRectangleList();
1534 if (isRectangleList) {
1535 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1536 } else {
1537 incrementThreshold = 0;
1538 }
1539
1540 mCaches.stencil.enableWrite(incrementThreshold);
1541
1542 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001543 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001544 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001545 if (resetScissor) {
1546 // The scissor was not set so we now need to update it
1547 setScissorFromClip();
1548 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001549
Romain Guy8ce00302013-01-15 18:51:42 -08001550 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001551
1552 // stash and disable the outline clip state, since stencil doesn't account for outline
1553 bool storedSkipOutlineClip = mSkipOutlineClip;
1554 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001555
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001556 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001557 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001558 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1559
Rob Tsuk487a92c2015-01-06 13:22:54 -08001560 if (isRectangleList) {
1561 drawRectangleList(clipArea.getRectangleList());
1562 } else {
1563 // NOTE: We could use the region contour path to generate a smaller mesh
1564 // Since we are using the stencil we could use the red book path
1565 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001566
Rob Tsuk487a92c2015-01-06 13:22:54 -08001567 // The last parameter is important: we are not drawing in the color buffer
1568 // so we don't want to dirty the current layer, if any
1569 drawRegionRects(clipArea.getClipRegion(), paint, false);
1570 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001571 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001572 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001573
Rob Tsuk487a92c2015-01-06 13:22:54 -08001574 mCaches.stencil.enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001575
1576 // Draw the region used to generate the stencil if the appropriate debug
1577 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001578 // TODO: Implement for rectangle list clip areas
1579 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1580 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001581 paint.setColor(0x7f0000ff);
1582 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001583 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001584 }
Romain Guy8ce00302013-01-15 18:51:42 -08001585 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001586 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001587 mCaches.stencil.disable();
1588 }
1589 }
1590}
1591
Chris Craikf0a59072013-11-19 18:00:46 -08001592/**
1593 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1594 *
1595 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1596 * style, and tessellated AA ramp
1597 */
1598bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001599 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001600 bool snapOut = paint && paint->isAntiAlias();
1601
1602 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1603 float outset = paint->getStrokeWidth() * 0.5f;
1604 left -= outset;
1605 top -= outset;
1606 right += outset;
1607 bottom += outset;
1608 }
1609
Chris Craikdeeda3d2014-05-05 19:09:33 -07001610 bool clipRequired = false;
1611 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001612 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001613 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001614 return true;
1615 }
1616
Chris Craikf23b25a2014-06-26 15:46:20 -07001617 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001618 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001619 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001620 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001621}
1622
Romain Guy8ce00302013-01-15 18:51:42 -08001623void OpenGLRenderer::debugClip() {
1624#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001625 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001626 SkPaint paint;
1627 paint.setColor(0x7f00ff00);
1628 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1629
Romain Guy8ce00302013-01-15 18:51:42 -08001630 }
1631#endif
1632}
1633
Romain Guyf6a11b82010-06-23 17:47:49 -07001634///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001635// Drawing commands
1636///////////////////////////////////////////////////////////////////////////////
1637
Chris Craik62d307c2014-07-29 10:35:13 -07001638void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001639 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001640 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001641 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001642 // Make sure setScissor & setStencil happen at the beginning of
1643 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001644 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001645 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001646 setScissorFromClip();
1647 }
Chris Craik62d307c2014-07-29 10:35:13 -07001648
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001649 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001651
Romain Guy70ca14e2010-12-13 18:24:33 -08001652 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001653
Romain Guy70ca14e2010-12-13 18:24:33 -08001654 mSetShaderColor = false;
1655 mColorSet = false;
1656 mColorA = mColorR = mColorG = mColorB = 0.0f;
1657 mTextureUnit = 0;
1658 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001659
1660 // Enable debug highlight when what we're about to draw is tested against
1661 // the stencil buffer and if stencil highlight debugging is on
1662 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1663 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1664 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001665}
1666
1667void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1668 mDescription.hasTexture = true;
1669 mDescription.hasAlpha8Texture = isAlpha8;
1670}
1671
Romain Guyff316ec2013-02-13 18:39:43 -08001672void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1673 mDescription.hasTexture = true;
1674 mDescription.hasColors = true;
1675 mDescription.hasAlpha8Texture = isAlpha8;
1676}
1677
Romain Guyaa6c24c2011-04-28 18:40:04 -07001678void OpenGLRenderer::setupDrawWithExternalTexture() {
1679 mDescription.hasExternalTexture = true;
1680}
1681
Romain Guy15bc6432011-12-13 13:11:32 -08001682void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001683 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001684}
1685
Chris Craik91a8c7c2014-08-12 14:31:35 -07001686void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1687 mDescription.hasVertexAlpha = true;
1688 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001689}
1690
Romain Guy8d0d4782010-12-14 20:13:35 -08001691void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1692 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001693 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1694 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1695 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001696 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001697 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001698}
1699
Romain Guy86568192010-12-14 15:55:39 -08001700void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1701 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001702 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1703 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1704 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001705 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001706 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001707}
1708
Romain Guy41210632012-07-16 17:04:24 -07001709void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1710 mCaches.fontRenderer->describe(mDescription, paint);
1711}
1712
Romain Guy70ca14e2010-12-13 18:24:33 -08001713void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1714 mColorA = a;
1715 mColorR = r;
1716 mColorG = g;
1717 mColorB = b;
1718 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001719 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001720}
1721
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001722void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001723 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001724 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001725 }
1726}
1727
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001728void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001729 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001730 return;
1731 }
1732
1733 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001734 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001735 mDescription.colorOp = ProgramDescription::kColorBlend;
1736 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001737 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001738 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001739 }
1740}
1741
Romain Guyf09ef512011-05-27 11:43:46 -07001742void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1743 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1744 mColorA = 1.0f;
1745 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001746 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001747 }
1748}
1749
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001750void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1751 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001752 // When the blending mode is kClear_Mode, we need to use a modulate color
1753 // argb=1,0,0,0
1754 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001755 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001756 bool blend = layer->isBlend()
1757 || getLayerAlpha(layer) < 1.0f
1758 || (mColorSet && mColorA < 1.0f)
1759 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001760 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001761}
1762
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001763void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1764 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001765 // When the blending mode is kClear_Mode, we need to use a modulate color
1766 // argb=1,0,0,0
1767 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001768 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001769 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001770 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001771 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001772}
1773
1774void OpenGLRenderer::setupDrawProgram() {
1775 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001776 if (mDescription.hasRoundRectClip) {
1777 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001778 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001779 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001780 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001781 innerRect.left, innerRect.top,
1782 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001783 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1784 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001785
1786 // add half pixel to round out integer rect space to cover pixel centers
1787 float roundedOutRadius = state->radius + 0.5f;
1788 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1789 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001790 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001791}
1792
1793void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1794 mTrackDirtyRegions = false;
1795}
1796
Chris Craik4063a0e2013-11-15 16:06:56 -08001797void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1798 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001799 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001800 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001801 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001802 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001803
Romain Guy86568192010-12-14 15:55:39 -08001804 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001805 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001806 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1807 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001808 if (dirty && mTrackDirtyRegions) {
1809 if (!ignoreTransform) {
1810 dirtyLayer(left, top, right, bottom, *currentTransform());
1811 } else {
1812 dirtyLayer(left, top, right, bottom);
1813 }
Romain Guy86568192010-12-14 15:55:39 -08001814 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001815}
1816
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001817void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1818 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001819 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1820 }
1821}
1822
Romain Guy86568192010-12-14 15:55:39 -08001823void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001824 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001825 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001826 }
1827}
1828
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001829void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001830 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001831 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001832 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001833
1834 if (ignoreTransform) {
1835 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1836 // because it was built into modelView / the geometry, and the description needs to
1837 // compensate.
1838 mat4 modelViewWithoutTransform;
1839 modelViewWithoutTransform.loadInverse(*currentTransform());
1840 modelViewWithoutTransform.multiply(mModelViewMatrix);
1841 mModelViewMatrix.load(modelViewWithoutTransform);
1842 }
1843
1844 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001845}
1846
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001847void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001848 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001849 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001850 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001851
1852 SkColor color;
1853 SkXfermode::Mode mode;
1854 if (filter->asColorMode(&color, &mode)) {
1855 const int alpha = SkColorGetA(color);
1856 const GLfloat a = alpha / 255.0f;
1857 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1858 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1859 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1860 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1861 return;
1862 }
1863
1864 SkScalar srcColorMatrix[20];
1865 if (filter->asColorMatrix(srcColorMatrix)) {
1866
1867 float colorMatrix[16];
1868 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1869 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1870 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1871 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1872
1873 // Skia uses the range [0..255] for the addition vector, but we need
1874 // the [0..1] range to apply the vector in GLSL
1875 float colorVector[4];
1876 colorVector[0] = srcColorMatrix[4] / 255.0f;
1877 colorVector[1] = srcColorMatrix[9] / 255.0f;
1878 colorVector[2] = srcColorMatrix[14] / 255.0f;
1879 colorVector[3] = srcColorMatrix[19] / 255.0f;
1880
1881 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1882 GL_FALSE, colorMatrix);
1883 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1884 return;
1885 }
1886
1887 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001888}
1889
Romain Guy41210632012-07-16 17:04:24 -07001890void OpenGLRenderer::setupDrawTextGammaUniforms() {
1891 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1892}
1893
Romain Guy70ca14e2010-12-13 18:24:33 -08001894void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001895 bool force = mCaches.bindMeshBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08001896 mCaches.bindPositionVertexPointer(force, nullptr);
Romain Guy15bc6432011-12-13 13:11:32 -08001897 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001898}
1899
1900void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001901 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001902 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001903 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001904}
1905
Romain Guyaa6c24c2011-04-28 18:40:04 -07001906void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1907 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001908 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001909 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001910}
1911
Romain Guy8f0095c2011-05-02 17:24:22 -07001912void OpenGLRenderer::setupDrawTextureTransform() {
1913 mDescription.hasTextureTransform = true;
1914}
1915
1916void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001917 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1918 GL_FALSE, &transform.data[0]);
1919}
1920
Chris Craik564acf72014-01-02 16:46:18 -08001921void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1922 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001923 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001924 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001925 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001926 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001927 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001928 }
Romain Guyd71dd362011-12-12 19:03:35 -08001929
Chris Craikcb4d6002012-09-25 12:00:29 -07001930 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001931 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001932 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001933 }
1934
1935 mCaches.unbindIndicesBuffer();
1936}
1937
Chris Craik564acf72014-01-02 16:46:18 -08001938void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1939 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001940 bool force = mCaches.unbindMeshBuffer();
1941 GLsizei stride = sizeof(ColorTextureVertex);
1942
1943 mCaches.bindPositionVertexPointer(force, vertices, stride);
1944 if (mCaches.currentProgram->texCoords >= 0) {
1945 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1946 }
1947 int slot = mCaches.currentProgram->getAttrib("colors");
1948 if (slot >= 0) {
1949 glEnableVertexAttribArray(slot);
1950 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1951 }
1952
1953 mCaches.unbindIndicesBuffer();
1954}
1955
Chris Craik564acf72014-01-02 16:46:18 -08001956void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1957 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001958 bool force = false;
1959 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1960 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1961 // use the default VBO found in Caches
1962 if (!vertices || vbo) {
1963 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1964 } else {
1965 force = mCaches.unbindMeshBuffer();
1966 }
ztenghui63d41ab2014-02-14 13:13:41 -08001967 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001968
Chris Craikcb4d6002012-09-25 12:00:29 -07001969 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001970 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001971 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001972 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001973}
1974
Romain Guy448455f2013-07-22 13:57:50 -07001975void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001976 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001977 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001978 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001979}
1980
Romain Guy70ca14e2010-12-13 18:24:33 -08001981///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001982// Drawing
1983///////////////////////////////////////////////////////////////////////////////
1984
Tom Hudson107843d2014-09-08 11:26:26 -04001985void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001986 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1987 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001988 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001989 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001990 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001991 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001992 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001993 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001994 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001995 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001996 }
1997
Chris Craikef8d6f22014-12-17 11:10:28 -08001998 // Don't avoid overdraw when visualizing, since that makes it harder to
1999 // debug where it's coming from, and when the problem occurs.
2000 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08002001 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002002 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07002003 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002004
2005 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04002006 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002007
Tom Hudson107843d2014-09-08 11:26:26 -04002008 deferredList.flush(*this, dirty);
2009 } else {
2010 // Even if there is no drawing command(Ex: invisible),
2011 // it still needs startFrame to clear buffer and start tiling.
2012 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08002013 }
2014}
2015
Chris Craike84a2082014-12-22 14:28:49 -08002016void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
2017 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07002018 float x = left;
2019 float y = top;
2020
Romain Guy886b2752013-01-04 12:26:18 -08002021 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2022
Romain Guya168d732011-03-18 16:50:13 -07002023 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08002024 if (currentTransform()->isPureTranslate()) {
2025 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2026 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002027 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002028
2029 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002030 } else {
Chris Craik678625242014-02-28 12:26:34 -08002031 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002032 }
2033
Romain Guy3b748a42013-04-17 18:54:38 -07002034 // No need to check for a UV mapper on the texture object, only ARGB_8888
2035 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002036 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craike84a2082014-12-22 14:28:49 -08002037 paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07002038 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002039}
2040
Romain Guy03c00b52013-06-20 18:30:28 -07002041/**
2042 * Important note: this method is intended to draw batches of bitmaps and
2043 * will not set the scissor enable or dirty the current layer, if any.
2044 * The caller is responsible for properly dirtying the current layer.
2045 */
Tom Hudson107843d2014-09-08 11:26:26 -04002046void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002047 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2048 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002049 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002050 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002051 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002052
Chris Craik527a3aa2013-03-04 10:19:31 -08002053 const AutoTexture autoCleanup(texture);
2054
Chris Craik527a3aa2013-03-04 10:19:31 -08002055 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002056 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002057
2058 const float x = (int) floorf(bounds.left + 0.5f);
2059 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002060 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002061 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002062 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002063 GL_TRIANGLES, bitmapCount * 6, true,
2064 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002065 } else {
2066 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002067 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002068 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2069 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002070 }
2071
Tom Hudson107843d2014-09-08 11:26:26 -04002072 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002073}
2074
Tom Hudson107843d2014-09-08 11:26:26 -04002075void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002076 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002077 return;
Romain Guy6926c722010-07-12 20:20:03 -07002078 }
2079
Romain Guya1d3c912011-12-13 14:55:06 -08002080 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002081 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002082 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002083 const AutoTexture autoCleanup(texture);
2084
Mike Reed1103b322014-07-08 12:36:44 -04002085 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002086 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002087 } else {
Chris Craik79647502014-08-06 13:42:24 -07002088 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002089 }
Chet Haase48659092012-05-31 15:21:51 -07002090
Tom Hudson107843d2014-09-08 11:26:26 -04002091 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002092}
2093
Tom Hudson107843d2014-09-08 11:26:26 -04002094void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002095 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002096 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002097 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002098 }
2099
Chris Craik39a908c2013-06-13 14:39:01 -07002100 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002101 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002102
Romain Guyb18d2d02011-02-10 15:52:54 -08002103 float left = FLT_MAX;
2104 float top = FLT_MAX;
2105 float right = FLT_MIN;
2106 float bottom = FLT_MIN;
2107
Romain Guya92bb4d2012-10-16 11:08:44 -07002108 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002109
Chris Craik51d6a3d2014-12-22 17:16:56 -08002110 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2111 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002112
Chris Craik51d6a3d2014-12-22 17:16:56 -08002113 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002114 if (!colors) {
2115 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002116 tempColors.reset(new int[colorsCount]);
2117 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2118 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002119 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002120
Romain Guy3b748a42013-04-17 18:54:38 -07002121 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002122 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002123 const UvMapper& mapper(getMapper(texture));
2124
Romain Guy5a7b4662011-01-20 19:09:30 -08002125 for (int32_t y = 0; y < meshHeight; y++) {
2126 for (int32_t x = 0; x < meshWidth; x++) {
2127 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2128
2129 float u1 = float(x) / meshWidth;
2130 float u2 = float(x + 1) / meshWidth;
2131 float v1 = float(y) / meshHeight;
2132 float v2 = float(y + 1) / meshHeight;
2133
Romain Guy3b748a42013-04-17 18:54:38 -07002134 mapper.map(u1, v1, u2, v2);
2135
Romain Guy5a7b4662011-01-20 19:09:30 -08002136 int ax = i + (meshWidth + 1) * 2;
2137 int ay = ax + 1;
2138 int bx = i;
2139 int by = bx + 1;
2140 int cx = i + 2;
2141 int cy = cx + 1;
2142 int dx = i + (meshWidth + 1) * 2 + 2;
2143 int dy = dx + 1;
2144
Romain Guyff316ec2013-02-13 18:39:43 -08002145 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2146 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2147 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002148
Romain Guyff316ec2013-02-13 18:39:43 -08002149 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2150 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2151 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002152
Romain Guya92bb4d2012-10-16 11:08:44 -07002153 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2154 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2155 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2156 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002157 }
2158 }
2159
Chris Craikf0a59072013-11-19 18:00:46 -08002160 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002161 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002162 }
2163
Romain Guyff316ec2013-02-13 18:39:43 -08002164 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002165 texture = mCaches.textureCache.get(bitmap);
2166 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002167 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002168 }
Romain Guyff316ec2013-02-13 18:39:43 -08002169 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002170 const AutoTexture autoCleanup(texture);
2171
2172 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002173 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002174
2175 int alpha;
2176 SkXfermode::Mode mode;
2177 getAlphaAndMode(paint, &alpha, &mode);
2178
Romain Guyff316ec2013-02-13 18:39:43 -08002179 float a = alpha / 255.0f;
2180
Romain Guya92bb4d2012-10-16 11:08:44 -07002181 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002182 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002183 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002184
Romain Guyff316ec2013-02-13 18:39:43 -08002185 setupDraw();
2186 setupDrawWithTextureAndColor();
2187 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002188 setupDrawColorFilter(getColorFilter(paint));
2189 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002190 setupDrawProgram();
2191 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002192 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002193 setupDrawTexture(texture->id);
2194 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002195 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002196 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002197
2198 glDrawArrays(GL_TRIANGLES, 0, count);
2199
Romain Guyff316ec2013-02-13 18:39:43 -08002200 int slot = mCaches.currentProgram->getAttrib("colors");
2201 if (slot >= 0) {
2202 glDisableVertexAttribArray(slot);
2203 }
2204
Tom Hudson107843d2014-09-08 11:26:26 -04002205 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002206}
2207
Tom Hudson107843d2014-09-08 11:26:26 -04002208void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002209 float srcLeft, float srcTop, float srcRight, float srcBottom,
2210 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002211 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002212 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002213 return;
Romain Guy6926c722010-07-12 20:20:03 -07002214 }
2215
Romain Guya1d3c912011-12-13 14:55:06 -08002216 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002217 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002218 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002219 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002220
Romain Guy8ba548f2010-06-30 19:21:21 -07002221 const float width = texture->width;
2222 const float height = texture->height;
2223
Romain Guy3b748a42013-04-17 18:54:38 -07002224 float u1 = fmax(0.0f, srcLeft / width);
2225 float v1 = fmax(0.0f, srcTop / height);
2226 float u2 = fmin(1.0f, srcRight / width);
2227 float v2 = fmin(1.0f, srcBottom / height);
2228
2229 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002230
Romain Guy03750a02010-10-18 14:06:08 -07002231 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002232 resetDrawTextureTexCoords(u1, v1, u2, v2);
2233
Romain Guyd21b6e12011-11-30 20:21:23 -08002234 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2235
Romain Guy886b2752013-01-04 12:26:18 -08002236 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2237 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002238
Romain Guy886b2752013-01-04 12:26:18 -08002239 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2240 // Apply a scale transform on the canvas only when a shader is in use
2241 // Skia handles the ratio between the dst and src rects as a scale factor
2242 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002243 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002244 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002245
Chris Craikd6b65f62014-01-01 14:45:21 -08002246 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2247 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2248 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002249
2250 dstRight = x + (dstRight - dstLeft);
2251 dstBottom = y + (dstBottom - dstTop);
2252
2253 dstLeft = x;
2254 dstTop = y;
2255
Chris Craik678625242014-02-28 12:26:34 -08002256 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002257 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002258 } else {
Chris Craik678625242014-02-28 12:26:34 -08002259 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002260 }
2261
2262 if (CC_UNLIKELY(useScaleTransform)) {
2263 save(SkCanvas::kMatrix_SaveFlag);
2264 translate(dstLeft, dstTop);
2265 scale(scaleX, scaleY);
2266
2267 dstLeft = 0.0f;
2268 dstTop = 0.0f;
2269
2270 dstRight = srcRight - srcLeft;
2271 dstBottom = srcBottom - srcTop;
2272 }
2273
Mike Reed1103b322014-07-08 12:36:44 -04002274 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002275 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002276 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002277 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002278 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2279 } else {
2280 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002281 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002282 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002283 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2284 }
2285
2286 if (CC_UNLIKELY(useScaleTransform)) {
2287 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002288 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002289
2290 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002291
Tom Hudson107843d2014-09-08 11:26:26 -04002292 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002293}
2294
Tom Hudson107843d2014-09-08 11:26:26 -04002295void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002296 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002297 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002298 return;
Romain Guy6926c722010-07-12 20:20:03 -07002299 }
2300
John Reckebd52612014-12-10 16:47:36 -08002301 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002302 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2303 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002304
Tom Hudson107843d2014-09-08 11:26:26 -04002305 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002306}
2307
Tom Hudson107843d2014-09-08 11:26:26 -04002308void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002309 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2310 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002311 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002312 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002313 }
2314
Romain Guy211370f2012-02-01 16:10:55 -08002315 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002316 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002317 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002318 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002319 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002320
Romain Guya4adcf02013-02-28 12:15:35 -08002321 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2322 texture->setFilter(GL_LINEAR, true);
2323
Chris Craikd6b65f62014-01-01 14:45:21 -08002324 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002325 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002326 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002327 const float offsetX = left + currentTransform()->getTranslateX();
2328 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002329 const size_t count = mesh->quads.size();
2330 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002331 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002332 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002333 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2334 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2335 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002336 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002337 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002338 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002339 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002340 }
2341 }
2342
Chris Craik4063a0e2013-11-15 16:06:56 -08002343 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002344 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002345 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2346 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002347
Romain Guy3b748a42013-04-17 18:54:38 -07002348 right = x + right - left;
2349 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002350 left = x;
2351 top = y;
2352 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002353 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002354 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2355 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002356 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2357 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002358 }
Chet Haase48659092012-05-31 15:21:51 -07002359
Tom Hudson107843d2014-09-08 11:26:26 -04002360 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002361}
2362
Romain Guy03c00b52013-06-20 18:30:28 -07002363/**
2364 * Important note: this method is intended to draw batches of 9-patch objects and
2365 * will not set the scissor enable or dirty the current layer, if any.
2366 * The caller is responsible for properly dirtying the current layer.
2367 */
Tom Hudson107843d2014-09-08 11:26:26 -04002368void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002369 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002370 mCaches.activeTexture(0);
2371 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002372 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002373 const AutoTexture autoCleanup(texture);
2374
2375 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2376 texture->setFilter(GL_LINEAR, true);
2377
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002378 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2379 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002380 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002381
Tom Hudson107843d2014-09-08 11:26:26 -04002382 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002383}
2384
Tom Hudson107843d2014-09-08 11:26:26 -04002385void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002386 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002387 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002388 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002389 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002390 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002391 }
2392
Chris Craik0d5ac952014-07-15 13:01:02 -07002393 Rect bounds(vertexBuffer.getBounds());
2394 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002395 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2396
Chris Craikcb4d6002012-09-25 12:00:29 -07002397 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002398 bool isAA = paint->isAntiAlias();
2399
Chris Craik710f46d2012-09-17 17:25:49 -07002400 setupDraw();
2401 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002402 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002403 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002404 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002405 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002406 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002407 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002408 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2409 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002410 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002411 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002412 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002413
ztenghui55bfb4e2013-12-03 10:38:55 -08002414 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002415 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002416 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002417 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002418
Chris Craik710f46d2012-09-17 17:25:49 -07002419 int alphaSlot = -1;
2420 if (isAA) {
2421 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2422 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002423 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002424 glEnableVertexAttribArray(alphaSlot);
2425 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002426 }
Romain Guy04299382012-07-18 17:15:41 -07002427
Chris Craik05f3d6e2014-06-02 16:27:04 -07002428 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2429 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002430 mCaches.unbindIndicesBuffer();
2431 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002432 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002433 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002434 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2435 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002436 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002437 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002438 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2439 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002440 } else if (mode == VertexBuffer::kIndices) {
2441 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002442 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2443 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002444 }
Romain Guy04299382012-07-18 17:15:41 -07002445
Chris Craik710f46d2012-09-17 17:25:49 -07002446 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002447 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002448 }
Chris Craik65cd6122012-12-10 17:56:27 -08002449
Tom Hudson107843d2014-09-08 11:26:26 -04002450 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002451}
2452
2453/**
Chris Craik65cd6122012-12-10 17:56:27 -08002454 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2455 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2456 * screen space in all directions. However, instead of using a fragment shader to compute the
2457 * translucency of the color from its position, we simply use a varying parameter to define how far
2458 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2459 *
2460 * Doesn't yet support joins, caps, or path effects.
2461 */
Tom Hudson107843d2014-09-08 11:26:26 -04002462void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002463 VertexBuffer vertexBuffer;
2464 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002465 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002466 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002467}
2468
2469/**
2470 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2471 * and additional geometry for defining an alpha slope perimeter.
2472 *
2473 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2474 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2475 * in-shader alpha region, but found it to be taxing on some GPUs.
2476 *
2477 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2478 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002479 */
Tom Hudson107843d2014-09-08 11:26:26 -04002480void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002481 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002482
Chris Craik65cd6122012-12-10 17:56:27 -08002483 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002484
Chris Craik65cd6122012-12-10 17:56:27 -08002485 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002486 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2487 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002488
Chris Craik05f3d6e2014-06-02 16:27:04 -07002489 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002490 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002491 }
2492
Chris Craikbf759452014-08-11 16:00:44 -07002493 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002494 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002495}
2496
Tom Hudson107843d2014-09-08 11:26:26 -04002497void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002498 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002499
Chris Craik6d29c8d2013-05-08 18:35:44 -07002500 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002501
Chris Craik6d29c8d2013-05-08 18:35:44 -07002502 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002503 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002504
Chris Craik05f3d6e2014-06-02 16:27:04 -07002505 const Rect& bounds = buffer.getBounds();
2506 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002507 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002508 }
2509
Chris Craikbf759452014-08-11 16:00:44 -07002510 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002511 drawVertexBuffer(buffer, paint, displayFlags);
2512
2513 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002514}
2515
Tom Hudson107843d2014-09-08 11:26:26 -04002516void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002517 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002518 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002519
Rob Tsuk487a92c2015-01-06 13:22:54 -08002520 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002521 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002522
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002523 SkPaint paint;
2524 paint.setColor(color);
2525 paint.setXfermodeMode(mode);
2526
2527 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002528
Tom Hudson107843d2014-09-08 11:26:26 -04002529 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002530}
Romain Guy9d5316e2010-06-24 19:30:36 -07002531
Tom Hudson107843d2014-09-08 11:26:26 -04002532void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002533 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002534 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002535 const AutoTexture autoCleanup(texture);
2536
2537 const float x = left + texture->left - texture->offset;
2538 const float y = top + texture->top - texture->offset;
2539
2540 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002541
Tom Hudson107843d2014-09-08 11:26:26 -04002542 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002543}
2544
Tom Hudson107843d2014-09-08 11:26:26 -04002545void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002546 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002547 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002548 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002549 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002550 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002551 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002552
Chris Craike84a2082014-12-22 14:28:49 -08002553 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002554 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002555 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002556 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002557 drawShape(left, top, texture, p);
2558 } else {
2559 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2560 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2561 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002562 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002563}
2564
Tom Hudson107843d2014-09-08 11:26:26 -04002565void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002566 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002567 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002568 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002569 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002570 }
Chris Craike84a2082014-12-22 14:28:49 -08002571 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002572 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002573 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002574 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002575 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002576 SkPath path;
2577 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2578 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2579 } else {
2580 path.addCircle(x, y, radius);
2581 }
2582 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002583 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002584}
Romain Guy01d58e42011-01-19 21:54:02 -08002585
Tom Hudson107843d2014-09-08 11:26:26 -04002586void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002587 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002588 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002589 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002590 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002591 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002592 }
Romain Guy01d58e42011-01-19 21:54:02 -08002593
Chris Craike84a2082014-12-22 14:28:49 -08002594 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002595 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002596 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002597 drawShape(left, top, texture, p);
2598 } else {
2599 SkPath path;
2600 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2601 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2602 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2603 }
2604 path.addOval(rect);
2605 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002606 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002607}
2608
Tom Hudson107843d2014-09-08 11:26:26 -04002609void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002610 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002611 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002612 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002613 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002614 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002615 }
2616
Chris Craik780c1282012-10-04 14:10:49 -07002617 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002618 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002619 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002620 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002621 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002622 drawShape(left, top, texture, p);
2623 return;
Chris Craik780c1282012-10-04 14:10:49 -07002624 }
Chris Craik780c1282012-10-04 14:10:49 -07002625 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2626 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2627 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2628 }
2629
2630 SkPath path;
2631 if (useCenter) {
2632 path.moveTo(rect.centerX(), rect.centerY());
2633 }
2634 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2635 if (useCenter) {
2636 path.close();
2637 }
Tom Hudson107843d2014-09-08 11:26:26 -04002638 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002639}
2640
Romain Guycf8675e2012-10-02 12:32:25 -07002641// See SkPaintDefaults.h
2642#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2643
Tom Hudson107843d2014-09-08 11:26:26 -04002644void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002645 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002646 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002647 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002648 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002649 return;
Romain Guy6926c722010-07-12 20:20:03 -07002650 }
2651
Chris Craik710f46d2012-09-17 17:25:49 -07002652 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002653 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002654 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002655 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2656 mCaches.activeTexture(0);
2657 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002658 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002659 drawShape(left, top, texture, p);
2660 } else {
2661 SkPath path;
2662 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2663 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2664 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2665 }
2666 path.addRect(rect);
2667 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002668 }
Chet Haase858aa932011-05-12 09:06:00 -07002669 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002670 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2671 SkPath path;
2672 path.addRect(left, top, right, bottom);
2673 drawConvexPath(path, p);
2674 } else {
2675 drawColorRect(left, top, right, bottom, p);
2676
2677 mDirty = true;
2678 }
Chet Haase858aa932011-05-12 09:06:00 -07002679 }
Romain Guyc7d53492010-06-25 13:41:57 -07002680}
Romain Guy9d5316e2010-06-24 19:30:36 -07002681
Chris Craikd218a922014-01-02 17:13:34 -08002682void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2683 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002684 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002685 mCaches.activeTexture(0);
2686
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002687 TextShadow textShadow;
2688 if (!getTextShadow(paint, &textShadow)) {
2689 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2690 }
2691
Raph Levien416a8472012-07-19 22:48:17 -07002692 // NOTE: The drop shadow will not perform gamma correction
2693 // if shader-based correction is enabled
2694 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2695 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002696 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002697 // If the drop shadow exceeds the max texture size or couldn't be
2698 // allocated, skip drawing
2699 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002700 const AutoTexture autoCleanup(shadow);
2701
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002702 const float sx = x - shadow->left + textShadow.dx;
2703 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002704
Tom Hudson984162f2014-10-10 13:38:16 -04002705 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002706 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002707 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002708 }
2709
2710 setupDraw();
2711 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002712 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002713 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002714 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002715 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002716 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002717 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2718 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002719 setupDrawTexture(shadow->id);
2720 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002721 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002722 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08002723 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002724
2725 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2726}
2727
Romain Guy768bffc2013-02-27 13:50:45 -08002728bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002729 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002730 return MathUtils::isZero(alpha)
2731 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002732}
2733
Tom Hudson107843d2014-09-08 11:26:26 -04002734void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002735 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002736 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002737 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002738 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002739
Romain Guy671d6cf2012-01-18 12:39:17 -08002740 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002741 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002742 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002743 }
2744
Chris Craik65fe5ee2015-01-26 18:06:29 -08002745 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002746
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 float x = 0.0f;
2748 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002749 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002750 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002751 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2752 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002753 }
2754
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002755 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002756 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002757
2758 int alpha;
2759 SkXfermode::Mode mode;
2760 getAlphaAndMode(paint, &alpha, &mode);
2761
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002762 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002763 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002764 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002765 }
2766
Romain Guy671d6cf2012-01-18 12:39:17 -08002767 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002768 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002769 if (pureTranslate && !linearFilter) {
2770 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2771 }
Romain Guy257ae352013-03-20 16:31:12 -07002772 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002773
Rob Tsuk487a92c2015-01-06 13:22:54 -08002774 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002775 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2776
Romain Guy211370f2012-02-01 16:10:55 -08002777 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002778
Victoria Lease1e546812013-06-25 14:25:17 -07002779 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002780 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002781 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002782 if (hasActiveLayer) {
2783 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002784 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002785 }
2786 dirtyLayerUnchecked(bounds, getRegion());
2787 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002788 }
Chet Haase48659092012-05-31 15:21:51 -07002789
Tom Hudson107843d2014-09-08 11:26:26 -04002790 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002791}
2792
Chris Craik59744b72014-07-01 17:56:52 -07002793bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002794 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002795 outMatrix->setIdentity();
2796 return false;
2797 } else if (CC_UNLIKELY(transform.isPerspective())) {
2798 outMatrix->setIdentity();
2799 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002800 }
Chris Craik59744b72014-07-01 17:56:52 -07002801
2802 /**
Chris Craika736cd92014-08-04 13:18:38 -07002803 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2804 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002805 */
2806 float sx, sy;
2807 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002808 outMatrix->setScale(
2809 roundf(fmaxf(1.0f, sx)),
2810 roundf(fmaxf(1.0f, sy)));
2811 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002812}
2813
Tom Hudson984162f2014-10-10 13:38:16 -04002814int OpenGLRenderer::getSaveCount() const {
2815 return mState.getSaveCount();
2816}
2817
2818int OpenGLRenderer::save(int flags) {
2819 return mState.save(flags);
2820}
2821
2822void OpenGLRenderer::restore() {
2823 return mState.restore();
2824}
2825
2826void OpenGLRenderer::restoreToCount(int saveCount) {
2827 return mState.restoreToCount(saveCount);
2828}
2829
2830void OpenGLRenderer::translate(float dx, float dy, float dz) {
2831 return mState.translate(dx, dy, dz);
2832}
2833
2834void OpenGLRenderer::rotate(float degrees) {
2835 return mState.rotate(degrees);
2836}
2837
2838void OpenGLRenderer::scale(float sx, float sy) {
2839 return mState.scale(sx, sy);
2840}
2841
2842void OpenGLRenderer::skew(float sx, float sy) {
2843 return mState.skew(sx, sy);
2844}
2845
2846void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2847 mState.setMatrix(matrix);
2848}
2849
2850void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2851 mState.concatMatrix(matrix);
2852}
2853
2854bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2855 return mState.clipRect(left, top, right, bottom, op);
2856}
2857
2858bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2859 return mState.clipPath(path, op);
2860}
2861
2862bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2863 return mState.clipRegion(region, op);
2864}
2865
2866void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2867 mState.setClippingOutline(allocator, outline);
2868}
2869
2870void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2871 const Rect& rect, float radius, bool highPriority) {
2872 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2873}
2874
2875
2876
Tom Hudson107843d2014-09-08 11:26:26 -04002877void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002878 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002879 DrawOpMode drawOpMode) {
2880
Chris Craik527a3aa2013-03-04 10:19:31 -08002881 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002882 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2883 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002884 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002885 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002886 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002887 }
Romain Guycac5fd32011-12-01 20:08:50 -08002888 }
2889
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002890 const float oldX = x;
2891 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002892
Chris Craikd6b65f62014-01-01 14:45:21 -08002893 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002894 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002895
Romain Guy211370f2012-02-01 16:10:55 -08002896 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002897 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2898 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002899 }
2900
Romain Guy86568192010-12-14 15:55:39 -08002901 int alpha;
2902 SkXfermode::Mode mode;
2903 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002904
Romain Guyc74f45a2013-02-26 19:10:14 -08002905 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2906
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002907 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002908 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002909 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002910 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002911 }
2912
Romain Guy19d4dd82013-03-04 11:14:26 -08002913 const bool hasActiveLayer = hasLayer();
2914
Romain Guy624234f2013-03-05 16:43:31 -08002915 // We only pass a partial transform to the font renderer. That partial
2916 // matrix defines how glyphs are rasterized. Typically we want glyphs
2917 // to be rasterized at their final size on screen, which means the partial
2918 // matrix needs to take the scale factor into account.
2919 // When a partial matrix is used to transform glyphs during rasterization,
2920 // the mesh is generated with the inverse transform (in the case of scale,
2921 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2922 // apply the full transform matrix at draw time in the vertex shader.
2923 // Applying the full matrix in the shader is the easiest way to handle
2924 // rotation and perspective and allows us to always generated quads in the
2925 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002926 SkMatrix fontTransform;
2927 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2928 || fabs(y - (int) y) > 0.0f
2929 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002930 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002931 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002932
Romain Guy624234f2013-03-05 16:43:31 -08002933 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002934 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002935 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07002936
Raph Levien996e57c2012-07-23 15:22:52 -07002937 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002938 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002939
2940 // don't call issuedrawcommand, do it at end of batch
2941 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002942 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002943 SkPaint paintCopy(*paint);
2944 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2945 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002946 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002947 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002948 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002949 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002950 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002951
Chris Craik527a3aa2013-03-04 10:19:31 -08002952 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002953 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002954 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002955 }
Chris Craik41541822013-05-03 16:35:54 -07002956 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002957 }
Romain Guy694b5192010-07-21 21:33:20 -07002958
Chris Craike63f7c622013-10-17 10:30:55 -07002959 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002960
Tom Hudson107843d2014-09-08 11:26:26 -04002961 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002962}
2963
Tom Hudson107843d2014-09-08 11:26:26 -04002964void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002965 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002966 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002967 return;
Romain Guy03d58522012-02-24 17:54:07 -08002968 }
2969
Chris Craik39a908c2013-06-13 14:39:01 -07002970 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002971 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002972
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002973 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002974 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002975 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002976
2977 int alpha;
2978 SkXfermode::Mode mode;
2979 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002980 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002981
Tom Hudson984162f2014-10-10 13:38:16 -04002982 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002983 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy03d58522012-02-24 17:54:07 -08002984
Romain Guy97771732012-02-28 18:17:02 -08002985 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002986
2987 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002988 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002989 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002990 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002991 dirtyLayerUnchecked(bounds, getRegion());
2992 }
Romain Guy97771732012-02-28 18:17:02 -08002993 }
Chet Haase48659092012-05-31 15:21:51 -07002994
Tom Hudson107843d2014-09-08 11:26:26 -04002995 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002996}
2997
Tom Hudson107843d2014-09-08 11:26:26 -04002998void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002999 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07003000
Romain Guya1d3c912011-12-13 14:55:06 -08003001 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003002
Romain Guyfb8b7632010-08-23 21:05:08 -07003003 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003004 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003005 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003006
Romain Guy8b55f372010-08-18 17:10:07 -07003007 const float x = texture->left - texture->offset;
3008 const float y = texture->top - texture->offset;
3009
Romain Guy01d58e42011-01-19 21:54:02 -08003010 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003011 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003012}
3013
Tom Hudson107843d2014-09-08 11:26:26 -04003014void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003015 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003016 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003017 }
3018
Chris Craike84a2082014-12-22 14:28:49 -08003019 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003020 if (layer->isTextureLayer()) {
3021 transform = &layer->getTransform();
3022 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003023 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003024 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003025 }
3026 }
3027
Chris Craik39a908c2013-06-13 14:39:01 -07003028 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003029 const bool rejected = mState.calculateQuickRejectForScissor(
3030 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3031 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003032
3033 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003034 if (transform && !transform->isIdentity()) {
3035 restore();
3036 }
Tom Hudson107843d2014-09-08 11:26:26 -04003037 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003038 }
3039
Chris Craik62d307c2014-07-29 10:35:13 -07003040 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3041 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3042
Romain Guy5bb3c732012-11-29 17:52:58 -08003043 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003044
Chris Craik65fe5ee2015-01-26 18:06:29 -08003045 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003046 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003047
Romain Guy211370f2012-02-01 16:10:55 -08003048 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003049 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003050 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3051 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003052 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003053
Chris Craik16ecda52013-03-29 10:59:59 -07003054 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003055 setupDraw();
3056 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003057 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003058 setupDrawColorFilter(layer->getColorFilter());
3059 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003060 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003061 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003062 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003063 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003064 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3065 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3066 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003067
Romain Guyd21b6e12011-11-30 20:21:23 -08003068 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003069 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003070 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003071 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003072 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003073 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003074 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3075 }
Romain Guyf219da52011-01-16 12:54:25 -08003076
Romain Guy448455f2013-07-22 13:57:50 -07003077 TextureVertex* mesh = &layer->mesh[0];
3078 GLsizei elementsCount = layer->meshElementCount;
3079
3080 while (elementsCount > 0) {
3081 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3082
Romain Guy3380cfd2013-08-15 16:57:57 -07003083 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003084 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003085 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003086
3087 elementsCount -= drawCount;
3088 // Though there are 4 vertices in a quad, we use 6 indices per
3089 // quad to draw with GL_TRIANGLES
3090 mesh += (drawCount / 6) * 4;
3091 }
Romain Guyf219da52011-01-16 12:54:25 -08003092
Romain Guy3a3133d2011-02-01 22:59:58 -08003093#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003094 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003095#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003096 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003097
Romain Guy5bb3c732012-11-29 17:52:58 -08003098 if (layer->debugDrawUpdate) {
3099 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003100
3101 SkPaint paint;
3102 paint.setColor(0x7f00ff00);
3103 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003104 }
Romain Guyf219da52011-01-16 12:54:25 -08003105 }
Chris Craik34416ea2013-04-15 16:08:28 -07003106 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003107
Romain Guyb2e2f242012-10-17 18:18:35 -07003108 if (transform && !transform->isIdentity()) {
3109 restore();
3110 }
3111
Tom Hudson107843d2014-09-08 11:26:26 -04003112 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003113}
3114
Romain Guy6926c722010-07-12 20:20:03 -07003115///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003116// Draw filters
3117///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003118void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3119 // We should never get here since we apply the draw filter when stashing
3120 // the paints in the DisplayList.
3121 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003122}
3123
3124///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003125// Drawing implementation
3126///////////////////////////////////////////////////////////////////////////////
3127
Chris Craikd218a922014-01-02 17:13:34 -08003128Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003129 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003130 if (!texture) {
3131 return mCaches.textureCache.get(bitmap);
3132 }
3133 return texture;
3134}
3135
Romain Guy01d58e42011-01-19 21:54:02 -08003136void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003137 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003138 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003139 return;
3140 }
3141
3142 int alpha;
3143 SkXfermode::Mode mode;
3144 getAlphaAndMode(paint, &alpha, &mode);
3145
3146 setupDraw();
3147 setupDrawWithTexture(true);
3148 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003149 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003150 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003151 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003152 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003153 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3154 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003155 setupDrawTexture(texture->id);
3156 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003157 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003158 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003159 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003160
3161 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003162}
3163
Romain Guyf607bdc2010-09-10 19:20:06 -07003164// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003165#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3166#define kStdUnderline_Offset (1.0f / 9.0f)
3167#define kStdUnderline_Thickness (1.0f / 18.0f)
3168
Chris Craikd218a922014-01-02 17:13:34 -08003169void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3170 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003171 // Handle underline and strike-through
3172 uint32_t flags = paint->getFlags();
3173 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003174 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003175
Romain Guy211370f2012-02-01 16:10:55 -08003176 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003177 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003178 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003179
Raph Levien8b4072d2012-07-30 15:50:00 -07003180 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003181 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003182
Romain Guyf6834472011-01-23 13:32:12 -08003183 int linesCount = 0;
3184 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3185 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3186
3187 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003188 float points[pointsCount];
3189 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003190
3191 if (flags & SkPaint::kUnderlineText_Flag) {
3192 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003193 points[currentPoint++] = left;
3194 points[currentPoint++] = top;
3195 points[currentPoint++] = left + underlineWidth;
3196 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003197 }
3198
3199 if (flags & SkPaint::kStrikeThruText_Flag) {
3200 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003201 points[currentPoint++] = left;
3202 points[currentPoint++] = top;
3203 points[currentPoint++] = left + underlineWidth;
3204 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003205 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003206
Romain Guy726aeba2011-06-01 14:52:00 -07003207 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003208
Romain Guy726aeba2011-06-01 14:52:00 -07003209 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003210 }
3211 }
3212}
3213
Tom Hudson107843d2014-09-08 11:26:26 -04003214void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003215 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003216 return;
Romain Guy672433d2013-01-04 19:05:13 -08003217 }
3218
Tom Hudson107843d2014-09-08 11:26:26 -04003219 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003220}
3221
Tom Hudson107843d2014-09-08 11:26:26 -04003222void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003223 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003224 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003225
Chris Craik15a07a22014-01-26 13:43:53 -08003226 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003227 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003228
3229 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003230 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003231
ztenghui14a4e352014-08-13 10:44:39 -07003232 // The caller has made sure casterAlpha > 0.
3233 float ambientShadowAlpha = mAmbientShadowAlpha;
3234 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3235 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3236 }
3237 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3238 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003239 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003240 }
ztenghui7b4516e2014-01-07 10:42:55 -08003241
ztenghui14a4e352014-08-13 10:44:39 -07003242 float spotShadowAlpha = mSpotShadowAlpha;
3243 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3244 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3245 }
3246 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3247 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003248 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003249 }
ztenghui7b4516e2014-01-07 10:42:55 -08003250
Tom Hudson107843d2014-09-08 11:26:26 -04003251 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003252}
3253
Tom Hudson107843d2014-09-08 11:26:26 -04003254void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003255 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003256 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003257 return;
Romain Guy3b753822013-03-05 10:27:35 -08003258 }
Romain Guy735738c2012-12-03 12:34:51 -08003259
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003260 int color = paint->getColor();
3261 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003262 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003263 color |= 0x00ffffff;
3264 }
3265
Romain Guy672433d2013-01-04 19:05:13 -08003266 float left = FLT_MAX;
3267 float top = FLT_MAX;
3268 float right = FLT_MIN;
3269 float bottom = FLT_MIN;
3270
Romain Guy448455f2013-07-22 13:57:50 -07003271 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003272 Vertex* vertex = mesh;
3273
Chris Craik2af46352012-11-26 18:30:17 -08003274 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003275 float l = rects[index + 0];
3276 float t = rects[index + 1];
3277 float r = rects[index + 2];
3278 float b = rects[index + 3];
3279
Romain Guy3b753822013-03-05 10:27:35 -08003280 Vertex::set(vertex++, l, t);
3281 Vertex::set(vertex++, r, t);
3282 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003283 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003284
Romain Guy3b753822013-03-05 10:27:35 -08003285 left = fminf(left, l);
3286 top = fminf(top, t);
3287 right = fmaxf(right, r);
3288 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003289 }
3290
Chris Craikf0a59072013-11-19 18:00:46 -08003291 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003292 return;
Romain Guya362c692013-02-04 13:50:16 -08003293 }
Romain Guy672433d2013-01-04 19:05:13 -08003294
Romain Guy672433d2013-01-04 19:05:13 -08003295 setupDraw();
3296 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003297 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003298 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003299 setupDrawColorFilter(getColorFilter(paint));
3300 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003301 setupDrawProgram();
3302 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003303 setupDrawModelView(kModelViewMode_Translate, false,
3304 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003305 setupDrawColorUniforms(getShader(paint));
3306 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003307 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003308
Romain Guy8ce00302013-01-15 18:51:42 -08003309 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003310 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003311 }
3312
Chris Craik4063a0e2013-11-15 16:06:56 -08003313 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003314
Tom Hudson107843d2014-09-08 11:26:26 -04003315 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003316}
3317
Romain Guy026c5e162010-06-28 17:12:22 -07003318void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003319 const SkPaint* paint, bool ignoreTransform) {
3320 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003321 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003322 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003323 color |= 0x00ffffff;
3324 }
3325
Romain Guy70ca14e2010-12-13 18:24:33 -08003326 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003327 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003328 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003329 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003330 setupDrawColorFilter(getColorFilter(paint));
3331 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003332 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003333 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3334 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003335 setupDrawColorUniforms(getShader(paint));
3336 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003337 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003338 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003339
Romain Guyc95c8d62010-09-17 15:31:32 -07003340 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3341}
3342
Romain Guy82ba8142010-07-09 13:25:56 -07003343void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003344 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003345 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003346
Chris Craike84a2082014-12-22 14:28:49 -08003347 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003348 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3349
3350 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003351 vertices = &mMeshVertices[0].x;
3352 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003353
3354 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3355 texture->uvMapper->map(uvs);
3356
3357 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3358 }
3359
Chris Craikd6b65f62014-01-01 14:45:21 -08003360 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3361 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3362 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003363
Romain Guyd21b6e12011-11-30 20:21:23 -08003364 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003365 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003367 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003368 } else {
Chris Craik678625242014-02-28 12:26:34 -08003369 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003370 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003371 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3372 }
3373
3374 if (texture->uvMapper) {
3375 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003376 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003377}
3378
Romain Guyf7f93552010-07-08 19:17:03 -07003379void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003380 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003381 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003382 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3383 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003384
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003385 int a;
3386 SkXfermode::Mode mode;
3387 getAlphaAndMode(paint, &a, &mode);
3388 const float alpha = a / 255.0f;
3389
Romain Guy746b7402010-10-26 16:27:31 -07003390 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003391 setupDrawWithTexture();
3392 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003393 setupDrawColorFilter(getColorFilter(paint));
3394 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003395 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003396 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003397 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003398 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003399 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003400 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003401 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003402
Romain Guy6820ac82010-09-15 18:11:50 -07003403 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003404}
3405
Romain Guy3b748a42013-04-17 18:54:38 -07003406void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003407 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003408 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003409 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3410 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003411
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003412 int a;
3413 SkXfermode::Mode mode;
3414 getAlphaAndMode(paint, &a, &mode);
3415 const float alpha = a / 255.0f;
3416
Romain Guy3b748a42013-04-17 18:54:38 -07003417 setupDraw();
3418 setupDrawWithTexture();
3419 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003420 setupDrawColorFilter(getColorFilter(paint));
3421 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003422 setupDrawProgram();
3423 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003424 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003425 setupDrawTexture(texture);
3426 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003427 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003428 setupDrawMeshIndices(vertices, texCoords, vbo);
3429
Chris Craike84a2082014-12-22 14:28:49 -08003430 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003431}
3432
Romain Guy886b2752013-01-04 12:26:18 -08003433void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003434 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003435 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003436 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003437
Chris Craike84a2082014-12-22 14:28:49 -08003438 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003439 int alpha;
3440 SkXfermode::Mode mode;
3441 getAlphaAndMode(paint, &alpha, &mode);
3442
Romain Guy886b2752013-01-04 12:26:18 -08003443 setupDraw();
3444 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003445 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003446 setupDrawAlpha8Color(color, alpha);
3447 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003448 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003449 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003450 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003451 setupDrawProgram();
3452 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003453 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003454 setupDrawTexture(texture);
3455 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003456 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003457 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003458 setupDrawMesh(vertices, texCoords);
3459
3460 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003461}
3462
Romain Guya5aed0d2010-09-09 14:42:43 -07003463void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003464 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003465
Chris Craike84a2082014-12-22 14:28:49 -08003466 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003467 blend = true;
3468 mDescription.hasRoundRectClip = true;
3469 }
3470 mSkipOutlineClip = true;
3471
Romain Guy82ba8142010-07-09 13:25:56 -07003472 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003473
Romain Guy82ba8142010-07-09 13:25:56 -07003474 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003475 // These blend modes are not supported by OpenGL directly and have
3476 // to be implemented using shaders. Since the shader will perform
3477 // the blending, turn blending off here
3478 // If the blend mode cannot be implemented using shaders, fall
3479 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003480 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003481 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003482 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003483 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003484
Romain Guy82bc7a72012-01-03 14:13:39 -08003485 if (mCaches.blend) {
3486 glDisable(GL_BLEND);
3487 mCaches.blend = false;
3488 }
3489
3490 return;
3491 } else {
3492 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003493 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003494 }
3495
3496 if (!mCaches.blend) {
3497 glEnable(GL_BLEND);
3498 }
3499
3500 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3501 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3502
3503 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3504 glBlendFunc(sourceMode, destMode);
3505 mCaches.lastSrcMode = sourceMode;
3506 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003507 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003508 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003509 glDisable(GL_BLEND);
3510 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003511 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003512}
3513
Romain Guy889f8d12010-07-29 14:37:42 -07003514bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003515 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003516 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003517 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003518 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003519 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003520 }
Romain Guy6926c722010-07-12 20:20:03 -07003521 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003522}
3523
Romain Guy026c5e162010-06-28 17:12:22 -07003524void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003525 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003526 TextureVertex::setUV(v++, u1, v1);
3527 TextureVertex::setUV(v++, u2, v1);
3528 TextureVertex::setUV(v++, u1, v2);
3529 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003530}
3531
Chris Craike84a2082014-12-22 14:28:49 -08003532void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3533 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003534 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003535 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3536 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003537 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003538 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003539 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003540}
3541
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003542float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003543 float alpha;
3544 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3545 alpha = mDrawModifiers.mOverrideLayerAlpha;
3546 } else {
3547 alpha = layer->getAlpha() / 255.0f;
3548 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003549 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003550}
3551
Romain Guy9d5316e2010-06-24 19:30:36 -07003552}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003553}; // namespace android