blob: 76525e0cd9fa14fa58d906644f078f0dc99743c9 [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"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023#include "GammaFontRenderer.h"
24#include "Patch.h"
25#include "PathTessellator.h"
26#include "Properties.h"
27#include "RenderNode.h"
28#include "renderstate/RenderState.h"
29#include "ShadowTessellator.h"
30#include "SkiaShader.h"
31#include "Vector.h"
32#include "VertexBuffer.h"
33#include "utils/GLUtils.h"
34#include "utils/PaintUtils.h"
35#include "utils/TraceUtils.h"
36
Romain Guye4d01122010-06-16 18:44:05 -070037#include <stdlib.h>
38#include <stdint.h>
39#include <sys/types.h>
40
Romain Guy5cbbce52010-06-27 22:59:20 -070041#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070042#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040043#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070044#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070045
Romain Guye4d01122010-06-16 18:44:05 -070046#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070047#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070048
Romain Guy08aa2cb2011-03-17 11:06:57 -070049#include <private/hwui/DrawGlInfo.h>
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051#include <ui/Rect.h>
52
Chris Craik62d307c2014-07-29 10:35:13 -070053#if DEBUG_DETAILED_EVENTS
54 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
55#else
56 #define EVENT_LOGD(...)
57#endif
58
Romain Guye4d01122010-06-16 18:44:05 -070059namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070060namespace uirenderer {
61
Chris Craik678625242014-02-28 12:26:34 -080062static GLenum getFilter(const SkPaint* paint) {
63 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
64 return GL_LINEAR;
65 }
66 return GL_NEAREST;
67}
Romain Guyd21b6e12011-11-30 20:21:23 -080068
Romain Guy9d5316e2010-06-24 19:30:36 -070069///////////////////////////////////////////////////////////////////////////////
70// Globals
71///////////////////////////////////////////////////////////////////////////////
72
Romain Guy889f8d12010-07-29 14:37:42 -070073/**
74 * Structure mapping Skia xfermodes to OpenGL blending factors.
75 */
76struct Blender {
77 SkXfermode::Mode mode;
78 GLenum src;
79 GLenum dst;
80}; // struct Blender
81
Romain Guy026c5e162010-06-28 17:12:22 -070082// In this array, the index of each Blender equals the value of the first
83// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
84static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
88 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
90 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
92 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
96 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050098 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070099 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700100};
Romain Guye4d01122010-06-16 18:44:05 -0700101
Romain Guy87a76572010-09-13 18:11:21 -0700102// This array contains the swapped version of each SkXfermode. For instance
103// this array's SrcOver blending mode is actually DstOver. You can refer to
104// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700105static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
107 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
108 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
109 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
110 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
111 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
112 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
113 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
114 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
115 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
116 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
117 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
118 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500119 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700120 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700121};
122
Romain Guyf6a11b82010-06-23 17:47:49 -0700123///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700124// Functions
125///////////////////////////////////////////////////////////////////////////////
126
127template<typename T>
128static inline T min(T a, T b) {
129 return a < b ? a : b;
130}
131
132///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700133// Constructors/destructor
134///////////////////////////////////////////////////////////////////////////////
135
John Reck3b202512014-06-23 13:13:08 -0700136OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400137 : mState(*this)
Chris Craik058fc642014-07-23 18:19:28 -0700138 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700139 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700140 , mRenderState(renderState)
Chris Craik65fe5ee2015-01-26 18:06:29 -0800141 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700142 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700143 , mSuppressTiling(false)
144 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400145 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700146 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700147 , mLightRadius(FLT_MIN)
148 , mAmbientShadowAlpha(0)
149 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800150 // *set* draw modifiers to be 0
151 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700152 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700153
Chris Craik96a5c4c2015-01-27 15:46:35 -0800154 memcpy(mMeshVertices, kMeshVertices, sizeof(kMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700155}
156
Romain Guy85bf02f2010-06-22 13:11:24 -0700157OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700158 // The context has already been destroyed at this point, do not call
159 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700160}
161
Romain Guy87e2f7572012-09-24 11:37:12 -0700162void OpenGLRenderer::initProperties() {
163 char property[PROPERTY_VALUE_MAX];
164 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
165 mScissorOptimizationDisabled = !strcasecmp(property, "true");
166 INIT_LOGD(" Scissor optimization %s",
167 mScissorOptimizationDisabled ? "disabled" : "enabled");
168 } else {
169 INIT_LOGD(" Scissor optimization enabled");
170 }
Romain Guy13631f32012-01-30 17:41:55 -0800171}
172
Chris Craik058fc642014-07-23 18:19:28 -0700173void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
174 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
175 mLightCenter = lightCenter;
176 mLightRadius = lightRadius;
177 mAmbientShadowAlpha = ambientShadowAlpha;
178 mSpotShadowAlpha = spotShadowAlpha;
179}
180
Romain Guy13631f32012-01-30 17:41:55 -0800181///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700182// Setup
183///////////////////////////////////////////////////////////////////////////////
184
Chris Craik797b95b2014-05-20 18:10:25 -0700185void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700186 glDisable(GL_DITHER);
187 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
188
189 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700190 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700191}
192
Romain Guy96885eb2013-03-26 15:05:58 -0700193void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800194 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800195 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400196 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700197 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700198 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700199}
200
Tom Hudson107843d2014-09-08 11:26:26 -0400201void OpenGLRenderer::startFrame() {
202 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700203 mFrameStarted = true;
204
Tom Hudson984162f2014-10-10 13:38:16 -0400205 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700206
Romain Guy96885eb2013-03-26 15:05:58 -0700207 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700208
Tom Hudson984162f2014-10-10 13:38:16 -0400209 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800210
Romain Guy54c1a642012-09-27 17:55:46 -0700211 // Functors break the tiling extension in pretty spectacular ways
212 // This ensures we don't use tiling when a functor is going to be
213 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700214 mSuppressTiling = mCaches.hasRegisteredFunctors()
215 || mFirstFrameAfterResize;
216 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700217
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700219
Romain Guy7c450aa2012-09-21 19:15:00 -0700220 debugOverdraw(true, true);
221
Tom Hudson107843d2014-09-08 11:26:26 -0400222 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700223 mTilingClip.right, mTilingClip.bottom, mOpaque);
224}
225
Tom Hudson107843d2014-09-08 11:26:26 -0400226void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700227 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700228
Romain Guy96885eb2013-03-26 15:05:58 -0700229 setupFrameState(left, top, right, bottom, opaque);
230
231 // Layer renderers will start the frame immediately
232 // The framebuffer renderer will first defer the display list
233 // for each layer and wait until the first drawing command
234 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800235 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700236 syncState();
237 updateLayers();
238 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400239 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700240 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400248 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
249 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Tom Hudson107843d2014-09-08 11:26:26 -0400257void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700258 if (!opaque) {
Chris Craik65fe5ee2015-01-26 18:06:29 -0800259 mRenderState.scissor().setEnabled(true);
260 mRenderState.scissor().set(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400262 mDirty = true;
263 return;
Romain Guyddf74372012-05-22 14:07:07 -0700264 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700265
Chris Craik65fe5ee2015-01-26 18:06:29 -0800266 mRenderState.scissor().reset();
Romain Guyddf74372012-05-22 14:07:07 -0700267}
268
269void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700270 if (mCaches.blend) {
271 glEnable(GL_BLEND);
272 } else {
273 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
henry.uh_chen33f5a592014-07-02 19:36:56 +0800277void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700278 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800279 const Snapshot* snapshot = currentSnapshot();
280
Chris Craik14e51302013-12-30 15:32:54 -0800281 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800282 if (snapshot->flags & Snapshot::kFlagFboTarget) {
283 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700284 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700285
henry.uh_chen33f5a592014-07-02 19:36:56 +0800286 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800287 }
288}
289
henry.uh_chen33f5a592014-07-02 19:36:56 +0800290void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800291 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800292 if(expand) {
293 // Expand the startTiling region by 1
294 int leftNotZero = (clip.left > 0) ? 1 : 0;
295 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
296
297 mCaches.startTiling(
298 clip.left - leftNotZero,
299 windowHeight - clip.bottom - topNotZero,
300 clip.right - clip.left + leftNotZero + 1,
301 clip.bottom - clip.top + topNotZero + 1,
302 opaque);
303 } else {
304 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800305 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800306 }
Romain Guy54c1a642012-09-27 17:55:46 -0700307 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700308}
309
310void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700311 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700312}
313
Tom Hudson107843d2014-09-08 11:26:26 -0400314bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700315 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700316 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800317 mTempPaths.clear();
318
Romain Guyca89e2a2013-03-08 17:44:20 -0800319 // When finish() is invoked on FBO 0 we've reached the end
320 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400321 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800322 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700323 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800324 }
325
Romain Guy11cb6422012-09-21 00:39:43 -0700326 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700327#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700328 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700329#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700330
Romain Guyc15008e2010-11-10 11:59:15 -0800331#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800332 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700333#else
334 if (mCaches.getDebugLevel() & kDebugMemory) {
335 mCaches.dumpMemoryUsage();
336 }
Romain Guyc15008e2010-11-10 11:59:15 -0800337#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700338 }
Romain Guy96885eb2013-03-26 15:05:58 -0700339
340 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400341
342 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700343}
344
Romain Guy35643dd2012-09-18 15:40:58 -0700345void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700346 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
347 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700348 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700349
Chris Craik65fe5ee2015-01-26 18:06:29 -0800350 mRenderState.scissor().reset();
Romain Guy35643dd2012-09-18 15:40:58 -0700351 dirtyClip();
352}
353
Andreas Gampe64bb4132014-11-22 00:35:09 +0000354void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400355 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700356
Rob Tsuk487a92c2015-01-06 13:22:54 -0800357 Rect clip(mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700358 clip.snapToPixelBoundaries();
359
Romain Guyd643bb52011-03-01 14:55:21 -0800360 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800361 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800362 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800363 dirtyLayerUnchecked(clip, getRegion());
364 }
Romain Guyd643bb52011-03-01 14:55:21 -0800365
Romain Guy08aa2cb2011-03-17 11:06:57 -0700366 DrawGlInfo info;
367 info.clipLeft = clip.left;
368 info.clipTop = clip.top;
369 info.clipRight = clip.right;
370 info.clipBottom = clip.bottom;
371 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700372 info.width = getViewportWidth();
373 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800374 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700375
Tom Hudson984162f2014-10-10 13:38:16 -0400376 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700377 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400378 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700379 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
380 }
Chris Craik65fe5ee2015-01-26 18:06:29 -0800381 if (mRenderState.scissor().setEnabled(true) || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700382 setScissorFromClip();
383 }
Chris Craik54f574a2013-08-26 11:23:46 -0700384
John Reck3b202512014-06-23 13:13:08 -0700385 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
386 // Scissor may have been modified, reset dirty clip
387 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800388
Tom Hudson107843d2014-09-08 11:26:26 -0400389 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800390}
391
Romain Guyf6a11b82010-06-23 17:47:49 -0700392///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700393// Debug
394///////////////////////////////////////////////////////////////////////////////
395
Chris Craik62d307c2014-07-29 10:35:13 -0700396void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
397#if DEBUG_DETAILED_EVENTS
398 const int BUFFER_SIZE = 256;
399 va_list ap;
400 char buf[BUFFER_SIZE];
401
402 va_start(ap, fmt);
403 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
404 va_end(ap);
405
406 eventMark(buf);
407#endif
408}
409
410
Romain Guy0f667532013-03-01 14:31:04 -0800411void OpenGLRenderer::eventMark(const char* name) const {
412 mCaches.eventMark(0, name);
413}
414
Romain Guy87e2f7572012-09-24 11:37:12 -0700415void OpenGLRenderer::startMark(const char* name) const {
416 mCaches.startMark(0, name);
417}
418
419void OpenGLRenderer::endMark() const {
420 mCaches.endMark();
421}
422
423void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700424 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700425}
426
427void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400428 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700429 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700430
Chris Craik65fe5ee2015-01-26 18:06:29 -0800431 mRenderState.scissor().setEnabled(true);
432 mRenderState.scissor().set(clip->left,
433 mState.firstSnapshot()->getViewportHeight() - clip->bottom,
434 clip->right - clip->left,
435 clip->bottom - clip->top);
Romain Guy87e2f7572012-09-24 11:37:12 -0700436
Romain Guy627c6fd2013-08-21 11:53:18 -0700437 // 1x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800438 mRenderState.stencil().enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700439 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
440
441 // 2x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800442 mRenderState.stencil().enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700443 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
444
445 // 3x overdraw
Chris Craik96a5c4c2015-01-27 15:46:35 -0800446 mRenderState.stencil().enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700447 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
448
449 // 4x overdraw and higher
Chris Craik96a5c4c2015-01-27 15:46:35 -0800450 mRenderState.stencil().enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700451 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
452
Chris Craik96a5c4c2015-01-27 15:46:35 -0800453 mRenderState.stencil().disable();
Romain Guy87e2f7572012-09-24 11:37:12 -0700454 }
455}
456
457///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700458// Layers
459///////////////////////////////////////////////////////////////////////////////
460
461bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700462 if (layer->deferredUpdateScheduled && layer->renderer
463 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700464
Romain Guy7c450aa2012-09-21 19:15:00 -0700465 if (inFrame) {
466 endTiling();
467 debugOverdraw(false, false);
468 }
Romain Guy11cb6422012-09-21 00:39:43 -0700469
Romain Guy96885eb2013-03-26 15:05:58 -0700470 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700471 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700472 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700473 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700474 }
Romain Guy11cb6422012-09-21 00:39:43 -0700475
476 if (inFrame) {
477 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800478 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700479 }
480
Romain Guy5bb3c732012-11-29 17:52:58 -0800481 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700482 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700483
484 return true;
485 }
486
487 return false;
488}
489
490void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700491 // If draw deferring is enabled this method will simply defer
492 // the display list of each individual layer. The layers remain
493 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700494 int count = mLayerUpdates.size();
495 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700496 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
497 startMark("Layer Updates");
498 } else {
499 startMark("Defer Layer Updates");
500 }
Romain Guy11cb6422012-09-21 00:39:43 -0700501
Chris Craik1206b9b2013-04-04 14:46:24 -0700502 // Note: it is very important to update the layers in order
503 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700504 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700505 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700506 }
Romain Guy11cb6422012-09-21 00:39:43 -0700507
Romain Guy96885eb2013-03-26 15:05:58 -0700508 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
509 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400510 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700511 }
512 endMark();
513 }
514}
515
516void OpenGLRenderer::flushLayers() {
517 int count = mLayerUpdates.size();
518 if (count > 0) {
519 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700520
Chris Craik1206b9b2013-04-04 14:46:24 -0700521 // Note: it is very important to update the layers in order
522 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800523 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700524 }
525
526 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400527 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700528
Romain Guy11cb6422012-09-21 00:39:43 -0700529 endMark();
530 }
531}
532
533void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
534 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700535 // Make sure we don't introduce duplicates.
536 // SortedVector would do this automatically but we need to respect
537 // the insertion order. The linear search is not an issue since
538 // this list is usually very short (typically one item, at most a few)
539 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
540 if (mLayerUpdates.itemAt(i) == layer) {
541 return;
542 }
543 }
Romain Guy11cb6422012-09-21 00:39:43 -0700544 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700545 }
546}
547
Romain Guye93482f2013-06-17 13:14:51 -0700548void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
549 if (layer) {
550 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
551 if (mLayerUpdates.itemAt(i) == layer) {
552 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700553 break;
554 }
555 }
556 }
557}
558
Romain Guy40543602013-06-12 15:31:28 -0700559void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800560 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700561 syncState();
562 updateLayers();
563 flushLayers();
564 // Wait for all the layer updates to be executed
John Reck55156372015-01-21 07:46:37 -0800565 glFinish();
Romain Guy40543602013-06-12 15:31:28 -0700566}
567
John Reck443a7142014-09-04 17:40:05 -0700568void OpenGLRenderer::markLayersAsBuildLayers() {
569 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
570 mLayerUpdates[i]->wasBuildLayered = true;
571 }
572}
573
Romain Guy11cb6422012-09-21 00:39:43 -0700574///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700575// State management
576///////////////////////////////////////////////////////////////////////////////
577
Chris Craik14e51302013-12-30 15:32:54 -0800578void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700579 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800580 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
581 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700582
Chris Craika64a2be2014-05-14 14:17:01 -0700583 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700584 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700585 }
586
Romain Guy2542d192010-08-18 11:47:12 -0700587 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700588 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700589 }
Romain Guy2542d192010-08-18 11:47:12 -0700590
Romain Guy5ec99242010-11-03 16:19:08 -0700591 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700592 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700593 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700594 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800595 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700596 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700597 }
Romain Guybb9524b2010-06-22 18:56:38 -0700598}
599
Romain Guyf6a11b82010-06-23 17:47:49 -0700600///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700601// Layers
602///////////////////////////////////////////////////////////////////////////////
603
604int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700605 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700606 // force matrix/clip isolation for layer
607 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
608
Tom Hudson984162f2014-10-10 13:38:16 -0400609 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700610
Tom Hudson984162f2014-10-10 13:38:16 -0400611 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700612 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700613 }
Romain Guyd55a8612010-06-28 17:42:46 -0700614
615 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700616}
617
Chris Craikd90144d2013-03-19 15:03:48 -0700618void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
619 const Rect untransformedBounds(bounds);
620
Chris Craikd6b65f62014-01-01 14:45:21 -0800621 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700622
623 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800624 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700625 // We cannot work with sub-pixels in this case
626 bounds.snapToPixelBoundaries();
627
628 // When the layer is not an FBO, we may use glCopyTexImage so we
629 // need to make sure the layer does not extend outside the bounds
630 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700631 const Snapshot& previous = *(currentSnapshot()->previous);
632 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
633 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700634 bounds.setEmpty();
635 } else if (fboLayer) {
636 clip.set(bounds);
637 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800638 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700639 inverse.mapRect(clip);
640 clip.snapToPixelBoundaries();
641 if (clip.intersect(untransformedBounds)) {
642 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
643 bounds.set(untransformedBounds);
644 } else {
645 clip.setEmpty();
646 }
647 }
648 } else {
649 bounds.setEmpty();
650 }
651}
652
Chris Craik408eb122013-03-26 18:55:15 -0700653void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
654 bool fboLayer, int alpha) {
655 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
656 bounds.getHeight() > mCaches.maxTextureSize ||
657 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400658 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700659 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400660 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700661 }
662}
663
Chris Craikd90144d2013-03-19 15:03:48 -0700664int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500665 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400666 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700667
Tom Hudson984162f2014-10-10 13:38:16 -0400668 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700669 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
670 // operations will be able to store and restore the current clip and transform info, and
671 // quick rejection will be correct (for display lists)
672
673 Rect bounds(left, top, right, bottom);
674 Rect clip;
675 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500676 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700677
Tom Hudson984162f2014-10-10 13:38:16 -0400678 if (!mState.currentlyIgnored()) {
679 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
680 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
681 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800682 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700683 }
684 }
685
686 return count;
687}
688
Romain Guy1c740bc2010-09-13 18:00:09 -0700689/**
690 * Layers are viewed by Skia are slightly different than layers in image editing
691 * programs (for instance.) When a layer is created, previously created layers
692 * and the frame buffer still receive every drawing command. For instance, if a
693 * layer is created and a shape intersecting the bounds of the layers and the
694 * framebuffer is draw, the shape will be drawn on both (unless the layer was
695 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
696 *
697 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
698 * texture. Unfortunately, this is inefficient as it requires every primitive to
699 * be drawn n + 1 times, where n is the number of active layers. In practice this
700 * means, for every primitive:
701 * - Switch active frame buffer
702 * - Change viewport, clip and projection matrix
703 * - Issue the drawing
704 *
705 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700706 * To avoid this, layers are implemented in a different way here, at least in the
707 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
708 * is set. When this flag is set we can redirect all drawing operations into a
709 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700710 *
711 * This implementation relies on the frame buffer being at least RGBA 8888. When
712 * a layer is created, only a texture is created, not an FBO. The content of the
713 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700714 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700715 * buffer and drawing continues as normal. This technique therefore treats the
716 * frame buffer as a scratch buffer for the layers.
717 *
718 * To compose the layers back onto the frame buffer, each layer texture
719 * (containing the original frame buffer data) is drawn as a simple quad over
720 * the frame buffer. The trick is that the quad is set as the composition
721 * destination in the blending equation, and the frame buffer becomes the source
722 * of the composition.
723 *
724 * Drawing layers with an alpha value requires an extra step before composition.
725 * An empty quad is drawn over the layer's region in the frame buffer. This quad
726 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
727 * quad is used to multiply the colors in the frame buffer. This is achieved by
728 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
729 * GL_ZERO, GL_SRC_ALPHA.
730 *
731 * Because glCopyTexImage2D() can be slow, an alternative implementation might
732 * be use to draw a single clipped layer. The implementation described above
733 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700734 *
735 * (1) The frame buffer is actually not cleared right away. To allow the GPU
736 * to potentially optimize series of calls to glCopyTexImage2D, the frame
737 * buffer is left untouched until the first drawing operation. Only when
738 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700739 */
Chet Haased48885a2012-08-28 17:43:28 -0700740bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700741 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800742 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
743 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700744
Romain Guyeb993562010-10-05 18:14:38 -0700745 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
746
Romain Guyf607bdc2010-09-10 19:20:06 -0700747 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700748 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700749 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700750 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000751 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700752
753 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400754 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700755 return false;
756 }
Romain Guyf18fd992010-07-08 11:45:51 -0700757
Romain Guya1d3c912011-12-13 14:55:06 -0800758 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700759 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700760 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700761 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700762 }
763
Derek Sollenberger674554f2014-02-19 16:47:32 +0000764 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700765 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700766 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
767 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500768
Chet Haasea23eed82012-04-12 15:19:04 -0700769 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700770 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700771 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700772
Romain Guy8fb95422010-08-17 18:38:51 -0700773 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400774 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
775 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700776
Chris Craika8bea8e2014-09-24 11:29:43 -0700777 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
778 fboLayer ? "" : "unclipped ",
779 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700780 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700781 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700782 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700783 } else {
784 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800786 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700787 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700788 // Workaround for some GL drivers. When reading pixels lying outside
789 // of the window we should get undefined values for those pixels.
790 // Unfortunately some drivers will turn the entire target texture black
791 // when reading outside of the window.
792 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800793 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700794 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800795 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700796
Chris Craika64a2be2014-05-14 14:17:01 -0700797 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
798 bounds.left, getViewportHeight() - bounds.bottom,
799 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700800
Romain Guy54be1cd2011-06-13 19:04:27 -0700801 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800802 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700803 }
Romain Guyeb993562010-10-05 18:14:38 -0700804 }
Romain Guyf86ef572010-07-01 11:05:42 -0700805
Romain Guyd55a8612010-06-28 17:42:46 -0700806 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700807}
808
Chris Craike63f7c622013-10-17 10:30:55 -0700809bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800810 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700811 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700812
Tom Hudson984162f2014-10-10 13:38:16 -0400813 writableSnapshot()->region = &writableSnapshot()->layer->region;
814 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
815 writableSnapshot()->fbo = layer->getFbo();
816 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
817 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
818 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800819 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700820
Romain Guy2b7028e2012-09-19 17:25:38 -0700821 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700822 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700823 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700824 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700825 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700826
827 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700829 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700830 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700831 }
832
833 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
henry.uh_chen33f5a592014-07-02 19:36:56 +0800836 // Expand the startTiling region by 1
837 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700838
839 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Chris Craik65fe5ee2015-01-26 18:06:29 -0800840 mRenderState.scissor().setEnabled(true);
841 mRenderState.scissor().set(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700842 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700843 glClear(GL_COLOR_BUFFER_BIT);
844
845 dirtyClip();
846
847 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700848 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700849 return true;
850}
851
Romain Guy1c740bc2010-09-13 18:00:09 -0700852/**
853 * Read the documentation of createLayer() before doing anything in this method.
854 */
Chris Craik14e51302013-12-30 15:32:54 -0800855void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
856 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000857 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700858 return;
859 }
860
Chris Craik14e51302013-12-30 15:32:54 -0800861 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800862 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800863 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700864
Chris Craik39a908c2013-06-13 14:39:01 -0700865 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400866 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800867 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik65fe5ee2015-01-26 18:06:29 -0800868 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craik39a908c2013-06-13 14:39:01 -0700869
Romain Guyeb993562010-10-05 18:14:38 -0700870 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700871 endTiling();
872
Romain Guye0aa84b2012-04-03 19:30:26 -0700873 // Detach the texture from the FBO
874 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800875
876 layer->removeFbo(false);
877
Romain Guyeb993562010-10-05 18:14:38 -0700878 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700879 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700880 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700881
Chris Craikd6b65f62014-01-01 14:45:21 -0800882 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700883 }
884
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500886 SkPaint layerPaint;
887 layerPaint.setAlpha(layer->getAlpha());
888 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
889 layerPaint.setColorFilter(layer->getColorFilter());
890
891 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700892 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700893 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700894 }
895
Chris Craik96a5c4c2015-01-27 15:46:35 -0800896 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700897
Romain Guya1d3c912011-12-13 14:55:06 -0800898 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700899
Romain Guy5b3b3522010-10-27 18:57:51 -0700900 // When the layer is stored in an FBO, we can save a bit of fillrate by
901 // drawing only the dirty region
902 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800903 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700904 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 } else if (!rect.isEmpty()) {
906 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530907
908 save(0);
909 // the layer contains screen buffer content that shouldn't be alpha modulated
910 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400911 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700912 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530913 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700914 }
Romain Guy8b55f372010-08-18 17:10:07 -0700915
Romain Guy746b7402010-10-26 16:27:31 -0700916 dirtyClip();
917
Romain Guyeb993562010-10-05 18:14:38 -0700918 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800919 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700920 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800921 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800922 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700923 }
924}
925
Romain Guyaa6c24c2011-04-28 18:40:04 -0700926void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700927 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928
929 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700931 setupDrawWithTexture();
932 } else {
933 setupDrawWithExternalTexture();
934 }
935 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500937 setupDrawColorFilter(layer->getColorFilter());
938 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700939 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500941 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
943 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700944 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700946 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800947 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800948 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700949 layer->getWidth() == (uint32_t) rect.getWidth() &&
950 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800951 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
952 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700953
Romain Guyd21b6e12011-11-30 20:21:23 -0800954 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800955 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
956 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800958 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800959 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
960 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 }
962 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700963 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964
Chris Craik96a5c4c2015-01-27 15:46:35 -0800965 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700966}
967
Romain Guy5b3b3522010-10-27 18:57:51 -0700968void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700969 if (layer->isTextureLayer()) {
970 EVENT_LOGD("composeTextureLayerRect");
971 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
972 drawTextureLayer(layer, rect);
973 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
974 } else {
975 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700976 const Rect& texCoords = layer->texCoords;
977 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
978 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700979
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 float x = rect.left;
981 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800982 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700983 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700984 layer->getHeight() == (uint32_t) rect.getHeight();
985
986 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 // When we're swapping, the layer is already in screen coordinates
988 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800989 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
990 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 }
992
Romain Guyd21b6e12011-11-30 20:21:23 -0800993 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800995 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 }
997
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500998 SkPaint layerPaint;
999 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1000 layerPaint.setXfermodeMode(layer->getMode());
1001 layerPaint.setColorFilter(layer->getColorFilter());
1002
1003 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001004 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001005 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001006 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08001007 GL_TRIANGLE_STRIP, kMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001008
Romain Guyaa6c24c2011-04-28 18:40:04 -07001009 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001010 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001011}
1012
Chris Craik34416ea2013-04-15 16:08:28 -07001013/**
1014 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1015 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1016 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1017 * by saveLayer's restore
1018 */
Tom Hudson984162f2014-10-10 13:38:16 -04001019#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1020 DRAW_COMMAND; \
1021 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1022 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1023 DRAW_COMMAND; \
1024 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1025 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001026 }
1027
1028#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1029
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001030// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1031// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1032class LayerShader : public SkShader {
1033public:
1034 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1035 : INHERITED(localMatrix)
1036 , mLayer(layer) {
1037 }
1038
Chris Craike84a2082014-12-22 14:28:49 -08001039 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001040 if (data) {
1041 *data = static_cast<void*>(mLayer);
1042 }
1043 return true;
1044 }
1045
Chris Craike84a2082014-12-22 14:28:49 -08001046 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001047 return !mLayer->isBlend();
1048 }
1049
1050protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001051 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001052 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1053 }
1054
Chris Craike84a2082014-12-22 14:28:49 -08001055 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001056 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1057 }
1058
Chris Craike84a2082014-12-22 14:28:49 -08001059 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001060 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001061 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001062 }
1063private:
1064 // Unowned.
1065 Layer* mLayer;
1066 typedef SkShader INHERITED;
1067};
1068
Romain Guy5b3b3522010-10-27 18:57:51 -07001069void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001070 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1071
1072 if (layer->getConvexMask()) {
1073 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1074
1075 // clip to the area of the layer the mask can be larger
1076 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1077
1078 SkPaint paint;
1079 paint.setAntiAlias(true);
1080 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1081
Chris Craik3f0854292014-04-15 16:18:08 -07001082 // create LayerShader to map SaveLayer content into subsequent draw
1083 SkMatrix shaderMatrix;
1084 shaderMatrix.setTranslate(rect.left, rect.bottom);
1085 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001086 LayerShader layerShader(layer, &shaderMatrix);
1087 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001088
1089 // Since the drawing primitive is defined in local drawing space,
1090 // we don't need to modify the draw matrix
1091 const SkPath* maskPath = layer->getConvexMask();
1092 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1093
Chris Craike84a2082014-12-22 14:28:49 -08001094 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001095 restore();
1096
1097 return;
1098 }
1099
Romain Guy5b3b3522010-10-27 18:57:51 -07001100 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001101 layer->setRegionAsRect();
1102
Chris Craik34416ea2013-04-15 16:08:28 -07001103 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001104
Romain Guy5b3b3522010-10-27 18:57:51 -07001105 layer->region.clear();
1106 return;
1107 }
1108
Chris Craik62d307c2014-07-29 10:35:13 -07001109 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001110 // standard Region based draw
1111 size_t count;
1112 const android::Rect* rects;
1113 Region safeRegion;
1114 if (CC_LIKELY(hasRectToRectTransform())) {
1115 rects = layer->region.getArray(&count);
1116 } else {
1117 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1118 rects = safeRegion.getArray(&count);
1119 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001120
Chris Craik3f0854292014-04-15 16:18:08 -07001121 const float alpha = getLayerAlpha(layer);
1122 const float texX = 1.0f / float(layer->getWidth());
1123 const float texY = 1.0f / float(layer->getHeight());
1124 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001127
Chris Craik3f0854292014-04-15 16:18:08 -07001128 // We must get (and therefore bind) the region mesh buffer
1129 // after we setup drawing in case we need to mess with the
1130 // stencil buffer in setupDraw()
1131 TextureVertex* mesh = mCaches.getRegionMesh();
1132 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001133
Chris Craik3f0854292014-04-15 16:18:08 -07001134 setupDrawWithTexture();
1135 setupDrawColor(alpha, alpha, alpha, alpha);
1136 setupDrawColorFilter(layer->getColorFilter());
1137 setupDrawBlending(layer);
1138 setupDrawProgram();
1139 setupDrawDirtyRegionsDisabled();
1140 setupDrawPureColorUniforms();
1141 setupDrawColorFilterUniforms(layer->getColorFilter());
1142 setupDrawTexture(layer->getTexture());
1143 if (currentTransform()->isPureTranslate()) {
1144 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1145 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001146
Chris Craik3f0854292014-04-15 16:18:08 -07001147 layer->setFilter(GL_NEAREST);
1148 setupDrawModelView(kModelViewMode_Translate, false,
1149 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1150 } else {
1151 layer->setFilter(GL_LINEAR);
1152 setupDrawModelView(kModelViewMode_Translate, false,
1153 rect.left, rect.top, rect.right, rect.bottom);
1154 }
1155 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001156
Chris Craik3f0854292014-04-15 16:18:08 -07001157 for (size_t i = 0; i < count; i++) {
1158 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001159
Chris Craik3f0854292014-04-15 16:18:08 -07001160 const float u1 = r->left * texX;
1161 const float v1 = (height - r->top) * texY;
1162 const float u2 = r->right * texX;
1163 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001164
Chris Craik3f0854292014-04-15 16:18:08 -07001165 // TODO: Reject quads outside of the clip
1166 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1167 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1168 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1169 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik3f0854292014-04-15 16:18:08 -07001171 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001172
Chris Craik96a5c4c2015-01-27 15:46:35 -08001173 if (numQuads >= kMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001174 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001175 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001176 numQuads = 0;
1177 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001178 }
Chris Craik3f0854292014-04-15 16:18:08 -07001179 }
1180
1181 if (numQuads > 0) {
1182 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001183 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185
Romain Guy5b3b3522010-10-27 18:57:51 -07001186#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001187 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001188#endif
1189
Chris Craik3f0854292014-04-15 16:18:08 -07001190 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001191}
1192
Romain Guy3a3133d2011-02-01 22:59:58 -08001193#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001194void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001195 size_t count;
1196 const android::Rect* rects = region.getArray(&count);
1197
1198 uint32_t colors[] = {
1199 0x7fff0000, 0x7f00ff00,
1200 0x7f0000ff, 0x7fff00ff,
1201 };
1202
1203 int offset = 0;
1204 int32_t top = rects[0].top;
1205
1206 for (size_t i = 0; i < count; i++) {
1207 if (top != rects[i].top) {
1208 offset ^= 0x2;
1209 top = rects[i].top;
1210 }
1211
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001212 SkPaint paint;
1213 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001214 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001215 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001216 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001217}
Chris Craike63f7c622013-10-17 10:30:55 -07001218#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001219
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001220void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001221 Vector<float> rects;
1222
1223 SkRegion::Iterator it(region);
1224 while (!it.done()) {
1225 const SkIRect& r = it.rect();
1226 rects.push(r.fLeft);
1227 rects.push(r.fTop);
1228 rects.push(r.fRight);
1229 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001230 it.next();
1231 }
1232
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001233 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001234}
1235
Romain Guy5b3b3522010-10-27 18:57:51 -07001236void OpenGLRenderer::dirtyLayer(const float left, const float top,
1237 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001238 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001239 Rect bounds(left, top, right, bottom);
1240 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001241 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001242 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001243}
1244
1245void OpenGLRenderer::dirtyLayer(const float left, const float top,
1246 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001247 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001248 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001249 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001250 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001251}
1252
1253void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001254 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001255 bounds.snapToPixelBoundaries();
1256 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1257 if (!dirty.isEmpty()) {
1258 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001259 }
1260 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001261}
1262
Chris Craik4063a0e2013-11-15 16:06:56 -08001263void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001264 GLsizei elementsCount = quadsCount * 6;
1265 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001266 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07001267
Romain Guy3380cfd2013-08-15 16:57:57 -07001268 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001269 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001270
1271 elementsCount -= drawCount;
1272 // Though there are 4 vertices in a quad, we use 6 indices per
1273 // quad to draw with GL_TRIANGLES
1274 mesh += (drawCount / 6) * 4;
1275 }
1276}
1277
Romain Guy54be1cd2011-06-13 19:04:27 -07001278void OpenGLRenderer::clearLayerRegions() {
1279 const size_t count = mLayers.size();
1280 if (count == 0) return;
1281
Tom Hudson984162f2014-10-10 13:38:16 -04001282 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001283 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001284 // Doing several glScissor/glClear here can negatively impact
1285 // GPUs with a tiler architecture, instead we draw quads with
1286 // the Clear blending mode
1287
1288 // The list contains bounds that have already been clipped
1289 // against their initial clip rect, and the current clip
1290 // is likely different so we need to disable clipping here
Chris Craik65fe5ee2015-01-26 18:06:29 -08001291 bool scissorChanged = mRenderState.scissor().setEnabled(false);
Romain Guy54be1cd2011-06-13 19:04:27 -07001292
Romain Guy448455f2013-07-22 13:57:50 -07001293 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001294 Vertex* vertex = mesh;
1295
1296 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001297 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001298
Chris Craik51d6a3d2014-12-22 17:16:56 -08001299 Vertex::set(vertex++, bounds.left, bounds.top);
1300 Vertex::set(vertex++, bounds.right, bounds.top);
1301 Vertex::set(vertex++, bounds.left, bounds.bottom);
1302 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001303 }
Romain Guye67307c2013-02-11 18:01:20 -08001304 // We must clear the list of dirty rects before we
1305 // call setupDraw() to prevent stencil setup to do
1306 // the same thing again
1307 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001308
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001309 SkPaint clearPaint;
1310 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1311
Romain Guy54be1cd2011-06-13 19:04:27 -07001312 setupDraw(false);
1313 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001314 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001315 setupDrawProgram();
1316 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001317 setupDrawModelView(kModelViewMode_Translate, false,
1318 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001319
Chris Craik4063a0e2013-11-15 16:06:56 -08001320 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001321
Chris Craik65fe5ee2015-01-26 18:06:29 -08001322 if (scissorChanged) mRenderState.scissor().setEnabled(true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001323 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001324 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001325 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001326}
1327
Romain Guybd6b79b2010-06-26 00:13:53 -07001328///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001329// State Deferral
1330///////////////////////////////////////////////////////////////////////////////
1331
Chris Craikff785832013-03-08 13:12:16 -08001332bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001333 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001334 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001335
Chris Craikff785832013-03-08 13:12:16 -08001336 if (stateDeferFlags & kStateDeferFlag_Draw) {
1337 // state has bounds initialized in local coordinates
1338 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001339 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001340 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001341 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1342 // is used, it should more closely duplicate the quickReject logic (in how it uses
1343 // snapToPixelBoundaries)
1344
Rob Tsuk487a92c2015-01-06 13:22:54 -08001345 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001346 // quick rejected
1347 return true;
1348 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001349
Chris Craika02c4ed2013-06-14 13:43:58 -07001350 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001351 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001352 int& flags = state.mClipSideFlags;
1353 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001354 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1355 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1356 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1357 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001358 }
1359 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001360 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001361 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1362 // overdraw avoidance (since we don't know what it overlaps)
1363 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001364 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001365 }
1366 }
1367
Chris Craik527a3aa2013-03-04 10:19:31 -08001368 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1369 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001370 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001371 }
1372
Chris Craik7273daa2013-03-28 11:25:24 -07001373 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1374 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001376 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001378
1379 // always store/restore, since it's just a pointer
1380 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001381 return false;
1382}
1383
Chris Craik527a3aa2013-03-04 10:19:31 -08001384void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001385 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001386 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001387 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001388 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001389
Chris Craik527a3aa2013-03-04 10:19:31 -08001390 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001391 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001392 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001393 dirtyClip();
1394 }
Chris Craikc3566d02013-02-04 16:16:33 -08001395}
1396
Chris Craik28ce94a2013-05-31 11:38:03 -07001397/**
1398 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1399 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1400 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1401 *
1402 * This method should be called when restoreDisplayState() won't be restoring the clip
1403 */
1404void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001405 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001406 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001407 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001408 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001409 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001410 dirtyClip();
Chris Craik65fe5ee2015-01-26 18:06:29 -08001411 bool enableScissor = (clipRect != nullptr) || mScissorOptimizationDisabled;
1412 mRenderState.scissor().setEnabled(enableScissor);
Chris Craik527a3aa2013-03-04 10:19:31 -08001413}
1414
Chris Craikc3566d02013-02-04 16:16:33 -08001415///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001416// Clipping
1417///////////////////////////////////////////////////////////////////////////////
1418
Romain Guybb9524b2010-06-22 18:56:38 -07001419void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001420 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001421 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001422
Chris Craik65fe5ee2015-01-26 18:06:29 -08001423 if (mRenderState.scissor().set(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001424 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001425 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001426 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001427}
1428
Romain Guy8ce00302013-01-15 18:51:42 -08001429void OpenGLRenderer::ensureStencilBuffer() {
1430 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1431 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1432 // just hope we have one when hasLayer() returns false.
1433 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001434 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001435 }
1436}
1437
1438void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1439 // The layer's FBO is already bound when we reach this stage
1440 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001441 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1442 // is attached after we initiated tiling. We must turn it off,
1443 // attach the new render buffer then turn tiling back on
1444 endTiling();
1445
Romain Guy8d4aeb72013-02-12 16:08:55 -08001446 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001447 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001448 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001449
Romain Guyf735c8e2013-01-31 17:45:55 -08001450 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001451 }
1452}
1453
Rob Tsuk487a92c2015-01-06 13:22:54 -08001454static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1455 float x, float y) {
1456 Vertex v;
1457 v.x = x;
1458 v.y = y;
1459 transform.mapPoint(v.x, v.y);
1460 rectangleVertices.push_back(v);
1461}
1462
1463static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1464 Vertex v;
1465 v.x = x;
1466 v.y = y;
1467 rectangleVertices.push_back(v);
1468}
1469
1470void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1471 int count = rectangleList.getTransformedRectanglesCount();
1472 std::vector<Vertex> rectangleVertices(count * 4);
1473 Rect scissorBox = rectangleList.calculateBounds();
1474 scissorBox.snapToPixelBoundaries();
1475 for (int i = 0; i < count; ++i) {
1476 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1477 const Matrix4& transform = tr.getTransform();
1478 Rect bounds = tr.getBounds();
1479 if (transform.rectToRect()) {
1480 transform.mapRect(bounds);
1481 if (!bounds.intersect(scissorBox)) {
1482 bounds.setEmpty();
1483 } else {
1484 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1485 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1486 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1487 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1488 }
1489 } else {
1490 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1491 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1492 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1493 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1494 }
1495 }
1496
Chris Craik65fe5ee2015-01-26 18:06:29 -08001497 mRenderState.scissor().set(scissorBox.left, getViewportHeight() - scissorBox.bottom,
Rob Tsuk487a92c2015-01-06 13:22:54 -08001498 scissorBox.getWidth(), scissorBox.getHeight());
1499
1500 const SkPaint* paint = nullptr;
1501 setupDraw();
1502 setupDrawNoTexture();
1503 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1504 setupDrawShader(getShader(paint));
1505 setupDrawColorFilter(getColorFilter(paint));
1506 setupDrawBlending(paint);
1507 setupDrawProgram();
1508 setupDrawDirtyRegionsDisabled();
1509 setupDrawModelView(kModelViewMode_Translate, false,
1510 0.0f, 0.0f, 0.0f, 0.0f, true);
1511 setupDrawColorUniforms(getShader(paint));
1512 setupDrawShaderUniforms(getShader(paint));
1513 setupDrawColorFilterUniforms(getColorFilter(paint));
1514
1515 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1516}
1517
Romain Guy8ce00302013-01-15 18:51:42 -08001518void OpenGLRenderer::setStencilFromClip() {
1519 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001520 if (!currentSnapshot()->clipIsSimple()) {
1521 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001522 EVENT_LOGD("setStencilFromClip - enabling");
1523
Romain Guy8ce00302013-01-15 18:51:42 -08001524 // NOTE: The order here is important, we must set dirtyClip to false
1525 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001526 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001527
1528 ensureStencilBuffer();
1529
Rob Tsuk487a92c2015-01-06 13:22:54 -08001530 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001531
Rob Tsuk487a92c2015-01-06 13:22:54 -08001532 bool isRectangleList = clipArea.isRectangleList();
1533 if (isRectangleList) {
1534 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1535 } else {
1536 incrementThreshold = 0;
1537 }
1538
Chris Craik96a5c4c2015-01-27 15:46:35 -08001539 mRenderState.stencil().enableWrite(incrementThreshold);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001540
1541 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001542 // to the region's bounds
Chris Craik65fe5ee2015-01-26 18:06:29 -08001543 bool resetScissor = mRenderState.scissor().setEnabled(true);
Romain Guy8ce00302013-01-15 18:51:42 -08001544 if (resetScissor) {
1545 // The scissor was not set so we now need to update it
1546 setScissorFromClip();
1547 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001548
Chris Craik96a5c4c2015-01-27 15:46:35 -08001549 mRenderState.stencil().clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001550
1551 // stash and disable the outline clip state, since stencil doesn't account for outline
1552 bool storedSkipOutlineClip = mSkipOutlineClip;
1553 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001554
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001555 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001556 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001557 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1558
Rob Tsuk487a92c2015-01-06 13:22:54 -08001559 if (isRectangleList) {
1560 drawRectangleList(clipArea.getRectangleList());
1561 } else {
1562 // NOTE: We could use the region contour path to generate a smaller mesh
1563 // Since we are using the stencil we could use the red book path
1564 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001565
Rob Tsuk487a92c2015-01-06 13:22:54 -08001566 // The last parameter is important: we are not drawing in the color buffer
1567 // so we don't want to dirty the current layer, if any
1568 drawRegionRects(clipArea.getClipRegion(), paint, false);
1569 }
Chris Craik65fe5ee2015-01-26 18:06:29 -08001570 if (resetScissor) mRenderState.scissor().setEnabled(false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001571 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001572
Chris Craik96a5c4c2015-01-27 15:46:35 -08001573 mRenderState.stencil().enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001574
1575 // Draw the region used to generate the stencil if the appropriate debug
1576 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001577 // TODO: Implement for rectangle list clip areas
1578 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1579 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001580 paint.setColor(0x7f0000ff);
1581 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001582 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001583 }
Romain Guy8ce00302013-01-15 18:51:42 -08001584 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001585 EVENT_LOGD("setStencilFromClip - disabling");
Chris Craik96a5c4c2015-01-27 15:46:35 -08001586 mRenderState.stencil().disable();
Romain Guy8ce00302013-01-15 18:51:42 -08001587 }
1588 }
1589}
1590
Chris Craikf0a59072013-11-19 18:00:46 -08001591/**
1592 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1593 *
1594 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1595 * style, and tessellated AA ramp
1596 */
1597bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001598 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001599 bool snapOut = paint && paint->isAntiAlias();
1600
1601 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1602 float outset = paint->getStrokeWidth() * 0.5f;
1603 left -= outset;
1604 top -= outset;
1605 right += outset;
1606 bottom += outset;
1607 }
1608
Chris Craikdeeda3d2014-05-05 19:09:33 -07001609 bool clipRequired = false;
1610 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001611 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001612 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001613 return true;
1614 }
1615
Chris Craikf23b25a2014-06-26 15:46:20 -07001616 // not quick rejected, so enable the scissor if clipRequired
Chris Craik65fe5ee2015-01-26 18:06:29 -08001617 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Chris Craikf23b25a2014-06-26 15:46:20 -07001618 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001619 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001620}
1621
Romain Guy8ce00302013-01-15 18:51:42 -08001622void OpenGLRenderer::debugClip() {
1623#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001624 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001625 SkPaint paint;
1626 paint.setColor(0x7f00ff00);
1627 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1628
Romain Guy8ce00302013-01-15 18:51:42 -08001629 }
1630#endif
1631}
1632
Romain Guyf6a11b82010-06-23 17:47:49 -07001633///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001634// Drawing commands
1635///////////////////////////////////////////////////////////////////////////////
1636
Chris Craik62d307c2014-07-29 10:35:13 -07001637void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001638 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001639 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001640 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001641 // Make sure setScissor & setStencil happen at the beginning of
1642 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001643 if (mState.getDirtyClip()) {
Chris Craik65fe5ee2015-01-26 18:06:29 -08001644 if (mRenderState.scissor().isEnabled()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001645 setScissorFromClip();
1646 }
Chris Craik62d307c2014-07-29 10:35:13 -07001647
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001648 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001649 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001650
Romain Guy70ca14e2010-12-13 18:24:33 -08001651 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001652
Romain Guy70ca14e2010-12-13 18:24:33 -08001653 mSetShaderColor = false;
1654 mColorSet = false;
1655 mColorA = mColorR = mColorG = mColorB = 0.0f;
1656 mTextureUnit = 0;
1657 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001658
1659 // Enable debug highlight when what we're about to draw is tested against
1660 // the stencil buffer and if stencil highlight debugging is on
1661 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1662 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
Chris Craik96a5c4c2015-01-27 15:46:35 -08001663 mRenderState.stencil().isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001664}
1665
1666void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1667 mDescription.hasTexture = true;
1668 mDescription.hasAlpha8Texture = isAlpha8;
1669}
1670
Romain Guyff316ec2013-02-13 18:39:43 -08001671void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1672 mDescription.hasTexture = true;
1673 mDescription.hasColors = true;
1674 mDescription.hasAlpha8Texture = isAlpha8;
1675}
1676
Romain Guyaa6c24c2011-04-28 18:40:04 -07001677void OpenGLRenderer::setupDrawWithExternalTexture() {
1678 mDescription.hasExternalTexture = true;
1679}
1680
Romain Guy15bc6432011-12-13 13:11:32 -08001681void OpenGLRenderer::setupDrawNoTexture() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001682 mRenderState.meshState().disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001683}
1684
Chris Craik91a8c7c2014-08-12 14:31:35 -07001685void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1686 mDescription.hasVertexAlpha = true;
1687 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001688}
1689
Romain Guy8d0d4782010-12-14 20:13:35 -08001690void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1691 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001692 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1693 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1694 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001695 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001696 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001697}
1698
Romain Guy86568192010-12-14 15:55:39 -08001699void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1700 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001701 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1702 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1703 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001704 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001705 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001706}
1707
Romain Guy41210632012-07-16 17:04:24 -07001708void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1709 mCaches.fontRenderer->describe(mDescription, paint);
1710}
1711
Romain Guy70ca14e2010-12-13 18:24:33 -08001712void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1713 mColorA = a;
1714 mColorR = r;
1715 mColorG = g;
1716 mColorB = b;
1717 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001718 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001719}
1720
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001721void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001722 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001723 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001724 }
1725}
1726
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001727void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001728 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001729 return;
1730 }
1731
1732 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001733 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001734 mDescription.colorOp = ProgramDescription::kColorBlend;
1735 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001736 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001737 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001738 }
1739}
1740
Romain Guyf09ef512011-05-27 11:43:46 -07001741void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1742 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1743 mColorA = 1.0f;
1744 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001745 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001746 }
1747}
1748
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001749void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1750 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001751 // When the blending mode is kClear_Mode, we need to use a modulate color
1752 // argb=1,0,0,0
1753 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001754 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001755 bool blend = layer->isBlend()
1756 || getLayerAlpha(layer) < 1.0f
1757 || (mColorSet && mColorA < 1.0f)
1758 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001759 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001760}
1761
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001762void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1763 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001764 // When the blending mode is kClear_Mode, we need to use a modulate color
1765 // argb=1,0,0,0
1766 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001767 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001768 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001769 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001770 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001771}
1772
1773void OpenGLRenderer::setupDrawProgram() {
1774 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001775 if (mDescription.hasRoundRectClip) {
1776 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001777 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001778 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001779 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001780 innerRect.left, innerRect.top,
1781 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001782 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1783 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001784
1785 // add half pixel to round out integer rect space to cover pixel centers
1786 float roundedOutRadius = state->radius + 0.5f;
1787 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1788 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001789 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001790}
1791
1792void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1793 mTrackDirtyRegions = false;
1794}
1795
Chris Craik4063a0e2013-11-15 16:06:56 -08001796void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1797 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001798 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001799 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001800 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001801 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001802
Romain Guy86568192010-12-14 15:55:39 -08001803 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001804 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001805 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1806 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001807 if (dirty && mTrackDirtyRegions) {
1808 if (!ignoreTransform) {
1809 dirtyLayer(left, top, right, bottom, *currentTransform());
1810 } else {
1811 dirtyLayer(left, top, right, bottom);
1812 }
Romain Guy86568192010-12-14 15:55:39 -08001813 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001814}
1815
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001816void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1817 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001818 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1819 }
1820}
1821
Romain Guy86568192010-12-14 15:55:39 -08001822void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001823 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001824 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001825 }
1826}
1827
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001828void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001829 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001830 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001831 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001832
1833 if (ignoreTransform) {
1834 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1835 // because it was built into modelView / the geometry, and the description needs to
1836 // compensate.
1837 mat4 modelViewWithoutTransform;
1838 modelViewWithoutTransform.loadInverse(*currentTransform());
1839 modelViewWithoutTransform.multiply(mModelViewMatrix);
1840 mModelViewMatrix.load(modelViewWithoutTransform);
1841 }
1842
1843 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001844}
1845
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001846void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001847 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001848 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001849 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001850
1851 SkColor color;
1852 SkXfermode::Mode mode;
1853 if (filter->asColorMode(&color, &mode)) {
1854 const int alpha = SkColorGetA(color);
1855 const GLfloat a = alpha / 255.0f;
1856 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1857 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1858 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1859 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1860 return;
1861 }
1862
1863 SkScalar srcColorMatrix[20];
1864 if (filter->asColorMatrix(srcColorMatrix)) {
1865
1866 float colorMatrix[16];
1867 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1868 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1869 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1870 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1871
1872 // Skia uses the range [0..255] for the addition vector, but we need
1873 // the [0..1] range to apply the vector in GLSL
1874 float colorVector[4];
1875 colorVector[0] = srcColorMatrix[4] / 255.0f;
1876 colorVector[1] = srcColorMatrix[9] / 255.0f;
1877 colorVector[2] = srcColorMatrix[14] / 255.0f;
1878 colorVector[3] = srcColorMatrix[19] / 255.0f;
1879
1880 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1881 GL_FALSE, colorMatrix);
1882 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1883 return;
1884 }
1885
1886 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001887}
1888
Romain Guy41210632012-07-16 17:04:24 -07001889void OpenGLRenderer::setupDrawTextGammaUniforms() {
1890 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1891}
1892
Romain Guy70ca14e2010-12-13 18:24:33 -08001893void OpenGLRenderer::setupDrawSimpleMesh() {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001894 bool force = mRenderState.meshState().bindMeshBuffer();
1895 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, nullptr);
1896 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001897}
1898
1899void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001900 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001901 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001902 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001903}
1904
Romain Guyaa6c24c2011-04-28 18:40:04 -07001905void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1906 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001907 mTextureUnit++;
Chris Craik96a5c4c2015-01-27 15:46:35 -08001908 mRenderState.meshState().enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001909}
1910
Romain Guy8f0095c2011-05-02 17:24:22 -07001911void OpenGLRenderer::setupDrawTextureTransform() {
1912 mDescription.hasTextureTransform = true;
1913}
1914
1915void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001916 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1917 GL_FALSE, &transform.data[0]);
1918}
1919
Chris Craik564acf72014-01-02 16:46:18 -08001920void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1921 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001922 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001923 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001924 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001925 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001926 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001927 }
Romain Guyd71dd362011-12-12 19:03:35 -08001928
Chris Craik96a5c4c2015-01-27 15:46:35 -08001929 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001930 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001931 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram, force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001932 }
1933
Chris Craik96a5c4c2015-01-27 15:46:35 -08001934 mRenderState.meshState().unbindIndicesBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -08001935}
1936
Chris Craik564acf72014-01-02 16:46:18 -08001937void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1938 const GLvoid* texCoords, const GLvoid* colors) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001939 bool force = mRenderState.meshState().unbindMeshBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001940 GLsizei stride = sizeof(ColorTextureVertex);
1941
Chris Craik96a5c4c2015-01-27 15:46:35 -08001942 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force,
1943 vertices, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001944 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001945 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram, force,
1946 texCoords, stride);
Romain Guyff316ec2013-02-13 18:39:43 -08001947 }
1948 int slot = mCaches.currentProgram->getAttrib("colors");
1949 if (slot >= 0) {
1950 glEnableVertexAttribArray(slot);
1951 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1952 }
1953
Chris Craik96a5c4c2015-01-27 15:46:35 -08001954 mRenderState.meshState().unbindIndicesBuffer();
Romain Guyff316ec2013-02-13 18:39:43 -08001955}
1956
Chris Craik564acf72014-01-02 16:46:18 -08001957void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1958 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001959 bool force = false;
1960 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1961 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
Chris Craik96a5c4c2015-01-27 15:46:35 -08001962 // use the default VBO found in RenderState
Romain Guy3b748a42013-04-17 18:54:38 -07001963 if (!vertices || vbo) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001964 force = mRenderState.meshState().bindMeshBuffer(vbo);
Romain Guy3b748a42013-04-17 18:54:38 -07001965 } else {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001966 force = mRenderState.meshState().unbindMeshBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001967 }
Chris Craik96a5c4c2015-01-27 15:46:35 -08001968 mRenderState.meshState().bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001969
Chris Craik96a5c4c2015-01-27 15:46:35 -08001970 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001971 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001972 mRenderState.meshState().bindTexCoordsVertexPointer(mCaches.currentProgram,
1973 force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001974 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001975}
1976
Romain Guy448455f2013-07-22 13:57:50 -07001977void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08001978 bool force = mRenderState.meshState().unbindMeshBuffer();
1979 mRenderState.meshState().bindQuadIndicesBuffer();
1980 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram, force,
1981 vertices, kVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001982}
1983
Romain Guy70ca14e2010-12-13 18:24:33 -08001984///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001985// Drawing
1986///////////////////////////////////////////////////////////////////////////////
1987
Tom Hudson107843d2014-09-08 11:26:26 -04001988void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001989 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1990 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001991 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001992 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001993 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001994 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001995 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001996 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001997 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001998 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001999 }
2000
Chris Craikef8d6f22014-12-17 11:10:28 -08002001 // Don't avoid overdraw when visualizing, since that makes it harder to
2002 // debug where it's coming from, and when the problem occurs.
2003 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08002004 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002005 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07002006 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002007
2008 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04002009 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002010
Tom Hudson107843d2014-09-08 11:26:26 -04002011 deferredList.flush(*this, dirty);
2012 } else {
2013 // Even if there is no drawing command(Ex: invisible),
2014 // it still needs startFrame to clear buffer and start tiling.
2015 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08002016 }
2017}
2018
Chris Craike84a2082014-12-22 14:28:49 -08002019void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
2020 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07002021 float x = left;
2022 float y = top;
2023
Romain Guy886b2752013-01-04 12:26:18 -08002024 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2025
Romain Guya168d732011-03-18 16:50:13 -07002026 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08002027 if (currentTransform()->isPureTranslate()) {
2028 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2029 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002030 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002031
2032 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002033 } else {
Chris Craik678625242014-02-28 12:26:34 -08002034 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002035 }
2036
Romain Guy3b748a42013-04-17 18:54:38 -07002037 // No need to check for a UV mapper on the texture object, only ARGB_8888
2038 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002039 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002040 paint, (GLvoid*) nullptr, (GLvoid*) kMeshTextureOffset,
2041 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002042}
2043
Romain Guy03c00b52013-06-20 18:30:28 -07002044/**
2045 * Important note: this method is intended to draw batches of bitmaps and
2046 * will not set the scissor enable or dirty the current layer, if any.
2047 * The caller is responsible for properly dirtying the current layer.
2048 */
Tom Hudson107843d2014-09-08 11:26:26 -04002049void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002050 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2051 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002052 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002053 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002054 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002055
Chris Craik527a3aa2013-03-04 10:19:31 -08002056 const AutoTexture autoCleanup(texture);
2057
Chris Craik527a3aa2013-03-04 10:19:31 -08002058 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002059 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002060
2061 const float x = (int) floorf(bounds.left + 0.5f);
2062 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002063 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002064 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002065 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002066 GL_TRIANGLES, bitmapCount * 6, true,
2067 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002068 } else {
2069 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002070 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002071 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2072 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002073 }
2074
Tom Hudson107843d2014-09-08 11:26:26 -04002075 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002076}
2077
Tom Hudson107843d2014-09-08 11:26:26 -04002078void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002079 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002080 return;
Romain Guy6926c722010-07-12 20:20:03 -07002081 }
2082
Romain Guya1d3c912011-12-13 14:55:06 -08002083 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002084 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002085 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002086 const AutoTexture autoCleanup(texture);
2087
Mike Reed1103b322014-07-08 12:36:44 -04002088 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002089 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002090 } else {
Chris Craik79647502014-08-06 13:42:24 -07002091 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002092 }
Chet Haase48659092012-05-31 15:21:51 -07002093
Tom Hudson107843d2014-09-08 11:26:26 -04002094 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002095}
2096
Tom Hudson107843d2014-09-08 11:26:26 -04002097void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002098 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002099 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002100 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002101 }
2102
Chris Craik39a908c2013-06-13 14:39:01 -07002103 // TODO: use quickReject on bounds from vertices
Chris Craik65fe5ee2015-01-26 18:06:29 -08002104 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002105
Romain Guyb18d2d02011-02-10 15:52:54 -08002106 float left = FLT_MAX;
2107 float top = FLT_MAX;
2108 float right = FLT_MIN;
2109 float bottom = FLT_MIN;
2110
Romain Guya92bb4d2012-10-16 11:08:44 -07002111 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002112
Chris Craik51d6a3d2014-12-22 17:16:56 -08002113 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2114 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002115
Chris Craik51d6a3d2014-12-22 17:16:56 -08002116 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002117 if (!colors) {
2118 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002119 tempColors.reset(new int[colorsCount]);
2120 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2121 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002122 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002123
Romain Guy3b748a42013-04-17 18:54:38 -07002124 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002125 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002126 const UvMapper& mapper(getMapper(texture));
2127
Romain Guy5a7b4662011-01-20 19:09:30 -08002128 for (int32_t y = 0; y < meshHeight; y++) {
2129 for (int32_t x = 0; x < meshWidth; x++) {
2130 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2131
2132 float u1 = float(x) / meshWidth;
2133 float u2 = float(x + 1) / meshWidth;
2134 float v1 = float(y) / meshHeight;
2135 float v2 = float(y + 1) / meshHeight;
2136
Romain Guy3b748a42013-04-17 18:54:38 -07002137 mapper.map(u1, v1, u2, v2);
2138
Romain Guy5a7b4662011-01-20 19:09:30 -08002139 int ax = i + (meshWidth + 1) * 2;
2140 int ay = ax + 1;
2141 int bx = i;
2142 int by = bx + 1;
2143 int cx = i + 2;
2144 int cy = cx + 1;
2145 int dx = i + (meshWidth + 1) * 2 + 2;
2146 int dy = dx + 1;
2147
Romain Guyff316ec2013-02-13 18:39:43 -08002148 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2149 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2150 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002151
Romain Guyff316ec2013-02-13 18:39:43 -08002152 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2153 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2154 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002155
Romain Guya92bb4d2012-10-16 11:08:44 -07002156 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2157 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2158 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2159 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002160 }
2161 }
2162
Chris Craikf0a59072013-11-19 18:00:46 -08002163 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002164 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002165 }
2166
Romain Guyff316ec2013-02-13 18:39:43 -08002167 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002168 texture = mCaches.textureCache.get(bitmap);
2169 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002170 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002171 }
Romain Guyff316ec2013-02-13 18:39:43 -08002172 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002173 const AutoTexture autoCleanup(texture);
2174
2175 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002176 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002177
2178 int alpha;
2179 SkXfermode::Mode mode;
2180 getAlphaAndMode(paint, &alpha, &mode);
2181
Romain Guyff316ec2013-02-13 18:39:43 -08002182 float a = alpha / 255.0f;
2183
Romain Guya92bb4d2012-10-16 11:08:44 -07002184 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002185 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002186 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002187
Romain Guyff316ec2013-02-13 18:39:43 -08002188 setupDraw();
2189 setupDrawWithTextureAndColor();
2190 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002191 setupDrawColorFilter(getColorFilter(paint));
2192 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002193 setupDrawProgram();
2194 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002195 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002196 setupDrawTexture(texture->id);
2197 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002198 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002199 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002200
2201 glDrawArrays(GL_TRIANGLES, 0, count);
2202
Romain Guyff316ec2013-02-13 18:39:43 -08002203 int slot = mCaches.currentProgram->getAttrib("colors");
2204 if (slot >= 0) {
2205 glDisableVertexAttribArray(slot);
2206 }
2207
Tom Hudson107843d2014-09-08 11:26:26 -04002208 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002209}
2210
Tom Hudson107843d2014-09-08 11:26:26 -04002211void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002212 float srcLeft, float srcTop, float srcRight, float srcBottom,
2213 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002214 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002215 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002216 return;
Romain Guy6926c722010-07-12 20:20:03 -07002217 }
2218
Romain Guya1d3c912011-12-13 14:55:06 -08002219 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002220 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002221 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002222 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002223
Romain Guy8ba548f2010-06-30 19:21:21 -07002224 const float width = texture->width;
2225 const float height = texture->height;
2226
Romain Guy3b748a42013-04-17 18:54:38 -07002227 float u1 = fmax(0.0f, srcLeft / width);
2228 float v1 = fmax(0.0f, srcTop / height);
2229 float u2 = fmin(1.0f, srcRight / width);
2230 float v2 = fmin(1.0f, srcBottom / height);
2231
2232 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002233
Chris Craik96a5c4c2015-01-27 15:46:35 -08002234 mRenderState.meshState().unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002235 resetDrawTextureTexCoords(u1, v1, u2, v2);
2236
Romain Guyd21b6e12011-11-30 20:21:23 -08002237 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2238
Romain Guy886b2752013-01-04 12:26:18 -08002239 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2240 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002241
Romain Guy886b2752013-01-04 12:26:18 -08002242 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2243 // Apply a scale transform on the canvas only when a shader is in use
2244 // Skia handles the ratio between the dst and src rects as a scale factor
2245 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002246 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002247 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002248
Chris Craikd6b65f62014-01-01 14:45:21 -08002249 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2250 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2251 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002252
2253 dstRight = x + (dstRight - dstLeft);
2254 dstBottom = y + (dstBottom - dstTop);
2255
2256 dstLeft = x;
2257 dstTop = y;
2258
Chris Craik678625242014-02-28 12:26:34 -08002259 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002260 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002261 } else {
Chris Craik678625242014-02-28 12:26:34 -08002262 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002263 }
2264
2265 if (CC_UNLIKELY(useScaleTransform)) {
2266 save(SkCanvas::kMatrix_SaveFlag);
2267 translate(dstLeft, dstTop);
2268 scale(scaleX, scaleY);
2269
2270 dstLeft = 0.0f;
2271 dstTop = 0.0f;
2272
2273 dstRight = srcRight - srcLeft;
2274 dstBottom = srcBottom - srcTop;
2275 }
2276
Mike Reed1103b322014-07-08 12:36:44 -04002277 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002278 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002279 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002280 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002281 GL_TRIANGLE_STRIP, kMeshCount, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002282 } else {
2283 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002284 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002285 &mMeshVertices[0].x, &mMeshVertices[0].u,
Chris Craik96a5c4c2015-01-27 15:46:35 -08002286 GL_TRIANGLE_STRIP, kMeshCount, false, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08002287 }
2288
2289 if (CC_UNLIKELY(useScaleTransform)) {
2290 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002291 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002292
2293 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002294
Tom Hudson107843d2014-09-08 11:26:26 -04002295 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002296}
2297
Tom Hudson107843d2014-09-08 11:26:26 -04002298void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002299 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002300 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002301 return;
Romain Guy6926c722010-07-12 20:20:03 -07002302 }
2303
John Reckebd52612014-12-10 16:47:36 -08002304 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002305 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2306 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002307
Tom Hudson107843d2014-09-08 11:26:26 -04002308 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002309}
2310
Tom Hudson107843d2014-09-08 11:26:26 -04002311void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002312 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2313 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002314 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002315 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002316 }
2317
Romain Guy211370f2012-02-01 16:10:55 -08002318 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002319 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002320 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002321 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002322 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002323
Romain Guya4adcf02013-02-28 12:15:35 -08002324 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2325 texture->setFilter(GL_LINEAR, true);
2326
Chris Craikd6b65f62014-01-01 14:45:21 -08002327 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002328 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002329 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002330 const float offsetX = left + currentTransform()->getTranslateX();
2331 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002332 const size_t count = mesh->quads.size();
2333 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002334 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002335 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002336 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2337 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2338 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002339 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002340 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002341 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002342 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002343 }
2344 }
2345
Chris Craik4063a0e2013-11-15 16:06:56 -08002346 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002347 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002348 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2349 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002350
Romain Guy3b748a42013-04-17 18:54:38 -07002351 right = x + right - left;
2352 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002353 left = x;
2354 top = y;
2355 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002356 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002357 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2358 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002359 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2360 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002361 }
Chet Haase48659092012-05-31 15:21:51 -07002362
Tom Hudson107843d2014-09-08 11:26:26 -04002363 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002364}
2365
Romain Guy03c00b52013-06-20 18:30:28 -07002366/**
2367 * Important note: this method is intended to draw batches of 9-patch objects and
2368 * will not set the scissor enable or dirty the current layer, if any.
2369 * The caller is responsible for properly dirtying the current layer.
2370 */
Tom Hudson107843d2014-09-08 11:26:26 -04002371void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002372 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002373 mCaches.activeTexture(0);
2374 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002375 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002376 const AutoTexture autoCleanup(texture);
2377
2378 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2379 texture->setFilter(GL_LINEAR, true);
2380
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002381 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2382 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002383 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002384
Tom Hudson107843d2014-09-08 11:26:26 -04002385 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002386}
2387
Tom Hudson107843d2014-09-08 11:26:26 -04002388void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002389 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002390 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002391 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002392 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002393 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002394 }
2395
Chris Craik0d5ac952014-07-15 13:01:02 -07002396 Rect bounds(vertexBuffer.getBounds());
2397 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002398 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2399
Chris Craikcb4d6002012-09-25 12:00:29 -07002400 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002401 bool isAA = paint->isAntiAlias();
2402
Chris Craik710f46d2012-09-17 17:25:49 -07002403 setupDraw();
2404 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002405 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002406 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002407 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002408 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002409 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002410 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002411 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2412 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002413 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002414 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002415 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002416
ztenghui55bfb4e2013-12-03 10:38:55 -08002417 const void* vertices = vertexBuffer.getBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -08002418 mRenderState.meshState().unbindMeshBuffer();
2419 mRenderState.meshState().bindPositionVertexPointer(mCaches.currentProgram,
2420 true, vertices, isAA ? kAlphaVertexStride : kVertexStride);
2421 mRenderState.meshState().resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002422
Chris Craik710f46d2012-09-17 17:25:49 -07002423 int alphaSlot = -1;
2424 if (isAA) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002425 void* alphaCoords = ((GLbyte*) vertices) + kVertexAlphaOffset;
Chris Craik710f46d2012-09-17 17:25:49 -07002426 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002427 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002428 glEnableVertexAttribArray(alphaSlot);
Chris Craik96a5c4c2015-01-27 15:46:35 -08002429 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, kAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002430 }
Romain Guy04299382012-07-18 17:15:41 -07002431
Chris Craik05f3d6e2014-06-02 16:27:04 -07002432 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2433 if (mode == VertexBuffer::kStandard) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002434 mRenderState.meshState().unbindIndicesBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08002435 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002436 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002437 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002438 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2439 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002440 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002441 mRenderState.meshState().bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002442 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2443 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002444 } else if (mode == VertexBuffer::kIndices) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08002445 mRenderState.meshState().unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002446 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2447 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002448 }
Romain Guy04299382012-07-18 17:15:41 -07002449
Chris Craik710f46d2012-09-17 17:25:49 -07002450 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002451 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002452 }
Chris Craik65cd6122012-12-10 17:56:27 -08002453
Tom Hudson107843d2014-09-08 11:26:26 -04002454 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002455}
2456
2457/**
Chris Craik65cd6122012-12-10 17:56:27 -08002458 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2459 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2460 * screen space in all directions. However, instead of using a fragment shader to compute the
2461 * translucency of the color from its position, we simply use a varying parameter to define how far
2462 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2463 *
2464 * Doesn't yet support joins, caps, or path effects.
2465 */
Tom Hudson107843d2014-09-08 11:26:26 -04002466void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002467 VertexBuffer vertexBuffer;
2468 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002469 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002470 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002471}
2472
2473/**
2474 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2475 * and additional geometry for defining an alpha slope perimeter.
2476 *
2477 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2478 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2479 * in-shader alpha region, but found it to be taxing on some GPUs.
2480 *
2481 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2482 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002483 */
Tom Hudson107843d2014-09-08 11:26:26 -04002484void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002485 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002486
Chris Craik65cd6122012-12-10 17:56:27 -08002487 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002488
Chris Craik65cd6122012-12-10 17:56:27 -08002489 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002490 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2491 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002492
Chris Craik05f3d6e2014-06-02 16:27:04 -07002493 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002494 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002495 }
2496
Chris Craikbf759452014-08-11 16:00:44 -07002497 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002498 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002499}
2500
Tom Hudson107843d2014-09-08 11:26:26 -04002501void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002502 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002503
Chris Craik6d29c8d2013-05-08 18:35:44 -07002504 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002505
Chris Craik6d29c8d2013-05-08 18:35:44 -07002506 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002507 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002508
Chris Craik05f3d6e2014-06-02 16:27:04 -07002509 const Rect& bounds = buffer.getBounds();
2510 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002511 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002512 }
2513
Chris Craikbf759452014-08-11 16:00:44 -07002514 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002515 drawVertexBuffer(buffer, paint, displayFlags);
2516
2517 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002518}
2519
Tom Hudson107843d2014-09-08 11:26:26 -04002520void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002521 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002522 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002523
Rob Tsuk487a92c2015-01-06 13:22:54 -08002524 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002525 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002526
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002527 SkPaint paint;
2528 paint.setColor(color);
2529 paint.setXfermodeMode(mode);
2530
2531 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002532
Tom Hudson107843d2014-09-08 11:26:26 -04002533 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002534}
Romain Guy9d5316e2010-06-24 19:30:36 -07002535
Tom Hudson107843d2014-09-08 11:26:26 -04002536void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002537 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002538 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002539 const AutoTexture autoCleanup(texture);
2540
2541 const float x = left + texture->left - texture->offset;
2542 const float y = top + texture->top - texture->offset;
2543
2544 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002545
Tom Hudson107843d2014-09-08 11:26:26 -04002546 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002547}
2548
Tom Hudson107843d2014-09-08 11:26:26 -04002549void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002550 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002551 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002552 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002553 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002554 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002555 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002556
Chris Craike84a2082014-12-22 14:28:49 -08002557 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002558 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002559 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002560 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002561 drawShape(left, top, texture, p);
2562 } else {
2563 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2564 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2565 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002566 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002567}
2568
Tom Hudson107843d2014-09-08 11:26:26 -04002569void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002570 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002571 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002572 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002573 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002574 }
Chris Craike84a2082014-12-22 14:28:49 -08002575 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002576 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002577 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002578 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002579 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002580 SkPath path;
2581 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2582 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2583 } else {
2584 path.addCircle(x, y, radius);
2585 }
2586 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002587 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002588}
Romain Guy01d58e42011-01-19 21:54:02 -08002589
Tom Hudson107843d2014-09-08 11:26:26 -04002590void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002591 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002592 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002593 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002594 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002595 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002596 }
Romain Guy01d58e42011-01-19 21:54:02 -08002597
Chris Craike84a2082014-12-22 14:28:49 -08002598 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002599 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002600 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002601 drawShape(left, top, texture, p);
2602 } else {
2603 SkPath path;
2604 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2605 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2606 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2607 }
2608 path.addOval(rect);
2609 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002610 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002611}
2612
Tom Hudson107843d2014-09-08 11:26:26 -04002613void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002614 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002615 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002616 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002617 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002618 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002619 }
2620
Chris Craik780c1282012-10-04 14:10:49 -07002621 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002622 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002623 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002624 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002625 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002626 drawShape(left, top, texture, p);
2627 return;
Chris Craik780c1282012-10-04 14:10:49 -07002628 }
Chris Craik780c1282012-10-04 14:10:49 -07002629 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2630 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2631 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2632 }
2633
2634 SkPath path;
2635 if (useCenter) {
2636 path.moveTo(rect.centerX(), rect.centerY());
2637 }
2638 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2639 if (useCenter) {
2640 path.close();
2641 }
Tom Hudson107843d2014-09-08 11:26:26 -04002642 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002643}
2644
Romain Guycf8675e2012-10-02 12:32:25 -07002645// See SkPaintDefaults.h
2646#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2647
Tom Hudson107843d2014-09-08 11:26:26 -04002648void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002649 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002650 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002651 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002652 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002653 return;
Romain Guy6926c722010-07-12 20:20:03 -07002654 }
2655
Chris Craik710f46d2012-09-17 17:25:49 -07002656 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002657 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002658 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002659 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2660 mCaches.activeTexture(0);
2661 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002662 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002663 drawShape(left, top, texture, p);
2664 } else {
2665 SkPath path;
2666 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2667 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2668 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2669 }
2670 path.addRect(rect);
2671 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002672 }
Chet Haase858aa932011-05-12 09:06:00 -07002673 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002674 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2675 SkPath path;
2676 path.addRect(left, top, right, bottom);
2677 drawConvexPath(path, p);
2678 } else {
2679 drawColorRect(left, top, right, bottom, p);
2680
2681 mDirty = true;
2682 }
Chet Haase858aa932011-05-12 09:06:00 -07002683 }
Romain Guyc7d53492010-06-25 13:41:57 -07002684}
Romain Guy9d5316e2010-06-24 19:30:36 -07002685
Chris Craikd218a922014-01-02 17:13:34 -08002686void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2687 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002688 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002689 mCaches.activeTexture(0);
2690
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002691 TextShadow textShadow;
2692 if (!getTextShadow(paint, &textShadow)) {
2693 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2694 }
2695
Raph Levien416a8472012-07-19 22:48:17 -07002696 // NOTE: The drop shadow will not perform gamma correction
2697 // if shader-based correction is enabled
2698 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2699 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002700 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002701 // If the drop shadow exceeds the max texture size or couldn't be
2702 // allocated, skip drawing
2703 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002704 const AutoTexture autoCleanup(shadow);
2705
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002706 const float sx = x - shadow->left + textShadow.dx;
2707 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002708
Tom Hudson984162f2014-10-10 13:38:16 -04002709 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002710 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002711 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002712 }
2713
2714 setupDraw();
2715 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002716 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002717 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002718 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002719 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002720 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002721 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2722 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002723 setupDrawTexture(shadow->id);
2724 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002725 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002726 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08002727 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002728
Chris Craik96a5c4c2015-01-27 15:46:35 -08002729 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Raph Levien416a8472012-07-19 22:48:17 -07002730}
2731
Romain Guy768bffc2013-02-27 13:50:45 -08002732bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002733 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002734 return MathUtils::isZero(alpha)
2735 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002736}
2737
Tom Hudson107843d2014-09-08 11:26:26 -04002738void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002739 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002740 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002741 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002742 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002743
Romain Guy671d6cf2012-01-18 12:39:17 -08002744 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002745 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002746 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 }
2748
Chris Craik65fe5ee2015-01-26 18:06:29 -08002749 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002750
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 float x = 0.0f;
2752 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002753 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002755 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2756 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002757 }
2758
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002759 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002760 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002761
2762 int alpha;
2763 SkXfermode::Mode mode;
2764 getAlphaAndMode(paint, &alpha, &mode);
2765
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002766 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002767 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002768 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002769 }
2770
Romain Guy671d6cf2012-01-18 12:39:17 -08002771 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002772 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002773 if (pureTranslate && !linearFilter) {
2774 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2775 }
Romain Guy257ae352013-03-20 16:31:12 -07002776 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002777
Rob Tsuk487a92c2015-01-06 13:22:54 -08002778 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002779 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2780
Romain Guy211370f2012-02-01 16:10:55 -08002781 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002782
Victoria Lease1e546812013-06-25 14:25:17 -07002783 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002784 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002785 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002786 if (hasActiveLayer) {
2787 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002788 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002789 }
2790 dirtyLayerUnchecked(bounds, getRegion());
2791 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002792 }
Chet Haase48659092012-05-31 15:21:51 -07002793
Tom Hudson107843d2014-09-08 11:26:26 -04002794 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002795}
2796
Chris Craik59744b72014-07-01 17:56:52 -07002797bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002798 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002799 outMatrix->setIdentity();
2800 return false;
2801 } else if (CC_UNLIKELY(transform.isPerspective())) {
2802 outMatrix->setIdentity();
2803 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002804 }
Chris Craik59744b72014-07-01 17:56:52 -07002805
2806 /**
Chris Craika736cd92014-08-04 13:18:38 -07002807 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2808 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002809 */
2810 float sx, sy;
2811 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002812 outMatrix->setScale(
2813 roundf(fmaxf(1.0f, sx)),
2814 roundf(fmaxf(1.0f, sy)));
2815 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002816}
2817
Tom Hudson984162f2014-10-10 13:38:16 -04002818int OpenGLRenderer::getSaveCount() const {
2819 return mState.getSaveCount();
2820}
2821
2822int OpenGLRenderer::save(int flags) {
2823 return mState.save(flags);
2824}
2825
2826void OpenGLRenderer::restore() {
2827 return mState.restore();
2828}
2829
2830void OpenGLRenderer::restoreToCount(int saveCount) {
2831 return mState.restoreToCount(saveCount);
2832}
2833
2834void OpenGLRenderer::translate(float dx, float dy, float dz) {
2835 return mState.translate(dx, dy, dz);
2836}
2837
2838void OpenGLRenderer::rotate(float degrees) {
2839 return mState.rotate(degrees);
2840}
2841
2842void OpenGLRenderer::scale(float sx, float sy) {
2843 return mState.scale(sx, sy);
2844}
2845
2846void OpenGLRenderer::skew(float sx, float sy) {
2847 return mState.skew(sx, sy);
2848}
2849
2850void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2851 mState.setMatrix(matrix);
2852}
2853
2854void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2855 mState.concatMatrix(matrix);
2856}
2857
2858bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2859 return mState.clipRect(left, top, right, bottom, op);
2860}
2861
2862bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2863 return mState.clipPath(path, op);
2864}
2865
2866bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2867 return mState.clipRegion(region, op);
2868}
2869
2870void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2871 mState.setClippingOutline(allocator, outline);
2872}
2873
2874void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2875 const Rect& rect, float radius, bool highPriority) {
2876 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2877}
2878
Tom Hudson107843d2014-09-08 11:26:26 -04002879void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002880 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002881 DrawOpMode drawOpMode) {
2882
Chris Craik527a3aa2013-03-04 10:19:31 -08002883 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002884 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2885 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002886 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002887 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002888 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002889 }
Romain Guycac5fd32011-12-01 20:08:50 -08002890 }
2891
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002892 const float oldX = x;
2893 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002894
Chris Craikd6b65f62014-01-01 14:45:21 -08002895 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002896 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002897
Romain Guy211370f2012-02-01 16:10:55 -08002898 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002899 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2900 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002901 }
2902
Romain Guy86568192010-12-14 15:55:39 -08002903 int alpha;
2904 SkXfermode::Mode mode;
2905 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002906
Romain Guyc74f45a2013-02-26 19:10:14 -08002907 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2908
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002909 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002910 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002911 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002912 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002913 }
2914
Romain Guy19d4dd82013-03-04 11:14:26 -08002915 const bool hasActiveLayer = hasLayer();
2916
Romain Guy624234f2013-03-05 16:43:31 -08002917 // We only pass a partial transform to the font renderer. That partial
2918 // matrix defines how glyphs are rasterized. Typically we want glyphs
2919 // to be rasterized at their final size on screen, which means the partial
2920 // matrix needs to take the scale factor into account.
2921 // When a partial matrix is used to transform glyphs during rasterization,
2922 // the mesh is generated with the inverse transform (in the case of scale,
2923 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2924 // apply the full transform matrix at draw time in the vertex shader.
2925 // Applying the full matrix in the shader is the easiest way to handle
2926 // rotation and perspective and allows us to always generated quads in the
2927 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002928 SkMatrix fontTransform;
2929 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2930 || fabs(y - (int) y) > 0.0f
2931 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002932 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002933 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002934
Romain Guy624234f2013-03-05 16:43:31 -08002935 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002936 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002937 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 -07002938
Raph Levien996e57c2012-07-23 15:22:52 -07002939 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002940 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002941
2942 // don't call issuedrawcommand, do it at end of batch
2943 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002944 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002945 SkPaint paintCopy(*paint);
2946 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2947 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002948 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002949 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002950 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002951 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002952 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002953
Chris Craik527a3aa2013-03-04 10:19:31 -08002954 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002955 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002956 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002957 }
Chris Craik41541822013-05-03 16:35:54 -07002958 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002959 }
Romain Guy694b5192010-07-21 21:33:20 -07002960
Chris Craike63f7c622013-10-17 10:30:55 -07002961 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002962
Tom Hudson107843d2014-09-08 11:26:26 -04002963 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002964}
2965
Tom Hudson107843d2014-09-08 11:26:26 -04002966void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002967 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002968 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002969 return;
Romain Guy03d58522012-02-24 17:54:07 -08002970 }
2971
Chris Craik39a908c2013-06-13 14:39:01 -07002972 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
Chris Craik65fe5ee2015-01-26 18:06:29 -08002973 mRenderState.scissor().setEnabled(true);
Chris Craik39a908c2013-06-13 14:39:01 -07002974
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002975 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002976 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002977 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002978
2979 int alpha;
2980 SkXfermode::Mode mode;
2981 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002982 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002983
Tom Hudson984162f2014-10-10 13:38:16 -04002984 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002985 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 -08002986
Romain Guy97771732012-02-28 18:17:02 -08002987 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002988
2989 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002990 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002991 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002992 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002993 dirtyLayerUnchecked(bounds, getRegion());
2994 }
Romain Guy97771732012-02-28 18:17:02 -08002995 }
Chet Haase48659092012-05-31 15:21:51 -07002996
Tom Hudson107843d2014-09-08 11:26:26 -04002997 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002998}
2999
Tom Hudson107843d2014-09-08 11:26:26 -04003000void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003001 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07003002
Romain Guya1d3c912011-12-13 14:55:06 -08003003 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003004
Romain Guyfb8b7632010-08-23 21:05:08 -07003005 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003006 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003007 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003008
Romain Guy8b55f372010-08-18 17:10:07 -07003009 const float x = texture->left - texture->offset;
3010 const float y = texture->top - texture->offset;
3011
Romain Guy01d58e42011-01-19 21:54:02 -08003012 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003013 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003014}
3015
Tom Hudson107843d2014-09-08 11:26:26 -04003016void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003017 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003018 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003019 }
3020
Chris Craike84a2082014-12-22 14:28:49 -08003021 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003022 if (layer->isTextureLayer()) {
3023 transform = &layer->getTransform();
3024 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003025 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003026 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003027 }
3028 }
3029
Chris Craik39a908c2013-06-13 14:39:01 -07003030 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003031 const bool rejected = mState.calculateQuickRejectForScissor(
3032 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3033 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003034
3035 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003036 if (transform && !transform->isIdentity()) {
3037 restore();
3038 }
Tom Hudson107843d2014-09-08 11:26:26 -04003039 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003040 }
3041
Chris Craik62d307c2014-07-29 10:35:13 -07003042 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3043 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3044
Romain Guy5bb3c732012-11-29 17:52:58 -08003045 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003046
Chris Craik65fe5ee2015-01-26 18:06:29 -08003047 mRenderState.scissor().setEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003048 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003049
Romain Guy211370f2012-02-01 16:10:55 -08003050 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003051 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003052 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3053 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003054 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003055
Chris Craik16ecda52013-03-29 10:59:59 -07003056 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003057 setupDraw();
3058 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003059 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003060 setupDrawColorFilter(layer->getColorFilter());
3061 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003062 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003063 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003064 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003065 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003066 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3067 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3068 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003069
Romain Guyd21b6e12011-11-30 20:21:23 -08003070 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003071 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003072 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003073 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003074 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003075 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003076 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3077 }
Romain Guyf219da52011-01-16 12:54:25 -08003078
Romain Guy448455f2013-07-22 13:57:50 -07003079 TextureVertex* mesh = &layer->mesh[0];
3080 GLsizei elementsCount = layer->meshElementCount;
3081
3082 while (elementsCount > 0) {
Chris Craik96a5c4c2015-01-27 15:46:35 -08003083 GLsizei drawCount = min(elementsCount, (GLsizei) kMaxNumberOfQuads * 6);
Romain Guy448455f2013-07-22 13:57:50 -07003084
Romain Guy3380cfd2013-08-15 16:57:57 -07003085 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003086 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003087 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003088
3089 elementsCount -= drawCount;
3090 // Though there are 4 vertices in a quad, we use 6 indices per
3091 // quad to draw with GL_TRIANGLES
3092 mesh += (drawCount / 6) * 4;
3093 }
Romain Guyf219da52011-01-16 12:54:25 -08003094
Romain Guy3a3133d2011-02-01 22:59:58 -08003095#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003096 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003097#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003098 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003099
Romain Guy5bb3c732012-11-29 17:52:58 -08003100 if (layer->debugDrawUpdate) {
3101 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003102
3103 SkPaint paint;
3104 paint.setColor(0x7f00ff00);
3105 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003106 }
Romain Guyf219da52011-01-16 12:54:25 -08003107 }
Chris Craik34416ea2013-04-15 16:08:28 -07003108 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003109
Romain Guyb2e2f242012-10-17 18:18:35 -07003110 if (transform && !transform->isIdentity()) {
3111 restore();
3112 }
3113
Tom Hudson107843d2014-09-08 11:26:26 -04003114 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003115}
3116
Romain Guy6926c722010-07-12 20:20:03 -07003117///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003118// Draw filters
3119///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003120void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3121 // We should never get here since we apply the draw filter when stashing
3122 // the paints in the DisplayList.
3123 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003124}
3125
3126///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003127// Drawing implementation
3128///////////////////////////////////////////////////////////////////////////////
3129
Chris Craikd218a922014-01-02 17:13:34 -08003130Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003131 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003132 if (!texture) {
3133 return mCaches.textureCache.get(bitmap);
3134 }
3135 return texture;
3136}
3137
Romain Guy01d58e42011-01-19 21:54:02 -08003138void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003139 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003140 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003141 return;
3142 }
3143
3144 int alpha;
3145 SkXfermode::Mode mode;
3146 getAlphaAndMode(paint, &alpha, &mode);
3147
3148 setupDraw();
3149 setupDrawWithTexture(true);
3150 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003151 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003152 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003153 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003154 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003155 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3156 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003157 setupDrawTexture(texture->id);
3158 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003159 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003160 setupDrawShaderUniforms(getShader(paint));
Chris Craik96a5c4c2015-01-27 15:46:35 -08003161 setupDrawMesh(nullptr, (GLvoid*) kMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003162
Chris Craik96a5c4c2015-01-27 15:46:35 -08003163 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003164}
3165
Romain Guyf607bdc2010-09-10 19:20:06 -07003166// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003167#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3168#define kStdUnderline_Offset (1.0f / 9.0f)
3169#define kStdUnderline_Thickness (1.0f / 18.0f)
3170
Chris Craikd218a922014-01-02 17:13:34 -08003171void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3172 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003173 // Handle underline and strike-through
3174 uint32_t flags = paint->getFlags();
3175 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003176 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003177
Romain Guy211370f2012-02-01 16:10:55 -08003178 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003179 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003180 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003181
Raph Levien8b4072d2012-07-30 15:50:00 -07003182 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003183 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003184
Romain Guyf6834472011-01-23 13:32:12 -08003185 int linesCount = 0;
3186 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3187 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3188
3189 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003190 float points[pointsCount];
3191 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003192
3193 if (flags & SkPaint::kUnderlineText_Flag) {
3194 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003195 points[currentPoint++] = left;
3196 points[currentPoint++] = top;
3197 points[currentPoint++] = left + underlineWidth;
3198 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003199 }
3200
3201 if (flags & SkPaint::kStrikeThruText_Flag) {
3202 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003203 points[currentPoint++] = left;
3204 points[currentPoint++] = top;
3205 points[currentPoint++] = left + underlineWidth;
3206 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003207 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003208
Romain Guy726aeba2011-06-01 14:52:00 -07003209 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003210
Romain Guy726aeba2011-06-01 14:52:00 -07003211 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003212 }
3213 }
3214}
3215
Tom Hudson107843d2014-09-08 11:26:26 -04003216void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003217 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003218 return;
Romain Guy672433d2013-01-04 19:05:13 -08003219 }
3220
Tom Hudson107843d2014-09-08 11:26:26 -04003221 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003222}
3223
Tom Hudson107843d2014-09-08 11:26:26 -04003224void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003225 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003226 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003227
Chris Craik15a07a22014-01-26 13:43:53 -08003228 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craik65fe5ee2015-01-26 18:06:29 -08003229 mRenderState.scissor().setEnabled(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003230
3231 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003232 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003233
ztenghui14a4e352014-08-13 10:44:39 -07003234 // The caller has made sure casterAlpha > 0.
3235 float ambientShadowAlpha = mAmbientShadowAlpha;
3236 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3237 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3238 }
3239 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3240 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003241 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003242 }
ztenghui7b4516e2014-01-07 10:42:55 -08003243
ztenghui14a4e352014-08-13 10:44:39 -07003244 float spotShadowAlpha = mSpotShadowAlpha;
3245 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3246 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3247 }
3248 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3249 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003250 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003251 }
ztenghui7b4516e2014-01-07 10:42:55 -08003252
Tom Hudson107843d2014-09-08 11:26:26 -04003253 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003254}
3255
Tom Hudson107843d2014-09-08 11:26:26 -04003256void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003257 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003258 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003259 return;
Romain Guy3b753822013-03-05 10:27:35 -08003260 }
Romain Guy735738c2012-12-03 12:34:51 -08003261
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003262 int color = paint->getColor();
3263 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003265 color |= 0x00ffffff;
3266 }
3267
Romain Guy672433d2013-01-04 19:05:13 -08003268 float left = FLT_MAX;
3269 float top = FLT_MAX;
3270 float right = FLT_MIN;
3271 float bottom = FLT_MIN;
3272
Romain Guy448455f2013-07-22 13:57:50 -07003273 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003274 Vertex* vertex = mesh;
3275
Chris Craik2af46352012-11-26 18:30:17 -08003276 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003277 float l = rects[index + 0];
3278 float t = rects[index + 1];
3279 float r = rects[index + 2];
3280 float b = rects[index + 3];
3281
Romain Guy3b753822013-03-05 10:27:35 -08003282 Vertex::set(vertex++, l, t);
3283 Vertex::set(vertex++, r, t);
3284 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003285 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003286
Romain Guy3b753822013-03-05 10:27:35 -08003287 left = fminf(left, l);
3288 top = fminf(top, t);
3289 right = fmaxf(right, r);
3290 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003291 }
3292
Chris Craikf0a59072013-11-19 18:00:46 -08003293 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003294 return;
Romain Guya362c692013-02-04 13:50:16 -08003295 }
Romain Guy672433d2013-01-04 19:05:13 -08003296
Romain Guy672433d2013-01-04 19:05:13 -08003297 setupDraw();
3298 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003299 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003300 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003301 setupDrawColorFilter(getColorFilter(paint));
3302 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003303 setupDrawProgram();
3304 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003305 setupDrawModelView(kModelViewMode_Translate, false,
3306 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003307 setupDrawColorUniforms(getShader(paint));
3308 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003310
Romain Guy8ce00302013-01-15 18:51:42 -08003311 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003312 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003313 }
3314
Chris Craik4063a0e2013-11-15 16:06:56 -08003315 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003316
Tom Hudson107843d2014-09-08 11:26:26 -04003317 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003318}
3319
Romain Guy026c5e162010-06-28 17:12:22 -07003320void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003321 const SkPaint* paint, bool ignoreTransform) {
3322 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003323 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003324 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003325 color |= 0x00ffffff;
3326 }
3327
Romain Guy70ca14e2010-12-13 18:24:33 -08003328 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003329 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003330 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003331 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003332 setupDrawColorFilter(getColorFilter(paint));
3333 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003334 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003335 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3336 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003337 setupDrawColorUniforms(getShader(paint));
3338 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003339 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003340 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003341
Chris Craik96a5c4c2015-01-27 15:46:35 -08003342 glDrawArrays(GL_TRIANGLE_STRIP, 0, kMeshCount);
Romain Guyc95c8d62010-09-17 15:31:32 -07003343}
3344
Romain Guy82ba8142010-07-09 13:25:56 -07003345void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003346 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003347 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003348
Chris Craike84a2082014-12-22 14:28:49 -08003349 GLvoid* vertices = (GLvoid*) nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -08003350 GLvoid* texCoords = (GLvoid*) kMeshTextureOffset;
Romain Guy3b748a42013-04-17 18:54:38 -07003351
3352 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003353 vertices = &mMeshVertices[0].x;
3354 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003355
3356 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3357 texture->uvMapper->map(uvs);
3358
3359 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3360 }
3361
Chris Craikd6b65f62014-01-01 14:45:21 -08003362 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3363 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3364 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003365
Romain Guyd21b6e12011-11-30 20:21:23 -08003366 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003367 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003368 paint, texture->blend, vertices, texCoords,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003369 GL_TRIANGLE_STRIP, kMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003370 } else {
Chris Craik678625242014-02-28 12:26:34 -08003371 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Chris Craik96a5c4c2015-01-27 15:46:35 -08003373 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, kMeshCount);
Romain Guy3b748a42013-04-17 18:54:38 -07003374 }
3375
3376 if (texture->uvMapper) {
3377 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003378 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003379}
3380
Romain Guyf7f93552010-07-08 19:17:03 -07003381void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003382 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003383 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003384 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3385 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003386
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003387 int a;
3388 SkXfermode::Mode mode;
3389 getAlphaAndMode(paint, &a, &mode);
3390 const float alpha = a / 255.0f;
3391
Romain Guy746b7402010-10-26 16:27:31 -07003392 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003393 setupDrawWithTexture();
3394 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003395 setupDrawColorFilter(getColorFilter(paint));
3396 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003397 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003398 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003399 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003400 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003401 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003402 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003403 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003404
Romain Guy6820ac82010-09-15 18:11:50 -07003405 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003406}
3407
Romain Guy3b748a42013-04-17 18:54:38 -07003408void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003409 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003410 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003411 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3412 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003413
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003414 int a;
3415 SkXfermode::Mode mode;
3416 getAlphaAndMode(paint, &a, &mode);
3417 const float alpha = a / 255.0f;
3418
Romain Guy3b748a42013-04-17 18:54:38 -07003419 setupDraw();
3420 setupDrawWithTexture();
3421 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003422 setupDrawColorFilter(getColorFilter(paint));
3423 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003424 setupDrawProgram();
3425 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003426 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003427 setupDrawTexture(texture);
3428 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003429 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003430 setupDrawMeshIndices(vertices, texCoords, vbo);
3431
Chris Craike84a2082014-12-22 14:28:49 -08003432 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003433}
3434
Romain Guy886b2752013-01-04 12:26:18 -08003435void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003436 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003437 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003438 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003439
Chris Craike84a2082014-12-22 14:28:49 -08003440 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003441 int alpha;
3442 SkXfermode::Mode mode;
3443 getAlphaAndMode(paint, &alpha, &mode);
3444
Romain Guy886b2752013-01-04 12:26:18 -08003445 setupDraw();
3446 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003447 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003448 setupDrawAlpha8Color(color, alpha);
3449 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003450 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003451 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003452 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003453 setupDrawProgram();
3454 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003455 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003456 setupDrawTexture(texture);
3457 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003458 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003459 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003460 setupDrawMesh(vertices, texCoords);
3461
3462 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003463}
3464
Romain Guya5aed0d2010-09-09 14:42:43 -07003465void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003466 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003467
Chris Craike84a2082014-12-22 14:28:49 -08003468 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003469 blend = true;
3470 mDescription.hasRoundRectClip = true;
3471 }
3472 mSkipOutlineClip = true;
3473
Romain Guy82ba8142010-07-09 13:25:56 -07003474 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003475
Romain Guy82ba8142010-07-09 13:25:56 -07003476 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003477 // These blend modes are not supported by OpenGL directly and have
3478 // to be implemented using shaders. Since the shader will perform
3479 // the blending, turn blending off here
3480 // If the blend mode cannot be implemented using shaders, fall
3481 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003482 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003483 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003484 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003485 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003486
Romain Guy82bc7a72012-01-03 14:13:39 -08003487 if (mCaches.blend) {
3488 glDisable(GL_BLEND);
3489 mCaches.blend = false;
3490 }
3491
3492 return;
3493 } else {
3494 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003495 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003496 }
3497
3498 if (!mCaches.blend) {
3499 glEnable(GL_BLEND);
3500 }
3501
3502 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3503 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3504
3505 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3506 glBlendFunc(sourceMode, destMode);
3507 mCaches.lastSrcMode = sourceMode;
3508 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003509 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003510 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003511 glDisable(GL_BLEND);
3512 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003513 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003514}
3515
Romain Guy889f8d12010-07-29 14:37:42 -07003516bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003517 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003518 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003519 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003520 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003521 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003522 }
Romain Guy6926c722010-07-12 20:20:03 -07003523 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003524}
3525
Romain Guy026c5e162010-06-28 17:12:22 -07003526void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003527 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003528 TextureVertex::setUV(v++, u1, v1);
3529 TextureVertex::setUV(v++, u2, v1);
3530 TextureVertex::setUV(v++, u1, v2);
3531 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003532}
3533
Chris Craike84a2082014-12-22 14:28:49 -08003534void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3535 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003536 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003537 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3538 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003539 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003540 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003541 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003542}
3543
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003544float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003545 float alpha;
3546 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3547 alpha = mDrawModifiers.mOverrideLayerAlpha;
3548 } else {
3549 alpha = layer->getAlpha() / 255.0f;
3550 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003551 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003552}
3553
Romain Guy9d5316e2010-06-24 19:30:36 -07003554}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003555}; // namespace android