blob: 9ee37045453c9e94ac8bc7b7e0b7c94d58f2ba13 [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 Craik797b95b2014-05-20 18:10:25 -0700185void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700186 glDisable(GL_DITHER);
187 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
188
189 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700190 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700191}
192
Romain Guy96885eb2013-03-26 15:05:58 -0700193void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800194 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800195 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400196 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700197 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700198 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700199}
200
Tom Hudson107843d2014-09-08 11:26:26 -0400201void OpenGLRenderer::startFrame() {
202 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700203 mFrameStarted = true;
204
Tom Hudson984162f2014-10-10 13:38:16 -0400205 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700206
Romain Guy96885eb2013-03-26 15:05:58 -0700207 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700208
Tom Hudson984162f2014-10-10 13:38:16 -0400209 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800210
Romain Guy54c1a642012-09-27 17:55:46 -0700211 // Functors break the tiling extension in pretty spectacular ways
212 // This ensures we don't use tiling when a functor is going to be
213 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700214 mSuppressTiling = mCaches.hasRegisteredFunctors()
215 || mFirstFrameAfterResize;
216 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700217
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700219
Romain Guy7c450aa2012-09-21 19:15:00 -0700220 debugOverdraw(true, true);
221
Tom Hudson107843d2014-09-08 11:26:26 -0400222 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700223 mTilingClip.right, mTilingClip.bottom, mOpaque);
224}
225
Tom Hudson107843d2014-09-08 11:26:26 -0400226void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700227 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700228
Romain Guy96885eb2013-03-26 15:05:58 -0700229 setupFrameState(left, top, right, bottom, opaque);
230
231 // Layer renderers will start the frame immediately
232 // The framebuffer renderer will first defer the display list
233 // for each layer and wait until the first drawing command
234 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800235 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700236 syncState();
237 updateLayers();
238 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400239 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700240 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400248 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
249 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Tom Hudson107843d2014-09-08 11:26:26 -0400257void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700258 if (!opaque) {
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 Guy0f667532013-03-01 14:31:04 -0800411void OpenGLRenderer::eventMark(const char* name) const {
412 mCaches.eventMark(0, name);
413}
414
Romain Guy87e2f7572012-09-24 11:37:12 -0700415void OpenGLRenderer::startMark(const char* name) const {
416 mCaches.startMark(0, name);
417}
418
419void OpenGLRenderer::endMark() const {
420 mCaches.endMark();
421}
422
423void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700424 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700425}
426
427void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400428 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700429 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700430
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 Guydda57022010-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 Guydda57022010-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::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002091 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002092 return;
Romain Guye651cc62012-05-14 19:44:40 -07002093 }
2094
2095 mCaches.activeTexture(0);
2096 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2097 const AutoTexture autoCleanup(texture);
2098
Mike Reed1103b322014-07-08 12:36:44 -04002099 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002100 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002101 } else {
Chris Craik79647502014-08-06 13:42:24 -07002102 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002103 }
Chet Haase48659092012-05-31 15:21:51 -07002104
Tom Hudson107843d2014-09-08 11:26:26 -04002105 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002106}
2107
Tom Hudson107843d2014-09-08 11:26:26 -04002108void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002109 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002110 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002111 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002112 }
2113
Chris Craik39a908c2013-06-13 14:39:01 -07002114 // TODO: use quickReject on bounds from vertices
2115 mCaches.enableScissor();
2116
Romain Guyb18d2d02011-02-10 15:52:54 -08002117 float left = FLT_MAX;
2118 float top = FLT_MAX;
2119 float right = FLT_MIN;
2120 float bottom = FLT_MIN;
2121
Romain Guya92bb4d2012-10-16 11:08:44 -07002122 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002123
Chris Craik51d6a3d2014-12-22 17:16:56 -08002124 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2125 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002126
Chris Craik51d6a3d2014-12-22 17:16:56 -08002127 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002128 if (!colors) {
2129 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002130 tempColors.reset(new int[colorsCount]);
2131 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2132 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002133 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002134
Romain Guy3b748a42013-04-17 18:54:38 -07002135 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002136 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002137 const UvMapper& mapper(getMapper(texture));
2138
Romain Guy5a7b4662011-01-20 19:09:30 -08002139 for (int32_t y = 0; y < meshHeight; y++) {
2140 for (int32_t x = 0; x < meshWidth; x++) {
2141 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2142
2143 float u1 = float(x) / meshWidth;
2144 float u2 = float(x + 1) / meshWidth;
2145 float v1 = float(y) / meshHeight;
2146 float v2 = float(y + 1) / meshHeight;
2147
Romain Guy3b748a42013-04-17 18:54:38 -07002148 mapper.map(u1, v1, u2, v2);
2149
Romain Guy5a7b4662011-01-20 19:09:30 -08002150 int ax = i + (meshWidth + 1) * 2;
2151 int ay = ax + 1;
2152 int bx = i;
2153 int by = bx + 1;
2154 int cx = i + 2;
2155 int cy = cx + 1;
2156 int dx = i + (meshWidth + 1) * 2 + 2;
2157 int dy = dx + 1;
2158
Romain Guyff316ec2013-02-13 18:39:43 -08002159 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2160 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2161 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002162
Romain Guyff316ec2013-02-13 18:39:43 -08002163 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2164 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2165 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002166
Romain Guya92bb4d2012-10-16 11:08:44 -07002167 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2168 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2169 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2170 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002171 }
2172 }
2173
Chris Craikf0a59072013-11-19 18:00:46 -08002174 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002175 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002176 }
2177
Romain Guyff316ec2013-02-13 18:39:43 -08002178 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002179 texture = mCaches.textureCache.get(bitmap);
2180 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002181 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002182 }
Romain Guyff316ec2013-02-13 18:39:43 -08002183 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002184 const AutoTexture autoCleanup(texture);
2185
2186 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002187 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002188
2189 int alpha;
2190 SkXfermode::Mode mode;
2191 getAlphaAndMode(paint, &alpha, &mode);
2192
Romain Guyff316ec2013-02-13 18:39:43 -08002193 float a = alpha / 255.0f;
2194
Romain Guya92bb4d2012-10-16 11:08:44 -07002195 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002196 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002197 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002198
Romain Guyff316ec2013-02-13 18:39:43 -08002199 setupDraw();
2200 setupDrawWithTextureAndColor();
2201 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002202 setupDrawColorFilter(getColorFilter(paint));
2203 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002204 setupDrawProgram();
2205 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002206 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002207 setupDrawTexture(texture->id);
2208 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002209 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002210 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002211
2212 glDrawArrays(GL_TRIANGLES, 0, count);
2213
Romain Guyff316ec2013-02-13 18:39:43 -08002214 int slot = mCaches.currentProgram->getAttrib("colors");
2215 if (slot >= 0) {
2216 glDisableVertexAttribArray(slot);
2217 }
2218
Tom Hudson107843d2014-09-08 11:26:26 -04002219 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002220}
2221
Tom Hudson107843d2014-09-08 11:26:26 -04002222void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002223 float srcLeft, float srcTop, float srcRight, float srcBottom,
2224 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002225 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002226 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002227 return;
Romain Guy6926c722010-07-12 20:20:03 -07002228 }
2229
Romain Guya1d3c912011-12-13 14:55:06 -08002230 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002231 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002232 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002233 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002234
Romain Guy8ba548f2010-06-30 19:21:21 -07002235 const float width = texture->width;
2236 const float height = texture->height;
2237
Romain Guy3b748a42013-04-17 18:54:38 -07002238 float u1 = fmax(0.0f, srcLeft / width);
2239 float v1 = fmax(0.0f, srcTop / height);
2240 float u2 = fmin(1.0f, srcRight / width);
2241 float v2 = fmin(1.0f, srcBottom / height);
2242
2243 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002244
Romain Guy03750a02010-10-18 14:06:08 -07002245 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002246 resetDrawTextureTexCoords(u1, v1, u2, v2);
2247
Romain Guyd21b6e12011-11-30 20:21:23 -08002248 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2249
Romain Guy886b2752013-01-04 12:26:18 -08002250 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2251 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002252
Romain Guy886b2752013-01-04 12:26:18 -08002253 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2254 // Apply a scale transform on the canvas only when a shader is in use
2255 // Skia handles the ratio between the dst and src rects as a scale factor
2256 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002257 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002258 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002259
Chris Craikd6b65f62014-01-01 14:45:21 -08002260 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2261 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2262 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002263
2264 dstRight = x + (dstRight - dstLeft);
2265 dstBottom = y + (dstBottom - dstTop);
2266
2267 dstLeft = x;
2268 dstTop = y;
2269
Chris Craik678625242014-02-28 12:26:34 -08002270 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002271 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002272 } else {
Chris Craik678625242014-02-28 12:26:34 -08002273 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002274 }
2275
2276 if (CC_UNLIKELY(useScaleTransform)) {
2277 save(SkCanvas::kMatrix_SaveFlag);
2278 translate(dstLeft, dstTop);
2279 scale(scaleX, scaleY);
2280
2281 dstLeft = 0.0f;
2282 dstTop = 0.0f;
2283
2284 dstRight = srcRight - srcLeft;
2285 dstBottom = srcBottom - srcTop;
2286 }
2287
Mike Reed1103b322014-07-08 12:36:44 -04002288 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002289 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002290 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002291 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002292 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2293 } else {
2294 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002295 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002296 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002297 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2298 }
2299
2300 if (CC_UNLIKELY(useScaleTransform)) {
2301 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002302 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002303
2304 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002305
Tom Hudson107843d2014-09-08 11:26:26 -04002306 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002307}
2308
Tom Hudson107843d2014-09-08 11:26:26 -04002309void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002310 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002311 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002312 return;
Romain Guy6926c722010-07-12 20:20:03 -07002313 }
2314
John Reckebd52612014-12-10 16:47:36 -08002315 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002316 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2317 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002318
Tom Hudson107843d2014-09-08 11:26:26 -04002319 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002320}
2321
Tom Hudson107843d2014-09-08 11:26:26 -04002322void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002323 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2324 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002325 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002326 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002327 }
2328
Romain Guy211370f2012-02-01 16:10:55 -08002329 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002330 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002331 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002332 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002333 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002334
Romain Guya4adcf02013-02-28 12:15:35 -08002335 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2336 texture->setFilter(GL_LINEAR, true);
2337
Chris Craikd6b65f62014-01-01 14:45:21 -08002338 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002339 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002340 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002341 const float offsetX = left + currentTransform()->getTranslateX();
2342 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002343 const size_t count = mesh->quads.size();
2344 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002345 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002346 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002347 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2348 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2349 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002350 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002351 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002352 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002353 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002354 }
2355 }
2356
Chris Craik4063a0e2013-11-15 16:06:56 -08002357 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002358 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002359 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2360 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002361
Romain Guy3b748a42013-04-17 18:54:38 -07002362 right = x + right - left;
2363 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002364 left = x;
2365 top = y;
2366 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002367 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002368 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2369 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002370 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2371 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002372 }
Chet Haase48659092012-05-31 15:21:51 -07002373
Tom Hudson107843d2014-09-08 11:26:26 -04002374 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002375}
2376
Romain Guy03c00b52013-06-20 18:30:28 -07002377/**
2378 * Important note: this method is intended to draw batches of 9-patch objects and
2379 * will not set the scissor enable or dirty the current layer, if any.
2380 * The caller is responsible for properly dirtying the current layer.
2381 */
Tom Hudson107843d2014-09-08 11:26:26 -04002382void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002383 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002384 mCaches.activeTexture(0);
2385 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002386 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002387 const AutoTexture autoCleanup(texture);
2388
2389 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2390 texture->setFilter(GL_LINEAR, true);
2391
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002392 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2393 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002394 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002395
Tom Hudson107843d2014-09-08 11:26:26 -04002396 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002397}
2398
Tom Hudson107843d2014-09-08 11:26:26 -04002399void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002400 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002401 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002402 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002403 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002404 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002405 }
2406
Chris Craik0d5ac952014-07-15 13:01:02 -07002407 Rect bounds(vertexBuffer.getBounds());
2408 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002409 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2410
Chris Craikcb4d6002012-09-25 12:00:29 -07002411 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002412 bool isAA = paint->isAntiAlias();
2413
Chris Craik710f46d2012-09-17 17:25:49 -07002414 setupDraw();
2415 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002416 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002417 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002418 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002419 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002420 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002421 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002422 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2423 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002424 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002425 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002426 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002427
ztenghui55bfb4e2013-12-03 10:38:55 -08002428 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002429 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002430 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002431 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002432
Chris Craik710f46d2012-09-17 17:25:49 -07002433 int alphaSlot = -1;
2434 if (isAA) {
2435 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2436 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002437 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002438 glEnableVertexAttribArray(alphaSlot);
2439 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002440 }
Romain Guy04299382012-07-18 17:15:41 -07002441
Chris Craik05f3d6e2014-06-02 16:27:04 -07002442 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2443 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002444 mCaches.unbindIndicesBuffer();
2445 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002446 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002447 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002448 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2449 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002450 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002451 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002452 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2453 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002454 } else if (mode == VertexBuffer::kIndices) {
2455 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002456 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2457 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002458 }
Romain Guy04299382012-07-18 17:15:41 -07002459
Chris Craik710f46d2012-09-17 17:25:49 -07002460 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002461 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002462 }
Chris Craik65cd6122012-12-10 17:56:27 -08002463
Tom Hudson107843d2014-09-08 11:26:26 -04002464 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002465}
2466
2467/**
Chris Craik65cd6122012-12-10 17:56:27 -08002468 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2469 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2470 * screen space in all directions. However, instead of using a fragment shader to compute the
2471 * translucency of the color from its position, we simply use a varying parameter to define how far
2472 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2473 *
2474 * Doesn't yet support joins, caps, or path effects.
2475 */
Tom Hudson107843d2014-09-08 11:26:26 -04002476void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002477 VertexBuffer vertexBuffer;
2478 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002479 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002480 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002481}
2482
2483/**
2484 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2485 * and additional geometry for defining an alpha slope perimeter.
2486 *
2487 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2488 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2489 * in-shader alpha region, but found it to be taxing on some GPUs.
2490 *
2491 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2492 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002493 */
Tom Hudson107843d2014-09-08 11:26:26 -04002494void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002495 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002496
Chris Craik65cd6122012-12-10 17:56:27 -08002497 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002498
Chris Craik65cd6122012-12-10 17:56:27 -08002499 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002500 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2501 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002502
Chris Craik05f3d6e2014-06-02 16:27:04 -07002503 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002504 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002505 }
2506
Chris Craikbf759452014-08-11 16:00:44 -07002507 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002508 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002509}
2510
Tom Hudson107843d2014-09-08 11:26:26 -04002511void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002512 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002513
Chris Craik6d29c8d2013-05-08 18:35:44 -07002514 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002515
Chris Craik6d29c8d2013-05-08 18:35:44 -07002516 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002517 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002518
Chris Craik05f3d6e2014-06-02 16:27:04 -07002519 const Rect& bounds = buffer.getBounds();
2520 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002521 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002522 }
2523
Chris Craikbf759452014-08-11 16:00:44 -07002524 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002525 drawVertexBuffer(buffer, paint, displayFlags);
2526
2527 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002528}
2529
Tom Hudson107843d2014-09-08 11:26:26 -04002530void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002531 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002532 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002533
Rob Tsuk487a92c2015-01-06 13:22:54 -08002534 Rect clip(mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002535 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002536
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002537 SkPaint paint;
2538 paint.setColor(color);
2539 paint.setXfermodeMode(mode);
2540
2541 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002542
Tom Hudson107843d2014-09-08 11:26:26 -04002543 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002544}
Romain Guy9d5316e2010-06-24 19:30:36 -07002545
Tom Hudson107843d2014-09-08 11:26:26 -04002546void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002547 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002548 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002549 const AutoTexture autoCleanup(texture);
2550
2551 const float x = left + texture->left - texture->offset;
2552 const float y = top + texture->top - texture->offset;
2553
2554 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002555
Tom Hudson107843d2014-09-08 11:26:26 -04002556 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002557}
2558
Tom Hudson107843d2014-09-08 11:26:26 -04002559void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002560 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002561 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002562 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002563 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002564 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002565 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002566
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.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002570 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002571 drawShape(left, top, texture, p);
2572 } else {
2573 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2574 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2575 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002576 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002577}
2578
Tom Hudson107843d2014-09-08 11:26:26 -04002579void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002580 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002581 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002582 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002583 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002584 }
Chris Craike84a2082014-12-22 14:28:49 -08002585 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002586 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002587 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002588 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002589 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002590 SkPath path;
2591 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2592 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2593 } else {
2594 path.addCircle(x, y, radius);
2595 }
2596 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002597 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002598}
Romain Guy01d58e42011-01-19 21:54:02 -08002599
Tom Hudson107843d2014-09-08 11:26:26 -04002600void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002601 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002602 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002603 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002604 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002605 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002606 }
Romain Guy01d58e42011-01-19 21:54:02 -08002607
Chris Craike84a2082014-12-22 14:28:49 -08002608 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002609 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002610 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002611 drawShape(left, top, texture, p);
2612 } else {
2613 SkPath path;
2614 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2615 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2616 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2617 }
2618 path.addOval(rect);
2619 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002620 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002621}
2622
Tom Hudson107843d2014-09-08 11:26:26 -04002623void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002624 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002625 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002626 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002627 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002628 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002629 }
2630
Chris Craik780c1282012-10-04 14:10:49 -07002631 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002632 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002633 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002634 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002635 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002636 drawShape(left, top, texture, p);
2637 return;
Chris Craik780c1282012-10-04 14:10:49 -07002638 }
Chris Craik780c1282012-10-04 14:10:49 -07002639 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2640 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2641 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2642 }
2643
2644 SkPath path;
2645 if (useCenter) {
2646 path.moveTo(rect.centerX(), rect.centerY());
2647 }
2648 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2649 if (useCenter) {
2650 path.close();
2651 }
Tom Hudson107843d2014-09-08 11:26:26 -04002652 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002653}
2654
Romain Guycf8675e2012-10-02 12:32:25 -07002655// See SkPaintDefaults.h
2656#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2657
Tom Hudson107843d2014-09-08 11:26:26 -04002658void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002659 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002660 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002661 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002662 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002663 return;
Romain Guy6926c722010-07-12 20:20:03 -07002664 }
2665
Chris Craik710f46d2012-09-17 17:25:49 -07002666 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002667 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002668 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002669 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2670 mCaches.activeTexture(0);
2671 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002672 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002673 drawShape(left, top, texture, p);
2674 } else {
2675 SkPath path;
2676 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2677 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2678 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2679 }
2680 path.addRect(rect);
2681 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002682 }
Chet Haase858aa932011-05-12 09:06:00 -07002683 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002684 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2685 SkPath path;
2686 path.addRect(left, top, right, bottom);
2687 drawConvexPath(path, p);
2688 } else {
2689 drawColorRect(left, top, right, bottom, p);
2690
2691 mDirty = true;
2692 }
Chet Haase858aa932011-05-12 09:06:00 -07002693 }
Romain Guyc7d53492010-06-25 13:41:57 -07002694}
Romain Guy9d5316e2010-06-24 19:30:36 -07002695
Chris Craikd218a922014-01-02 17:13:34 -08002696void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2697 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002698 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002699 mCaches.activeTexture(0);
2700
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002701 TextShadow textShadow;
2702 if (!getTextShadow(paint, &textShadow)) {
2703 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2704 }
2705
Raph Levien416a8472012-07-19 22:48:17 -07002706 // NOTE: The drop shadow will not perform gamma correction
2707 // if shader-based correction is enabled
2708 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2709 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002710 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002711 // If the drop shadow exceeds the max texture size or couldn't be
2712 // allocated, skip drawing
2713 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002714 const AutoTexture autoCleanup(shadow);
2715
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002716 const float sx = x - shadow->left + textShadow.dx;
2717 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002718
Tom Hudson984162f2014-10-10 13:38:16 -04002719 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002720 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002721 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002722 }
2723
2724 setupDraw();
2725 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002726 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002727 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002728 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002729 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002730 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002731 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2732 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002733 setupDrawTexture(shadow->id);
2734 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002735 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002736 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08002737 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002738
2739 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2740}
2741
Romain Guy768bffc2013-02-27 13:50:45 -08002742bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002743 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002744 return MathUtils::isZero(alpha)
2745 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002746}
2747
Tom Hudson107843d2014-09-08 11:26:26 -04002748void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002749 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002750 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002751 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002752 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002753
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002755 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002756 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002757 }
2758
Chris Craik39a908c2013-06-13 14:39:01 -07002759 mCaches.enableScissor();
2760
Romain Guy671d6cf2012-01-18 12:39:17 -08002761 float x = 0.0f;
2762 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002763 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002764 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002765 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2766 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002767 }
2768
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002769 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002770 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002771
2772 int alpha;
2773 SkXfermode::Mode mode;
2774 getAlphaAndMode(paint, &alpha, &mode);
2775
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002776 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002777 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002778 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002779 }
2780
Romain Guy671d6cf2012-01-18 12:39:17 -08002781 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002782 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002783 if (pureTranslate && !linearFilter) {
2784 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2785 }
Romain Guy257ae352013-03-20 16:31:12 -07002786 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002787
Rob Tsuk487a92c2015-01-06 13:22:54 -08002788 const Rect& clip(pureTranslate ? writableSnapshot()->getClipRect() : writableSnapshot()->getLocalClip());
Romain Guy671d6cf2012-01-18 12:39:17 -08002789 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2790
Romain Guy211370f2012-02-01 16:10:55 -08002791 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002792
Victoria Lease1e546812013-06-25 14:25:17 -07002793 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Rob Tsuk487a92c2015-01-06 13:22:54 -08002794 if (fontRenderer.renderPosText(paint, &clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002795 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002796 if (hasActiveLayer) {
2797 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002798 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002799 }
2800 dirtyLayerUnchecked(bounds, getRegion());
2801 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002802 }
Chet Haase48659092012-05-31 15:21:51 -07002803
Tom Hudson107843d2014-09-08 11:26:26 -04002804 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002805}
2806
Chris Craik59744b72014-07-01 17:56:52 -07002807bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002808 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002809 outMatrix->setIdentity();
2810 return false;
2811 } else if (CC_UNLIKELY(transform.isPerspective())) {
2812 outMatrix->setIdentity();
2813 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002814 }
Chris Craik59744b72014-07-01 17:56:52 -07002815
2816 /**
Chris Craika736cd92014-08-04 13:18:38 -07002817 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2818 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002819 */
2820 float sx, sy;
2821 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002822 outMatrix->setScale(
2823 roundf(fmaxf(1.0f, sx)),
2824 roundf(fmaxf(1.0f, sy)));
2825 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002826}
2827
Tom Hudson984162f2014-10-10 13:38:16 -04002828int OpenGLRenderer::getSaveCount() const {
2829 return mState.getSaveCount();
2830}
2831
2832int OpenGLRenderer::save(int flags) {
2833 return mState.save(flags);
2834}
2835
2836void OpenGLRenderer::restore() {
2837 return mState.restore();
2838}
2839
2840void OpenGLRenderer::restoreToCount(int saveCount) {
2841 return mState.restoreToCount(saveCount);
2842}
2843
2844void OpenGLRenderer::translate(float dx, float dy, float dz) {
2845 return mState.translate(dx, dy, dz);
2846}
2847
2848void OpenGLRenderer::rotate(float degrees) {
2849 return mState.rotate(degrees);
2850}
2851
2852void OpenGLRenderer::scale(float sx, float sy) {
2853 return mState.scale(sx, sy);
2854}
2855
2856void OpenGLRenderer::skew(float sx, float sy) {
2857 return mState.skew(sx, sy);
2858}
2859
2860void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2861 mState.setMatrix(matrix);
2862}
2863
2864void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2865 mState.concatMatrix(matrix);
2866}
2867
2868bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2869 return mState.clipRect(left, top, right, bottom, op);
2870}
2871
2872bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2873 return mState.clipPath(path, op);
2874}
2875
2876bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2877 return mState.clipRegion(region, op);
2878}
2879
2880void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2881 mState.setClippingOutline(allocator, outline);
2882}
2883
2884void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2885 const Rect& rect, float radius, bool highPriority) {
2886 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2887}
2888
2889
2890
Tom Hudson107843d2014-09-08 11:26:26 -04002891void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002892 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002893 DrawOpMode drawOpMode) {
2894
Chris Craik527a3aa2013-03-04 10:19:31 -08002895 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002896 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2897 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002898 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002899 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002900 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002901 }
Romain Guycac5fd32011-12-01 20:08:50 -08002902 }
2903
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002904 const float oldX = x;
2905 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002906
Chris Craikd6b65f62014-01-01 14:45:21 -08002907 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002908 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002909
Romain Guy211370f2012-02-01 16:10:55 -08002910 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002911 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2912 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002913 }
2914
Romain Guy86568192010-12-14 15:55:39 -08002915 int alpha;
2916 SkXfermode::Mode mode;
2917 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002918
Romain Guyc74f45a2013-02-26 19:10:14 -08002919 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2920
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002921 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002922 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002923 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002924 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002925 }
2926
Romain Guy19d4dd82013-03-04 11:14:26 -08002927 const bool hasActiveLayer = hasLayer();
2928
Romain Guy624234f2013-03-05 16:43:31 -08002929 // We only pass a partial transform to the font renderer. That partial
2930 // matrix defines how glyphs are rasterized. Typically we want glyphs
2931 // to be rasterized at their final size on screen, which means the partial
2932 // matrix needs to take the scale factor into account.
2933 // When a partial matrix is used to transform glyphs during rasterization,
2934 // the mesh is generated with the inverse transform (in the case of scale,
2935 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2936 // apply the full transform matrix at draw time in the vertex shader.
2937 // Applying the full matrix in the shader is the easiest way to handle
2938 // rotation and perspective and allows us to always generated quads in the
2939 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002940 SkMatrix fontTransform;
2941 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2942 || fabs(y - (int) y) > 0.0f
2943 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002944 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002945 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002946
Romain Guy624234f2013-03-05 16:43:31 -08002947 // TODO: Implement better clipping for scaled/rotated text
Rob Tsuk487a92c2015-01-06 13:22:54 -08002948 const Rect* clip = !pureTranslate ? nullptr : &mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002949 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 -07002950
Raph Levien996e57c2012-07-23 15:22:52 -07002951 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002952 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002953
2954 // don't call issuedrawcommand, do it at end of batch
2955 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002956 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002957 SkPaint paintCopy(*paint);
2958 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2959 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002960 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002961 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002962 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002963 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002964 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002965
Chris Craik527a3aa2013-03-04 10:19:31 -08002966 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002967 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002968 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002969 }
Chris Craik41541822013-05-03 16:35:54 -07002970 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002971 }
Romain Guy694b5192010-07-21 21:33:20 -07002972
Chris Craike63f7c622013-10-17 10:30:55 -07002973 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002974
Tom Hudson107843d2014-09-08 11:26:26 -04002975 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002976}
2977
Tom Hudson107843d2014-09-08 11:26:26 -04002978void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002979 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002980 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002981 return;
Romain Guy03d58522012-02-24 17:54:07 -08002982 }
2983
Chris Craik39a908c2013-06-13 14:39:01 -07002984 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2985 mCaches.enableScissor();
2986
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002987 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002988 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002989 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002990
2991 int alpha;
2992 SkXfermode::Mode mode;
2993 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002994 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002995
Tom Hudson984162f2014-10-10 13:38:16 -04002996 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002997 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 -08002998
Romain Guy97771732012-02-28 18:17:02 -08002999 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003000
3001 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08003002 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003003 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003004 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003005 dirtyLayerUnchecked(bounds, getRegion());
3006 }
Romain Guy97771732012-02-28 18:17:02 -08003007 }
Chet Haase48659092012-05-31 15:21:51 -07003008
Tom Hudson107843d2014-09-08 11:26:26 -04003009 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08003010}
3011
Tom Hudson107843d2014-09-08 11:26:26 -04003012void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003013 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07003014
Romain Guya1d3c912011-12-13 14:55:06 -08003015 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003016
Romain Guyfb8b7632010-08-23 21:05:08 -07003017 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003018 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07003019 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003020
Romain Guy8b55f372010-08-18 17:10:07 -07003021 const float x = texture->left - texture->offset;
3022 const float y = texture->top - texture->offset;
3023
Romain Guy01d58e42011-01-19 21:54:02 -08003024 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04003025 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07003026}
3027
Tom Hudson107843d2014-09-08 11:26:26 -04003028void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003029 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003030 return;
Romain Guy35643dd2012-09-18 15:40:58 -07003031 }
3032
Chris Craike84a2082014-12-22 14:28:49 -08003033 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07003034 if (layer->isTextureLayer()) {
3035 transform = &layer->getTransform();
3036 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07003037 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08003038 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003039 }
3040 }
3041
Chris Craik39a908c2013-06-13 14:39:01 -07003042 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08003043 const bool rejected = mState.calculateQuickRejectForScissor(
3044 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3045 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003046
3047 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003048 if (transform && !transform->isIdentity()) {
3049 restore();
3050 }
Tom Hudson107843d2014-09-08 11:26:26 -04003051 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003052 }
3053
Chris Craik62d307c2014-07-29 10:35:13 -07003054 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3055 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3056
Romain Guy5bb3c732012-11-29 17:52:58 -08003057 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003058
Chris Craik39a908c2013-06-13 14:39:01 -07003059 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003060 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003061
Romain Guy211370f2012-02-01 16:10:55 -08003062 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003063 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003064 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3065 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003066 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003067
Chris Craik16ecda52013-03-29 10:59:59 -07003068 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003069 setupDraw();
3070 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003071 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003072 setupDrawColorFilter(layer->getColorFilter());
3073 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003074 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003075 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003076 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003077 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003078 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3079 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3080 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003081
Romain Guyd21b6e12011-11-30 20:21:23 -08003082 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003083 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003084 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003085 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003086 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003087 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003088 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3089 }
Romain Guyf219da52011-01-16 12:54:25 -08003090
Romain Guy448455f2013-07-22 13:57:50 -07003091 TextureVertex* mesh = &layer->mesh[0];
3092 GLsizei elementsCount = layer->meshElementCount;
3093
3094 while (elementsCount > 0) {
3095 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3096
Romain Guy3380cfd2013-08-15 16:57:57 -07003097 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003098 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003099 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003100
3101 elementsCount -= drawCount;
3102 // Though there are 4 vertices in a quad, we use 6 indices per
3103 // quad to draw with GL_TRIANGLES
3104 mesh += (drawCount / 6) * 4;
3105 }
Romain Guyf219da52011-01-16 12:54:25 -08003106
Romain Guy3a3133d2011-02-01 22:59:58 -08003107#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003108 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003109#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003110 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003111
Romain Guy5bb3c732012-11-29 17:52:58 -08003112 if (layer->debugDrawUpdate) {
3113 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003114
3115 SkPaint paint;
3116 paint.setColor(0x7f00ff00);
3117 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003118 }
Romain Guyf219da52011-01-16 12:54:25 -08003119 }
Chris Craik34416ea2013-04-15 16:08:28 -07003120 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003121
Romain Guyb2e2f242012-10-17 18:18:35 -07003122 if (transform && !transform->isIdentity()) {
3123 restore();
3124 }
3125
Tom Hudson107843d2014-09-08 11:26:26 -04003126 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003127}
3128
Romain Guy6926c722010-07-12 20:20:03 -07003129///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003130// Draw filters
3131///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003132void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3133 // We should never get here since we apply the draw filter when stashing
3134 // the paints in the DisplayList.
3135 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003136}
3137
3138///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003139// Drawing implementation
3140///////////////////////////////////////////////////////////////////////////////
3141
Chris Craikd218a922014-01-02 17:13:34 -08003142Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003143 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003144 if (!texture) {
3145 return mCaches.textureCache.get(bitmap);
3146 }
3147 return texture;
3148}
3149
Romain Guy01d58e42011-01-19 21:54:02 -08003150void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003151 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003152 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003153 return;
3154 }
3155
3156 int alpha;
3157 SkXfermode::Mode mode;
3158 getAlphaAndMode(paint, &alpha, &mode);
3159
3160 setupDraw();
3161 setupDrawWithTexture(true);
3162 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003163 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003164 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003165 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003166 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003167 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3168 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003169 setupDrawTexture(texture->id);
3170 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003171 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003172 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003173 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003174
3175 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003176}
3177
Romain Guyf607bdc2010-09-10 19:20:06 -07003178// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003179#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3180#define kStdUnderline_Offset (1.0f / 9.0f)
3181#define kStdUnderline_Thickness (1.0f / 18.0f)
3182
Chris Craikd218a922014-01-02 17:13:34 -08003183void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3184 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003185 // Handle underline and strike-through
3186 uint32_t flags = paint->getFlags();
3187 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003188 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003189
Romain Guy211370f2012-02-01 16:10:55 -08003190 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003191 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003192 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003193
Raph Levien8b4072d2012-07-30 15:50:00 -07003194 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003195 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003196
Romain Guyf6834472011-01-23 13:32:12 -08003197 int linesCount = 0;
3198 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3199 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3200
3201 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003202 float points[pointsCount];
3203 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003204
3205 if (flags & SkPaint::kUnderlineText_Flag) {
3206 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003207 points[currentPoint++] = left;
3208 points[currentPoint++] = top;
3209 points[currentPoint++] = left + underlineWidth;
3210 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003211 }
3212
3213 if (flags & SkPaint::kStrikeThruText_Flag) {
3214 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003215 points[currentPoint++] = left;
3216 points[currentPoint++] = top;
3217 points[currentPoint++] = left + underlineWidth;
3218 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003219 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003220
Romain Guy726aeba2011-06-01 14:52:00 -07003221 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003222
Romain Guy726aeba2011-06-01 14:52:00 -07003223 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003224 }
3225 }
3226}
3227
Tom Hudson107843d2014-09-08 11:26:26 -04003228void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003229 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003230 return;
Romain Guy672433d2013-01-04 19:05:13 -08003231 }
3232
Tom Hudson107843d2014-09-08 11:26:26 -04003233 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003234}
3235
Tom Hudson107843d2014-09-08 11:26:26 -04003236void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003237 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003238 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003239
Chris Craik15a07a22014-01-26 13:43:53 -08003240 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003241 mCaches.enableScissor();
3242
3243 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003244 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003245
ztenghui14a4e352014-08-13 10:44:39 -07003246 // The caller has made sure casterAlpha > 0.
3247 float ambientShadowAlpha = mAmbientShadowAlpha;
3248 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3249 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3250 }
3251 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3252 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003253 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003254 }
ztenghui7b4516e2014-01-07 10:42:55 -08003255
ztenghui14a4e352014-08-13 10:44:39 -07003256 float spotShadowAlpha = mSpotShadowAlpha;
3257 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3258 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3259 }
3260 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3261 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003262 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003263 }
ztenghui7b4516e2014-01-07 10:42:55 -08003264
Tom Hudson107843d2014-09-08 11:26:26 -04003265 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003266}
3267
Tom Hudson107843d2014-09-08 11:26:26 -04003268void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003269 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003270 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003271 return;
Romain Guy3b753822013-03-05 10:27:35 -08003272 }
Romain Guy735738c2012-12-03 12:34:51 -08003273
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003274 int color = paint->getColor();
3275 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003276 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003277 color |= 0x00ffffff;
3278 }
3279
Romain Guy672433d2013-01-04 19:05:13 -08003280 float left = FLT_MAX;
3281 float top = FLT_MAX;
3282 float right = FLT_MIN;
3283 float bottom = FLT_MIN;
3284
Romain Guy448455f2013-07-22 13:57:50 -07003285 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003286 Vertex* vertex = mesh;
3287
Chris Craik2af46352012-11-26 18:30:17 -08003288 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003289 float l = rects[index + 0];
3290 float t = rects[index + 1];
3291 float r = rects[index + 2];
3292 float b = rects[index + 3];
3293
Romain Guy3b753822013-03-05 10:27:35 -08003294 Vertex::set(vertex++, l, t);
3295 Vertex::set(vertex++, r, t);
3296 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003297 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003298
Romain Guy3b753822013-03-05 10:27:35 -08003299 left = fminf(left, l);
3300 top = fminf(top, t);
3301 right = fmaxf(right, r);
3302 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003303 }
3304
Chris Craikf0a59072013-11-19 18:00:46 -08003305 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003306 return;
Romain Guya362c692013-02-04 13:50:16 -08003307 }
Romain Guy672433d2013-01-04 19:05:13 -08003308
Romain Guy672433d2013-01-04 19:05:13 -08003309 setupDraw();
3310 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003311 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003312 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003313 setupDrawColorFilter(getColorFilter(paint));
3314 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003315 setupDrawProgram();
3316 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003317 setupDrawModelView(kModelViewMode_Translate, false,
3318 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003319 setupDrawColorUniforms(getShader(paint));
3320 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003321 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003322
Romain Guy8ce00302013-01-15 18:51:42 -08003323 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003324 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003325 }
3326
Chris Craik4063a0e2013-11-15 16:06:56 -08003327 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003328
Tom Hudson107843d2014-09-08 11:26:26 -04003329 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003330}
3331
Romain Guy026c5e162010-06-28 17:12:22 -07003332void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003333 const SkPaint* paint, bool ignoreTransform) {
3334 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003335 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003336 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003337 color |= 0x00ffffff;
3338 }
3339
Romain Guy70ca14e2010-12-13 18:24:33 -08003340 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003341 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003342 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003343 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003344 setupDrawColorFilter(getColorFilter(paint));
3345 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003346 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003347 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3348 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003349 setupDrawColorUniforms(getShader(paint));
3350 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003351 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003352 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003353
Romain Guyc95c8d62010-09-17 15:31:32 -07003354 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3355}
3356
Romain Guy82ba8142010-07-09 13:25:56 -07003357void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003358 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003359 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003360
Chris Craike84a2082014-12-22 14:28:49 -08003361 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003362 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3363
3364 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003365 vertices = &mMeshVertices[0].x;
3366 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003367
3368 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3369 texture->uvMapper->map(uvs);
3370
3371 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3372 }
3373
Chris Craikd6b65f62014-01-01 14:45:21 -08003374 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3375 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3376 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003377
Romain Guyd21b6e12011-11-30 20:21:23 -08003378 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003379 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003380 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003381 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003382 } else {
Chris Craik678625242014-02-28 12:26:34 -08003383 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003384 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003385 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3386 }
3387
3388 if (texture->uvMapper) {
3389 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003390 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003391}
3392
Romain Guyf7f93552010-07-08 19:17:03 -07003393void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003394 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003395 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003396 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3397 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003398
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003399 int a;
3400 SkXfermode::Mode mode;
3401 getAlphaAndMode(paint, &a, &mode);
3402 const float alpha = a / 255.0f;
3403
Romain Guy746b7402010-10-26 16:27:31 -07003404 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003405 setupDrawWithTexture();
3406 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003407 setupDrawColorFilter(getColorFilter(paint));
3408 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003409 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003410 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003411 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003412 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003413 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003414 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003415 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003416
Romain Guy6820ac82010-09-15 18:11:50 -07003417 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003418}
3419
Romain Guy3b748a42013-04-17 18:54:38 -07003420void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003421 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003422 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003423 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3424 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003425
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003426 int a;
3427 SkXfermode::Mode mode;
3428 getAlphaAndMode(paint, &a, &mode);
3429 const float alpha = a / 255.0f;
3430
Romain Guy3b748a42013-04-17 18:54:38 -07003431 setupDraw();
3432 setupDrawWithTexture();
3433 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003434 setupDrawColorFilter(getColorFilter(paint));
3435 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003436 setupDrawProgram();
3437 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003438 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003439 setupDrawTexture(texture);
3440 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003441 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003442 setupDrawMeshIndices(vertices, texCoords, vbo);
3443
Chris Craike84a2082014-12-22 14:28:49 -08003444 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003445}
3446
Romain Guy886b2752013-01-04 12:26:18 -08003447void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003448 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003449 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003450 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003451
Chris Craike84a2082014-12-22 14:28:49 -08003452 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003453 int alpha;
3454 SkXfermode::Mode mode;
3455 getAlphaAndMode(paint, &alpha, &mode);
3456
Romain Guy886b2752013-01-04 12:26:18 -08003457 setupDraw();
3458 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003459 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003460 setupDrawAlpha8Color(color, alpha);
3461 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003462 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003463 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003464 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003465 setupDrawProgram();
3466 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003467 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003468 setupDrawTexture(texture);
3469 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003470 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003471 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003472 setupDrawMesh(vertices, texCoords);
3473
3474 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003475}
3476
Romain Guya5aed0d2010-09-09 14:42:43 -07003477void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003478 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003479
Chris Craike84a2082014-12-22 14:28:49 -08003480 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003481 blend = true;
3482 mDescription.hasRoundRectClip = true;
3483 }
3484 mSkipOutlineClip = true;
3485
Romain Guy82ba8142010-07-09 13:25:56 -07003486 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003487
Romain Guy82ba8142010-07-09 13:25:56 -07003488 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003489 // These blend modes are not supported by OpenGL directly and have
3490 // to be implemented using shaders. Since the shader will perform
3491 // the blending, turn blending off here
3492 // If the blend mode cannot be implemented using shaders, fall
3493 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003494 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003495 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003496 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003497 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003498
Romain Guy82bc7a72012-01-03 14:13:39 -08003499 if (mCaches.blend) {
3500 glDisable(GL_BLEND);
3501 mCaches.blend = false;
3502 }
3503
3504 return;
3505 } else {
3506 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003507 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003508 }
3509
3510 if (!mCaches.blend) {
3511 glEnable(GL_BLEND);
3512 }
3513
3514 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3515 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3516
3517 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3518 glBlendFunc(sourceMode, destMode);
3519 mCaches.lastSrcMode = sourceMode;
3520 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003521 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003522 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003523 glDisable(GL_BLEND);
3524 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003525 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003526}
3527
Romain Guy889f8d12010-07-29 14:37:42 -07003528bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003529 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003530 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003531 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003532 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003533 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003534 }
Romain Guy6926c722010-07-12 20:20:03 -07003535 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003536}
3537
Romain Guy026c5e162010-06-28 17:12:22 -07003538void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003539 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003540 TextureVertex::setUV(v++, u1, v1);
3541 TextureVertex::setUV(v++, u2, v1);
3542 TextureVertex::setUV(v++, u1, v2);
3543 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003544}
3545
Chris Craike84a2082014-12-22 14:28:49 -08003546void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3547 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003548 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003549 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3550 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003551 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003552 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003553 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003554}
3555
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003556float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003557 float alpha;
3558 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3559 alpha = mDrawModifiers.mOverrideLayerAlpha;
3560 } else {
3561 alpha = layer->getAlpha() / 255.0f;
3562 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003563 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003564}
3565
Romain Guy9d5316e2010-06-24 19:30:36 -07003566}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003567}; // namespace android