blob: 82f6ddd687a365da8eeef688fe920c34fae2dd89 [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
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070024#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040025#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070026#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070027
Romain Guye4d01122010-06-16 18:44:05 -070028#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070029#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070030
Romain Guy08aa2cb2011-03-17 11:06:57 -070031#include <private/hwui/DrawGlInfo.h>
32
Romain Guy5b3b3522010-10-27 18:57:51 -070033#include <ui/Rect.h>
34
Romain Guy85bf02f2010-06-22 13:11:24 -070035#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080036#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080037#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070038#include "Fence.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040039#include "GammaFontRenderer.h"
40#include "Patch.h"
Chris Craik65cd6122012-12-10 17:56:27 -080041#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070042#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040043#include "RenderNode.h"
44#include "RenderState.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080045#include "ShadowTessellator.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040046#include "SkiaShader.h"
Romain Guya957eea2010-12-08 18:34:42 -080047#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080048#include "VertexBuffer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040049#include "utils/GLUtils.h"
Tom Hudson8dfaa492014-12-09 15:03:44 -050050#include "utils/PaintUtils.h"
Chris Craik06e7fe52014-11-20 17:27:36 -080051#include "utils/TraceUtils.h"
Romain Guye4d01122010-06-16 18:44:05 -070052
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)
138 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700139 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700140 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700141 , mRenderState(renderState)
142 , 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
Romain Guyac670c02010-07-27 17:39:27 -0700154 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
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 Craik797b95b22014-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) {
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700260 mCaches.setScissor(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
Romain Guy7c25aab2012-10-18 15:05:02 -0700266 mCaches.resetScissor();
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
350 mCaches.resetScissor();
351 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 }
John Reck3b202512014-06-23 13:13:08 -0700381 if (mCaches.enableScissor() || 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 Guy0f6675332013-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
431 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400432 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700433 clip->right - clip->left, clip->bottom - clip->top);
434
Romain Guy627c6fd2013-08-21 11:53:18 -0700435 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700436 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700437 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
438
439 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700440 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700441 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
442
443 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700444 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700445 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
446
447 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700448 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700449 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
450
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.disable();
452 }
453}
454
455///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700456// Layers
457///////////////////////////////////////////////////////////////////////////////
458
459bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700460 if (layer->deferredUpdateScheduled && layer->renderer
461 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700462
Romain Guy7c450aa2012-09-21 19:15:00 -0700463 if (inFrame) {
464 endTiling();
465 debugOverdraw(false, false);
466 }
Romain Guy11cb6422012-09-21 00:39:43 -0700467
Romain Guy96885eb2013-03-26 15:05:58 -0700468 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700469 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700470 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700471 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700472 }
Romain Guy11cb6422012-09-21 00:39:43 -0700473
474 if (inFrame) {
475 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800476 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700477 }
478
Romain Guy5bb3c732012-11-29 17:52:58 -0800479 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700480 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700481
482 return true;
483 }
484
485 return false;
486}
487
488void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700489 // If draw deferring is enabled this method will simply defer
490 // the display list of each individual layer. The layers remain
491 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700492 int count = mLayerUpdates.size();
493 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700494 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
495 startMark("Layer Updates");
496 } else {
497 startMark("Defer Layer Updates");
498 }
Romain Guy11cb6422012-09-21 00:39:43 -0700499
Chris Craik1206b9b2013-04-04 14:46:24 -0700500 // Note: it is very important to update the layers in order
501 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700502 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700503 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700504 }
Romain Guy11cb6422012-09-21 00:39:43 -0700505
Romain Guy96885eb2013-03-26 15:05:58 -0700506 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
507 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400508 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700509 }
510 endMark();
511 }
512}
513
514void OpenGLRenderer::flushLayers() {
515 int count = mLayerUpdates.size();
516 if (count > 0) {
517 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700518
Chris Craik1206b9b2013-04-04 14:46:24 -0700519 // Note: it is very important to update the layers in order
520 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800521 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700522 }
523
524 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400525 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700526
Romain Guy11cb6422012-09-21 00:39:43 -0700527 endMark();
528 }
529}
530
531void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
532 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700533 // Make sure we don't introduce duplicates.
534 // SortedVector would do this automatically but we need to respect
535 // the insertion order. The linear search is not an issue since
536 // this list is usually very short (typically one item, at most a few)
537 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
538 if (mLayerUpdates.itemAt(i) == layer) {
539 return;
540 }
541 }
Romain Guy11cb6422012-09-21 00:39:43 -0700542 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700543 }
544}
545
Romain Guye93482f2013-06-17 13:14:51 -0700546void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
547 if (layer) {
548 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
549 if (mLayerUpdates.itemAt(i) == layer) {
550 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700551 break;
552 }
553 }
554 }
555}
556
Romain Guy40543602013-06-12 15:31:28 -0700557void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800558 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700559 syncState();
560 updateLayers();
561 flushLayers();
562 // Wait for all the layer updates to be executed
563 AutoFence fence;
564}
565
John Reck443a7142014-09-04 17:40:05 -0700566void OpenGLRenderer::markLayersAsBuildLayers() {
567 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
568 mLayerUpdates[i]->wasBuildLayered = true;
569 }
570}
571
Romain Guy11cb6422012-09-21 00:39:43 -0700572///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700573// State management
574///////////////////////////////////////////////////////////////////////////////
575
Chris Craik14e51302013-12-30 15:32:54 -0800576void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700577 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800578 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
579 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700580
Chris Craika64a2be2014-05-14 14:17:01 -0700581 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700582 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700583 }
584
Romain Guy2542d192010-08-18 11:47:12 -0700585 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700586 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700587 }
Romain Guy2542d192010-08-18 11:47:12 -0700588
Romain Guy5ec99242010-11-03 16:19:08 -0700589 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700590 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700591 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700592 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800593 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700594 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700595 }
Romain Guybb9524b2010-06-22 18:56:38 -0700596}
597
Romain Guyf6a11b82010-06-23 17:47:49 -0700598///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700599// Layers
600///////////////////////////////////////////////////////////////////////////////
601
602int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700603 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700604 // force matrix/clip isolation for layer
605 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
606
Tom Hudson984162f2014-10-10 13:38:16 -0400607 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700608
Tom Hudson984162f2014-10-10 13:38:16 -0400609 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700610 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700611 }
Romain Guyd55a8612010-06-28 17:42:46 -0700612
613 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700614}
615
Chris Craikd90144d2013-03-19 15:03:48 -0700616void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
617 const Rect untransformedBounds(bounds);
618
Chris Craikd6b65f62014-01-01 14:45:21 -0800619 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700620
621 // Layers only make sense if they are in the framebuffer's bounds
Rob Tsuk487a92c2015-01-06 13:22:54 -0800622 if (bounds.intersect(mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700623 // We cannot work with sub-pixels in this case
624 bounds.snapToPixelBoundaries();
625
626 // When the layer is not an FBO, we may use glCopyTexImage so we
627 // need to make sure the layer does not extend outside the bounds
628 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700629 const Snapshot& previous = *(currentSnapshot()->previous);
630 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
631 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700632 bounds.setEmpty();
633 } else if (fboLayer) {
634 clip.set(bounds);
635 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800636 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700637 inverse.mapRect(clip);
638 clip.snapToPixelBoundaries();
639 if (clip.intersect(untransformedBounds)) {
640 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
641 bounds.set(untransformedBounds);
642 } else {
643 clip.setEmpty();
644 }
645 }
646 } else {
647 bounds.setEmpty();
648 }
649}
650
Chris Craik408eb122013-03-26 18:55:15 -0700651void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
652 bool fboLayer, int alpha) {
653 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
654 bounds.getHeight() > mCaches.maxTextureSize ||
655 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400656 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700657 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400658 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700659 }
660}
661
Chris Craikd90144d2013-03-19 15:03:48 -0700662int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500663 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400664 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700665
Tom Hudson984162f2014-10-10 13:38:16 -0400666 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700667 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
668 // operations will be able to store and restore the current clip and transform info, and
669 // quick rejection will be correct (for display lists)
670
671 Rect bounds(left, top, right, bottom);
672 Rect clip;
673 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500674 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700675
Tom Hudson984162f2014-10-10 13:38:16 -0400676 if (!mState.currentlyIgnored()) {
677 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
678 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
679 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800680 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700681 }
682 }
683
684 return count;
685}
686
Romain Guy1c740bc2010-09-13 18:00:09 -0700687/**
688 * Layers are viewed by Skia are slightly different than layers in image editing
689 * programs (for instance.) When a layer is created, previously created layers
690 * and the frame buffer still receive every drawing command. For instance, if a
691 * layer is created and a shape intersecting the bounds of the layers and the
692 * framebuffer is draw, the shape will be drawn on both (unless the layer was
693 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
694 *
695 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
696 * texture. Unfortunately, this is inefficient as it requires every primitive to
697 * be drawn n + 1 times, where n is the number of active layers. In practice this
698 * means, for every primitive:
699 * - Switch active frame buffer
700 * - Change viewport, clip and projection matrix
701 * - Issue the drawing
702 *
703 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700704 * To avoid this, layers are implemented in a different way here, at least in the
705 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
706 * is set. When this flag is set we can redirect all drawing operations into a
707 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700708 *
709 * This implementation relies on the frame buffer being at least RGBA 8888. When
710 * a layer is created, only a texture is created, not an FBO. The content of the
711 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700712 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700713 * buffer and drawing continues as normal. This technique therefore treats the
714 * frame buffer as a scratch buffer for the layers.
715 *
716 * To compose the layers back onto the frame buffer, each layer texture
717 * (containing the original frame buffer data) is drawn as a simple quad over
718 * the frame buffer. The trick is that the quad is set as the composition
719 * destination in the blending equation, and the frame buffer becomes the source
720 * of the composition.
721 *
722 * Drawing layers with an alpha value requires an extra step before composition.
723 * An empty quad is drawn over the layer's region in the frame buffer. This quad
724 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
725 * quad is used to multiply the colors in the frame buffer. This is achieved by
726 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
727 * GL_ZERO, GL_SRC_ALPHA.
728 *
729 * Because glCopyTexImage2D() can be slow, an alternative implementation might
730 * be use to draw a single clipped layer. The implementation described above
731 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700732 *
733 * (1) The frame buffer is actually not cleared right away. To allow the GPU
734 * to potentially optimize series of calls to glCopyTexImage2D, the frame
735 * buffer is left untouched until the first drawing operation. Only when
736 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700737 */
Chet Haased48885a2012-08-28 17:43:28 -0700738bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700739 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800740 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
741 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700742
Romain Guyeb993562010-10-05 18:14:38 -0700743 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
744
Romain Guyf607bdc2010-09-10 19:20:06 -0700745 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700746 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700747 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700748 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000749 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700750
751 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400752 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700753 return false;
754 }
Romain Guyf18fd992010-07-08 11:45:51 -0700755
Romain Guya1d3c912011-12-13 14:55:06 -0800756 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700757 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700758 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700759 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700760 }
761
Derek Sollenberger674554f2014-02-19 16:47:32 +0000762 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700763 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
765 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500766
Chet Haasea23eed82012-04-12 15:19:04 -0700767 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700768 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700769 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700770
Romain Guy8fb95422010-08-17 18:38:51 -0700771 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400772 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
773 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700774
Chris Craika8bea8e2014-09-24 11:29:43 -0700775 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
776 fboLayer ? "" : "unclipped ",
777 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700778 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700779 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700780 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700781 } else {
782 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700783 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800784 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700786 // Workaround for some GL drivers. When reading pixels lying outside
787 // of the window we should get undefined values for those pixels.
788 // Unfortunately some drivers will turn the entire target texture black
789 // when reading outside of the window.
790 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800791 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800793 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700794
Chris Craika64a2be2014-05-14 14:17:01 -0700795 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
796 bounds.left, getViewportHeight() - bounds.bottom,
797 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700798
Romain Guy54be1cd2011-06-13 19:04:27 -0700799 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800800 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700801 }
Romain Guyeb993562010-10-05 18:14:38 -0700802 }
Romain Guyf86ef572010-07-01 11:05:42 -0700803
Romain Guyd55a8612010-06-28 17:42:46 -0700804 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700805}
806
Chris Craike63f7c622013-10-17 10:30:55 -0700807bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800808 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700810
Tom Hudson984162f2014-10-10 13:38:16 -0400811 writableSnapshot()->region = &writableSnapshot()->layer->region;
812 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
813 writableSnapshot()->fbo = layer->getFbo();
814 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
815 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
816 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800817 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy2b7028e2012-09-19 17:25:38 -0700819 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700820 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700822 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700827 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829 }
830
831 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
henry.uh_chen33f5a592014-07-02 19:36:56 +0800834 // Expand the startTiling region by 1
835 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
837 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700838 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800839 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 glClear(GL_COLOR_BUFFER_BIT);
842
843 dirtyClip();
844
845 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700846 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 return true;
848}
849
Romain Guy1c740bc2010-09-13 18:00:09 -0700850/**
851 * Read the documentation of createLayer() before doing anything in this method.
852 */
Chris Craik14e51302013-12-30 15:32:54 -0800853void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
854 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000855 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700856 return;
857 }
858
Chris Craik14e51302013-12-30 15:32:54 -0800859 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800860 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800861 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700862
Chris Craik39a908c2013-06-13 14:39:01 -0700863 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400864 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800865 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700866 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
867
Romain Guyeb993562010-10-05 18:14:38 -0700868 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700869 endTiling();
870
Romain Guye0aa84b2012-04-03 19:30:26 -0700871 // Detach the texture from the FBO
872 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800873
874 layer->removeFbo(false);
875
Romain Guyeb993562010-10-05 18:14:38 -0700876 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700877 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700878 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700879
Chris Craikd6b65f62014-01-01 14:45:21 -0800880 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700881 }
882
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500884 SkPaint layerPaint;
885 layerPaint.setAlpha(layer->getAlpha());
886 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
887 layerPaint.setColorFilter(layer->getColorFilter());
888
889 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700891 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700892 }
893
Romain Guy03750a02010-10-18 14:06:08 -0700894 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700895
Romain Guya1d3c912011-12-13 14:55:06 -0800896 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700897
Romain Guy5b3b3522010-10-27 18:57:51 -0700898 // When the layer is stored in an FBO, we can save a bit of fillrate by
899 // drawing only the dirty region
900 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800901 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 } else if (!rect.isEmpty()) {
904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530905
906 save(0);
907 // the layer contains screen buffer content that shouldn't be alpha modulated
908 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400909 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700910 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530911 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700912 }
Romain Guy8b55f372010-08-18 17:10:07 -0700913
Romain Guy746b7402010-10-26 16:27:31 -0700914 dirtyClip();
915
Romain Guyeb993562010-10-05 18:14:38 -0700916 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800917 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700918 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800919 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800920 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700921 }
922}
923
Romain Guyaa6c24c2011-04-28 18:40:04 -0700924void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700925 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700926
927 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700929 setupDrawWithTexture();
930 } else {
931 setupDrawWithExternalTexture();
932 }
933 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500935 setupDrawColorFilter(layer->getColorFilter());
936 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700938 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500939 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
941 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700942 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700944 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800945 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800946 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700947 layer->getWidth() == (uint32_t) rect.getWidth() &&
948 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800949 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
950 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700951
Romain Guyd21b6e12011-11-30 20:21:23 -0800952 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800953 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
954 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700955 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800956 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800957 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
958 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700959 }
960 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700961 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962
963 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964}
965
Romain Guy5b3b3522010-10-27 18:57:51 -0700966void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700967 if (layer->isTextureLayer()) {
968 EVENT_LOGD("composeTextureLayerRect");
969 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
970 drawTextureLayer(layer, rect);
971 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
972 } else {
973 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700974 const Rect& texCoords = layer->texCoords;
975 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
976 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700977
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 float x = rect.left;
979 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800980 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700981 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700982 layer->getHeight() == (uint32_t) rect.getHeight();
983
984 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700985 // When we're swapping, the layer is already in screen coordinates
986 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800987 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
988 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700989 }
990
Romain Guyd21b6e12011-11-30 20:21:23 -0800991 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800993 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 }
995
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500996 SkPaint layerPaint;
997 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
998 layerPaint.setXfermodeMode(layer->getMode());
999 layerPaint.setColorFilter(layer->getColorFilter());
1000
1001 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001003 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001004 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001006
Romain Guyaa6c24c2011-04-28 18:40:04 -07001007 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001008 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001009}
1010
Chris Craik34416ea2013-04-15 16:08:28 -07001011/**
1012 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1013 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1014 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1015 * by saveLayer's restore
1016 */
Tom Hudson984162f2014-10-10 13:38:16 -04001017#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1018 DRAW_COMMAND; \
1019 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1020 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1021 DRAW_COMMAND; \
1022 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1023 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001024 }
1025
1026#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1027
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001028// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1029// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1030class LayerShader : public SkShader {
1031public:
1032 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1033 : INHERITED(localMatrix)
1034 , mLayer(layer) {
1035 }
1036
Chris Craike84a2082014-12-22 14:28:49 -08001037 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001038 if (data) {
1039 *data = static_cast<void*>(mLayer);
1040 }
1041 return true;
1042 }
1043
Chris Craike84a2082014-12-22 14:28:49 -08001044 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001045 return !mLayer->isBlend();
1046 }
1047
1048protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001049 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001050 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1051 }
1052
Chris Craike84a2082014-12-22 14:28:49 -08001053 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001054 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1055 }
1056
Chris Craike84a2082014-12-22 14:28:49 -08001057 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001058 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001059 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001060 }
1061private:
1062 // Unowned.
1063 Layer* mLayer;
1064 typedef SkShader INHERITED;
1065};
1066
Romain Guy5b3b3522010-10-27 18:57:51 -07001067void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001068 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1069
1070 if (layer->getConvexMask()) {
1071 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1072
1073 // clip to the area of the layer the mask can be larger
1074 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1075
1076 SkPaint paint;
1077 paint.setAntiAlias(true);
1078 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1079
Chris Craik3f0854292014-04-15 16:18:08 -07001080 // create LayerShader to map SaveLayer content into subsequent draw
1081 SkMatrix shaderMatrix;
1082 shaderMatrix.setTranslate(rect.left, rect.bottom);
1083 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001084 LayerShader layerShader(layer, &shaderMatrix);
1085 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001086
1087 // Since the drawing primitive is defined in local drawing space,
1088 // we don't need to modify the draw matrix
1089 const SkPath* maskPath = layer->getConvexMask();
1090 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1091
Chris Craike84a2082014-12-22 14:28:49 -08001092 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001093 restore();
1094
1095 return;
1096 }
1097
Romain Guy5b3b3522010-10-27 18:57:51 -07001098 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001099 layer->setRegionAsRect();
1100
Chris Craik34416ea2013-04-15 16:08:28 -07001101 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001102
Romain Guy5b3b3522010-10-27 18:57:51 -07001103 layer->region.clear();
1104 return;
1105 }
1106
Chris Craik62d307c2014-07-29 10:35:13 -07001107 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001108 // standard Region based draw
1109 size_t count;
1110 const android::Rect* rects;
1111 Region safeRegion;
1112 if (CC_LIKELY(hasRectToRectTransform())) {
1113 rects = layer->region.getArray(&count);
1114 } else {
1115 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1116 rects = safeRegion.getArray(&count);
1117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001118
Chris Craik3f0854292014-04-15 16:18:08 -07001119 const float alpha = getLayerAlpha(layer);
1120 const float texX = 1.0f / float(layer->getWidth());
1121 const float texY = 1.0f / float(layer->getHeight());
1122 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001123
Chris Craik3f0854292014-04-15 16:18:08 -07001124 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 // We must get (and therefore bind) the region mesh buffer
1127 // after we setup drawing in case we need to mess with the
1128 // stencil buffer in setupDraw()
1129 TextureVertex* mesh = mCaches.getRegionMesh();
1130 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001131
Chris Craik3f0854292014-04-15 16:18:08 -07001132 setupDrawWithTexture();
1133 setupDrawColor(alpha, alpha, alpha, alpha);
1134 setupDrawColorFilter(layer->getColorFilter());
1135 setupDrawBlending(layer);
1136 setupDrawProgram();
1137 setupDrawDirtyRegionsDisabled();
1138 setupDrawPureColorUniforms();
1139 setupDrawColorFilterUniforms(layer->getColorFilter());
1140 setupDrawTexture(layer->getTexture());
1141 if (currentTransform()->isPureTranslate()) {
1142 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1143 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144
Chris Craik3f0854292014-04-15 16:18:08 -07001145 layer->setFilter(GL_NEAREST);
1146 setupDrawModelView(kModelViewMode_Translate, false,
1147 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1148 } else {
1149 layer->setFilter(GL_LINEAR);
1150 setupDrawModelView(kModelViewMode_Translate, false,
1151 rect.left, rect.top, rect.right, rect.bottom);
1152 }
1153 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001154
Chris Craik3f0854292014-04-15 16:18:08 -07001155 for (size_t i = 0; i < count; i++) {
1156 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Chris Craik3f0854292014-04-15 16:18:08 -07001158 const float u1 = r->left * texX;
1159 const float v1 = (height - r->top) * texY;
1160 const float u2 = r->right * texX;
1161 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001162
Chris Craik3f0854292014-04-15 16:18:08 -07001163 // TODO: Reject quads outside of the clip
1164 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1165 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1166 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1167 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001168
Chris Craik3f0854292014-04-15 16:18:08 -07001169 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik3f0854292014-04-15 16:18:08 -07001171 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001172 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001173 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001174 numQuads = 0;
1175 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 }
Chris Craik3f0854292014-04-15 16:18:08 -07001177 }
1178
1179 if (numQuads > 0) {
1180 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001181 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001182 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001183
Romain Guy5b3b3522010-10-27 18:57:51 -07001184#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001185 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001186#endif
1187
Chris Craik3f0854292014-04-15 16:18:08 -07001188 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001189}
1190
Romain Guy3a3133d2011-02-01 22:59:58 -08001191#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001192void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001193 size_t count;
1194 const android::Rect* rects = region.getArray(&count);
1195
1196 uint32_t colors[] = {
1197 0x7fff0000, 0x7f00ff00,
1198 0x7f0000ff, 0x7fff00ff,
1199 };
1200
1201 int offset = 0;
1202 int32_t top = rects[0].top;
1203
1204 for (size_t i = 0; i < count; i++) {
1205 if (top != rects[i].top) {
1206 offset ^= 0x2;
1207 top = rects[i].top;
1208 }
1209
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001210 SkPaint paint;
1211 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001212 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001213 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001214 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001215}
Chris Craike63f7c622013-10-17 10:30:55 -07001216#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001217
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001218void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001219 Vector<float> rects;
1220
1221 SkRegion::Iterator it(region);
1222 while (!it.done()) {
1223 const SkIRect& r = it.rect();
1224 rects.push(r.fLeft);
1225 rects.push(r.fTop);
1226 rects.push(r.fRight);
1227 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001228 it.next();
1229 }
1230
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001231 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001232}
1233
Romain Guy5b3b3522010-10-27 18:57:51 -07001234void OpenGLRenderer::dirtyLayer(const float left, const float top,
1235 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001236 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001237 Rect bounds(left, top, right, bottom);
1238 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001239 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001241}
1242
1243void OpenGLRenderer::dirtyLayer(const float left, const float top,
1244 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001245 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001247 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001249}
1250
1251void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001252 if (bounds.intersect(mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001253 bounds.snapToPixelBoundaries();
1254 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1255 if (!dirty.isEmpty()) {
1256 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 }
1258 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001259}
1260
Chris Craik4063a0e2013-11-15 16:06:56 -08001261void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001262 GLsizei elementsCount = quadsCount * 6;
1263 while (elementsCount > 0) {
1264 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1265
Romain Guy3380cfd2013-08-15 16:57:57 -07001266 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001267 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001268
1269 elementsCount -= drawCount;
1270 // Though there are 4 vertices in a quad, we use 6 indices per
1271 // quad to draw with GL_TRIANGLES
1272 mesh += (drawCount / 6) * 4;
1273 }
1274}
1275
Romain Guy54be1cd2011-06-13 19:04:27 -07001276void OpenGLRenderer::clearLayerRegions() {
1277 const size_t count = mLayers.size();
1278 if (count == 0) return;
1279
Tom Hudson984162f2014-10-10 13:38:16 -04001280 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001281 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001282 // Doing several glScissor/glClear here can negatively impact
1283 // GPUs with a tiler architecture, instead we draw quads with
1284 // the Clear blending mode
1285
1286 // The list contains bounds that have already been clipped
1287 // against their initial clip rect, and the current clip
1288 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001289 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001290
Romain Guy448455f2013-07-22 13:57:50 -07001291 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001292 Vertex* vertex = mesh;
1293
1294 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001295 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001296
Chris Craik51d6a3d2014-12-22 17:16:56 -08001297 Vertex::set(vertex++, bounds.left, bounds.top);
1298 Vertex::set(vertex++, bounds.right, bounds.top);
1299 Vertex::set(vertex++, bounds.left, bounds.bottom);
1300 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001301 }
Romain Guye67307c2013-02-11 18:01:20 -08001302 // We must clear the list of dirty rects before we
1303 // call setupDraw() to prevent stencil setup to do
1304 // the same thing again
1305 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001306
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001307 SkPaint clearPaint;
1308 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1309
Romain Guy54be1cd2011-06-13 19:04:27 -07001310 setupDraw(false);
1311 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001312 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001313 setupDrawProgram();
1314 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001315 setupDrawModelView(kModelViewMode_Translate, false,
1316 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001317
Chris Craik4063a0e2013-11-15 16:06:56 -08001318 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001319
1320 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001321 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001322 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001323 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001324}
1325
Romain Guybd6b79b2010-06-26 00:13:53 -07001326///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001327// State Deferral
1328///////////////////////////////////////////////////////////////////////////////
1329
Chris Craikff785832013-03-08 13:12:16 -08001330bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001331 const Rect& currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001332 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001333
Chris Craikff785832013-03-08 13:12:16 -08001334 if (stateDeferFlags & kStateDeferFlag_Draw) {
1335 // state has bounds initialized in local coordinates
1336 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001337 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001338 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001339 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1340 // is used, it should more closely duplicate the quickReject logic (in how it uses
1341 // snapToPixelBoundaries)
1342
Rob Tsuk487a92c2015-01-06 13:22:54 -08001343 if (!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001344 // quick rejected
1345 return true;
1346 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001347
Chris Craika02c4ed2013-06-14 13:43:58 -07001348 state.mClipSideFlags = kClipSide_None;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001349 if (!currentClip.contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001350 int& flags = state.mClipSideFlags;
1351 // op partially clipped, so record which sides are clipped for clip-aware merging
Rob Tsuk487a92c2015-01-06 13:22:54 -08001352 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1353 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1354 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1355 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001356 }
1357 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001358 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001359 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1360 // overdraw avoidance (since we don't know what it overlaps)
1361 state.mClipSideFlags = kClipSide_ConservativeFull;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001362 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001363 }
1364 }
1365
Chris Craik527a3aa2013-03-04 10:19:31 -08001366 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1367 if (state.mClipValid) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001368 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001369 }
1370
Chris Craik7273daa2013-03-28 11:25:24 -07001371 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1372 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001373 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001374 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001376
1377 // always store/restore, since it's just a pointer
1378 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001379 return false;
1380}
1381
Chris Craik527a3aa2013-03-04 10:19:31 -08001382void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001383 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001384 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001385 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001386 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001387
Chris Craik527a3aa2013-03-04 10:19:31 -08001388 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001389 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001390 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001391 dirtyClip();
1392 }
Chris Craikc3566d02013-02-04 16:16:33 -08001393}
1394
Chris Craik28ce94a2013-05-31 11:38:03 -07001395/**
1396 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1397 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1398 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1399 *
1400 * This method should be called when restoreDisplayState() won't be restoring the clip
1401 */
1402void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001403 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001404 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001405 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001406 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001407 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001408 dirtyClip();
Chris Craike84a2082014-12-22 14:28:49 -08001409 mCaches.setScissorEnabled(clipRect != nullptr || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001410}
1411
Chris Craikc3566d02013-02-04 16:16:33 -08001412///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001413// Clipping
1414///////////////////////////////////////////////////////////////////////////////
1415
Romain Guybb9524b2010-06-22 18:56:38 -07001416void OpenGLRenderer::setScissorFromClip() {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001417 Rect clip(mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001418 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001419
Chris Craika64a2be2014-05-14 14:17:01 -07001420 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001421 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001422 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001423 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001424}
1425
Romain Guy8ce00302013-01-15 18:51:42 -08001426void OpenGLRenderer::ensureStencilBuffer() {
1427 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1428 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1429 // just hope we have one when hasLayer() returns false.
1430 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001431 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001432 }
1433}
1434
1435void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1436 // The layer's FBO is already bound when we reach this stage
1437 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001438 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1439 // is attached after we initiated tiling. We must turn it off,
1440 // attach the new render buffer then turn tiling back on
1441 endTiling();
1442
Romain Guy8d4aeb72013-02-12 16:08:55 -08001443 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001444 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001445 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001446
Romain Guyf735c8e2013-01-31 17:45:55 -08001447 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001448 }
1449}
1450
Rob Tsuk487a92c2015-01-06 13:22:54 -08001451static void handlePoint(std::vector<Vertex>& rectangleVertices, const Matrix4& transform,
1452 float x, float y) {
1453 Vertex v;
1454 v.x = x;
1455 v.y = y;
1456 transform.mapPoint(v.x, v.y);
1457 rectangleVertices.push_back(v);
1458}
1459
1460static void handlePointNoTransform(std::vector<Vertex>& rectangleVertices, float x, float y) {
1461 Vertex v;
1462 v.x = x;
1463 v.y = y;
1464 rectangleVertices.push_back(v);
1465}
1466
1467void OpenGLRenderer::drawRectangleList(const RectangleList& rectangleList) {
1468 int count = rectangleList.getTransformedRectanglesCount();
1469 std::vector<Vertex> rectangleVertices(count * 4);
1470 Rect scissorBox = rectangleList.calculateBounds();
1471 scissorBox.snapToPixelBoundaries();
1472 for (int i = 0; i < count; ++i) {
1473 const TransformedRectangle& tr(rectangleList.getTransformedRectangle(i));
1474 const Matrix4& transform = tr.getTransform();
1475 Rect bounds = tr.getBounds();
1476 if (transform.rectToRect()) {
1477 transform.mapRect(bounds);
1478 if (!bounds.intersect(scissorBox)) {
1479 bounds.setEmpty();
1480 } else {
1481 handlePointNoTransform(rectangleVertices, bounds.left, bounds.top);
1482 handlePointNoTransform(rectangleVertices, bounds.right, bounds.top);
1483 handlePointNoTransform(rectangleVertices, bounds.left, bounds.bottom);
1484 handlePointNoTransform(rectangleVertices, bounds.right, bounds.bottom);
1485 }
1486 } else {
1487 handlePoint(rectangleVertices, transform, bounds.left, bounds.top);
1488 handlePoint(rectangleVertices, transform, bounds.right, bounds.top);
1489 handlePoint(rectangleVertices, transform, bounds.left, bounds.bottom);
1490 handlePoint(rectangleVertices, transform, bounds.right, bounds.bottom);
1491 }
1492 }
1493
1494 mCaches.setScissor(scissorBox.left, getViewportHeight() - scissorBox.bottom,
1495 scissorBox.getWidth(), scissorBox.getHeight());
1496
1497 const SkPaint* paint = nullptr;
1498 setupDraw();
1499 setupDrawNoTexture();
1500 setupDrawColor(0, 0xff * currentSnapshot()->alpha);
1501 setupDrawShader(getShader(paint));
1502 setupDrawColorFilter(getColorFilter(paint));
1503 setupDrawBlending(paint);
1504 setupDrawProgram();
1505 setupDrawDirtyRegionsDisabled();
1506 setupDrawModelView(kModelViewMode_Translate, false,
1507 0.0f, 0.0f, 0.0f, 0.0f, true);
1508 setupDrawColorUniforms(getShader(paint));
1509 setupDrawShaderUniforms(getShader(paint));
1510 setupDrawColorFilterUniforms(getColorFilter(paint));
1511
1512 issueIndexedQuadDraw(&rectangleVertices[0], rectangleVertices.size() / 4);
1513}
1514
Romain Guy8ce00302013-01-15 18:51:42 -08001515void OpenGLRenderer::setStencilFromClip() {
1516 if (!mCaches.debugOverdraw) {
Rob Tsuk487a92c2015-01-06 13:22:54 -08001517 if (!currentSnapshot()->clipIsSimple()) {
1518 int incrementThreshold;
Chris Craik62d307c2014-07-29 10:35:13 -07001519 EVENT_LOGD("setStencilFromClip - enabling");
1520
Romain Guy8ce00302013-01-15 18:51:42 -08001521 // NOTE: The order here is important, we must set dirtyClip to false
1522 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001523 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001524
1525 ensureStencilBuffer();
1526
Rob Tsuk487a92c2015-01-06 13:22:54 -08001527 const ClipArea& clipArea = currentSnapshot()->getClipArea();
Romain Guy8ce00302013-01-15 18:51:42 -08001528
Rob Tsuk487a92c2015-01-06 13:22:54 -08001529 bool isRectangleList = clipArea.isRectangleList();
1530 if (isRectangleList) {
1531 incrementThreshold = clipArea.getRectangleList().getTransformedRectanglesCount();
1532 } else {
1533 incrementThreshold = 0;
1534 }
1535
1536 mCaches.stencil.enableWrite(incrementThreshold);
1537
1538 // Clean and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001539 // to the region's bounds
1540 bool resetScissor = mCaches.enableScissor();
1541 if (resetScissor) {
1542 // The scissor was not set so we now need to update it
1543 setScissorFromClip();
1544 }
Rob Tsuk487a92c2015-01-06 13:22:54 -08001545
Romain Guy8ce00302013-01-15 18:51:42 -08001546 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001547
1548 // stash and disable the outline clip state, since stencil doesn't account for outline
1549 bool storedSkipOutlineClip = mSkipOutlineClip;
1550 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001551
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001552 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001553 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001554 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1555
Rob Tsuk487a92c2015-01-06 13:22:54 -08001556 if (isRectangleList) {
1557 drawRectangleList(clipArea.getRectangleList());
1558 } else {
1559 // NOTE: We could use the region contour path to generate a smaller mesh
1560 // Since we are using the stencil we could use the red book path
1561 // drawing technique. It might increase bandwidth usage though.
Romain Guy8ce00302013-01-15 18:51:42 -08001562
Rob Tsuk487a92c2015-01-06 13:22:54 -08001563 // The last parameter is important: we are not drawing in the color buffer
1564 // so we don't want to dirty the current layer, if any
1565 drawRegionRects(clipArea.getClipRegion(), paint, false);
1566 }
Chris Craikdeeda3d2014-05-05 19:09:33 -07001567 if (resetScissor) mCaches.disableScissor();
1568 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001569
Rob Tsuk487a92c2015-01-06 13:22:54 -08001570 mCaches.stencil.enableTest(incrementThreshold);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001571
1572 // Draw the region used to generate the stencil if the appropriate debug
1573 // mode is enabled
Rob Tsuk487a92c2015-01-06 13:22:54 -08001574 // TODO: Implement for rectangle list clip areas
1575 if (mCaches.debugStencilClip == Caches::kStencilShowRegion &&
1576 !clipArea.isRectangleList()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001577 paint.setColor(0x7f0000ff);
1578 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
Rob Tsuk487a92c2015-01-06 13:22:54 -08001579 drawRegionRects(currentSnapshot()->getClipRegion(), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001580 }
Romain Guy8ce00302013-01-15 18:51:42 -08001581 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001582 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001583 mCaches.stencil.disable();
1584 }
1585 }
1586}
1587
Chris Craikf0a59072013-11-19 18:00:46 -08001588/**
1589 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1590 *
1591 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1592 * style, and tessellated AA ramp
1593 */
1594bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001595 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001596 bool snapOut = paint && paint->isAntiAlias();
1597
1598 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1599 float outset = paint->getStrokeWidth() * 0.5f;
1600 left -= outset;
1601 top -= outset;
1602 right += outset;
1603 bottom += outset;
1604 }
1605
Chris Craikdeeda3d2014-05-05 19:09:33 -07001606 bool clipRequired = false;
1607 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001608 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001609 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001610 return true;
1611 }
1612
Chris Craikf23b25a2014-06-26 15:46:20 -07001613 // not quick rejected, so enable the scissor if clipRequired
1614 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1615 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001616 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001617}
1618
Romain Guy8ce00302013-01-15 18:51:42 -08001619void OpenGLRenderer::debugClip() {
1620#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001621 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001622 SkPaint paint;
1623 paint.setColor(0x7f00ff00);
1624 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1625
Romain Guy8ce00302013-01-15 18:51:42 -08001626 }
1627#endif
1628}
1629
Romain Guyf6a11b82010-06-23 17:47:49 -07001630///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001631// Drawing commands
1632///////////////////////////////////////////////////////////////////////////////
1633
Chris Craik62d307c2014-07-29 10:35:13 -07001634void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001635 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001636 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001637 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001638 // Make sure setScissor & setStencil happen at the beginning of
1639 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001640 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001641 if (mCaches.scissorEnabled) {
1642 setScissorFromClip();
1643 }
Chris Craik62d307c2014-07-29 10:35:13 -07001644
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001645 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001646 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001649
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 mSetShaderColor = false;
1651 mColorSet = false;
1652 mColorA = mColorR = mColorG = mColorB = 0.0f;
1653 mTextureUnit = 0;
1654 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001655
1656 // Enable debug highlight when what we're about to draw is tested against
1657 // the stencil buffer and if stencil highlight debugging is on
1658 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1659 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1660 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001661}
1662
1663void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1664 mDescription.hasTexture = true;
1665 mDescription.hasAlpha8Texture = isAlpha8;
1666}
1667
Romain Guyff316ec2013-02-13 18:39:43 -08001668void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1669 mDescription.hasTexture = true;
1670 mDescription.hasColors = true;
1671 mDescription.hasAlpha8Texture = isAlpha8;
1672}
1673
Romain Guyaa6c24c2011-04-28 18:40:04 -07001674void OpenGLRenderer::setupDrawWithExternalTexture() {
1675 mDescription.hasExternalTexture = true;
1676}
1677
Romain Guy15bc6432011-12-13 13:11:32 -08001678void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001679 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001680}
1681
Chris Craik91a8c7c2014-08-12 14:31:35 -07001682void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1683 mDescription.hasVertexAlpha = true;
1684 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001685}
1686
Romain Guy8d0d4782010-12-14 20:13:35 -08001687void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1688 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001689 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1690 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1691 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001692 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001693 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001694}
1695
Romain Guy86568192010-12-14 15:55:39 -08001696void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1697 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001698 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1699 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1700 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001701 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001702 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001703}
1704
Romain Guy41210632012-07-16 17:04:24 -07001705void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1706 mCaches.fontRenderer->describe(mDescription, paint);
1707}
1708
Romain Guy70ca14e2010-12-13 18:24:33 -08001709void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1710 mColorA = a;
1711 mColorR = r;
1712 mColorG = g;
1713 mColorB = b;
1714 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001715 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001716}
1717
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001718void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001719 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001720 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001721 }
1722}
1723
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001724void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001725 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001726 return;
1727 }
1728
1729 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001730 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001731 mDescription.colorOp = ProgramDescription::kColorBlend;
1732 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001733 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001734 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001735 }
1736}
1737
Romain Guyf09ef512011-05-27 11:43:46 -07001738void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1739 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1740 mColorA = 1.0f;
1741 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001742 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001743 }
1744}
1745
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001746void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1747 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001748 // When the blending mode is kClear_Mode, we need to use a modulate color
1749 // argb=1,0,0,0
1750 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001751 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001752 bool blend = layer->isBlend()
1753 || getLayerAlpha(layer) < 1.0f
1754 || (mColorSet && mColorA < 1.0f)
1755 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001756 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001757}
1758
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001759void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1760 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001761 // When the blending mode is kClear_Mode, we need to use a modulate color
1762 // argb=1,0,0,0
1763 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001764 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001765 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001766 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001767 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001768}
1769
1770void OpenGLRenderer::setupDrawProgram() {
1771 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001772 if (mDescription.hasRoundRectClip) {
1773 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001774 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001775 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001776 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001777 innerRect.left, innerRect.top,
1778 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001779 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1780 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001781
1782 // add half pixel to round out integer rect space to cover pixel centers
1783 float roundedOutRadius = state->radius + 0.5f;
1784 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1785 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001786 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001787}
1788
1789void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1790 mTrackDirtyRegions = false;
1791}
1792
Chris Craik4063a0e2013-11-15 16:06:56 -08001793void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1794 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001795 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001796 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001797 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001799
Romain Guy86568192010-12-14 15:55:39 -08001800 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001801 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001802 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1803 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001804 if (dirty && mTrackDirtyRegions) {
1805 if (!ignoreTransform) {
1806 dirtyLayer(left, top, right, bottom, *currentTransform());
1807 } else {
1808 dirtyLayer(left, top, right, bottom);
1809 }
Romain Guy86568192010-12-14 15:55:39 -08001810 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001811}
1812
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001813void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1814 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001815 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1816 }
1817}
1818
Romain Guy86568192010-12-14 15:55:39 -08001819void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001820 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001821 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001822 }
1823}
1824
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001825void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001826 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001827 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001828 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001829
1830 if (ignoreTransform) {
1831 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1832 // because it was built into modelView / the geometry, and the description needs to
1833 // compensate.
1834 mat4 modelViewWithoutTransform;
1835 modelViewWithoutTransform.loadInverse(*currentTransform());
1836 modelViewWithoutTransform.multiply(mModelViewMatrix);
1837 mModelViewMatrix.load(modelViewWithoutTransform);
1838 }
1839
1840 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001841}
1842
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001843void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001844 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001845 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001846 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001847
1848 SkColor color;
1849 SkXfermode::Mode mode;
1850 if (filter->asColorMode(&color, &mode)) {
1851 const int alpha = SkColorGetA(color);
1852 const GLfloat a = alpha / 255.0f;
1853 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1854 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1855 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1856 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1857 return;
1858 }
1859
1860 SkScalar srcColorMatrix[20];
1861 if (filter->asColorMatrix(srcColorMatrix)) {
1862
1863 float colorMatrix[16];
1864 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1865 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1866 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1867 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1868
1869 // Skia uses the range [0..255] for the addition vector, but we need
1870 // the [0..1] range to apply the vector in GLSL
1871 float colorVector[4];
1872 colorVector[0] = srcColorMatrix[4] / 255.0f;
1873 colorVector[1] = srcColorMatrix[9] / 255.0f;
1874 colorVector[2] = srcColorMatrix[14] / 255.0f;
1875 colorVector[3] = srcColorMatrix[19] / 255.0f;
1876
1877 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1878 GL_FALSE, colorMatrix);
1879 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1880 return;
1881 }
1882
1883 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001884}
1885
Romain Guy41210632012-07-16 17:04:24 -07001886void OpenGLRenderer::setupDrawTextGammaUniforms() {
1887 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1888}
1889
Romain Guy70ca14e2010-12-13 18:24:33 -08001890void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001891 bool force = mCaches.bindMeshBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08001892 mCaches.bindPositionVertexPointer(force, nullptr);
Romain Guy15bc6432011-12-13 13:11:32 -08001893 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001894}
1895
1896void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001897 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001898 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001899 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001900}
1901
Romain Guyaa6c24c2011-04-28 18:40:04 -07001902void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1903 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001904 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001905 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001906}
1907
Romain Guy8f0095c2011-05-02 17:24:22 -07001908void OpenGLRenderer::setupDrawTextureTransform() {
1909 mDescription.hasTextureTransform = true;
1910}
1911
1912void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001913 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1914 GL_FALSE, &transform.data[0]);
1915}
1916
Chris Craik564acf72014-01-02 16:46:18 -08001917void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1918 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001919 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001920 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001921 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001922 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001923 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001924 }
Romain Guyd71dd362011-12-12 19:03:35 -08001925
Chris Craikcb4d6002012-09-25 12:00:29 -07001926 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001927 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001928 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001929 }
1930
1931 mCaches.unbindIndicesBuffer();
1932}
1933
Chris Craik564acf72014-01-02 16:46:18 -08001934void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1935 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001936 bool force = mCaches.unbindMeshBuffer();
1937 GLsizei stride = sizeof(ColorTextureVertex);
1938
1939 mCaches.bindPositionVertexPointer(force, vertices, stride);
1940 if (mCaches.currentProgram->texCoords >= 0) {
1941 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1942 }
1943 int slot = mCaches.currentProgram->getAttrib("colors");
1944 if (slot >= 0) {
1945 glEnableVertexAttribArray(slot);
1946 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1947 }
1948
1949 mCaches.unbindIndicesBuffer();
1950}
1951
Chris Craik564acf72014-01-02 16:46:18 -08001952void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1953 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001954 bool force = false;
1955 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1956 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1957 // use the default VBO found in Caches
1958 if (!vertices || vbo) {
1959 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1960 } else {
1961 force = mCaches.unbindMeshBuffer();
1962 }
ztenghui63d41ab2014-02-14 13:13:41 -08001963 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001964
Chris Craikcb4d6002012-09-25 12:00:29 -07001965 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001966 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001967 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001968 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001969}
1970
Romain Guy448455f2013-07-22 13:57:50 -07001971void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001972 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001973 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001974 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001975}
1976
Romain Guy70ca14e2010-12-13 18:24:33 -08001977///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001978// Drawing
1979///////////////////////////////////////////////////////////////////////////////
1980
Tom Hudson107843d2014-09-08 11:26:26 -04001981void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001982 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1983 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001984 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001985 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001986 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001987 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001988 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001989 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001990 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001991 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001992 }
1993
Chris Craikef8d6f22014-12-17 11:10:28 -08001994 // Don't avoid overdraw when visualizing, since that makes it harder to
1995 // debug where it's coming from, and when the problem occurs.
1996 bool avoidOverdraw = !mCaches.debugOverdraw;
Rob Tsuk487a92c2015-01-06 13:22:54 -08001997 DeferredDisplayList deferredList(mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001998 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001999 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002000
2001 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04002002 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002003
Tom Hudson107843d2014-09-08 11:26:26 -04002004 deferredList.flush(*this, dirty);
2005 } else {
2006 // Even if there is no drawing command(Ex: invisible),
2007 // it still needs startFrame to clear buffer and start tiling.
2008 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08002009 }
2010}
2011
Chris Craike84a2082014-12-22 14:28:49 -08002012void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
2013 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07002014 float x = left;
2015 float y = top;
2016
Romain Guy886b2752013-01-04 12:26:18 -08002017 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2018
Romain Guya168d732011-03-18 16:50:13 -07002019 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08002020 if (currentTransform()->isPureTranslate()) {
2021 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2022 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002023 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002024
2025 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002026 } else {
Chris Craik678625242014-02-28 12:26:34 -08002027 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002028 }
2029
Romain Guy3b748a42013-04-17 18:54:38 -07002030 // No need to check for a UV mapper on the texture object, only ARGB_8888
2031 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002032 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craike84a2082014-12-22 14:28:49 -08002033 paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07002034 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002035}
2036
Romain Guy03c00b52013-06-20 18:30:28 -07002037/**
2038 * Important note: this method is intended to draw batches of bitmaps and
2039 * will not set the scissor enable or dirty the current layer, if any.
2040 * The caller is responsible for properly dirtying the current layer.
2041 */
Tom Hudson107843d2014-09-08 11:26:26 -04002042void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002043 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
2044 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002045 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002046 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002047 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07002048
Chris Craik527a3aa2013-03-04 10:19:31 -08002049 const AutoTexture autoCleanup(texture);
2050
Chris Craik527a3aa2013-03-04 10:19:31 -08002051 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002052 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002053
2054 const float x = (int) floorf(bounds.left + 0.5f);
2055 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002056 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002057 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002058 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002059 GL_TRIANGLES, bitmapCount * 6, true,
2060 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002061 } else {
2062 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002063 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002064 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2065 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002066 }
2067
Tom Hudson107843d2014-09-08 11:26:26 -04002068 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002069}
2070
Tom Hudson107843d2014-09-08 11:26:26 -04002071void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002072 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002073 return;
Romain Guy6926c722010-07-12 20:20:03 -07002074 }
2075
Romain Guya1d3c912011-12-13 14:55:06 -08002076 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002077 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002078 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002079 const AutoTexture autoCleanup(texture);
2080
Mike Reed1103b322014-07-08 12:36:44 -04002081 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002082 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002083 } else {
Chris Craik79647502014-08-06 13:42:24 -07002084 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002085 }
Chet Haase48659092012-05-31 15:21:51 -07002086
Tom Hudson107843d2014-09-08 11:26:26 -04002087 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002088}
2089
Tom Hudson107843d2014-09-08 11:26:26 -04002090void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002091 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002092 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002093 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002094 }
2095
Chris Craik39a908c2013-06-13 14:39:01 -07002096 // TODO: use quickReject on bounds from vertices
2097 mCaches.enableScissor();
2098
Romain Guyb18d2d02011-02-10 15:52:54 -08002099 float left = FLT_MAX;
2100 float top = FLT_MAX;
2101 float right = FLT_MIN;
2102 float bottom = FLT_MIN;
2103
Romain Guya92bb4d2012-10-16 11:08:44 -07002104 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002105
Chris Craik51d6a3d2014-12-22 17:16:56 -08002106 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2107 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002108
Chris Craik51d6a3d2014-12-22 17:16:56 -08002109 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002110 if (!colors) {
2111 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002112 tempColors.reset(new int[colorsCount]);
2113 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2114 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002115 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002116
Romain Guy3b748a42013-04-17 18:54:38 -07002117 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002118 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002119 const UvMapper& mapper(getMapper(texture));
2120
Romain Guy5a7b4662011-01-20 19:09:30 -08002121 for (int32_t y = 0; y < meshHeight; y++) {
2122 for (int32_t x = 0; x < meshWidth; x++) {
2123 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2124
2125 float u1 = float(x) / meshWidth;
2126 float u2 = float(x + 1) / meshWidth;
2127 float v1 = float(y) / meshHeight;
2128 float v2 = float(y + 1) / meshHeight;
2129
Romain Guy3b748a42013-04-17 18:54:38 -07002130 mapper.map(u1, v1, u2, v2);
2131
Romain Guy5a7b4662011-01-20 19:09:30 -08002132 int ax = i + (meshWidth + 1) * 2;
2133 int ay = ax + 1;
2134 int bx = i;
2135 int by = bx + 1;
2136 int cx = i + 2;
2137 int cy = cx + 1;
2138 int dx = i + (meshWidth + 1) * 2 + 2;
2139 int dy = dx + 1;
2140
Romain Guyff316ec2013-02-13 18:39:43 -08002141 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2142 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2143 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002144
Romain Guyff316ec2013-02-13 18:39:43 -08002145 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2146 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2147 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002148
Romain Guya92bb4d2012-10-16 11:08:44 -07002149 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2150 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2151 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2152 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002153 }
2154 }
2155
Chris Craikf0a59072013-11-19 18:00:46 -08002156 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002157 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002158 }
2159
Romain Guyff316ec2013-02-13 18:39:43 -08002160 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002161 texture = mCaches.textureCache.get(bitmap);
2162 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002163 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002164 }
Romain Guyff316ec2013-02-13 18:39:43 -08002165 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002166 const AutoTexture autoCleanup(texture);
2167
2168 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002169 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002170
2171 int alpha;
2172 SkXfermode::Mode mode;
2173 getAlphaAndMode(paint, &alpha, &mode);
2174
Romain Guyff316ec2013-02-13 18:39:43 -08002175 float a = alpha / 255.0f;
2176
Romain Guya92bb4d2012-10-16 11:08:44 -07002177 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002178 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002179 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002180
Romain Guyff316ec2013-02-13 18:39:43 -08002181 setupDraw();
2182 setupDrawWithTextureAndColor();
2183 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002184 setupDrawColorFilter(getColorFilter(paint));
2185 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002186 setupDrawProgram();
2187 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002188 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002189 setupDrawTexture(texture->id);
2190 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002191 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002192 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002193
2194 glDrawArrays(GL_TRIANGLES, 0, count);
2195
Romain Guyff316ec2013-02-13 18:39:43 -08002196 int slot = mCaches.currentProgram->getAttrib("colors");
2197 if (slot >= 0) {
2198 glDisableVertexAttribArray(slot);
2199 }
2200
Tom Hudson107843d2014-09-08 11:26:26 -04002201 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002202}
2203
Tom Hudson107843d2014-09-08 11:26:26 -04002204void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002205 float srcLeft, float srcTop, float srcRight, float srcBottom,
2206 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002207 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002208 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002209 return;
Romain Guy6926c722010-07-12 20:20:03 -07002210 }
2211
Romain Guya1d3c912011-12-13 14:55:06 -08002212 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002213 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002214 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002215 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002216
Romain Guy8ba548f2010-06-30 19:21:21 -07002217 const float width = texture->width;
2218 const float height = texture->height;
2219
Romain Guy3b748a42013-04-17 18:54:38 -07002220 float u1 = fmax(0.0f, srcLeft / width);
2221 float v1 = fmax(0.0f, srcTop / height);
2222 float u2 = fmin(1.0f, srcRight / width);
2223 float v2 = fmin(1.0f, srcBottom / height);
2224
2225 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002226
Romain Guy03750a02010-10-18 14:06:08 -07002227 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002228 resetDrawTextureTexCoords(u1, v1, u2, v2);
2229
Romain Guyd21b6e12011-11-30 20:21:23 -08002230 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2231
Romain Guy886b2752013-01-04 12:26:18 -08002232 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2233 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002234
Romain Guy886b2752013-01-04 12:26:18 -08002235 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2236 // Apply a scale transform on the canvas only when a shader is in use
2237 // Skia handles the ratio between the dst and src rects as a scale factor
2238 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002239 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002240 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002241
Chris Craikd6b65f62014-01-01 14:45:21 -08002242 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2243 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2244 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002245
2246 dstRight = x + (dstRight - dstLeft);
2247 dstBottom = y + (dstBottom - dstTop);
2248
2249 dstLeft = x;
2250 dstTop = y;
2251
Chris Craik678625242014-02-28 12:26:34 -08002252 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002253 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002254 } else {
Chris Craik678625242014-02-28 12:26:34 -08002255 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002256 }
2257
2258 if (CC_UNLIKELY(useScaleTransform)) {
2259 save(SkCanvas::kMatrix_SaveFlag);
2260 translate(dstLeft, dstTop);
2261 scale(scaleX, scaleY);
2262
2263 dstLeft = 0.0f;
2264 dstTop = 0.0f;
2265
2266 dstRight = srcRight - srcLeft;
2267 dstBottom = srcBottom - srcTop;
2268 }
2269
Mike Reed1103b322014-07-08 12:36:44 -04002270 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002271 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002272 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002273 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002274 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2275 } else {
2276 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002277 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002278 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002279 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2280 }
2281
2282 if (CC_UNLIKELY(useScaleTransform)) {
2283 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002285
2286 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002287
Tom Hudson107843d2014-09-08 11:26:26 -04002288 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002289}
2290
Tom Hudson107843d2014-09-08 11:26:26 -04002291void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002292 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002293 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002294 return;
Romain Guy6926c722010-07-12 20:20:03 -07002295 }
2296
John Reckebd52612014-12-10 16:47:36 -08002297 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002298 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2299 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002300
Tom Hudson107843d2014-09-08 11:26:26 -04002301 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002302}
2303
Tom Hudson107843d2014-09-08 11:26:26 -04002304void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002305 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2306 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002307 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002308 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002309 }
2310
Romain Guy211370f2012-02-01 16:10:55 -08002311 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002312 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002313 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002314 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002315 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002316
Romain Guya4adcf02013-02-28 12:15:35 -08002317 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2318 texture->setFilter(GL_LINEAR, true);
2319
Chris Craikd6b65f62014-01-01 14:45:21 -08002320 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002321 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002322 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002323 const float offsetX = left + currentTransform()->getTranslateX();
2324 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002325 const size_t count = mesh->quads.size();
2326 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002327 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002328 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002329 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2330 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2331 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002332 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002333 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002334 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002335 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002336 }
2337 }
2338
Chris Craik4063a0e2013-11-15 16:06:56 -08002339 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002340 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002341 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2342 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002343
Romain Guy3b748a42013-04-17 18:54:38 -07002344 right = x + right - left;
2345 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002346 left = x;
2347 top = y;
2348 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002349 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002350 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2351 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002352 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2353 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002354 }
Chet Haase48659092012-05-31 15:21:51 -07002355
Tom Hudson107843d2014-09-08 11:26:26 -04002356 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002357}
2358
Romain Guy03c00b52013-06-20 18:30:28 -07002359/**
2360 * Important note: this method is intended to draw batches of 9-patch objects and
2361 * will not set the scissor enable or dirty the current layer, if any.
2362 * The caller is responsible for properly dirtying the current layer.
2363 */
Tom Hudson107843d2014-09-08 11:26:26 -04002364void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002365 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002366 mCaches.activeTexture(0);
2367 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002368 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002369 const AutoTexture autoCleanup(texture);
2370
2371 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2372 texture->setFilter(GL_LINEAR, true);
2373
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002374 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2375 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002376 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002377
Tom Hudson107843d2014-09-08 11:26:26 -04002378 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002379}
2380
Tom Hudson107843d2014-09-08 11:26:26 -04002381void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002382 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002383 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002384 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002385 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002386 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002387 }
2388
Chris Craik0d5ac952014-07-15 13:01:02 -07002389 Rect bounds(vertexBuffer.getBounds());
2390 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002391 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2392
Chris Craikcb4d6002012-09-25 12:00:29 -07002393 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002394 bool isAA = paint->isAntiAlias();
2395
Chris Craik710f46d2012-09-17 17:25:49 -07002396 setupDraw();
2397 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002398 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002399 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002400 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002401 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002402 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002403 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002404 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2405 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002406 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002407 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002408 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002409
ztenghui55bfb4e2013-12-03 10:38:55 -08002410 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002411 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002412 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002413 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002414
Chris Craik710f46d2012-09-17 17:25:49 -07002415 int alphaSlot = -1;
2416 if (isAA) {
2417 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2418 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002419 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002420 glEnableVertexAttribArray(alphaSlot);
2421 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002422 }
Romain Guy04299382012-07-18 17:15:41 -07002423
Chris Craik05f3d6e2014-06-02 16:27:04 -07002424 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2425 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002426 mCaches.unbindIndicesBuffer();
2427 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002428 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002429 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002430 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2431 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002432 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002433 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002434 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2435 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002436 } else if (mode == VertexBuffer::kIndices) {
2437 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002438 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2439 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002440 }
Romain Guy04299382012-07-18 17:15:41 -07002441
Chris Craik710f46d2012-09-17 17:25:49 -07002442 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002443 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002444 }
Chris Craik65cd6122012-12-10 17:56:27 -08002445
Tom Hudson107843d2014-09-08 11:26:26 -04002446 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002447}
2448
2449/**
Chris Craik65cd6122012-12-10 17:56:27 -08002450 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2451 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2452 * screen space in all directions. However, instead of using a fragment shader to compute the
2453 * translucency of the color from its position, we simply use a varying parameter to define how far
2454 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2455 *
2456 * Doesn't yet support joins, caps, or path effects.
2457 */
Tom Hudson107843d2014-09-08 11:26:26 -04002458void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002459 VertexBuffer vertexBuffer;
2460 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002461 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002462 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002463}
2464
2465/**
2466 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2467 * and additional geometry for defining an alpha slope perimeter.
2468 *
2469 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2470 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2471 * in-shader alpha region, but found it to be taxing on some GPUs.
2472 *
2473 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2474 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002475 */
Tom Hudson107843d2014-09-08 11:26:26 -04002476void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002477 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002478
Chris Craik65cd6122012-12-10 17:56:27 -08002479 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002480
Chris Craik65cd6122012-12-10 17:56:27 -08002481 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002482 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2483 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002484
Chris Craik05f3d6e2014-06-02 16:27:04 -07002485 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002486 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002487 }
2488
Chris Craikbf759452014-08-11 16:00:44 -07002489 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002490 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002491}
2492
Tom Hudson107843d2014-09-08 11:26:26 -04002493void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002494 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002495
Chris Craik6d29c8d2013-05-08 18:35:44 -07002496 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002497
Chris Craik6d29c8d2013-05-08 18:35:44 -07002498 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002499 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002500
Chris Craik05f3d6e2014-06-02 16:27:04 -07002501 const Rect& bounds = buffer.getBounds();
2502 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002503 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002504 }
2505
Chris Craikbf759452014-08-11 16:00:44 -07002506 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002507 drawVertexBuffer(buffer, paint, displayFlags);
2508
2509 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002510}
2511
Tom Hudson107843d2014-09-08 11:26:26 -04002512void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002513 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002514 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002515
Rob Tsuk487a92c2015-01-06 13:22:54 -08002516 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002517 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002518
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002519 SkPaint paint;
2520 paint.setColor(color);
2521 paint.setXfermodeMode(mode);
2522
2523 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002524
Tom Hudson107843d2014-09-08 11:26:26 -04002525 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002526}
Romain Guy9d5316e2010-06-24 19:30:36 -07002527
Tom Hudson107843d2014-09-08 11:26:26 -04002528void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002529 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002530 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002531 const AutoTexture autoCleanup(texture);
2532
2533 const float x = left + texture->left - texture->offset;
2534 const float y = top + texture->top - texture->offset;
2535
2536 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002537
Tom Hudson107843d2014-09-08 11:26:26 -04002538 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002539}
2540
Tom Hudson107843d2014-09-08 11:26:26 -04002541void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002542 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002543 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002544 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002545 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002546 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002547 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002548
Chris Craike84a2082014-12-22 14:28:49 -08002549 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002550 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002551 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002552 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002553 drawShape(left, top, texture, p);
2554 } else {
2555 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2556 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2557 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002558 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002559}
2560
Tom Hudson107843d2014-09-08 11:26:26 -04002561void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002562 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002563 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002564 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002565 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002566 }
Chris Craike84a2082014-12-22 14:28:49 -08002567 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002568 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002569 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002570 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002571 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002572 SkPath path;
2573 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2574 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2575 } else {
2576 path.addCircle(x, y, radius);
2577 }
2578 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002579 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002580}
Romain Guy01d58e42011-01-19 21:54:02 -08002581
Tom Hudson107843d2014-09-08 11:26:26 -04002582void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002583 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002584 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002585 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002586 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002587 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002588 }
Romain Guy01d58e42011-01-19 21:54:02 -08002589
Chris Craike84a2082014-12-22 14:28:49 -08002590 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002591 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002592 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002593 drawShape(left, top, texture, p);
2594 } else {
2595 SkPath path;
2596 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2597 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2598 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2599 }
2600 path.addOval(rect);
2601 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002602 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002603}
2604
Tom Hudson107843d2014-09-08 11:26:26 -04002605void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002606 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002607 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002608 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002609 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002610 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002611 }
2612
Chris Craik780c1282012-10-04 14:10:49 -07002613 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002614 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002615 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002616 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002617 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002618 drawShape(left, top, texture, p);
2619 return;
Chris Craik780c1282012-10-04 14:10:49 -07002620 }
Chris Craik780c1282012-10-04 14:10:49 -07002621 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2622 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2623 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2624 }
2625
2626 SkPath path;
2627 if (useCenter) {
2628 path.moveTo(rect.centerX(), rect.centerY());
2629 }
2630 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2631 if (useCenter) {
2632 path.close();
2633 }
Tom Hudson107843d2014-09-08 11:26:26 -04002634 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002635}
2636
Romain Guycf8675e2012-10-02 12:32:25 -07002637// See SkPaintDefaults.h
2638#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2639
Tom Hudson107843d2014-09-08 11:26:26 -04002640void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002641 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002642 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002643 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002644 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002645 return;
Romain Guy6926c722010-07-12 20:20:03 -07002646 }
2647
Chris Craik710f46d2012-09-17 17:25:49 -07002648 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002649 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002650 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002651 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2652 mCaches.activeTexture(0);
2653 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002654 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002655 drawShape(left, top, texture, p);
2656 } else {
2657 SkPath path;
2658 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2659 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2660 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2661 }
2662 path.addRect(rect);
2663 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002664 }
Chet Haase858aa932011-05-12 09:06:00 -07002665 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002666 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2667 SkPath path;
2668 path.addRect(left, top, right, bottom);
2669 drawConvexPath(path, p);
2670 } else {
2671 drawColorRect(left, top, right, bottom, p);
2672
2673 mDirty = true;
2674 }
Chet Haase858aa932011-05-12 09:06:00 -07002675 }
Romain Guyc7d53492010-06-25 13:41:57 -07002676}
Romain Guy9d5316e2010-06-24 19:30:36 -07002677
Chris Craikd218a922014-01-02 17:13:34 -08002678void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2679 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002680 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002681 mCaches.activeTexture(0);
2682
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002683 TextShadow textShadow;
2684 if (!getTextShadow(paint, &textShadow)) {
2685 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2686 }
2687
Raph Levien416a8472012-07-19 22:48:17 -07002688 // NOTE: The drop shadow will not perform gamma correction
2689 // if shader-based correction is enabled
2690 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2691 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002692 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002693 // If the drop shadow exceeds the max texture size or couldn't be
2694 // allocated, skip drawing
2695 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002696 const AutoTexture autoCleanup(shadow);
2697
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002698 const float sx = x - shadow->left + textShadow.dx;
2699 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002700
Tom Hudson984162f2014-10-10 13:38:16 -04002701 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002702 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002703 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002704 }
2705
2706 setupDraw();
2707 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002708 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002709 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002710 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002711 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002712 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002713 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2714 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002715 setupDrawTexture(shadow->id);
2716 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002717 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002718 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08002719 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002720
2721 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2722}
2723
Romain Guy768bffc2013-02-27 13:50:45 -08002724bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002725 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002726 return MathUtils::isZero(alpha)
2727 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002728}
2729
Tom Hudson107843d2014-09-08 11:26:26 -04002730void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002731 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002732 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002733 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002734 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002735
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002737 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002738 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002739 }
2740
Chris Craik39a908c2013-06-13 14:39:01 -07002741 mCaches.enableScissor();
2742
Romain Guy671d6cf2012-01-18 12:39:17 -08002743 float x = 0.0f;
2744 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002745 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002746 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002747 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2748 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 }
2750
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002751 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002752 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002753
2754 int alpha;
2755 SkXfermode::Mode mode;
2756 getAlphaAndMode(paint, &alpha, &mode);
2757
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002758 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002759 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002760 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002761 }
2762
Romain Guy671d6cf2012-01-18 12:39:17 -08002763 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002764 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002765 if (pureTranslate && !linearFilter) {
2766 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2767 }
Romain Guy257ae352013-03-20 16:31:12 -07002768 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002769
Rob Tsuk487a92c2015-01-06 13:22:54 -08002770 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002771 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2772
Romain Guy211370f2012-02-01 16:10:55 -08002773 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002774
Victoria Lease1e546812013-06-25 14:25:17 -07002775 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002776 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002777 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002778 if (hasActiveLayer) {
2779 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002780 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002781 }
2782 dirtyLayerUnchecked(bounds, getRegion());
2783 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002784 }
Chet Haase48659092012-05-31 15:21:51 -07002785
Tom Hudson107843d2014-09-08 11:26:26 -04002786 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002787}
2788
Chris Craik59744b72014-07-01 17:56:52 -07002789bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002790 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002791 outMatrix->setIdentity();
2792 return false;
2793 } else if (CC_UNLIKELY(transform.isPerspective())) {
2794 outMatrix->setIdentity();
2795 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002796 }
Chris Craik59744b72014-07-01 17:56:52 -07002797
2798 /**
Chris Craika736cd92014-08-04 13:18:38 -07002799 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2800 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002801 */
2802 float sx, sy;
2803 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002804 outMatrix->setScale(
2805 roundf(fmaxf(1.0f, sx)),
2806 roundf(fmaxf(1.0f, sy)));
2807 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002808}
2809
Tom Hudson984162f2014-10-10 13:38:16 -04002810int OpenGLRenderer::getSaveCount() const {
2811 return mState.getSaveCount();
2812}
2813
2814int OpenGLRenderer::save(int flags) {
2815 return mState.save(flags);
2816}
2817
2818void OpenGLRenderer::restore() {
2819 return mState.restore();
2820}
2821
2822void OpenGLRenderer::restoreToCount(int saveCount) {
2823 return mState.restoreToCount(saveCount);
2824}
2825
2826void OpenGLRenderer::translate(float dx, float dy, float dz) {
2827 return mState.translate(dx, dy, dz);
2828}
2829
2830void OpenGLRenderer::rotate(float degrees) {
2831 return mState.rotate(degrees);
2832}
2833
2834void OpenGLRenderer::scale(float sx, float sy) {
2835 return mState.scale(sx, sy);
2836}
2837
2838void OpenGLRenderer::skew(float sx, float sy) {
2839 return mState.skew(sx, sy);
2840}
2841
2842void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2843 mState.setMatrix(matrix);
2844}
2845
2846void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2847 mState.concatMatrix(matrix);
2848}
2849
2850bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2851 return mState.clipRect(left, top, right, bottom, op);
2852}
2853
2854bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2855 return mState.clipPath(path, op);
2856}
2857
2858bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2859 return mState.clipRegion(region, op);
2860}
2861
2862void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2863 mState.setClippingOutline(allocator, outline);
2864}
2865
2866void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2867 const Rect& rect, float radius, bool highPriority) {
2868 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2869}
2870
2871
2872
Tom Hudson107843d2014-09-08 11:26:26 -04002873void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002874 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002875 DrawOpMode drawOpMode) {
2876
Chris Craik527a3aa2013-03-04 10:19:31 -08002877 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002878 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2879 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002880 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002881 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002882 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002883 }
Romain Guycac5fd32011-12-01 20:08:50 -08002884 }
2885
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002886 const float oldX = x;
2887 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002888
Chris Craikd6b65f62014-01-01 14:45:21 -08002889 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002890 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002891
Romain Guy211370f2012-02-01 16:10:55 -08002892 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002893 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2894 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002895 }
2896
Romain Guy86568192010-12-14 15:55:39 -08002897 int alpha;
2898 SkXfermode::Mode mode;
2899 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002900
Romain Guyc74f45a2013-02-26 19:10:14 -08002901 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2902
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002903 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002904 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002905 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002906 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002907 }
2908
Romain Guy19d4dd82013-03-04 11:14:26 -08002909 const bool hasActiveLayer = hasLayer();
2910
Romain Guy624234f2013-03-05 16:43:31 -08002911 // We only pass a partial transform to the font renderer. That partial
2912 // matrix defines how glyphs are rasterized. Typically we want glyphs
2913 // to be rasterized at their final size on screen, which means the partial
2914 // matrix needs to take the scale factor into account.
2915 // When a partial matrix is used to transform glyphs during rasterization,
2916 // the mesh is generated with the inverse transform (in the case of scale,
2917 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2918 // apply the full transform matrix at draw time in the vertex shader.
2919 // Applying the full matrix in the shader is the easiest way to handle
2920 // rotation and perspective and allows us to always generated quads in the
2921 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002922 SkMatrix fontTransform;
2923 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2924 || fabs(y - (int) y) > 0.0f
2925 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002926 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002927 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002928
Romain Guy624234f2013-03-05 16:43:31 -08002929 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002930 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002931 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 -07002932
Raph Levien996e57c2012-07-23 15:22:52 -07002933 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002934 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002935
2936 // don't call issuedrawcommand, do it at end of batch
2937 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002938 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002939 SkPaint paintCopy(*paint);
2940 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2941 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002942 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002943 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002944 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002945 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002946 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002947
Chris Craik527a3aa2013-03-04 10:19:31 -08002948 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002949 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002950 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002951 }
Chris Craik41541822013-05-03 16:35:54 -07002952 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002953 }
Romain Guy694b5192010-07-21 21:33:20 -07002954
Chris Craike63f7c622013-10-17 10:30:55 -07002955 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002956
Tom Hudson107843d2014-09-08 11:26:26 -04002957 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002958}
2959
Tom Hudson107843d2014-09-08 11:26:26 -04002960void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002961 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002962 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002963 return;
Romain Guy03d58522012-02-24 17:54:07 -08002964 }
2965
Chris Craik39a908c2013-06-13 14:39:01 -07002966 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2967 mCaches.enableScissor();
2968
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002969 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002970 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002971 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002972
2973 int alpha;
2974 SkXfermode::Mode mode;
2975 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002976 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002977
Tom Hudson984162f2014-10-10 13:38:16 -04002978 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002979 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 -08002980
Romain Guy97771732012-02-28 18:17:02 -08002981 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002982
2983 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002984 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002985 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002986 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002987 dirtyLayerUnchecked(bounds, getRegion());
2988 }
Romain Guy97771732012-02-28 18:17:02 -08002989 }
Chet Haase48659092012-05-31 15:21:51 -07002990
Tom Hudson107843d2014-09-08 11:26:26 -04002991 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002992}
2993
Tom Hudson107843d2014-09-08 11:26:26 -04002994void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002995 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002996
Romain Guya1d3c912011-12-13 14:55:06 -08002997 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002998
Romain Guyfb8b7632010-08-23 21:05:08 -07002999 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003000 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003001 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003002
Romain Guy8b55f372010-08-18 17:10:07 -07003003 const float x = texture->left - texture->offset;
3004 const float y = texture->top - texture->offset;
3005
Romain Guy01d58e42011-01-19 21:54:02 -08003006 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003007 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003008}
3009
Tom Hudson107843d2014-09-08 11:26:26 -04003010void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003011 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003012 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003013 }
3014
Chris Craike84a2082014-12-22 14:28:49 -08003015 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003016 if (layer->isTextureLayer()) {
3017 transform = &layer->getTransform();
3018 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003019 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003020 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003021 }
3022 }
3023
Chris Craik39a908c2013-06-13 14:39:01 -07003024 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003025 const bool rejected = mState.calculateQuickRejectForScissor(
3026 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3027 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003028
3029 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003030 if (transform && !transform->isIdentity()) {
3031 restore();
3032 }
Tom Hudson107843d2014-09-08 11:26:26 -04003033 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003034 }
3035
Chris Craik62d307c2014-07-29 10:35:13 -07003036 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3037 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3038
Romain Guy5bb3c732012-11-29 17:52:58 -08003039 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003040
Chris Craik39a908c2013-06-13 14:39:01 -07003041 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003042 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003043
Romain Guy211370f2012-02-01 16:10:55 -08003044 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003045 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003046 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3047 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003048 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003049
Chris Craik16ecda52013-03-29 10:59:59 -07003050 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003051 setupDraw();
3052 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003053 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003054 setupDrawColorFilter(layer->getColorFilter());
3055 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003056 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003057 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003058 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003059 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003060 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3061 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3062 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003063
Romain Guyd21b6e12011-11-30 20:21:23 -08003064 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003065 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003066 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003067 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003068 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003069 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003070 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3071 }
Romain Guyf219da52011-01-16 12:54:25 -08003072
Romain Guy448455f2013-07-22 13:57:50 -07003073 TextureVertex* mesh = &layer->mesh[0];
3074 GLsizei elementsCount = layer->meshElementCount;
3075
3076 while (elementsCount > 0) {
3077 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3078
Romain Guy3380cfd2013-08-15 16:57:57 -07003079 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003080 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003081 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003082
3083 elementsCount -= drawCount;
3084 // Though there are 4 vertices in a quad, we use 6 indices per
3085 // quad to draw with GL_TRIANGLES
3086 mesh += (drawCount / 6) * 4;
3087 }
Romain Guyf219da52011-01-16 12:54:25 -08003088
Romain Guy3a3133d2011-02-01 22:59:58 -08003089#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003090 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003091#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003092 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003093
Romain Guy5bb3c732012-11-29 17:52:58 -08003094 if (layer->debugDrawUpdate) {
3095 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003096
3097 SkPaint paint;
3098 paint.setColor(0x7f00ff00);
3099 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003100 }
Romain Guyf219da52011-01-16 12:54:25 -08003101 }
Chris Craik34416ea2013-04-15 16:08:28 -07003102 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003103
Romain Guyb2e2f242012-10-17 18:18:35 -07003104 if (transform && !transform->isIdentity()) {
3105 restore();
3106 }
3107
Tom Hudson107843d2014-09-08 11:26:26 -04003108 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003109}
3110
Romain Guy6926c722010-07-12 20:20:03 -07003111///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003112// Draw filters
3113///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003114void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3115 // We should never get here since we apply the draw filter when stashing
3116 // the paints in the DisplayList.
3117 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003118}
3119
3120///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003121// Drawing implementation
3122///////////////////////////////////////////////////////////////////////////////
3123
Chris Craikd218a922014-01-02 17:13:34 -08003124Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003125 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003126 if (!texture) {
3127 return mCaches.textureCache.get(bitmap);
3128 }
3129 return texture;
3130}
3131
Romain Guy01d58e42011-01-19 21:54:02 -08003132void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003133 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003134 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003135 return;
3136 }
3137
3138 int alpha;
3139 SkXfermode::Mode mode;
3140 getAlphaAndMode(paint, &alpha, &mode);
3141
3142 setupDraw();
3143 setupDrawWithTexture(true);
3144 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003145 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003146 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003147 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003148 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003149 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3150 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003151 setupDrawTexture(texture->id);
3152 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003153 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003154 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003155 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003156
3157 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003158}
3159
Romain Guyf607bdc2010-09-10 19:20:06 -07003160// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003161#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3162#define kStdUnderline_Offset (1.0f / 9.0f)
3163#define kStdUnderline_Thickness (1.0f / 18.0f)
3164
Chris Craikd218a922014-01-02 17:13:34 -08003165void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3166 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003167 // Handle underline and strike-through
3168 uint32_t flags = paint->getFlags();
3169 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003170 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003171
Romain Guy211370f2012-02-01 16:10:55 -08003172 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003173 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003174 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003175
Raph Levien8b4072d2012-07-30 15:50:00 -07003176 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003177 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003178
Romain Guyf6834472011-01-23 13:32:12 -08003179 int linesCount = 0;
3180 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3181 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3182
3183 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003184 float points[pointsCount];
3185 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003186
3187 if (flags & SkPaint::kUnderlineText_Flag) {
3188 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003189 points[currentPoint++] = left;
3190 points[currentPoint++] = top;
3191 points[currentPoint++] = left + underlineWidth;
3192 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003193 }
3194
3195 if (flags & SkPaint::kStrikeThruText_Flag) {
3196 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003197 points[currentPoint++] = left;
3198 points[currentPoint++] = top;
3199 points[currentPoint++] = left + underlineWidth;
3200 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003201 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003202
Romain Guy726aeba2011-06-01 14:52:00 -07003203 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003204
Romain Guy726aeba2011-06-01 14:52:00 -07003205 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003206 }
3207 }
3208}
3209
Tom Hudson107843d2014-09-08 11:26:26 -04003210void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003211 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003212 return;
Romain Guy672433d2013-01-04 19:05:13 -08003213 }
3214
Tom Hudson107843d2014-09-08 11:26:26 -04003215 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003216}
3217
Tom Hudson107843d2014-09-08 11:26:26 -04003218void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003219 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003220 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003221
Chris Craik15a07a22014-01-26 13:43:53 -08003222 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003223 mCaches.enableScissor();
3224
3225 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003226 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003227
ztenghui14a4e352014-08-13 10:44:39 -07003228 // The caller has made sure casterAlpha > 0.
3229 float ambientShadowAlpha = mAmbientShadowAlpha;
3230 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3231 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3232 }
3233 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3234 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003235 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003236 }
ztenghui7b4516e2014-01-07 10:42:55 -08003237
ztenghui14a4e352014-08-13 10:44:39 -07003238 float spotShadowAlpha = mSpotShadowAlpha;
3239 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3240 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3241 }
3242 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3243 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003244 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003245 }
ztenghui7b4516e2014-01-07 10:42:55 -08003246
Tom Hudson107843d2014-09-08 11:26:26 -04003247 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003248}
3249
Tom Hudson107843d2014-09-08 11:26:26 -04003250void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003251 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003252 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003253 return;
Romain Guy3b753822013-03-05 10:27:35 -08003254 }
Romain Guy735738c2012-12-03 12:34:51 -08003255
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003256 int color = paint->getColor();
3257 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003258 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003259 color |= 0x00ffffff;
3260 }
3261
Romain Guy672433d2013-01-04 19:05:13 -08003262 float left = FLT_MAX;
3263 float top = FLT_MAX;
3264 float right = FLT_MIN;
3265 float bottom = FLT_MIN;
3266
Romain Guy448455f2013-07-22 13:57:50 -07003267 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003268 Vertex* vertex = mesh;
3269
Chris Craik2af46352012-11-26 18:30:17 -08003270 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003271 float l = rects[index + 0];
3272 float t = rects[index + 1];
3273 float r = rects[index + 2];
3274 float b = rects[index + 3];
3275
Romain Guy3b753822013-03-05 10:27:35 -08003276 Vertex::set(vertex++, l, t);
3277 Vertex::set(vertex++, r, t);
3278 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003279 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003280
Romain Guy3b753822013-03-05 10:27:35 -08003281 left = fminf(left, l);
3282 top = fminf(top, t);
3283 right = fmaxf(right, r);
3284 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003285 }
3286
Chris Craikf0a59072013-11-19 18:00:46 -08003287 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003288 return;
Romain Guya362c692013-02-04 13:50:16 -08003289 }
Romain Guy672433d2013-01-04 19:05:13 -08003290
Romain Guy672433d2013-01-04 19:05:13 -08003291 setupDraw();
3292 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003293 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003294 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003295 setupDrawColorFilter(getColorFilter(paint));
3296 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003297 setupDrawProgram();
3298 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003299 setupDrawModelView(kModelViewMode_Translate, false,
3300 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003301 setupDrawColorUniforms(getShader(paint));
3302 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003303 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003304
Romain Guy8ce00302013-01-15 18:51:42 -08003305 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003306 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003307 }
3308
Chris Craik4063a0e2013-11-15 16:06:56 -08003309 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003310
Tom Hudson107843d2014-09-08 11:26:26 -04003311 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003312}
3313
Romain Guy026c5e162010-06-28 17:12:22 -07003314void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003315 const SkPaint* paint, bool ignoreTransform) {
3316 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003317 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003318 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003319 color |= 0x00ffffff;
3320 }
3321
Romain Guy70ca14e2010-12-13 18:24:33 -08003322 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003323 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003324 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003325 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003326 setupDrawColorFilter(getColorFilter(paint));
3327 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003328 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003329 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3330 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003331 setupDrawColorUniforms(getShader(paint));
3332 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003333 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003334 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003335
Romain Guyc95c8d62010-09-17 15:31:32 -07003336 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3337}
3338
Romain Guy82ba8142010-07-09 13:25:56 -07003339void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003340 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003341 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003342
Chris Craike84a2082014-12-22 14:28:49 -08003343 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003344 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3345
3346 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003347 vertices = &mMeshVertices[0].x;
3348 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003349
3350 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3351 texture->uvMapper->map(uvs);
3352
3353 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3354 }
3355
Chris Craikd6b65f62014-01-01 14:45:21 -08003356 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3357 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3358 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003359
Romain Guyd21b6e12011-11-30 20:21:23 -08003360 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003361 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003362 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003363 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003364 } else {
Chris Craik678625242014-02-28 12:26:34 -08003365 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003367 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3368 }
3369
3370 if (texture->uvMapper) {
3371 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003372 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003373}
3374
Romain Guyf7f93552010-07-08 19:17:03 -07003375void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003376 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003377 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003378 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3379 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003380
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003381 int a;
3382 SkXfermode::Mode mode;
3383 getAlphaAndMode(paint, &a, &mode);
3384 const float alpha = a / 255.0f;
3385
Romain Guy746b7402010-10-26 16:27:31 -07003386 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003387 setupDrawWithTexture();
3388 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003389 setupDrawColorFilter(getColorFilter(paint));
3390 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003391 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003392 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003393 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003394 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003395 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003396 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003397 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003398
Romain Guy6820ac82010-09-15 18:11:50 -07003399 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003400}
3401
Romain Guy3b748a42013-04-17 18:54:38 -07003402void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003403 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003404 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003405 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3406 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003407
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003408 int a;
3409 SkXfermode::Mode mode;
3410 getAlphaAndMode(paint, &a, &mode);
3411 const float alpha = a / 255.0f;
3412
Romain Guy3b748a42013-04-17 18:54:38 -07003413 setupDraw();
3414 setupDrawWithTexture();
3415 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003416 setupDrawColorFilter(getColorFilter(paint));
3417 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003418 setupDrawProgram();
3419 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003420 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003421 setupDrawTexture(texture);
3422 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003423 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003424 setupDrawMeshIndices(vertices, texCoords, vbo);
3425
Chris Craike84a2082014-12-22 14:28:49 -08003426 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003427}
3428
Romain Guy886b2752013-01-04 12:26:18 -08003429void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003430 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003431 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003432 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003433
Chris Craike84a2082014-12-22 14:28:49 -08003434 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003435 int alpha;
3436 SkXfermode::Mode mode;
3437 getAlphaAndMode(paint, &alpha, &mode);
3438
Romain Guy886b2752013-01-04 12:26:18 -08003439 setupDraw();
3440 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003441 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003442 setupDrawAlpha8Color(color, alpha);
3443 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003444 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003445 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003446 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003447 setupDrawProgram();
3448 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003449 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003450 setupDrawTexture(texture);
3451 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003452 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003453 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003454 setupDrawMesh(vertices, texCoords);
3455
3456 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003457}
3458
Romain Guya5aed0d2010-09-09 14:42:43 -07003459void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003460 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003461
Chris Craike84a2082014-12-22 14:28:49 -08003462 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003463 blend = true;
3464 mDescription.hasRoundRectClip = true;
3465 }
3466 mSkipOutlineClip = true;
3467
Romain Guy82ba8142010-07-09 13:25:56 -07003468 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003469
Romain Guy82ba8142010-07-09 13:25:56 -07003470 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003471 // These blend modes are not supported by OpenGL directly and have
3472 // to be implemented using shaders. Since the shader will perform
3473 // the blending, turn blending off here
3474 // If the blend mode cannot be implemented using shaders, fall
3475 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003476 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003477 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003478 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003479 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003480
Romain Guy82bc7a72012-01-03 14:13:39 -08003481 if (mCaches.blend) {
3482 glDisable(GL_BLEND);
3483 mCaches.blend = false;
3484 }
3485
3486 return;
3487 } else {
3488 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003489 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003490 }
3491
3492 if (!mCaches.blend) {
3493 glEnable(GL_BLEND);
3494 }
3495
3496 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3497 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3498
3499 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3500 glBlendFunc(sourceMode, destMode);
3501 mCaches.lastSrcMode = sourceMode;
3502 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003503 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003504 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003505 glDisable(GL_BLEND);
3506 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003507 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003508}
3509
Romain Guy889f8d12010-07-29 14:37:42 -07003510bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003511 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003512 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003513 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003514 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003515 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003516 }
Romain Guy6926c722010-07-12 20:20:03 -07003517 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003518}
3519
Romain Guy026c5e162010-06-28 17:12:22 -07003520void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003521 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003522 TextureVertex::setUV(v++, u1, v1);
3523 TextureVertex::setUV(v++, u2, v1);
3524 TextureVertex::setUV(v++, u1, v2);
3525 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003526}
3527
Chris Craike84a2082014-12-22 14:28:49 -08003528void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3529 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003530 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003531 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3532 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003533 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003534 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003535 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003536}
3537
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003538float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003539 float alpha;
3540 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3541 alpha = mDrawModifiers.mOverrideLayerAlpha;
3542 } else {
3543 alpha = layer->getAlpha() / 255.0f;
3544 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003545 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003546}
3547
Romain Guy9d5316e2010-06-24 19:30:36 -07003548}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003549}; // namespace android