blob: 3c8fb8bbde6df1bf2d5fd8c709ac3bcb0908867c [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Chris Craik98d608d2014-07-17 12:25:11 -070024#include <SkColor.h>
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040025#include <SkShader.h>
Romain Guy694b5192010-07-21 21:33:20 -070026#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070027
Romain Guye4d01122010-06-16 18:44:05 -070028#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070029#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070030
Romain Guy08aa2cb2011-03-17 11:06:57 -070031#include <private/hwui/DrawGlInfo.h>
32
Romain Guy5b3b3522010-10-27 18:57:51 -070033#include <ui/Rect.h>
34
Romain Guy85bf02f2010-06-22 13:11:24 -070035#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080036#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080037#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070038#include "Fence.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040039#include "GammaFontRenderer.h"
40#include "Patch.h"
Chris Craik65cd6122012-12-10 17:56:27 -080041#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070042#include "Properties.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040043#include "RenderNode.h"
44#include "RenderState.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080045#include "ShadowTessellator.h"
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -040046#include "SkiaShader.h"
Romain Guya957eea2010-12-08 18:34:42 -080047#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080048#include "VertexBuffer.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040049#include "utils/GLUtils.h"
Tom Hudson8dfaa492014-12-09 15:03:44 -050050#include "utils/PaintUtils.h"
Chris Craik06e7fe52014-11-20 17:27:36 -080051#include "utils/TraceUtils.h"
Romain Guye4d01122010-06-16 18:44:05 -070052
Chris Craik62d307c2014-07-29 10:35:13 -070053#if DEBUG_DETAILED_EVENTS
54 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
55#else
56 #define EVENT_LOGD(...)
57#endif
58
Romain Guye4d01122010-06-16 18:44:05 -070059namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070060namespace uirenderer {
61
Chris Craik678625242014-02-28 12:26:34 -080062static GLenum getFilter(const SkPaint* paint) {
63 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
64 return GL_LINEAR;
65 }
66 return GL_NEAREST;
67}
Romain Guyd21b6e12011-11-30 20:21:23 -080068
Romain Guy9d5316e2010-06-24 19:30:36 -070069///////////////////////////////////////////////////////////////////////////////
70// Globals
71///////////////////////////////////////////////////////////////////////////////
72
Romain Guy889f8d12010-07-29 14:37:42 -070073/**
74 * Structure mapping Skia xfermodes to OpenGL blending factors.
75 */
76struct Blender {
77 SkXfermode::Mode mode;
78 GLenum src;
79 GLenum dst;
80}; // struct Blender
81
Romain Guy026c5e162010-06-28 17:12:22 -070082// In this array, the index of each Blender equals the value of the first
83// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
84static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
86 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
87 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
88 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
90 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
91 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
92 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
95 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
96 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050098 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070099 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700100};
Romain Guye4d01122010-06-16 18:44:05 -0700101
Romain Guy87a76572010-09-13 18:11:21 -0700102// This array contains the swapped version of each SkXfermode. For instance
103// this array's SrcOver blending mode is actually DstOver. You can refer to
104// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700105static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
107 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
108 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
109 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
110 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
111 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
112 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
113 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
114 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
115 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
116 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
117 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
118 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500119 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700120 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700121};
122
Romain Guyf6a11b82010-06-23 17:47:49 -0700123///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700124// Functions
125///////////////////////////////////////////////////////////////////////////////
126
127template<typename T>
128static inline T min(T a, T b) {
129 return a < b ? a : b;
130}
131
132///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700133// Constructors/destructor
134///////////////////////////////////////////////////////////////////////////////
135
John Reck3b202512014-06-23 13:13:08 -0700136OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400137 : mState(*this)
138 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700139 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700140 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700141 , mRenderState(renderState)
142 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700143 , mSuppressTiling(false)
144 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400145 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700146 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700147 , mLightRadius(FLT_MIN)
148 , mAmbientShadowAlpha(0)
149 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800150 // *set* draw modifiers to be 0
151 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700152 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700153
Romain Guyac670c02010-07-27 17:39:27 -0700154 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700155}
156
Romain Guy85bf02f2010-06-22 13:11:24 -0700157OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700158 // The context has already been destroyed at this point, do not call
159 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700160}
161
Romain Guy87e2f7572012-09-24 11:37:12 -0700162void OpenGLRenderer::initProperties() {
163 char property[PROPERTY_VALUE_MAX];
164 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
165 mScissorOptimizationDisabled = !strcasecmp(property, "true");
166 INIT_LOGD(" Scissor optimization %s",
167 mScissorOptimizationDisabled ? "disabled" : "enabled");
168 } else {
169 INIT_LOGD(" Scissor optimization enabled");
170 }
Romain Guy13631f32012-01-30 17:41:55 -0800171}
172
Chris Craik058fc642014-07-23 18:19:28 -0700173void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
174 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
175 mLightCenter = lightCenter;
176 mLightRadius = lightRadius;
177 mAmbientShadowAlpha = ambientShadowAlpha;
178 mSpotShadowAlpha = spotShadowAlpha;
179}
180
Romain Guy13631f32012-01-30 17:41:55 -0800181///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700182// Setup
183///////////////////////////////////////////////////////////////////////////////
184
Chris Craik797b95b22014-05-20 18:10:25 -0700185void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700186 glDisable(GL_DITHER);
187 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
188
189 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700190 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700191}
192
Romain Guy96885eb2013-03-26 15:05:58 -0700193void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800194 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800195 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400196 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700197 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700198 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700199}
200
Tom Hudson107843d2014-09-08 11:26:26 -0400201void OpenGLRenderer::startFrame() {
202 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700203 mFrameStarted = true;
204
Tom Hudson984162f2014-10-10 13:38:16 -0400205 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700206
Romain Guy96885eb2013-03-26 15:05:58 -0700207 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700208
Tom Hudson984162f2014-10-10 13:38:16 -0400209 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800210
Romain Guy54c1a642012-09-27 17:55:46 -0700211 // Functors break the tiling extension in pretty spectacular ways
212 // This ensures we don't use tiling when a functor is going to be
213 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700214 mSuppressTiling = mCaches.hasRegisteredFunctors()
215 || mFirstFrameAfterResize;
216 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700217
Chris Craikd6b65f62014-01-01 14:45:21 -0800218 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700219
Romain Guy7c450aa2012-09-21 19:15:00 -0700220 debugOverdraw(true, true);
221
Tom Hudson107843d2014-09-08 11:26:26 -0400222 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700223 mTilingClip.right, mTilingClip.bottom, mOpaque);
224}
225
Tom Hudson107843d2014-09-08 11:26:26 -0400226void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700227 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700228
Romain Guy96885eb2013-03-26 15:05:58 -0700229 setupFrameState(left, top, right, bottom, opaque);
230
231 // Layer renderers will start the frame immediately
232 // The framebuffer renderer will first defer the display list
233 // for each layer and wait until the first drawing command
234 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800235 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700236 syncState();
237 updateLayers();
238 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400239 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700240 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400248 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
249 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Tom Hudson107843d2014-09-08 11:26:26 -0400257void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700258 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700260 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400262 mDirty = true;
263 return;
Romain Guyddf74372012-05-22 14:07:07 -0700264 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700267}
268
269void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700270 if (mCaches.blend) {
271 glEnable(GL_BLEND);
272 } else {
273 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
henry.uh_chen33f5a592014-07-02 19:36:56 +0800277void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700278 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800279 const Snapshot* snapshot = currentSnapshot();
280
Chris Craik14e51302013-12-30 15:32:54 -0800281 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800282 if (snapshot->flags & Snapshot::kFlagFboTarget) {
283 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700284 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700285
henry.uh_chen33f5a592014-07-02 19:36:56 +0800286 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800287 }
288}
289
henry.uh_chen33f5a592014-07-02 19:36:56 +0800290void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800291 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800292 if(expand) {
293 // Expand the startTiling region by 1
294 int leftNotZero = (clip.left > 0) ? 1 : 0;
295 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
296
297 mCaches.startTiling(
298 clip.left - leftNotZero,
299 windowHeight - clip.bottom - topNotZero,
300 clip.right - clip.left + leftNotZero + 1,
301 clip.bottom - clip.top + topNotZero + 1,
302 opaque);
303 } else {
304 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800305 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800306 }
Romain Guy54c1a642012-09-27 17:55:46 -0700307 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700308}
309
310void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700311 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700312}
313
Tom Hudson107843d2014-09-08 11:26:26 -0400314bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700315 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700316 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800317 mTempPaths.clear();
318
Romain Guyca89e2a2013-03-08 17:44:20 -0800319 // When finish() is invoked on FBO 0 we've reached the end
320 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400321 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800322 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700323 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800324 }
325
Romain Guy11cb6422012-09-21 00:39:43 -0700326 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700327#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700328 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700329#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700330
Romain Guyc15008e2010-11-10 11:59:15 -0800331#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800332 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700333#else
334 if (mCaches.getDebugLevel() & kDebugMemory) {
335 mCaches.dumpMemoryUsage();
336 }
Romain Guyc15008e2010-11-10 11:59:15 -0800337#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700338 }
Romain Guy96885eb2013-03-26 15:05:58 -0700339
340 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400341
342 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700343}
344
Romain Guy35643dd2012-09-18 15:40:58 -0700345void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700346 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
347 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700348 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700349
350 mCaches.resetScissor();
351 dirtyClip();
352}
353
Andreas Gampe64bb4132014-11-22 00:35:09 +0000354void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400355 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700356
Tom Hudson984162f2014-10-10 13:38:16 -0400357 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700358 clip.snapToPixelBoundaries();
359
Romain Guyd643bb52011-03-01 14:55:21 -0800360 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800361 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800362 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800363 dirtyLayerUnchecked(clip, getRegion());
364 }
Romain Guyd643bb52011-03-01 14:55:21 -0800365
Romain Guy08aa2cb2011-03-17 11:06:57 -0700366 DrawGlInfo info;
367 info.clipLeft = clip.left;
368 info.clipTop = clip.top;
369 info.clipRight = clip.right;
370 info.clipBottom = clip.bottom;
371 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700372 info.width = getViewportWidth();
373 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800374 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700375
Tom Hudson984162f2014-10-10 13:38:16 -0400376 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700377 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400378 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700379 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
380 }
John Reck3b202512014-06-23 13:13:08 -0700381 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700382 setScissorFromClip();
383 }
Chris Craik54f574a2013-08-26 11:23:46 -0700384
John Reck3b202512014-06-23 13:13:08 -0700385 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
386 // Scissor may have been modified, reset dirty clip
387 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800388
Tom Hudson107843d2014-09-08 11:26:26 -0400389 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800390}
391
Romain Guyf6a11b82010-06-23 17:47:49 -0700392///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700393// Debug
394///////////////////////////////////////////////////////////////////////////////
395
Chris Craik62d307c2014-07-29 10:35:13 -0700396void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
397#if DEBUG_DETAILED_EVENTS
398 const int BUFFER_SIZE = 256;
399 va_list ap;
400 char buf[BUFFER_SIZE];
401
402 va_start(ap, fmt);
403 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
404 va_end(ap);
405
406 eventMark(buf);
407#endif
408}
409
410
Romain Guy0f6675332013-03-01 14:31:04 -0800411void OpenGLRenderer::eventMark(const char* name) const {
412 mCaches.eventMark(0, name);
413}
414
Romain Guy87e2f7572012-09-24 11:37:12 -0700415void OpenGLRenderer::startMark(const char* name) const {
416 mCaches.startMark(0, name);
417}
418
419void OpenGLRenderer::endMark() const {
420 mCaches.endMark();
421}
422
423void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700424 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700425}
426
427void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400428 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700429 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700430
431 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400432 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700433 clip->right - clip->left, clip->bottom - clip->top);
434
Romain Guy627c6fd2013-08-21 11:53:18 -0700435 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700436 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700437 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
438
439 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700440 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700441 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
442
443 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700444 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700445 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
446
447 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700448 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700449 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
450
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.disable();
452 }
453}
454
455///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700456// Layers
457///////////////////////////////////////////////////////////////////////////////
458
459bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700460 if (layer->deferredUpdateScheduled && layer->renderer
461 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700462
Romain Guy7c450aa2012-09-21 19:15:00 -0700463 if (inFrame) {
464 endTiling();
465 debugOverdraw(false, false);
466 }
Romain Guy11cb6422012-09-21 00:39:43 -0700467
Romain Guy96885eb2013-03-26 15:05:58 -0700468 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700469 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700470 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700471 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700472 }
Romain Guy11cb6422012-09-21 00:39:43 -0700473
474 if (inFrame) {
475 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800476 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700477 }
478
Romain Guy5bb3c732012-11-29 17:52:58 -0800479 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700480 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700481
482 return true;
483 }
484
485 return false;
486}
487
488void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700489 // If draw deferring is enabled this method will simply defer
490 // the display list of each individual layer. The layers remain
491 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700492 int count = mLayerUpdates.size();
493 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700494 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
495 startMark("Layer Updates");
496 } else {
497 startMark("Defer Layer Updates");
498 }
Romain Guy11cb6422012-09-21 00:39:43 -0700499
Chris Craik1206b9b2013-04-04 14:46:24 -0700500 // Note: it is very important to update the layers in order
501 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700502 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700503 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700504 }
Romain Guy11cb6422012-09-21 00:39:43 -0700505
Romain Guy96885eb2013-03-26 15:05:58 -0700506 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
507 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400508 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700509 }
510 endMark();
511 }
512}
513
514void OpenGLRenderer::flushLayers() {
515 int count = mLayerUpdates.size();
516 if (count > 0) {
517 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700518
Chris Craik1206b9b2013-04-04 14:46:24 -0700519 // Note: it is very important to update the layers in order
520 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800521 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700522 }
523
524 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400525 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700526
Romain Guy11cb6422012-09-21 00:39:43 -0700527 endMark();
528 }
529}
530
531void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
532 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700533 // Make sure we don't introduce duplicates.
534 // SortedVector would do this automatically but we need to respect
535 // the insertion order. The linear search is not an issue since
536 // this list is usually very short (typically one item, at most a few)
537 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
538 if (mLayerUpdates.itemAt(i) == layer) {
539 return;
540 }
541 }
Romain Guy11cb6422012-09-21 00:39:43 -0700542 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700543 }
544}
545
Romain Guye93482f2013-06-17 13:14:51 -0700546void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
547 if (layer) {
548 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
549 if (mLayerUpdates.itemAt(i) == layer) {
550 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700551 break;
552 }
553 }
554 }
555}
556
Romain Guy40543602013-06-12 15:31:28 -0700557void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800558 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700559 syncState();
560 updateLayers();
561 flushLayers();
562 // Wait for all the layer updates to be executed
563 AutoFence fence;
564}
565
John Reck443a7142014-09-04 17:40:05 -0700566void OpenGLRenderer::markLayersAsBuildLayers() {
567 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
568 mLayerUpdates[i]->wasBuildLayered = true;
569 }
570}
571
Romain Guy11cb6422012-09-21 00:39:43 -0700572///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700573// State management
574///////////////////////////////////////////////////////////////////////////////
575
Chris Craik14e51302013-12-30 15:32:54 -0800576void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700577 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800578 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
579 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700580
Chris Craika64a2be2014-05-14 14:17:01 -0700581 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700582 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700583 }
584
Romain Guy2542d192010-08-18 11:47:12 -0700585 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700586 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700587 }
Romain Guy2542d192010-08-18 11:47:12 -0700588
Romain Guy5ec99242010-11-03 16:19:08 -0700589 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700590 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700591 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700592 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800593 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700594 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700595 }
Romain Guybb9524b2010-06-22 18:56:38 -0700596}
597
Romain Guyf6a11b82010-06-23 17:47:49 -0700598///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700599// Layers
600///////////////////////////////////////////////////////////////////////////////
601
602int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700603 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700604 // force matrix/clip isolation for layer
605 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
606
Tom Hudson984162f2014-10-10 13:38:16 -0400607 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700608
Tom Hudson984162f2014-10-10 13:38:16 -0400609 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700610 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700611 }
Romain Guyd55a8612010-06-28 17:42:46 -0700612
613 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700614}
615
Chris Craikd90144d2013-03-19 15:03:48 -0700616void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
617 const Rect untransformedBounds(bounds);
618
Chris Craikd6b65f62014-01-01 14:45:21 -0800619 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700620
621 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400622 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700623 // We cannot work with sub-pixels in this case
624 bounds.snapToPixelBoundaries();
625
626 // When the layer is not an FBO, we may use glCopyTexImage so we
627 // need to make sure the layer does not extend outside the bounds
628 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700629 const Snapshot& previous = *(currentSnapshot()->previous);
630 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
631 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700632 bounds.setEmpty();
633 } else if (fboLayer) {
634 clip.set(bounds);
635 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800636 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700637 inverse.mapRect(clip);
638 clip.snapToPixelBoundaries();
639 if (clip.intersect(untransformedBounds)) {
640 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
641 bounds.set(untransformedBounds);
642 } else {
643 clip.setEmpty();
644 }
645 }
646 } else {
647 bounds.setEmpty();
648 }
649}
650
Chris Craik408eb122013-03-26 18:55:15 -0700651void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
652 bool fboLayer, int alpha) {
653 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
654 bounds.getHeight() > mCaches.maxTextureSize ||
655 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400656 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700657 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400658 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700659 }
660}
661
Chris Craikd90144d2013-03-19 15:03:48 -0700662int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500663 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400664 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700665
Tom Hudson984162f2014-10-10 13:38:16 -0400666 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700667 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
668 // operations will be able to store and restore the current clip and transform info, and
669 // quick rejection will be correct (for display lists)
670
671 Rect bounds(left, top, right, bottom);
672 Rect clip;
673 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500674 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700675
Tom Hudson984162f2014-10-10 13:38:16 -0400676 if (!mState.currentlyIgnored()) {
677 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
678 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
679 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800680 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700681 }
682 }
683
684 return count;
685}
686
Romain Guy1c740bc2010-09-13 18:00:09 -0700687/**
688 * Layers are viewed by Skia are slightly different than layers in image editing
689 * programs (for instance.) When a layer is created, previously created layers
690 * and the frame buffer still receive every drawing command. For instance, if a
691 * layer is created and a shape intersecting the bounds of the layers and the
692 * framebuffer is draw, the shape will be drawn on both (unless the layer was
693 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
694 *
695 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
696 * texture. Unfortunately, this is inefficient as it requires every primitive to
697 * be drawn n + 1 times, where n is the number of active layers. In practice this
698 * means, for every primitive:
699 * - Switch active frame buffer
700 * - Change viewport, clip and projection matrix
701 * - Issue the drawing
702 *
703 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700704 * To avoid this, layers are implemented in a different way here, at least in the
705 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
706 * is set. When this flag is set we can redirect all drawing operations into a
707 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700708 *
709 * This implementation relies on the frame buffer being at least RGBA 8888. When
710 * a layer is created, only a texture is created, not an FBO. The content of the
711 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700712 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700713 * buffer and drawing continues as normal. This technique therefore treats the
714 * frame buffer as a scratch buffer for the layers.
715 *
716 * To compose the layers back onto the frame buffer, each layer texture
717 * (containing the original frame buffer data) is drawn as a simple quad over
718 * the frame buffer. The trick is that the quad is set as the composition
719 * destination in the blending equation, and the frame buffer becomes the source
720 * of the composition.
721 *
722 * Drawing layers with an alpha value requires an extra step before composition.
723 * An empty quad is drawn over the layer's region in the frame buffer. This quad
724 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
725 * quad is used to multiply the colors in the frame buffer. This is achieved by
726 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
727 * GL_ZERO, GL_SRC_ALPHA.
728 *
729 * Because glCopyTexImage2D() can be slow, an alternative implementation might
730 * be use to draw a single clipped layer. The implementation described above
731 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700732 *
733 * (1) The frame buffer is actually not cleared right away. To allow the GPU
734 * to potentially optimize series of calls to glCopyTexImage2D, the frame
735 * buffer is left untouched until the first drawing operation. Only when
736 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700737 */
Chet Haased48885a2012-08-28 17:43:28 -0700738bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700739 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800740 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
741 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700742
Romain Guyeb993562010-10-05 18:14:38 -0700743 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
744
Romain Guyf607bdc2010-09-10 19:20:06 -0700745 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700746 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700747 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700748 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000749 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700750
751 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400752 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700753 return false;
754 }
Romain Guyf18fd992010-07-08 11:45:51 -0700755
Romain Guya1d3c912011-12-13 14:55:06 -0800756 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700757 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700758 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700759 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700760 }
761
Derek Sollenberger674554f2014-02-19 16:47:32 +0000762 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700763 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
765 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500766
Chet Haasea23eed82012-04-12 15:19:04 -0700767 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700768 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700769 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700770
Romain Guy8fb95422010-08-17 18:38:51 -0700771 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400772 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
773 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700774
Chris Craika8bea8e2014-09-24 11:29:43 -0700775 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
776 fboLayer ? "" : "unclipped ",
777 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700778 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700779 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700780 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700781 } else {
782 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700783 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800784 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700786 // Workaround for some GL drivers. When reading pixels lying outside
787 // of the window we should get undefined values for those pixels.
788 // Unfortunately some drivers will turn the entire target texture black
789 // when reading outside of the window.
790 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800791 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800793 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700794
Chris Craika64a2be2014-05-14 14:17:01 -0700795 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
796 bounds.left, getViewportHeight() - bounds.bottom,
797 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700798
Romain Guy54be1cd2011-06-13 19:04:27 -0700799 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800800 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700801 }
Romain Guyeb993562010-10-05 18:14:38 -0700802 }
Romain Guyf86ef572010-07-01 11:05:42 -0700803
Romain Guyd55a8612010-06-28 17:42:46 -0700804 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700805}
806
Chris Craike63f7c622013-10-17 10:30:55 -0700807bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800808 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700810
Tom Hudson984162f2014-10-10 13:38:16 -0400811 writableSnapshot()->region = &writableSnapshot()->layer->region;
812 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
813 writableSnapshot()->fbo = layer->getFbo();
814 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
815 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
816 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800817 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy2b7028e2012-09-19 17:25:38 -0700819 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700820 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700822 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700827 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829 }
830
831 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
henry.uh_chen33f5a592014-07-02 19:36:56 +0800834 // Expand the startTiling region by 1
835 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
837 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700838 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800839 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 glClear(GL_COLOR_BUFFER_BIT);
842
843 dirtyClip();
844
845 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700846 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 return true;
848}
849
Romain Guy1c740bc2010-09-13 18:00:09 -0700850/**
851 * Read the documentation of createLayer() before doing anything in this method.
852 */
Chris Craik14e51302013-12-30 15:32:54 -0800853void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
854 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000855 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700856 return;
857 }
858
Chris Craik14e51302013-12-30 15:32:54 -0800859 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800860 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800861 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700862
Chris Craik39a908c2013-06-13 14:39:01 -0700863 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400864 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800865 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700866 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
867
Romain Guyeb993562010-10-05 18:14:38 -0700868 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700869 endTiling();
870
Romain Guye0aa84b2012-04-03 19:30:26 -0700871 // Detach the texture from the FBO
872 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800873
874 layer->removeFbo(false);
875
Romain Guyeb993562010-10-05 18:14:38 -0700876 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700877 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700878 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700879
Chris Craikd6b65f62014-01-01 14:45:21 -0800880 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700881 }
882
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500884 SkPaint layerPaint;
885 layerPaint.setAlpha(layer->getAlpha());
886 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
887 layerPaint.setColorFilter(layer->getColorFilter());
888
889 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700891 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700892 }
893
Romain Guy03750a02010-10-18 14:06:08 -0700894 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700895
Romain Guya1d3c912011-12-13 14:55:06 -0800896 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700897
Romain Guy5b3b3522010-10-27 18:57:51 -0700898 // When the layer is stored in an FBO, we can save a bit of fillrate by
899 // drawing only the dirty region
900 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800901 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 } else if (!rect.isEmpty()) {
904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530905
906 save(0);
907 // the layer contains screen buffer content that shouldn't be alpha modulated
908 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400909 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700910 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530911 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700912 }
Romain Guy8b55f372010-08-18 17:10:07 -0700913
Romain Guy746b7402010-10-26 16:27:31 -0700914 dirtyClip();
915
Romain Guyeb993562010-10-05 18:14:38 -0700916 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800917 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700918 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800919 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800920 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700921 }
922}
923
Romain Guyaa6c24c2011-04-28 18:40:04 -0700924void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700925 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700926
927 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700929 setupDrawWithTexture();
930 } else {
931 setupDrawWithExternalTexture();
932 }
933 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700934 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500935 setupDrawColorFilter(layer->getColorFilter());
936 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700938 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500939 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
941 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700942 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700944 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800945 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800946 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700947 layer->getWidth() == (uint32_t) rect.getWidth() &&
948 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800949 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
950 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700951
Romain Guyd21b6e12011-11-30 20:21:23 -0800952 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800953 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
954 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700955 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800956 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800957 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
958 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700959 }
960 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700961 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962
963 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964}
965
Romain Guy5b3b3522010-10-27 18:57:51 -0700966void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700967 if (layer->isTextureLayer()) {
968 EVENT_LOGD("composeTextureLayerRect");
969 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
970 drawTextureLayer(layer, rect);
971 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
972 } else {
973 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700974 const Rect& texCoords = layer->texCoords;
975 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
976 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700977
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 float x = rect.left;
979 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800980 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700981 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700982 layer->getHeight() == (uint32_t) rect.getHeight();
983
984 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700985 // When we're swapping, the layer is already in screen coordinates
986 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800987 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
988 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700989 }
990
Romain Guyd21b6e12011-11-30 20:21:23 -0800991 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800993 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 }
995
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500996 SkPaint layerPaint;
997 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
998 layerPaint.setXfermodeMode(layer->getMode());
999 layerPaint.setColorFilter(layer->getColorFilter());
1000
1001 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001003 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001004 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001006
Romain Guyaa6c24c2011-04-28 18:40:04 -07001007 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001008 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001009}
1010
Chris Craik34416ea2013-04-15 16:08:28 -07001011/**
1012 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1013 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1014 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1015 * by saveLayer's restore
1016 */
Tom Hudson984162f2014-10-10 13:38:16 -04001017#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1018 DRAW_COMMAND; \
1019 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1020 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1021 DRAW_COMMAND; \
1022 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1023 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001024 }
1025
1026#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1027
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001028// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1029// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1030class LayerShader : public SkShader {
1031public:
1032 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1033 : INHERITED(localMatrix)
1034 , mLayer(layer) {
1035 }
1036
Chris Craike84a2082014-12-22 14:28:49 -08001037 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001038 if (data) {
1039 *data = static_cast<void*>(mLayer);
1040 }
1041 return true;
1042 }
1043
Chris Craike84a2082014-12-22 14:28:49 -08001044 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001045 return !mLayer->isBlend();
1046 }
1047
1048protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001049 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001050 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1051 }
1052
Chris Craike84a2082014-12-22 14:28:49 -08001053 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001054 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1055 }
1056
Chris Craike84a2082014-12-22 14:28:49 -08001057 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001058 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001059 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001060 }
1061private:
1062 // Unowned.
1063 Layer* mLayer;
1064 typedef SkShader INHERITED;
1065};
1066
Romain Guy5b3b3522010-10-27 18:57:51 -07001067void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001068 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1069
1070 if (layer->getConvexMask()) {
1071 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1072
1073 // clip to the area of the layer the mask can be larger
1074 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1075
1076 SkPaint paint;
1077 paint.setAntiAlias(true);
1078 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1079
Chris Craik3f0854292014-04-15 16:18:08 -07001080 // create LayerShader to map SaveLayer content into subsequent draw
1081 SkMatrix shaderMatrix;
1082 shaderMatrix.setTranslate(rect.left, rect.bottom);
1083 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001084 LayerShader layerShader(layer, &shaderMatrix);
1085 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001086
1087 // Since the drawing primitive is defined in local drawing space,
1088 // we don't need to modify the draw matrix
1089 const SkPath* maskPath = layer->getConvexMask();
1090 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1091
Chris Craike84a2082014-12-22 14:28:49 -08001092 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001093 restore();
1094
1095 return;
1096 }
1097
Romain Guy5b3b3522010-10-27 18:57:51 -07001098 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001099 layer->setRegionAsRect();
1100
Chris Craik34416ea2013-04-15 16:08:28 -07001101 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001102
Romain Guy5b3b3522010-10-27 18:57:51 -07001103 layer->region.clear();
1104 return;
1105 }
1106
Chris Craik62d307c2014-07-29 10:35:13 -07001107 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001108 // standard Region based draw
1109 size_t count;
1110 const android::Rect* rects;
1111 Region safeRegion;
1112 if (CC_LIKELY(hasRectToRectTransform())) {
1113 rects = layer->region.getArray(&count);
1114 } else {
1115 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1116 rects = safeRegion.getArray(&count);
1117 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001118
Chris Craik3f0854292014-04-15 16:18:08 -07001119 const float alpha = getLayerAlpha(layer);
1120 const float texX = 1.0f / float(layer->getWidth());
1121 const float texY = 1.0f / float(layer->getHeight());
1122 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001123
Chris Craik3f0854292014-04-15 16:18:08 -07001124 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 // We must get (and therefore bind) the region mesh buffer
1127 // after we setup drawing in case we need to mess with the
1128 // stencil buffer in setupDraw()
1129 TextureVertex* mesh = mCaches.getRegionMesh();
1130 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001131
Chris Craik3f0854292014-04-15 16:18:08 -07001132 setupDrawWithTexture();
1133 setupDrawColor(alpha, alpha, alpha, alpha);
1134 setupDrawColorFilter(layer->getColorFilter());
1135 setupDrawBlending(layer);
1136 setupDrawProgram();
1137 setupDrawDirtyRegionsDisabled();
1138 setupDrawPureColorUniforms();
1139 setupDrawColorFilterUniforms(layer->getColorFilter());
1140 setupDrawTexture(layer->getTexture());
1141 if (currentTransform()->isPureTranslate()) {
1142 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1143 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144
Chris Craik3f0854292014-04-15 16:18:08 -07001145 layer->setFilter(GL_NEAREST);
1146 setupDrawModelView(kModelViewMode_Translate, false,
1147 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1148 } else {
1149 layer->setFilter(GL_LINEAR);
1150 setupDrawModelView(kModelViewMode_Translate, false,
1151 rect.left, rect.top, rect.right, rect.bottom);
1152 }
1153 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001154
Chris Craik3f0854292014-04-15 16:18:08 -07001155 for (size_t i = 0; i < count; i++) {
1156 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Chris Craik3f0854292014-04-15 16:18:08 -07001158 const float u1 = r->left * texX;
1159 const float v1 = (height - r->top) * texY;
1160 const float u2 = r->right * texX;
1161 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001162
Chris Craik3f0854292014-04-15 16:18:08 -07001163 // TODO: Reject quads outside of the clip
1164 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1165 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1166 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1167 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001168
Chris Craik3f0854292014-04-15 16:18:08 -07001169 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik3f0854292014-04-15 16:18:08 -07001171 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001172 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001173 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001174 numQuads = 0;
1175 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 }
Chris Craik3f0854292014-04-15 16:18:08 -07001177 }
1178
1179 if (numQuads > 0) {
1180 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001181 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001182 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001183
Romain Guy5b3b3522010-10-27 18:57:51 -07001184#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001185 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001186#endif
1187
Chris Craik3f0854292014-04-15 16:18:08 -07001188 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001189}
1190
Romain Guy3a3133d2011-02-01 22:59:58 -08001191#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001192void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001193 size_t count;
1194 const android::Rect* rects = region.getArray(&count);
1195
1196 uint32_t colors[] = {
1197 0x7fff0000, 0x7f00ff00,
1198 0x7f0000ff, 0x7fff00ff,
1199 };
1200
1201 int offset = 0;
1202 int32_t top = rects[0].top;
1203
1204 for (size_t i = 0; i < count; i++) {
1205 if (top != rects[i].top) {
1206 offset ^= 0x2;
1207 top = rects[i].top;
1208 }
1209
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001210 SkPaint paint;
1211 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001212 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001213 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001214 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001215}
Chris Craike63f7c622013-10-17 10:30:55 -07001216#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001217
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001218void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001219 Vector<float> rects;
1220
1221 SkRegion::Iterator it(region);
1222 while (!it.done()) {
1223 const SkIRect& r = it.rect();
1224 rects.push(r.fLeft);
1225 rects.push(r.fTop);
1226 rects.push(r.fRight);
1227 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001228 it.next();
1229 }
1230
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001231 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001232}
1233
Romain Guy5b3b3522010-10-27 18:57:51 -07001234void OpenGLRenderer::dirtyLayer(const float left, const float top,
1235 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001236 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001237 Rect bounds(left, top, right, bottom);
1238 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001239 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001241}
1242
1243void OpenGLRenderer::dirtyLayer(const float left, const float top,
1244 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001245 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001247 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001249}
1250
1251void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001252 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) {
Tom Hudson984162f2014-10-10 13:38:16 -04001331 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
Chris Craikd6b65f62014-01-01 14:45:21 -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;
Chris Craikd6b65f62014-01-01 14:45:21 -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
Chris Craikd6b65f62014-01-01 14:45:21 -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;
Chris Craikd6b65f62014-01-01 14:45:21 -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) {
Chris Craikd6b65f62014-01-01 14:45:21 -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() {
Tom Hudson984162f2014-10-10 13:38:16 -04001417 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
1451void OpenGLRenderer::setStencilFromClip() {
1452 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001453 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001454 EVENT_LOGD("setStencilFromClip - enabling");
1455
Romain Guy8ce00302013-01-15 18:51:42 -08001456 // NOTE: The order here is important, we must set dirtyClip to false
1457 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001458 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001459
1460 ensureStencilBuffer();
1461
1462 mCaches.stencil.enableWrite();
1463
Chris Craikdeeda3d2014-05-05 19:09:33 -07001464 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001465 // to the region's bounds
1466 bool resetScissor = mCaches.enableScissor();
1467 if (resetScissor) {
1468 // The scissor was not set so we now need to update it
1469 setScissorFromClip();
1470 }
1471 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001472
1473 // stash and disable the outline clip state, since stencil doesn't account for outline
1474 bool storedSkipOutlineClip = mSkipOutlineClip;
1475 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001476
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001477 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001478 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001479 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1480
Romain Guy8ce00302013-01-15 18:51:42 -08001481 // NOTE: We could use the region contour path to generate a smaller mesh
1482 // Since we are using the stencil we could use the red book path
1483 // drawing technique. It might increase bandwidth usage though.
1484
1485 // The last parameter is important: we are not drawing in the color buffer
1486 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001487 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001488 if (resetScissor) mCaches.disableScissor();
1489 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001490
1491 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001492
1493 // Draw the region used to generate the stencil if the appropriate debug
1494 // mode is enabled
1495 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001496 paint.setColor(0x7f0000ff);
1497 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1498 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001499 }
Romain Guy8ce00302013-01-15 18:51:42 -08001500 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001501 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001502 mCaches.stencil.disable();
1503 }
1504 }
1505}
1506
Chris Craikf0a59072013-11-19 18:00:46 -08001507/**
1508 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1509 *
1510 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1511 * style, and tessellated AA ramp
1512 */
1513bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001514 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001515 bool snapOut = paint && paint->isAntiAlias();
1516
1517 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1518 float outset = paint->getStrokeWidth() * 0.5f;
1519 left -= outset;
1520 top -= outset;
1521 right += outset;
1522 bottom += outset;
1523 }
1524
Chris Craikdeeda3d2014-05-05 19:09:33 -07001525 bool clipRequired = false;
1526 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001527 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001528 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001529 return true;
1530 }
1531
Chris Craikf23b25a2014-06-26 15:46:20 -07001532 // not quick rejected, so enable the scissor if clipRequired
1533 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1534 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001535 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001536}
1537
Romain Guy8ce00302013-01-15 18:51:42 -08001538void OpenGLRenderer::debugClip() {
1539#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001540 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001541 SkPaint paint;
1542 paint.setColor(0x7f00ff00);
1543 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1544
Romain Guy8ce00302013-01-15 18:51:42 -08001545 }
1546#endif
1547}
1548
Romain Guyf6a11b82010-06-23 17:47:49 -07001549///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001550// Drawing commands
1551///////////////////////////////////////////////////////////////////////////////
1552
Chris Craik62d307c2014-07-29 10:35:13 -07001553void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001554 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001555 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001556 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001557 // Make sure setScissor & setStencil happen at the beginning of
1558 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001559 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001560 if (mCaches.scissorEnabled) {
1561 setScissorFromClip();
1562 }
Chris Craik62d307c2014-07-29 10:35:13 -07001563
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001564 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001565 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001566
Romain Guy70ca14e2010-12-13 18:24:33 -08001567 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001568
Romain Guy70ca14e2010-12-13 18:24:33 -08001569 mSetShaderColor = false;
1570 mColorSet = false;
1571 mColorA = mColorR = mColorG = mColorB = 0.0f;
1572 mTextureUnit = 0;
1573 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001574
1575 // Enable debug highlight when what we're about to draw is tested against
1576 // the stencil buffer and if stencil highlight debugging is on
1577 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1578 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1579 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001580}
1581
1582void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1583 mDescription.hasTexture = true;
1584 mDescription.hasAlpha8Texture = isAlpha8;
1585}
1586
Romain Guyff316ec2013-02-13 18:39:43 -08001587void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1588 mDescription.hasTexture = true;
1589 mDescription.hasColors = true;
1590 mDescription.hasAlpha8Texture = isAlpha8;
1591}
1592
Romain Guyaa6c24c2011-04-28 18:40:04 -07001593void OpenGLRenderer::setupDrawWithExternalTexture() {
1594 mDescription.hasExternalTexture = true;
1595}
1596
Romain Guy15bc6432011-12-13 13:11:32 -08001597void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001598 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001599}
1600
Chris Craik91a8c7c2014-08-12 14:31:35 -07001601void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1602 mDescription.hasVertexAlpha = true;
1603 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001604}
1605
Romain Guy8d0d4782010-12-14 20:13:35 -08001606void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1607 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001608 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1609 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1610 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001611 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001612 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001613}
1614
Romain Guy86568192010-12-14 15:55:39 -08001615void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1616 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001617 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1618 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1619 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001620 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001621 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001622}
1623
Romain Guy41210632012-07-16 17:04:24 -07001624void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1625 mCaches.fontRenderer->describe(mDescription, paint);
1626}
1627
Romain Guy70ca14e2010-12-13 18:24:33 -08001628void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1629 mColorA = a;
1630 mColorR = r;
1631 mColorG = g;
1632 mColorB = b;
1633 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001634 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001635}
1636
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001637void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001638 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001639 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001640 }
1641}
1642
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001643void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001644 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001645 return;
1646 }
1647
1648 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001649 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001650 mDescription.colorOp = ProgramDescription::kColorBlend;
1651 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001652 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001653 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001654 }
1655}
1656
Romain Guyf09ef512011-05-27 11:43:46 -07001657void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1658 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1659 mColorA = 1.0f;
1660 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001661 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001662 }
1663}
1664
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001665void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1666 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001667 // When the blending mode is kClear_Mode, we need to use a modulate color
1668 // argb=1,0,0,0
1669 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001670 // TODO: check shader blending, once we have shader drawing support for layers.
Tom Hudson8dfaa492014-12-09 15:03:44 -05001671 bool blend = layer->isBlend()
1672 || getLayerAlpha(layer) < 1.0f
1673 || (mColorSet && mColorA < 1.0f)
1674 || PaintUtils::isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001675 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001676}
1677
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001678void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1679 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001680 // When the blending mode is kClear_Mode, we need to use a modulate color
1681 // argb=1,0,0,0
1682 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001683 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001684 (getShader(paint) && !getShader(paint)->isOpaque()) ||
Tom Hudson8dfaa492014-12-09 15:03:44 -05001685 PaintUtils::isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001686 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001687}
1688
1689void OpenGLRenderer::setupDrawProgram() {
1690 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001691 if (mDescription.hasRoundRectClip) {
1692 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001693 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001694 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001695 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001696 innerRect.left, innerRect.top,
1697 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001698 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1699 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001700
1701 // add half pixel to round out integer rect space to cover pixel centers
1702 float roundedOutRadius = state->radius + 0.5f;
1703 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1704 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001705 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001706}
1707
1708void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1709 mTrackDirtyRegions = false;
1710}
1711
Chris Craik4063a0e2013-11-15 16:06:56 -08001712void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1713 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001714 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001715 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001716 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001717 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001718
Romain Guy86568192010-12-14 15:55:39 -08001719 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001720 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001721 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1722 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001723 if (dirty && mTrackDirtyRegions) {
1724 if (!ignoreTransform) {
1725 dirtyLayer(left, top, right, bottom, *currentTransform());
1726 } else {
1727 dirtyLayer(left, top, right, bottom);
1728 }
Romain Guy86568192010-12-14 15:55:39 -08001729 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001730}
1731
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001732void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1733 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001734 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1735 }
1736}
1737
Romain Guy86568192010-12-14 15:55:39 -08001738void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001739 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001740 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001741 }
1742}
1743
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001744void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001745 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001746 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001747 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001748
1749 if (ignoreTransform) {
1750 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1751 // because it was built into modelView / the geometry, and the description needs to
1752 // compensate.
1753 mat4 modelViewWithoutTransform;
1754 modelViewWithoutTransform.loadInverse(*currentTransform());
1755 modelViewWithoutTransform.multiply(mModelViewMatrix);
1756 mModelViewMatrix.load(modelViewWithoutTransform);
1757 }
1758
1759 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001760}
1761
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001762void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001763 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001764 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001765 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001766
1767 SkColor color;
1768 SkXfermode::Mode mode;
1769 if (filter->asColorMode(&color, &mode)) {
1770 const int alpha = SkColorGetA(color);
1771 const GLfloat a = alpha / 255.0f;
1772 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1773 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1774 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1775 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1776 return;
1777 }
1778
1779 SkScalar srcColorMatrix[20];
1780 if (filter->asColorMatrix(srcColorMatrix)) {
1781
1782 float colorMatrix[16];
1783 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1784 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1785 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1786 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1787
1788 // Skia uses the range [0..255] for the addition vector, but we need
1789 // the [0..1] range to apply the vector in GLSL
1790 float colorVector[4];
1791 colorVector[0] = srcColorMatrix[4] / 255.0f;
1792 colorVector[1] = srcColorMatrix[9] / 255.0f;
1793 colorVector[2] = srcColorMatrix[14] / 255.0f;
1794 colorVector[3] = srcColorMatrix[19] / 255.0f;
1795
1796 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1797 GL_FALSE, colorMatrix);
1798 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1799 return;
1800 }
1801
1802 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001803}
1804
Romain Guy41210632012-07-16 17:04:24 -07001805void OpenGLRenderer::setupDrawTextGammaUniforms() {
1806 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1807}
1808
Romain Guy70ca14e2010-12-13 18:24:33 -08001809void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001810 bool force = mCaches.bindMeshBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08001811 mCaches.bindPositionVertexPointer(force, nullptr);
Romain Guy15bc6432011-12-13 13:11:32 -08001812 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001813}
1814
1815void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001816 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001817 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001818 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001819}
1820
Romain Guyaa6c24c2011-04-28 18:40:04 -07001821void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1822 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001823 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001824 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001825}
1826
Romain Guy8f0095c2011-05-02 17:24:22 -07001827void OpenGLRenderer::setupDrawTextureTransform() {
1828 mDescription.hasTextureTransform = true;
1829}
1830
1831void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001832 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1833 GL_FALSE, &transform.data[0]);
1834}
1835
Chris Craik564acf72014-01-02 16:46:18 -08001836void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1837 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001838 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001839 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001840 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001841 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001842 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001843 }
Romain Guyd71dd362011-12-12 19:03:35 -08001844
Chris Craikcb4d6002012-09-25 12:00:29 -07001845 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001846 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001847 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001848 }
1849
1850 mCaches.unbindIndicesBuffer();
1851}
1852
Chris Craik564acf72014-01-02 16:46:18 -08001853void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1854 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001855 bool force = mCaches.unbindMeshBuffer();
1856 GLsizei stride = sizeof(ColorTextureVertex);
1857
1858 mCaches.bindPositionVertexPointer(force, vertices, stride);
1859 if (mCaches.currentProgram->texCoords >= 0) {
1860 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1861 }
1862 int slot = mCaches.currentProgram->getAttrib("colors");
1863 if (slot >= 0) {
1864 glEnableVertexAttribArray(slot);
1865 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1866 }
1867
1868 mCaches.unbindIndicesBuffer();
1869}
1870
Chris Craik564acf72014-01-02 16:46:18 -08001871void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1872 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001873 bool force = false;
1874 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1875 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1876 // use the default VBO found in Caches
1877 if (!vertices || vbo) {
1878 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1879 } else {
1880 force = mCaches.unbindMeshBuffer();
1881 }
ztenghui63d41ab2014-02-14 13:13:41 -08001882 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001883
Chris Craikcb4d6002012-09-25 12:00:29 -07001884 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001885 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001886 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001887 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001888}
1889
Romain Guy448455f2013-07-22 13:57:50 -07001890void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001891 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001892 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001893 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001894}
1895
Romain Guy70ca14e2010-12-13 18:24:33 -08001896///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001897// Drawing
1898///////////////////////////////////////////////////////////////////////////////
1899
Tom Hudson107843d2014-09-08 11:26:26 -04001900void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001901 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1902 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001903 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001904 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001905 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001906 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001907 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001908 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001909 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001910 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001911 }
1912
Chris Craikef8d6f22014-12-17 11:10:28 -08001913 // Don't avoid overdraw when visualizing, since that makes it harder to
1914 // debug where it's coming from, and when the problem occurs.
1915 bool avoidOverdraw = !mCaches.debugOverdraw;
Chris Craika5f09182014-12-17 15:11:30 -08001916 DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001917 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001918 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001919
1920 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001921 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001922
Tom Hudson107843d2014-09-08 11:26:26 -04001923 deferredList.flush(*this, dirty);
1924 } else {
1925 // Even if there is no drawing command(Ex: invisible),
1926 // it still needs startFrame to clear buffer and start tiling.
1927 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001928 }
1929}
1930
Chris Craike84a2082014-12-22 14:28:49 -08001931void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1932 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001933 float x = left;
1934 float y = top;
1935
Romain Guy886b2752013-01-04 12:26:18 -08001936 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1937
Romain Guya168d732011-03-18 16:50:13 -07001938 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001939 if (currentTransform()->isPureTranslate()) {
1940 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1941 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001942 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001943
1944 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001945 } else {
Chris Craik678625242014-02-28 12:26:34 -08001946 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001947 }
1948
Romain Guy3b748a42013-04-17 18:54:38 -07001949 // No need to check for a UV mapper on the texture object, only ARGB_8888
1950 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001951 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craike84a2082014-12-22 14:28:49 -08001952 paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001953 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001954}
1955
Romain Guy03c00b52013-06-20 18:30:28 -07001956/**
1957 * Important note: this method is intended to draw batches of bitmaps and
1958 * will not set the scissor enable or dirty the current layer, if any.
1959 * The caller is responsible for properly dirtying the current layer.
1960 */
Tom Hudson107843d2014-09-08 11:26:26 -04001961void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001962 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1963 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001964 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001965 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001966 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001967
Chris Craik527a3aa2013-03-04 10:19:31 -08001968 const AutoTexture autoCleanup(texture);
1969
Chris Craik527a3aa2013-03-04 10:19:31 -08001970 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001971 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001972
1973 const float x = (int) floorf(bounds.left + 0.5f);
1974 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001975 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001977 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001978 GL_TRIANGLES, bitmapCount * 6, true,
1979 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001980 } else {
1981 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001982 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001983 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1984 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001985 }
1986
Tom Hudson107843d2014-09-08 11:26:26 -04001987 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001988}
1989
Tom Hudson107843d2014-09-08 11:26:26 -04001990void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001991 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001992 return;
Romain Guy6926c722010-07-12 20:20:03 -07001993 }
1994
Romain Guya1d3c912011-12-13 14:55:06 -08001995 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001996 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001997 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001998 const AutoTexture autoCleanup(texture);
1999
Mike Reed1103b322014-07-08 12:36:44 -04002000 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002001 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002002 } else {
Chris Craik79647502014-08-06 13:42:24 -07002003 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002004 }
Chet Haase48659092012-05-31 15:21:51 -07002005
Tom Hudson107843d2014-09-08 11:26:26 -04002006 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002007}
2008
Tom Hudson107843d2014-09-08 11:26:26 -04002009void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002010 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002011 return;
Romain Guye651cc62012-05-14 19:44:40 -07002012 }
2013
2014 mCaches.activeTexture(0);
2015 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2016 const AutoTexture autoCleanup(texture);
2017
Mike Reed1103b322014-07-08 12:36:44 -04002018 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002019 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002020 } else {
Chris Craik79647502014-08-06 13:42:24 -07002021 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002022 }
Chet Haase48659092012-05-31 15:21:51 -07002023
Tom Hudson107843d2014-09-08 11:26:26 -04002024 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002025}
2026
Tom Hudson107843d2014-09-08 11:26:26 -04002027void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002028 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002029 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002030 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002031 }
2032
Chris Craik39a908c2013-06-13 14:39:01 -07002033 // TODO: use quickReject on bounds from vertices
2034 mCaches.enableScissor();
2035
Romain Guyb18d2d02011-02-10 15:52:54 -08002036 float left = FLT_MAX;
2037 float top = FLT_MAX;
2038 float right = FLT_MIN;
2039 float bottom = FLT_MIN;
2040
Romain Guya92bb4d2012-10-16 11:08:44 -07002041 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002042
Chris Craik51d6a3d2014-12-22 17:16:56 -08002043 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2044 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002045
Chris Craik51d6a3d2014-12-22 17:16:56 -08002046 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002047 if (!colors) {
2048 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002049 tempColors.reset(new int[colorsCount]);
2050 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2051 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002052 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002053
Romain Guy3b748a42013-04-17 18:54:38 -07002054 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002055 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002056 const UvMapper& mapper(getMapper(texture));
2057
Romain Guy5a7b4662011-01-20 19:09:30 -08002058 for (int32_t y = 0; y < meshHeight; y++) {
2059 for (int32_t x = 0; x < meshWidth; x++) {
2060 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2061
2062 float u1 = float(x) / meshWidth;
2063 float u2 = float(x + 1) / meshWidth;
2064 float v1 = float(y) / meshHeight;
2065 float v2 = float(y + 1) / meshHeight;
2066
Romain Guy3b748a42013-04-17 18:54:38 -07002067 mapper.map(u1, v1, u2, v2);
2068
Romain Guy5a7b4662011-01-20 19:09:30 -08002069 int ax = i + (meshWidth + 1) * 2;
2070 int ay = ax + 1;
2071 int bx = i;
2072 int by = bx + 1;
2073 int cx = i + 2;
2074 int cy = cx + 1;
2075 int dx = i + (meshWidth + 1) * 2 + 2;
2076 int dy = dx + 1;
2077
Romain Guyff316ec2013-02-13 18:39:43 -08002078 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2079 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2080 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002081
Romain Guyff316ec2013-02-13 18:39:43 -08002082 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2083 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2084 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002085
Romain Guya92bb4d2012-10-16 11:08:44 -07002086 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2087 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2088 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2089 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002090 }
2091 }
2092
Chris Craikf0a59072013-11-19 18:00:46 -08002093 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002094 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002095 }
2096
Romain Guyff316ec2013-02-13 18:39:43 -08002097 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002098 texture = mCaches.textureCache.get(bitmap);
2099 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002100 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002101 }
Romain Guyff316ec2013-02-13 18:39:43 -08002102 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002103 const AutoTexture autoCleanup(texture);
2104
2105 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002106 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002107
2108 int alpha;
2109 SkXfermode::Mode mode;
2110 getAlphaAndMode(paint, &alpha, &mode);
2111
Romain Guyff316ec2013-02-13 18:39:43 -08002112 float a = alpha / 255.0f;
2113
Romain Guya92bb4d2012-10-16 11:08:44 -07002114 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002115 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002116 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002117
Romain Guyff316ec2013-02-13 18:39:43 -08002118 setupDraw();
2119 setupDrawWithTextureAndColor();
2120 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002121 setupDrawColorFilter(getColorFilter(paint));
2122 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002123 setupDrawProgram();
2124 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002125 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002126 setupDrawTexture(texture->id);
2127 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002128 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002129 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002130
2131 glDrawArrays(GL_TRIANGLES, 0, count);
2132
Romain Guyff316ec2013-02-13 18:39:43 -08002133 int slot = mCaches.currentProgram->getAttrib("colors");
2134 if (slot >= 0) {
2135 glDisableVertexAttribArray(slot);
2136 }
2137
Tom Hudson107843d2014-09-08 11:26:26 -04002138 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002139}
2140
Tom Hudson107843d2014-09-08 11:26:26 -04002141void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002142 float srcLeft, float srcTop, float srcRight, float srcBottom,
2143 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002144 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002145 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002146 return;
Romain Guy6926c722010-07-12 20:20:03 -07002147 }
2148
Romain Guya1d3c912011-12-13 14:55:06 -08002149 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002150 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002151 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002152 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002153
Romain Guy8ba548f2010-06-30 19:21:21 -07002154 const float width = texture->width;
2155 const float height = texture->height;
2156
Romain Guy3b748a42013-04-17 18:54:38 -07002157 float u1 = fmax(0.0f, srcLeft / width);
2158 float v1 = fmax(0.0f, srcTop / height);
2159 float u2 = fmin(1.0f, srcRight / width);
2160 float v2 = fmin(1.0f, srcBottom / height);
2161
2162 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002163
Romain Guy03750a02010-10-18 14:06:08 -07002164 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002165 resetDrawTextureTexCoords(u1, v1, u2, v2);
2166
Romain Guyd21b6e12011-11-30 20:21:23 -08002167 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2168
Romain Guy886b2752013-01-04 12:26:18 -08002169 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2170 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002171
Romain Guy886b2752013-01-04 12:26:18 -08002172 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2173 // Apply a scale transform on the canvas only when a shader is in use
2174 // Skia handles the ratio between the dst and src rects as a scale factor
2175 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002176 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002177 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002178
Chris Craikd6b65f62014-01-01 14:45:21 -08002179 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2180 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2181 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002182
2183 dstRight = x + (dstRight - dstLeft);
2184 dstBottom = y + (dstBottom - dstTop);
2185
2186 dstLeft = x;
2187 dstTop = y;
2188
Chris Craik678625242014-02-28 12:26:34 -08002189 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002190 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002191 } else {
Chris Craik678625242014-02-28 12:26:34 -08002192 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002193 }
2194
2195 if (CC_UNLIKELY(useScaleTransform)) {
2196 save(SkCanvas::kMatrix_SaveFlag);
2197 translate(dstLeft, dstTop);
2198 scale(scaleX, scaleY);
2199
2200 dstLeft = 0.0f;
2201 dstTop = 0.0f;
2202
2203 dstRight = srcRight - srcLeft;
2204 dstBottom = srcBottom - srcTop;
2205 }
2206
Mike Reed1103b322014-07-08 12:36:44 -04002207 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002208 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002209 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002210 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002211 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2212 } else {
2213 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002214 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002215 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002216 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2217 }
2218
2219 if (CC_UNLIKELY(useScaleTransform)) {
2220 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002221 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002222
2223 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002224
Tom Hudson107843d2014-09-08 11:26:26 -04002225 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002226}
2227
Tom Hudson107843d2014-09-08 11:26:26 -04002228void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002229 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002230 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002231 return;
Romain Guy6926c722010-07-12 20:20:03 -07002232 }
2233
John Reckebd52612014-12-10 16:47:36 -08002234 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002235 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2236 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002237
Tom Hudson107843d2014-09-08 11:26:26 -04002238 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002239}
2240
Tom Hudson107843d2014-09-08 11:26:26 -04002241void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002242 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2243 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002244 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002245 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002246 }
2247
Romain Guy211370f2012-02-01 16:10:55 -08002248 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002249 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002250 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002251 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002252 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002253
Romain Guya4adcf02013-02-28 12:15:35 -08002254 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2255 texture->setFilter(GL_LINEAR, true);
2256
Chris Craikd6b65f62014-01-01 14:45:21 -08002257 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002258 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002259 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002260 const float offsetX = left + currentTransform()->getTranslateX();
2261 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002262 const size_t count = mesh->quads.size();
2263 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002264 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002265 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002266 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2267 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2268 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002269 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002270 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002271 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002272 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002273 }
2274 }
2275
Chris Craik4063a0e2013-11-15 16:06:56 -08002276 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002277 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002278 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2279 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002280
Romain Guy3b748a42013-04-17 18:54:38 -07002281 right = x + right - left;
2282 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002283 left = x;
2284 top = y;
2285 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002286 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002287 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2288 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002289 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2290 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002291 }
Chet Haase48659092012-05-31 15:21:51 -07002292
Tom Hudson107843d2014-09-08 11:26:26 -04002293 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002294}
2295
Romain Guy03c00b52013-06-20 18:30:28 -07002296/**
2297 * Important note: this method is intended to draw batches of 9-patch objects and
2298 * will not set the scissor enable or dirty the current layer, if any.
2299 * The caller is responsible for properly dirtying the current layer.
2300 */
Tom Hudson107843d2014-09-08 11:26:26 -04002301void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002302 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002303 mCaches.activeTexture(0);
2304 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002305 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002306 const AutoTexture autoCleanup(texture);
2307
2308 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2309 texture->setFilter(GL_LINEAR, true);
2310
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002311 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2312 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002313 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002314
Tom Hudson107843d2014-09-08 11:26:26 -04002315 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002316}
2317
Tom Hudson107843d2014-09-08 11:26:26 -04002318void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002319 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002320 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002321 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002322 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002323 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002324 }
2325
Chris Craik0d5ac952014-07-15 13:01:02 -07002326 Rect bounds(vertexBuffer.getBounds());
2327 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002328 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2329
Chris Craikcb4d6002012-09-25 12:00:29 -07002330 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002331 bool isAA = paint->isAntiAlias();
2332
Chris Craik710f46d2012-09-17 17:25:49 -07002333 setupDraw();
2334 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002335 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002336 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002337 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002338 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002339 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002340 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002341 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2342 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002343 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002344 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002345 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002346
ztenghui55bfb4e2013-12-03 10:38:55 -08002347 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002348 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002349 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002350 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002351
Chris Craik710f46d2012-09-17 17:25:49 -07002352 int alphaSlot = -1;
2353 if (isAA) {
2354 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2355 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002356 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002357 glEnableVertexAttribArray(alphaSlot);
2358 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002359 }
Romain Guy04299382012-07-18 17:15:41 -07002360
Chris Craik05f3d6e2014-06-02 16:27:04 -07002361 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2362 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002363 mCaches.unbindIndicesBuffer();
2364 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002365 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002366 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002367 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2368 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002369 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002370 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002371 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2372 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002373 } else if (mode == VertexBuffer::kIndices) {
2374 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002375 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2376 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002377 }
Romain Guy04299382012-07-18 17:15:41 -07002378
Chris Craik710f46d2012-09-17 17:25:49 -07002379 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002380 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002381 }
Chris Craik65cd6122012-12-10 17:56:27 -08002382
Tom Hudson107843d2014-09-08 11:26:26 -04002383 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002384}
2385
2386/**
Chris Craik65cd6122012-12-10 17:56:27 -08002387 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2388 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2389 * screen space in all directions. However, instead of using a fragment shader to compute the
2390 * translucency of the color from its position, we simply use a varying parameter to define how far
2391 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2392 *
2393 * Doesn't yet support joins, caps, or path effects.
2394 */
Tom Hudson107843d2014-09-08 11:26:26 -04002395void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002396 VertexBuffer vertexBuffer;
2397 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002398 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002399 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002400}
2401
2402/**
2403 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2404 * and additional geometry for defining an alpha slope perimeter.
2405 *
2406 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2407 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2408 * in-shader alpha region, but found it to be taxing on some GPUs.
2409 *
2410 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2411 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002412 */
Tom Hudson107843d2014-09-08 11:26:26 -04002413void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002414 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002415
Chris Craik65cd6122012-12-10 17:56:27 -08002416 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002417
Chris Craik65cd6122012-12-10 17:56:27 -08002418 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002419 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2420 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002421
Chris Craik05f3d6e2014-06-02 16:27:04 -07002422 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002423 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002424 }
2425
Chris Craikbf759452014-08-11 16:00:44 -07002426 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002427 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002428}
2429
Tom Hudson107843d2014-09-08 11:26:26 -04002430void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002431 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002432
Chris Craik6d29c8d2013-05-08 18:35:44 -07002433 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002434
Chris Craik6d29c8d2013-05-08 18:35:44 -07002435 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002436 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002437
Chris Craik05f3d6e2014-06-02 16:27:04 -07002438 const Rect& bounds = buffer.getBounds();
2439 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002440 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002441 }
2442
Chris Craikbf759452014-08-11 16:00:44 -07002443 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002444 drawVertexBuffer(buffer, paint, displayFlags);
2445
2446 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002447}
2448
Tom Hudson107843d2014-09-08 11:26:26 -04002449void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002450 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002451 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002452
Tom Hudson984162f2014-10-10 13:38:16 -04002453 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002454 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002455
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002456 SkPaint paint;
2457 paint.setColor(color);
2458 paint.setXfermodeMode(mode);
2459
2460 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002461
Tom Hudson107843d2014-09-08 11:26:26 -04002462 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002463}
Romain Guy9d5316e2010-06-24 19:30:36 -07002464
Tom Hudson107843d2014-09-08 11:26:26 -04002465void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002466 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002467 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002468 const AutoTexture autoCleanup(texture);
2469
2470 const float x = left + texture->left - texture->offset;
2471 const float y = top + texture->top - texture->offset;
2472
2473 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002474
Tom Hudson107843d2014-09-08 11:26:26 -04002475 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002476}
2477
Tom Hudson107843d2014-09-08 11:26:26 -04002478void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002479 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002480 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002481 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002482 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002483 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002484 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002485
Chris Craike84a2082014-12-22 14:28:49 -08002486 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002487 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002488 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002489 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002490 drawShape(left, top, texture, p);
2491 } else {
2492 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2493 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2494 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002495 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002496}
2497
Tom Hudson107843d2014-09-08 11:26:26 -04002498void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002499 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002500 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002501 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002502 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002503 }
Chris Craike84a2082014-12-22 14:28:49 -08002504 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002505 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002506 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002507 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002508 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002509 SkPath path;
2510 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2511 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2512 } else {
2513 path.addCircle(x, y, radius);
2514 }
2515 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002517}
Romain Guy01d58e42011-01-19 21:54:02 -08002518
Tom Hudson107843d2014-09-08 11:26:26 -04002519void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002520 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002521 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002522 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002523 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002524 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002525 }
Romain Guy01d58e42011-01-19 21:54:02 -08002526
Chris Craike84a2082014-12-22 14:28:49 -08002527 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002528 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002529 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002530 drawShape(left, top, texture, p);
2531 } else {
2532 SkPath path;
2533 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2534 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2535 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2536 }
2537 path.addOval(rect);
2538 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002539 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002540}
2541
Tom Hudson107843d2014-09-08 11:26:26 -04002542void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002543 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002544 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002545 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002546 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002547 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002548 }
2549
Chris Craik780c1282012-10-04 14:10:49 -07002550 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002551 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002552 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002553 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002554 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002555 drawShape(left, top, texture, p);
2556 return;
Chris Craik780c1282012-10-04 14:10:49 -07002557 }
Chris Craik780c1282012-10-04 14:10:49 -07002558 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2559 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2560 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2561 }
2562
2563 SkPath path;
2564 if (useCenter) {
2565 path.moveTo(rect.centerX(), rect.centerY());
2566 }
2567 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2568 if (useCenter) {
2569 path.close();
2570 }
Tom Hudson107843d2014-09-08 11:26:26 -04002571 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002572}
2573
Romain Guycf8675e2012-10-02 12:32:25 -07002574// See SkPaintDefaults.h
2575#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2576
Tom Hudson107843d2014-09-08 11:26:26 -04002577void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002578 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002579 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002580 || quickRejectSetupScissor(left, top, right, bottom, p)
Tom Hudson8dfaa492014-12-09 15:03:44 -05002581 || PaintUtils::paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002582 return;
Romain Guy6926c722010-07-12 20:20:03 -07002583 }
2584
Chris Craik710f46d2012-09-17 17:25:49 -07002585 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002586 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002587 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002588 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2589 mCaches.activeTexture(0);
2590 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002591 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002592 drawShape(left, top, texture, p);
2593 } else {
2594 SkPath path;
2595 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2596 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2597 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2598 }
2599 path.addRect(rect);
2600 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002601 }
Chet Haase858aa932011-05-12 09:06:00 -07002602 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002603 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2604 SkPath path;
2605 path.addRect(left, top, right, bottom);
2606 drawConvexPath(path, p);
2607 } else {
2608 drawColorRect(left, top, right, bottom, p);
2609
2610 mDirty = true;
2611 }
Chet Haase858aa932011-05-12 09:06:00 -07002612 }
Romain Guyc7d53492010-06-25 13:41:57 -07002613}
Romain Guy9d5316e2010-06-24 19:30:36 -07002614
Chris Craikd218a922014-01-02 17:13:34 -08002615void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2616 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002617 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002618 mCaches.activeTexture(0);
2619
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002620 TextShadow textShadow;
2621 if (!getTextShadow(paint, &textShadow)) {
2622 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2623 }
2624
Raph Levien416a8472012-07-19 22:48:17 -07002625 // NOTE: The drop shadow will not perform gamma correction
2626 // if shader-based correction is enabled
2627 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2628 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002629 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002630 // If the drop shadow exceeds the max texture size or couldn't be
2631 // allocated, skip drawing
2632 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002633 const AutoTexture autoCleanup(shadow);
2634
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002635 const float sx = x - shadow->left + textShadow.dx;
2636 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002637
Tom Hudson984162f2014-10-10 13:38:16 -04002638 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002639 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002640 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002641 }
2642
2643 setupDraw();
2644 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002645 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002646 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002647 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002648 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002649 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002650 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2651 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002652 setupDrawTexture(shadow->id);
2653 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002654 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002655 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08002656 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002657
2658 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2659}
2660
Romain Guy768bffc2013-02-27 13:50:45 -08002661bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002662 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Tom Hudson8dfaa492014-12-09 15:03:44 -05002663 return MathUtils::isZero(alpha)
2664 && PaintUtils::getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
Romain Guy768bffc2013-02-27 13:50:45 -08002665}
2666
Tom Hudson107843d2014-09-08 11:26:26 -04002667void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002668 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002669 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002670 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002671 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002672
Romain Guy671d6cf2012-01-18 12:39:17 -08002673 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002674 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002675 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002676 }
2677
Chris Craik39a908c2013-06-13 14:39:01 -07002678 mCaches.enableScissor();
2679
Romain Guy671d6cf2012-01-18 12:39:17 -08002680 float x = 0.0f;
2681 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002682 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002683 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002684 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2685 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002686 }
2687
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002688 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002689 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002690
2691 int alpha;
2692 SkXfermode::Mode mode;
2693 getAlphaAndMode(paint, &alpha, &mode);
2694
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002695 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002696 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002697 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002698 }
2699
Romain Guy671d6cf2012-01-18 12:39:17 -08002700 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002701 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002702 if (pureTranslate && !linearFilter) {
2703 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2704 }
Romain Guy257ae352013-03-20 16:31:12 -07002705 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002706
Tom Hudson984162f2014-10-10 13:38:16 -04002707 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002708 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2709
Romain Guy211370f2012-02-01 16:10:55 -08002710 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002711
Victoria Lease1e546812013-06-25 14:25:17 -07002712 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002714 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002715 if (hasActiveLayer) {
2716 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002717 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002718 }
2719 dirtyLayerUnchecked(bounds, getRegion());
2720 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002721 }
Chet Haase48659092012-05-31 15:21:51 -07002722
Tom Hudson107843d2014-09-08 11:26:26 -04002723 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002724}
2725
Chris Craik59744b72014-07-01 17:56:52 -07002726bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002727 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002728 outMatrix->setIdentity();
2729 return false;
2730 } else if (CC_UNLIKELY(transform.isPerspective())) {
2731 outMatrix->setIdentity();
2732 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002733 }
Chris Craik59744b72014-07-01 17:56:52 -07002734
2735 /**
Chris Craika736cd92014-08-04 13:18:38 -07002736 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2737 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002738 */
2739 float sx, sy;
2740 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002741 outMatrix->setScale(
2742 roundf(fmaxf(1.0f, sx)),
2743 roundf(fmaxf(1.0f, sy)));
2744 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002745}
2746
Tom Hudson984162f2014-10-10 13:38:16 -04002747int OpenGLRenderer::getSaveCount() const {
2748 return mState.getSaveCount();
2749}
2750
2751int OpenGLRenderer::save(int flags) {
2752 return mState.save(flags);
2753}
2754
2755void OpenGLRenderer::restore() {
2756 return mState.restore();
2757}
2758
2759void OpenGLRenderer::restoreToCount(int saveCount) {
2760 return mState.restoreToCount(saveCount);
2761}
2762
2763void OpenGLRenderer::translate(float dx, float dy, float dz) {
2764 return mState.translate(dx, dy, dz);
2765}
2766
2767void OpenGLRenderer::rotate(float degrees) {
2768 return mState.rotate(degrees);
2769}
2770
2771void OpenGLRenderer::scale(float sx, float sy) {
2772 return mState.scale(sx, sy);
2773}
2774
2775void OpenGLRenderer::skew(float sx, float sy) {
2776 return mState.skew(sx, sy);
2777}
2778
2779void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2780 mState.setMatrix(matrix);
2781}
2782
2783void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2784 mState.concatMatrix(matrix);
2785}
2786
2787bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2788 return mState.clipRect(left, top, right, bottom, op);
2789}
2790
2791bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2792 return mState.clipPath(path, op);
2793}
2794
2795bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2796 return mState.clipRegion(region, op);
2797}
2798
2799void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2800 mState.setClippingOutline(allocator, outline);
2801}
2802
2803void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2804 const Rect& rect, float radius, bool highPriority) {
2805 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2806}
2807
2808
2809
Tom Hudson107843d2014-09-08 11:26:26 -04002810void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002811 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002812 DrawOpMode drawOpMode) {
2813
Chris Craik527a3aa2013-03-04 10:19:31 -08002814 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002815 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2816 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002817 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002818 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002819 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002820 }
Romain Guycac5fd32011-12-01 20:08:50 -08002821 }
2822
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002823 const float oldX = x;
2824 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002825
Chris Craikd6b65f62014-01-01 14:45:21 -08002826 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002827 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002828
Romain Guy211370f2012-02-01 16:10:55 -08002829 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002830 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2831 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002832 }
2833
Romain Guy86568192010-12-14 15:55:39 -08002834 int alpha;
2835 SkXfermode::Mode mode;
2836 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002837
Romain Guyc74f45a2013-02-26 19:10:14 -08002838 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2839
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002840 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002841 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002842 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002843 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002844 }
2845
Romain Guy19d4dd82013-03-04 11:14:26 -08002846 const bool hasActiveLayer = hasLayer();
2847
Romain Guy624234f2013-03-05 16:43:31 -08002848 // We only pass a partial transform to the font renderer. That partial
2849 // matrix defines how glyphs are rasterized. Typically we want glyphs
2850 // to be rasterized at their final size on screen, which means the partial
2851 // matrix needs to take the scale factor into account.
2852 // When a partial matrix is used to transform glyphs during rasterization,
2853 // the mesh is generated with the inverse transform (in the case of scale,
2854 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2855 // apply the full transform matrix at draw time in the vertex shader.
2856 // Applying the full matrix in the shader is the easiest way to handle
2857 // rotation and perspective and allows us to always generated quads in the
2858 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002859 SkMatrix fontTransform;
2860 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2861 || fabs(y - (int) y) > 0.0f
2862 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002863 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002864 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002865
Romain Guy624234f2013-03-05 16:43:31 -08002866 // TODO: Implement better clipping for scaled/rotated text
Chris Craike84a2082014-12-22 14:28:49 -08002867 const Rect* clip = !pureTranslate ? nullptr : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002868 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 -07002869
Raph Levien996e57c2012-07-23 15:22:52 -07002870 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002871 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002872
2873 // don't call issuedrawcommand, do it at end of batch
2874 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002875 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002876 SkPaint paintCopy(*paint);
2877 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2878 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002879 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002880 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002881 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002882 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002883 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002884
Chris Craik527a3aa2013-03-04 10:19:31 -08002885 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002886 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002887 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002888 }
Chris Craik41541822013-05-03 16:35:54 -07002889 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002890 }
Romain Guy694b5192010-07-21 21:33:20 -07002891
Chris Craike63f7c622013-10-17 10:30:55 -07002892 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002893
Tom Hudson107843d2014-09-08 11:26:26 -04002894 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002895}
2896
Tom Hudson107843d2014-09-08 11:26:26 -04002897void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002898 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002899 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002900 return;
Romain Guy03d58522012-02-24 17:54:07 -08002901 }
2902
Chris Craik39a908c2013-06-13 14:39:01 -07002903 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2904 mCaches.enableScissor();
2905
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002906 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002907 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002908 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002909
2910 int alpha;
2911 SkXfermode::Mode mode;
2912 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002913 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002914
Tom Hudson984162f2014-10-10 13:38:16 -04002915 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002916 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 -08002917
Romain Guy97771732012-02-28 18:17:02 -08002918 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002919
2920 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002921 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002922 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002923 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002924 dirtyLayerUnchecked(bounds, getRegion());
2925 }
Romain Guy97771732012-02-28 18:17:02 -08002926 }
Chet Haase48659092012-05-31 15:21:51 -07002927
Tom Hudson107843d2014-09-08 11:26:26 -04002928 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002929}
2930
Tom Hudson107843d2014-09-08 11:26:26 -04002931void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002932 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002933
Romain Guya1d3c912011-12-13 14:55:06 -08002934 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002935
Romain Guyfb8b7632010-08-23 21:05:08 -07002936 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002937 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002938 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002939
Romain Guy8b55f372010-08-18 17:10:07 -07002940 const float x = texture->left - texture->offset;
2941 const float y = texture->top - texture->offset;
2942
Romain Guy01d58e42011-01-19 21:54:02 -08002943 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002944 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002945}
2946
Tom Hudson107843d2014-09-08 11:26:26 -04002947void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002948 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002949 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002950 }
2951
Chris Craike84a2082014-12-22 14:28:49 -08002952 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002953 if (layer->isTextureLayer()) {
2954 transform = &layer->getTransform();
2955 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002956 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002957 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002958 }
2959 }
2960
Chris Craik39a908c2013-06-13 14:39:01 -07002961 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002962 const bool rejected = mState.calculateQuickRejectForScissor(
2963 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2964 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002965
2966 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002967 if (transform && !transform->isIdentity()) {
2968 restore();
2969 }
Tom Hudson107843d2014-09-08 11:26:26 -04002970 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002971 }
2972
Chris Craik62d307c2014-07-29 10:35:13 -07002973 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2974 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2975
Romain Guy5bb3c732012-11-29 17:52:58 -08002976 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002977
Chris Craik39a908c2013-06-13 14:39:01 -07002978 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002979 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002980
Romain Guy211370f2012-02-01 16:10:55 -08002981 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002982 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002983 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2984 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002985 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002986
Chris Craik16ecda52013-03-29 10:59:59 -07002987 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002988 setupDraw();
2989 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002990 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002991 setupDrawColorFilter(layer->getColorFilter());
2992 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002993 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002994 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002995 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002996 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002997 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2998 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2999 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003000
Romain Guyd21b6e12011-11-30 20:21:23 -08003001 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003002 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003003 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003004 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003005 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003006 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003007 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3008 }
Romain Guyf219da52011-01-16 12:54:25 -08003009
Romain Guy448455f2013-07-22 13:57:50 -07003010 TextureVertex* mesh = &layer->mesh[0];
3011 GLsizei elementsCount = layer->meshElementCount;
3012
3013 while (elementsCount > 0) {
3014 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3015
Romain Guy3380cfd2013-08-15 16:57:57 -07003016 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003017 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003018 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003019
3020 elementsCount -= drawCount;
3021 // Though there are 4 vertices in a quad, we use 6 indices per
3022 // quad to draw with GL_TRIANGLES
3023 mesh += (drawCount / 6) * 4;
3024 }
Romain Guyf219da52011-01-16 12:54:25 -08003025
Romain Guy3a3133d2011-02-01 22:59:58 -08003026#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003027 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003028#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003029 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003030
Romain Guy5bb3c732012-11-29 17:52:58 -08003031 if (layer->debugDrawUpdate) {
3032 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003033
3034 SkPaint paint;
3035 paint.setColor(0x7f00ff00);
3036 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003037 }
Romain Guyf219da52011-01-16 12:54:25 -08003038 }
Chris Craik34416ea2013-04-15 16:08:28 -07003039 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003040
Romain Guyb2e2f242012-10-17 18:18:35 -07003041 if (transform && !transform->isIdentity()) {
3042 restore();
3043 }
3044
Tom Hudson107843d2014-09-08 11:26:26 -04003045 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003046}
3047
Romain Guy6926c722010-07-12 20:20:03 -07003048///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003049// Draw filters
3050///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003051void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3052 // We should never get here since we apply the draw filter when stashing
3053 // the paints in the DisplayList.
3054 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003055}
3056
3057///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003058// Drawing implementation
3059///////////////////////////////////////////////////////////////////////////////
3060
Chris Craikd218a922014-01-02 17:13:34 -08003061Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003062 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003063 if (!texture) {
3064 return mCaches.textureCache.get(bitmap);
3065 }
3066 return texture;
3067}
3068
Romain Guy01d58e42011-01-19 21:54:02 -08003069void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003070 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003071 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003072 return;
3073 }
3074
3075 int alpha;
3076 SkXfermode::Mode mode;
3077 getAlphaAndMode(paint, &alpha, &mode);
3078
3079 setupDraw();
3080 setupDrawWithTexture(true);
3081 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003082 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003083 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003084 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003085 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003086 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3087 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003088 setupDrawTexture(texture->id);
3089 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003090 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003091 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003092 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003093
3094 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003095}
3096
Romain Guyf607bdc2010-09-10 19:20:06 -07003097// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003098#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3099#define kStdUnderline_Offset (1.0f / 9.0f)
3100#define kStdUnderline_Thickness (1.0f / 18.0f)
3101
Chris Craikd218a922014-01-02 17:13:34 -08003102void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3103 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003104 // Handle underline and strike-through
3105 uint32_t flags = paint->getFlags();
3106 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003107 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003108
Romain Guy211370f2012-02-01 16:10:55 -08003109 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003110 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003111 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003112
Raph Levien8b4072d2012-07-30 15:50:00 -07003113 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003114 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003115
Romain Guyf6834472011-01-23 13:32:12 -08003116 int linesCount = 0;
3117 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3118 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3119
3120 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003121 float points[pointsCount];
3122 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003123
3124 if (flags & SkPaint::kUnderlineText_Flag) {
3125 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003126 points[currentPoint++] = left;
3127 points[currentPoint++] = top;
3128 points[currentPoint++] = left + underlineWidth;
3129 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003130 }
3131
3132 if (flags & SkPaint::kStrikeThruText_Flag) {
3133 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003134 points[currentPoint++] = left;
3135 points[currentPoint++] = top;
3136 points[currentPoint++] = left + underlineWidth;
3137 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003138 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003139
Romain Guy726aeba2011-06-01 14:52:00 -07003140 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003141
Romain Guy726aeba2011-06-01 14:52:00 -07003142 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003143 }
3144 }
3145}
3146
Tom Hudson107843d2014-09-08 11:26:26 -04003147void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003148 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003149 return;
Romain Guy672433d2013-01-04 19:05:13 -08003150 }
3151
Tom Hudson107843d2014-09-08 11:26:26 -04003152 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003153}
3154
Tom Hudson107843d2014-09-08 11:26:26 -04003155void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003156 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003157 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003158
Chris Craik15a07a22014-01-26 13:43:53 -08003159 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003160 mCaches.enableScissor();
3161
3162 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003163 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003164
ztenghui14a4e352014-08-13 10:44:39 -07003165 // The caller has made sure casterAlpha > 0.
3166 float ambientShadowAlpha = mAmbientShadowAlpha;
3167 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3168 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3169 }
3170 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3171 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003172 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003173 }
ztenghui7b4516e2014-01-07 10:42:55 -08003174
ztenghui14a4e352014-08-13 10:44:39 -07003175 float spotShadowAlpha = mSpotShadowAlpha;
3176 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3177 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3178 }
3179 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3180 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003181 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003182 }
ztenghui7b4516e2014-01-07 10:42:55 -08003183
Tom Hudson107843d2014-09-08 11:26:26 -04003184 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003185}
3186
Tom Hudson107843d2014-09-08 11:26:26 -04003187void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003188 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003189 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003190 return;
Romain Guy3b753822013-03-05 10:27:35 -08003191 }
Romain Guy735738c2012-12-03 12:34:51 -08003192
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003193 int color = paint->getColor();
3194 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003195 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003196 color |= 0x00ffffff;
3197 }
3198
Romain Guy672433d2013-01-04 19:05:13 -08003199 float left = FLT_MAX;
3200 float top = FLT_MAX;
3201 float right = FLT_MIN;
3202 float bottom = FLT_MIN;
3203
Romain Guy448455f2013-07-22 13:57:50 -07003204 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003205 Vertex* vertex = mesh;
3206
Chris Craik2af46352012-11-26 18:30:17 -08003207 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003208 float l = rects[index + 0];
3209 float t = rects[index + 1];
3210 float r = rects[index + 2];
3211 float b = rects[index + 3];
3212
Romain Guy3b753822013-03-05 10:27:35 -08003213 Vertex::set(vertex++, l, t);
3214 Vertex::set(vertex++, r, t);
3215 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003216 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003217
Romain Guy3b753822013-03-05 10:27:35 -08003218 left = fminf(left, l);
3219 top = fminf(top, t);
3220 right = fmaxf(right, r);
3221 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003222 }
3223
Chris Craikf0a59072013-11-19 18:00:46 -08003224 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003225 return;
Romain Guya362c692013-02-04 13:50:16 -08003226 }
Romain Guy672433d2013-01-04 19:05:13 -08003227
Romain Guy672433d2013-01-04 19:05:13 -08003228 setupDraw();
3229 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003230 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003231 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003232 setupDrawColorFilter(getColorFilter(paint));
3233 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003234 setupDrawProgram();
3235 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003236 setupDrawModelView(kModelViewMode_Translate, false,
3237 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003238 setupDrawColorUniforms(getShader(paint));
3239 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003240 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003241
Romain Guy8ce00302013-01-15 18:51:42 -08003242 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003243 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003244 }
3245
Chris Craik4063a0e2013-11-15 16:06:56 -08003246 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003247
Tom Hudson107843d2014-09-08 11:26:26 -04003248 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003249}
3250
Romain Guy026c5e162010-06-28 17:12:22 -07003251void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003252 const SkPaint* paint, bool ignoreTransform) {
3253 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003254 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003255 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003256 color |= 0x00ffffff;
3257 }
3258
Romain Guy70ca14e2010-12-13 18:24:33 -08003259 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003260 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003261 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003262 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003263 setupDrawColorFilter(getColorFilter(paint));
3264 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003265 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003266 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3267 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003268 setupDrawColorUniforms(getShader(paint));
3269 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003270 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003271 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003272
Romain Guyc95c8d62010-09-17 15:31:32 -07003273 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3274}
3275
Romain Guy82ba8142010-07-09 13:25:56 -07003276void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003277 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003278 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003279
Chris Craike84a2082014-12-22 14:28:49 -08003280 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003281 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3282
3283 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003284 vertices = &mMeshVertices[0].x;
3285 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003286
3287 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3288 texture->uvMapper->map(uvs);
3289
3290 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3291 }
3292
Chris Craikd6b65f62014-01-01 14:45:21 -08003293 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3294 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3295 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003296
Romain Guyd21b6e12011-11-30 20:21:23 -08003297 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003298 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003299 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003300 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003301 } else {
Chris Craik678625242014-02-28 12:26:34 -08003302 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003303 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003304 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3305 }
3306
3307 if (texture->uvMapper) {
3308 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003309 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003310}
3311
Romain Guyf7f93552010-07-08 19:17:03 -07003312void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003313 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003314 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003315 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3316 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003317
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003318 int a;
3319 SkXfermode::Mode mode;
3320 getAlphaAndMode(paint, &a, &mode);
3321 const float alpha = a / 255.0f;
3322
Romain Guy746b7402010-10-26 16:27:31 -07003323 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003324 setupDrawWithTexture();
3325 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003326 setupDrawColorFilter(getColorFilter(paint));
3327 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003328 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003329 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003330 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003331 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003332 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003333 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003334 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003335
Romain Guy6820ac82010-09-15 18:11:50 -07003336 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003337}
3338
Romain Guy3b748a42013-04-17 18:54:38 -07003339void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003340 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003341 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003342 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3343 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003344
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003345 int a;
3346 SkXfermode::Mode mode;
3347 getAlphaAndMode(paint, &a, &mode);
3348 const float alpha = a / 255.0f;
3349
Romain Guy3b748a42013-04-17 18:54:38 -07003350 setupDraw();
3351 setupDrawWithTexture();
3352 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003353 setupDrawColorFilter(getColorFilter(paint));
3354 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003355 setupDrawProgram();
3356 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003357 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003358 setupDrawTexture(texture);
3359 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003360 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003361 setupDrawMeshIndices(vertices, texCoords, vbo);
3362
Chris Craike84a2082014-12-22 14:28:49 -08003363 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003364}
3365
Romain Guy886b2752013-01-04 12:26:18 -08003366void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003367 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003368 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003369 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003370
Chris Craike84a2082014-12-22 14:28:49 -08003371 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 int alpha;
3373 SkXfermode::Mode mode;
3374 getAlphaAndMode(paint, &alpha, &mode);
3375
Romain Guy886b2752013-01-04 12:26:18 -08003376 setupDraw();
3377 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003378 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003379 setupDrawAlpha8Color(color, alpha);
3380 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003381 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003382 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003383 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003384 setupDrawProgram();
3385 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003386 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003387 setupDrawTexture(texture);
3388 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003389 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003390 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003391 setupDrawMesh(vertices, texCoords);
3392
3393 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003394}
3395
Romain Guya5aed0d2010-09-09 14:42:43 -07003396void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003397 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003398
Chris Craike84a2082014-12-22 14:28:49 -08003399 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003400 blend = true;
3401 mDescription.hasRoundRectClip = true;
3402 }
3403 mSkipOutlineClip = true;
3404
Romain Guy82ba8142010-07-09 13:25:56 -07003405 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003406
Romain Guy82ba8142010-07-09 13:25:56 -07003407 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003408 // These blend modes are not supported by OpenGL directly and have
3409 // to be implemented using shaders. Since the shader will perform
3410 // the blending, turn blending off here
3411 // If the blend mode cannot be implemented using shaders, fall
3412 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003413 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003414 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003415 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003416 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003417
Romain Guy82bc7a72012-01-03 14:13:39 -08003418 if (mCaches.blend) {
3419 glDisable(GL_BLEND);
3420 mCaches.blend = false;
3421 }
3422
3423 return;
3424 } else {
3425 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003426 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003427 }
3428
3429 if (!mCaches.blend) {
3430 glEnable(GL_BLEND);
3431 }
3432
3433 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3434 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3435
3436 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3437 glBlendFunc(sourceMode, destMode);
3438 mCaches.lastSrcMode = sourceMode;
3439 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003440 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003441 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003442 glDisable(GL_BLEND);
3443 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003444 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003445}
3446
Romain Guy889f8d12010-07-29 14:37:42 -07003447bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003448 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003449 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003450 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003451 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003452 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003453 }
Romain Guy6926c722010-07-12 20:20:03 -07003454 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003455}
3456
Romain Guy026c5e162010-06-28 17:12:22 -07003457void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003458 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003459 TextureVertex::setUV(v++, u1, v1);
3460 TextureVertex::setUV(v++, u2, v1);
3461 TextureVertex::setUV(v++, u1, v2);
3462 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003463}
3464
Chris Craike84a2082014-12-22 14:28:49 -08003465void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3466 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003467 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003468 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3469 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003470 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003471 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003472 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003473}
3474
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003475float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003476 float alpha;
3477 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3478 alpha = mDrawModifiers.mOverrideLayerAlpha;
3479 } else {
3480 alpha = layer->getAlpha() / 255.0f;
3481 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003482 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003483}
3484
Romain Guy9d5316e2010-06-24 19:30:36 -07003485}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003486}; // namespace android