blob: fb3d462e8f59696bf51dd426380db64bd859a1ec [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"
Chris Craik06e7fe52014-11-20 17:27:36 -080050#include "utils/TraceUtils.h"
Romain Guye4d01122010-06-16 18:44:05 -070051
Chris Craik62d307c2014-07-29 10:35:13 -070052#if DEBUG_DETAILED_EVENTS
53 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
54#else
55 #define EVENT_LOGD(...)
56#endif
57
Romain Guye4d01122010-06-16 18:44:05 -070058namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070059namespace uirenderer {
60
Chris Craik678625242014-02-28 12:26:34 -080061static GLenum getFilter(const SkPaint* paint) {
62 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
63 return GL_LINEAR;
64 }
65 return GL_NEAREST;
66}
Romain Guyd21b6e12011-11-30 20:21:23 -080067
Romain Guy9d5316e2010-06-24 19:30:36 -070068///////////////////////////////////////////////////////////////////////////////
69// Globals
70///////////////////////////////////////////////////////////////////////////////
71
Romain Guy889f8d12010-07-29 14:37:42 -070072/**
73 * Structure mapping Skia xfermodes to OpenGL blending factors.
74 */
75struct Blender {
76 SkXfermode::Mode mode;
77 GLenum src;
78 GLenum dst;
79}; // struct Blender
80
Romain Guy026c5e162010-06-28 17:12:22 -070081// In this array, the index of each Blender equals the value of the first
82// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
83static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070084 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
86 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
87 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
89 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
90 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
91 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
93 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
94 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
95 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050097 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070098 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070099};
Romain Guye4d01122010-06-16 18:44:05 -0700100
Romain Guy87a76572010-09-13 18:11:21 -0700101// This array contains the swapped version of each SkXfermode. For instance
102// this array's SrcOver blending mode is actually DstOver. You can refer to
103// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700104static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700105 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
106 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
107 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
108 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
109 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
110 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
111 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
112 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
113 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
114 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
115 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
116 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
117 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500118 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700119 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700120};
121
Romain Guyf6a11b82010-06-23 17:47:49 -0700122///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700123// Functions
124///////////////////////////////////////////////////////////////////////////////
125
126template<typename T>
127static inline T min(T a, T b) {
128 return a < b ? a : b;
129}
130
131///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700132// Constructors/destructor
133///////////////////////////////////////////////////////////////////////////////
134
John Reck3b202512014-06-23 13:13:08 -0700135OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400136 : mState(*this)
137 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700138 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700139 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700140 , mRenderState(renderState)
141 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700142 , mSuppressTiling(false)
143 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400144 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700145 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700146 , mLightRadius(FLT_MIN)
147 , mAmbientShadowAlpha(0)
148 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800149 // *set* draw modifiers to be 0
150 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700151 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700152
Romain Guyac670c02010-07-27 17:39:27 -0700153 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700154}
155
Romain Guy85bf02f2010-06-22 13:11:24 -0700156OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700157 // The context has already been destroyed at this point, do not call
158 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700159}
160
Romain Guy87e2f7572012-09-24 11:37:12 -0700161void OpenGLRenderer::initProperties() {
162 char property[PROPERTY_VALUE_MAX];
163 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
164 mScissorOptimizationDisabled = !strcasecmp(property, "true");
165 INIT_LOGD(" Scissor optimization %s",
166 mScissorOptimizationDisabled ? "disabled" : "enabled");
167 } else {
168 INIT_LOGD(" Scissor optimization enabled");
169 }
Romain Guy13631f32012-01-30 17:41:55 -0800170}
171
Chris Craik058fc642014-07-23 18:19:28 -0700172void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
173 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
174 mLightCenter = lightCenter;
175 mLightRadius = lightRadius;
176 mAmbientShadowAlpha = ambientShadowAlpha;
177 mSpotShadowAlpha = spotShadowAlpha;
178}
179
Romain Guy13631f32012-01-30 17:41:55 -0800180///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700181// Setup
182///////////////////////////////////////////////////////////////////////////////
183
Chris Craik797b95b2014-05-20 18:10:25 -0700184void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700185 glDisable(GL_DITHER);
186 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
187
188 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700189 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400195 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700197 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700198}
199
Tom Hudson107843d2014-09-08 11:26:26 -0400200void OpenGLRenderer::startFrame() {
201 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700202 mFrameStarted = true;
203
Tom Hudson984162f2014-10-10 13:38:16 -0400204 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700205
Romain Guy96885eb2013-03-26 15:05:58 -0700206 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700207
Tom Hudson984162f2014-10-10 13:38:16 -0400208 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800209
Romain Guy54c1a642012-09-27 17:55:46 -0700210 // Functors break the tiling extension in pretty spectacular ways
211 // This ensures we don't use tiling when a functor is going to be
212 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700213 mSuppressTiling = mCaches.hasRegisteredFunctors()
214 || mFirstFrameAfterResize;
215 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700216
Chris Craikd6b65f62014-01-01 14:45:21 -0800217 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700218
Romain Guy7c450aa2012-09-21 19:15:00 -0700219 debugOverdraw(true, true);
220
Tom Hudson107843d2014-09-08 11:26:26 -0400221 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700222 mTilingClip.right, mTilingClip.bottom, mOpaque);
223}
224
Tom Hudson107843d2014-09-08 11:26:26 -0400225void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700226 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700227
Romain Guy96885eb2013-03-26 15:05:58 -0700228 setupFrameState(left, top, right, bottom, opaque);
229
230 // Layer renderers will start the frame immediately
231 // The framebuffer renderer will first defer the display list
232 // for each layer and wait until the first drawing command
233 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800234 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700235 syncState();
236 updateLayers();
237 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400238 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700239 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700240}
241
Romain Guydcfc8362013-01-03 13:08:57 -0800242void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
243 // If we know that we are going to redraw the entire framebuffer,
244 // perform a discard to let the driver know we don't need to preserve
245 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800246 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400247 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
248 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800249 const GLenum attachments[] = {
250 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
251 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800252 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
253 }
254}
255
Tom Hudson107843d2014-09-08 11:26:26 -0400256void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700257 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700258 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700259 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700260 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400261 mDirty = true;
262 return;
Romain Guyddf74372012-05-22 14:07:07 -0700263 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700264
Romain Guy7c25aab2012-10-18 15:05:02 -0700265 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700266}
267
268void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700269 if (mCaches.blend) {
270 glEnable(GL_BLEND);
271 } else {
272 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
henry.uh_chen33f5a592014-07-02 19:36:56 +0800276void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700277 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800278 const Snapshot* snapshot = currentSnapshot();
279
Chris Craik14e51302013-12-30 15:32:54 -0800280 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800281 if (snapshot->flags & Snapshot::kFlagFboTarget) {
282 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700283 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700284
henry.uh_chen33f5a592014-07-02 19:36:56 +0800285 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800286 }
287}
288
henry.uh_chen33f5a592014-07-02 19:36:56 +0800289void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800290 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800291 if(expand) {
292 // Expand the startTiling region by 1
293 int leftNotZero = (clip.left > 0) ? 1 : 0;
294 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
295
296 mCaches.startTiling(
297 clip.left - leftNotZero,
298 windowHeight - clip.bottom - topNotZero,
299 clip.right - clip.left + leftNotZero + 1,
300 clip.bottom - clip.top + topNotZero + 1,
301 opaque);
302 } else {
303 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800304 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800305 }
Romain Guy54c1a642012-09-27 17:55:46 -0700306 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700307}
308
309void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700310 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700311}
312
Tom Hudson107843d2014-09-08 11:26:26 -0400313bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700314 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700315 endTiling();
Chris Craik4ac36f82014-12-09 16:54:03 -0800316 mTempPaths.clear();
317
Romain Guyca89e2a2013-03-08 17:44:20 -0800318 // When finish() is invoked on FBO 0 we've reached the end
319 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400320 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800321 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700322 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800323 }
324
Romain Guy11cb6422012-09-21 00:39:43 -0700325 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700326#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700327 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700328#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700329
Romain Guyc15008e2010-11-10 11:59:15 -0800330#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800331 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700332#else
333 if (mCaches.getDebugLevel() & kDebugMemory) {
334 mCaches.dumpMemoryUsage();
335 }
Romain Guyc15008e2010-11-10 11:59:15 -0800336#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700337 }
Romain Guy96885eb2013-03-26 15:05:58 -0700338
339 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400340
341 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700342}
343
Romain Guy35643dd2012-09-18 15:40:58 -0700344void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700345 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
346 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700347 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700348
349 mCaches.resetScissor();
350 dirtyClip();
351}
352
Andreas Gampe64bb4132014-11-22 00:35:09 +0000353void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400354 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700355
Tom Hudson984162f2014-10-10 13:38:16 -0400356 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700357 clip.snapToPixelBoundaries();
358
Romain Guyd643bb52011-03-01 14:55:21 -0800359 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800360 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800361 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800362 dirtyLayerUnchecked(clip, getRegion());
363 }
Romain Guyd643bb52011-03-01 14:55:21 -0800364
Romain Guy08aa2cb2011-03-17 11:06:57 -0700365 DrawGlInfo info;
366 info.clipLeft = clip.left;
367 info.clipTop = clip.top;
368 info.clipRight = clip.right;
369 info.clipBottom = clip.bottom;
370 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700371 info.width = getViewportWidth();
372 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800373 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700374
Tom Hudson984162f2014-10-10 13:38:16 -0400375 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700376 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400377 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700378 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
379 }
John Reck3b202512014-06-23 13:13:08 -0700380 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700381 setScissorFromClip();
382 }
Chris Craik54f574a2013-08-26 11:23:46 -0700383
John Reck3b202512014-06-23 13:13:08 -0700384 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
385 // Scissor may have been modified, reset dirty clip
386 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800387
Tom Hudson107843d2014-09-08 11:26:26 -0400388 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800389}
390
Romain Guyf6a11b82010-06-23 17:47:49 -0700391///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700392// Debug
393///////////////////////////////////////////////////////////////////////////////
394
Chris Craik62d307c2014-07-29 10:35:13 -0700395void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
396#if DEBUG_DETAILED_EVENTS
397 const int BUFFER_SIZE = 256;
398 va_list ap;
399 char buf[BUFFER_SIZE];
400
401 va_start(ap, fmt);
402 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
403 va_end(ap);
404
405 eventMark(buf);
406#endif
407}
408
409
Romain Guy0f667532013-03-01 14:31:04 -0800410void OpenGLRenderer::eventMark(const char* name) const {
411 mCaches.eventMark(0, name);
412}
413
Romain Guy87e2f7572012-09-24 11:37:12 -0700414void OpenGLRenderer::startMark(const char* name) const {
415 mCaches.startMark(0, name);
416}
417
418void OpenGLRenderer::endMark() const {
419 mCaches.endMark();
420}
421
422void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700423 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700424}
425
426void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400427 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700428 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700429
430 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400431 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700432 clip->right - clip->left, clip->bottom - clip->top);
433
Romain Guy627c6fd2013-08-21 11:53:18 -0700434 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700435 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700436 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
437
438 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700439 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700440 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
441
442 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700443 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700444 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
445
446 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700447 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700448 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
449
Romain Guy87e2f7572012-09-24 11:37:12 -0700450 mCaches.stencil.disable();
451 }
452}
453
454///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700455// Layers
456///////////////////////////////////////////////////////////////////////////////
457
458bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700459 if (layer->deferredUpdateScheduled && layer->renderer
460 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700461
Romain Guy7c450aa2012-09-21 19:15:00 -0700462 if (inFrame) {
463 endTiling();
464 debugOverdraw(false, false);
465 }
Romain Guy11cb6422012-09-21 00:39:43 -0700466
Romain Guy96885eb2013-03-26 15:05:58 -0700467 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700468 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700469 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700470 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700471 }
Romain Guy11cb6422012-09-21 00:39:43 -0700472
473 if (inFrame) {
474 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800475 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700476 }
477
Romain Guy5bb3c732012-11-29 17:52:58 -0800478 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700479 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700480
481 return true;
482 }
483
484 return false;
485}
486
487void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700488 // If draw deferring is enabled this method will simply defer
489 // the display list of each individual layer. The layers remain
490 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700491 int count = mLayerUpdates.size();
492 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700493 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
494 startMark("Layer Updates");
495 } else {
496 startMark("Defer Layer Updates");
497 }
Romain Guy11cb6422012-09-21 00:39:43 -0700498
Chris Craik1206b9b2013-04-04 14:46:24 -0700499 // Note: it is very important to update the layers in order
500 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700501 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700502 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700503 }
Romain Guy11cb6422012-09-21 00:39:43 -0700504
Romain Guy96885eb2013-03-26 15:05:58 -0700505 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
506 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400507 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700508 }
509 endMark();
510 }
511}
512
513void OpenGLRenderer::flushLayers() {
514 int count = mLayerUpdates.size();
515 if (count > 0) {
516 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700517
Chris Craik1206b9b2013-04-04 14:46:24 -0700518 // Note: it is very important to update the layers in order
519 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800520 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700521 }
522
523 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400524 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700525
Romain Guy11cb6422012-09-21 00:39:43 -0700526 endMark();
527 }
528}
529
530void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
531 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700532 // Make sure we don't introduce duplicates.
533 // SortedVector would do this automatically but we need to respect
534 // the insertion order. The linear search is not an issue since
535 // this list is usually very short (typically one item, at most a few)
536 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
537 if (mLayerUpdates.itemAt(i) == layer) {
538 return;
539 }
540 }
Romain Guy11cb6422012-09-21 00:39:43 -0700541 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700542 }
543}
544
Romain Guye93482f2013-06-17 13:14:51 -0700545void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
546 if (layer) {
547 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
548 if (mLayerUpdates.itemAt(i) == layer) {
549 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700550 break;
551 }
552 }
553 }
554}
555
Romain Guy40543602013-06-12 15:31:28 -0700556void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800557 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700558 syncState();
559 updateLayers();
560 flushLayers();
561 // Wait for all the layer updates to be executed
562 AutoFence fence;
563}
564
John Reck443a7142014-09-04 17:40:05 -0700565void OpenGLRenderer::markLayersAsBuildLayers() {
566 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
567 mLayerUpdates[i]->wasBuildLayered = true;
568 }
569}
570
Romain Guy11cb6422012-09-21 00:39:43 -0700571///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700572// State management
573///////////////////////////////////////////////////////////////////////////////
574
Chris Craik14e51302013-12-30 15:32:54 -0800575void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700576 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800577 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
578 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700579
Chris Craika64a2be2014-05-14 14:17:01 -0700580 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700581 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700582 }
583
Romain Guy2542d192010-08-18 11:47:12 -0700584 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700585 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700586 }
Romain Guy2542d192010-08-18 11:47:12 -0700587
Romain Guy5ec99242010-11-03 16:19:08 -0700588 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700589 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700590 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700591 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800592 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700593 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700594 }
Romain Guybb9524b2010-06-22 18:56:38 -0700595}
596
Romain Guyf6a11b82010-06-23 17:47:49 -0700597///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700598// Layers
599///////////////////////////////////////////////////////////////////////////////
600
601int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700602 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700603 // force matrix/clip isolation for layer
604 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
605
Tom Hudson984162f2014-10-10 13:38:16 -0400606 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700607
Tom Hudson984162f2014-10-10 13:38:16 -0400608 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700609 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700610 }
Romain Guyd55a8612010-06-28 17:42:46 -0700611
612 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700613}
614
Chris Craikd90144d2013-03-19 15:03:48 -0700615void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
616 const Rect untransformedBounds(bounds);
617
Chris Craikd6b65f62014-01-01 14:45:21 -0800618 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700619
620 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400621 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700622 // We cannot work with sub-pixels in this case
623 bounds.snapToPixelBoundaries();
624
625 // When the layer is not an FBO, we may use glCopyTexImage so we
626 // need to make sure the layer does not extend outside the bounds
627 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700628 const Snapshot& previous = *(currentSnapshot()->previous);
629 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
630 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700631 bounds.setEmpty();
632 } else if (fboLayer) {
633 clip.set(bounds);
634 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800635 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700636 inverse.mapRect(clip);
637 clip.snapToPixelBoundaries();
638 if (clip.intersect(untransformedBounds)) {
639 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
640 bounds.set(untransformedBounds);
641 } else {
642 clip.setEmpty();
643 }
644 }
645 } else {
646 bounds.setEmpty();
647 }
648}
649
Chris Craik408eb122013-03-26 18:55:15 -0700650void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
651 bool fboLayer, int alpha) {
652 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
653 bounds.getHeight() > mCaches.maxTextureSize ||
654 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400655 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700656 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400657 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700658 }
659}
660
Chris Craikd90144d2013-03-19 15:03:48 -0700661int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500662 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400663 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700664
Tom Hudson984162f2014-10-10 13:38:16 -0400665 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700666 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
667 // operations will be able to store and restore the current clip and transform info, and
668 // quick rejection will be correct (for display lists)
669
670 Rect bounds(left, top, right, bottom);
671 Rect clip;
672 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500673 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700674
Tom Hudson984162f2014-10-10 13:38:16 -0400675 if (!mState.currentlyIgnored()) {
676 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
677 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
678 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800679 writableSnapshot()->roundRectClipState = nullptr;
Chris Craikd90144d2013-03-19 15:03:48 -0700680 }
681 }
682
683 return count;
684}
685
Romain Guy1c740bc2010-09-13 18:00:09 -0700686/**
687 * Layers are viewed by Skia are slightly different than layers in image editing
688 * programs (for instance.) When a layer is created, previously created layers
689 * and the frame buffer still receive every drawing command. For instance, if a
690 * layer is created and a shape intersecting the bounds of the layers and the
691 * framebuffer is draw, the shape will be drawn on both (unless the layer was
692 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
693 *
694 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
695 * texture. Unfortunately, this is inefficient as it requires every primitive to
696 * be drawn n + 1 times, where n is the number of active layers. In practice this
697 * means, for every primitive:
698 * - Switch active frame buffer
699 * - Change viewport, clip and projection matrix
700 * - Issue the drawing
701 *
702 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700703 * To avoid this, layers are implemented in a different way here, at least in the
704 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
705 * is set. When this flag is set we can redirect all drawing operations into a
706 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700707 *
708 * This implementation relies on the frame buffer being at least RGBA 8888. When
709 * a layer is created, only a texture is created, not an FBO. The content of the
710 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700711 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700712 * buffer and drawing continues as normal. This technique therefore treats the
713 * frame buffer as a scratch buffer for the layers.
714 *
715 * To compose the layers back onto the frame buffer, each layer texture
716 * (containing the original frame buffer data) is drawn as a simple quad over
717 * the frame buffer. The trick is that the quad is set as the composition
718 * destination in the blending equation, and the frame buffer becomes the source
719 * of the composition.
720 *
721 * Drawing layers with an alpha value requires an extra step before composition.
722 * An empty quad is drawn over the layer's region in the frame buffer. This quad
723 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
724 * quad is used to multiply the colors in the frame buffer. This is achieved by
725 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
726 * GL_ZERO, GL_SRC_ALPHA.
727 *
728 * Because glCopyTexImage2D() can be slow, an alternative implementation might
729 * be use to draw a single clipped layer. The implementation described above
730 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700731 *
732 * (1) The frame buffer is actually not cleared right away. To allow the GPU
733 * to potentially optimize series of calls to glCopyTexImage2D, the frame
734 * buffer is left untouched until the first drawing operation. Only when
735 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700736 */
Chet Haased48885a2012-08-28 17:43:28 -0700737bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700738 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik07adacf2014-12-19 10:08:40 -0800739 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
740 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700741
Romain Guyeb993562010-10-05 18:14:38 -0700742 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
743
Romain Guyf607bdc2010-09-10 19:20:06 -0700744 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700745 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700746 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700747 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000748 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700749
750 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400751 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700752 return false;
753 }
Romain Guyf18fd992010-07-08 11:45:51 -0700754
Romain Guya1d3c912011-12-13 14:55:06 -0800755 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700756 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700757 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700758 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700759 }
760
Derek Sollenberger674554f2014-02-19 16:47:32 +0000761 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700762 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700763 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
764 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500765
Chet Haasea23eed82012-04-12 15:19:04 -0700766 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700767 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700768 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700769
Romain Guy8fb95422010-08-17 18:38:51 -0700770 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400771 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
772 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700773
Chris Craika8bea8e2014-09-24 11:29:43 -0700774 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
775 fboLayer ? "" : "unclipped ",
776 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700777 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700778 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700779 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700780 } else {
781 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700782 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800783 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700784 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700785 // Workaround for some GL drivers. When reading pixels lying outside
786 // of the window we should get undefined values for those pixels.
787 // Unfortunately some drivers will turn the entire target texture black
788 // when reading outside of the window.
789 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
Chris Craike84a2082014-12-22 14:28:49 -0800790 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
Romain Guy9ace8f52011-07-07 20:50:11 -0700791 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800792 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700793
Chris Craika64a2be2014-05-14 14:17:01 -0700794 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
795 bounds.left, getViewportHeight() - bounds.bottom,
796 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700797
Romain Guy54be1cd2011-06-13 19:04:27 -0700798 // Enqueue the buffer coordinates to clear the corresponding region later
Chris Craik51d6a3d2014-12-22 17:16:56 -0800799 mLayers.push_back(Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700800 }
Romain Guyeb993562010-10-05 18:14:38 -0700801 }
Romain Guyf86ef572010-07-01 11:05:42 -0700802
Romain Guyd55a8612010-06-28 17:42:46 -0700803 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700804}
805
Chris Craike63f7c622013-10-17 10:30:55 -0700806bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800807 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700809
Tom Hudson984162f2014-10-10 13:38:16 -0400810 writableSnapshot()->region = &writableSnapshot()->layer->region;
811 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
812 writableSnapshot()->fbo = layer->getFbo();
813 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
814 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
815 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craike84a2082014-12-22 14:28:49 -0800816 writableSnapshot()->roundRectClipState = nullptr;
Romain Guy5b3b3522010-10-27 18:57:51 -0700817
Romain Guy2b7028e2012-09-19 17:25:38 -0700818 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700819 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700820 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700821 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700822 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700823
824 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700825 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700826 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700827 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700828 }
829
830 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700831 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700832
henry.uh_chen33f5a592014-07-02 19:36:56 +0800833 // Expand the startTiling region by 1
834 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
836 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700837 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800838 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700839 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 glClear(GL_COLOR_BUFFER_BIT);
841
842 dirtyClip();
843
844 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700845 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 return true;
847}
848
Romain Guy1c740bc2010-09-13 18:00:09 -0700849/**
850 * Read the documentation of createLayer() before doing anything in this method.
851 */
Chris Craik14e51302013-12-30 15:32:54 -0800852void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
853 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000854 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700855 return;
856 }
857
Chris Craik14e51302013-12-30 15:32:54 -0800858 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800859 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800860 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700861
Chris Craik39a908c2013-06-13 14:39:01 -0700862 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400863 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craike84a2082014-12-22 14:28:49 -0800864 &clipRequired, nullptr, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700865 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
866
Romain Guyeb993562010-10-05 18:14:38 -0700867 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700868 endTiling();
869
Romain Guye0aa84b2012-04-03 19:30:26 -0700870 // Detach the texture from the FBO
871 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800872
873 layer->removeFbo(false);
874
Romain Guyeb993562010-10-05 18:14:38 -0700875 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700876 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700877 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700878
Chris Craikd6b65f62014-01-01 14:45:21 -0800879 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700880 }
881
Romain Guy9ace8f52011-07-07 20:50:11 -0700882 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500883 SkPaint layerPaint;
884 layerPaint.setAlpha(layer->getAlpha());
885 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
886 layerPaint.setColorFilter(layer->getColorFilter());
887
888 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700889 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700890 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700891 }
892
Romain Guy03750a02010-10-18 14:06:08 -0700893 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700894
Romain Guya1d3c912011-12-13 14:55:06 -0800895 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700896
Romain Guy5b3b3522010-10-27 18:57:51 -0700897 // When the layer is stored in an FBO, we can save a bit of fillrate by
898 // drawing only the dirty region
899 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800900 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700901 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 } else if (!rect.isEmpty()) {
903 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530904
905 save(0);
906 // the layer contains screen buffer content that shouldn't be alpha modulated
907 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400908 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700909 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530910 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700911 }
Romain Guy8b55f372010-08-18 17:10:07 -0700912
Romain Guy746b7402010-10-26 16:27:31 -0700913 dirtyClip();
914
Romain Guyeb993562010-10-05 18:14:38 -0700915 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craike84a2082014-12-22 14:28:49 -0800916 layer->setConvexMask(nullptr);
Romain Guy8550c4c2010-10-08 15:49:53 -0700917 if (!mCaches.layerCache.put(layer)) {
Chris Craik07adacf2014-12-19 10:08:40 -0800918 LAYER_LOGD("Deleting layer");
Chris Craike84a2082014-12-22 14:28:49 -0800919 layer->decStrong(nullptr);
Romain Guy1d83e192010-08-17 11:37:00 -0700920 }
921}
922
Romain Guyaa6c24c2011-04-28 18:40:04 -0700923void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700924 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700925
926 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700928 setupDrawWithTexture();
929 } else {
930 setupDrawWithExternalTexture();
931 }
932 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700933 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500934 setupDrawColorFilter(layer->getColorFilter());
935 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700937 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500938 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
940 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700941 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700943 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800944 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800945 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700946 layer->getWidth() == (uint32_t) rect.getWidth() &&
947 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800948 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
949 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700950
Romain Guyd21b6e12011-11-30 20:21:23 -0800951 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800952 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
953 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800955 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800956 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
957 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 }
959 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700960 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700961
962 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700963}
964
Romain Guy5b3b3522010-10-27 18:57:51 -0700965void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700966 if (layer->isTextureLayer()) {
967 EVENT_LOGD("composeTextureLayerRect");
968 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
969 drawTextureLayer(layer, rect);
970 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
971 } else {
972 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700973 const Rect& texCoords = layer->texCoords;
974 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
975 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700976
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 float x = rect.left;
978 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800979 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700980 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700981 layer->getHeight() == (uint32_t) rect.getHeight();
982
983 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700984 // When we're swapping, the layer is already in screen coordinates
985 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800986 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
987 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700988 }
989
Romain Guyd21b6e12011-11-30 20:21:23 -0800990 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800992 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 }
994
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500995 SkPaint layerPaint;
996 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
997 layerPaint.setXfermodeMode(layer->getMode());
998 layerPaint.setColorFilter(layer->getColorFilter());
999
1000 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001001 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001002 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001003 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001004 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
Romain Guyaa6c24c2011-04-28 18:40:04 -07001006 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001007 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001008}
1009
Chris Craik34416ea2013-04-15 16:08:28 -07001010/**
1011 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1012 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1013 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1014 * by saveLayer's restore
1015 */
Tom Hudson984162f2014-10-10 13:38:16 -04001016#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1017 DRAW_COMMAND; \
1018 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1019 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1020 DRAW_COMMAND; \
1021 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1022 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001023 }
1024
1025#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1026
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001027// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1028// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1029class LayerShader : public SkShader {
1030public:
1031 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1032 : INHERITED(localMatrix)
1033 , mLayer(layer) {
1034 }
1035
Chris Craike84a2082014-12-22 14:28:49 -08001036 virtual bool asACustomShader(void** data) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001037 if (data) {
1038 *data = static_cast<void*>(mLayer);
1039 }
1040 return true;
1041 }
1042
Chris Craike84a2082014-12-22 14:28:49 -08001043 virtual bool isOpaque() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001044 return !mLayer->isBlend();
1045 }
1046
1047protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001048 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001049 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1050 }
1051
Chris Craike84a2082014-12-22 14:28:49 -08001052 virtual void flatten(SkWriteBuffer&) const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001053 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1054 }
1055
Chris Craike84a2082014-12-22 14:28:49 -08001056 virtual Factory getFactory() const override {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001057 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
Chris Craike84a2082014-12-22 14:28:49 -08001058 return nullptr;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001059 }
1060private:
1061 // Unowned.
1062 Layer* mLayer;
1063 typedef SkShader INHERITED;
1064};
1065
Romain Guy5b3b3522010-10-27 18:57:51 -07001066void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001067 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1068
1069 if (layer->getConvexMask()) {
1070 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1071
1072 // clip to the area of the layer the mask can be larger
1073 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1074
1075 SkPaint paint;
1076 paint.setAntiAlias(true);
1077 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1078
Chris Craik3f0854292014-04-15 16:18:08 -07001079 // create LayerShader to map SaveLayer content into subsequent draw
1080 SkMatrix shaderMatrix;
1081 shaderMatrix.setTranslate(rect.left, rect.bottom);
1082 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001083 LayerShader layerShader(layer, &shaderMatrix);
1084 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001085
1086 // Since the drawing primitive is defined in local drawing space,
1087 // we don't need to modify the draw matrix
1088 const SkPath* maskPath = layer->getConvexMask();
1089 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1090
Chris Craike84a2082014-12-22 14:28:49 -08001091 paint.setShader(nullptr);
Chris Craik3f0854292014-04-15 16:18:08 -07001092 restore();
1093
1094 return;
1095 }
1096
Romain Guy5b3b3522010-10-27 18:57:51 -07001097 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001098 layer->setRegionAsRect();
1099
Chris Craik34416ea2013-04-15 16:08:28 -07001100 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001101
Romain Guy5b3b3522010-10-27 18:57:51 -07001102 layer->region.clear();
1103 return;
1104 }
1105
Chris Craik62d307c2014-07-29 10:35:13 -07001106 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001107 // standard Region based draw
1108 size_t count;
1109 const android::Rect* rects;
1110 Region safeRegion;
1111 if (CC_LIKELY(hasRectToRectTransform())) {
1112 rects = layer->region.getArray(&count);
1113 } else {
1114 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1115 rects = safeRegion.getArray(&count);
1116 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001117
Chris Craik3f0854292014-04-15 16:18:08 -07001118 const float alpha = getLayerAlpha(layer);
1119 const float texX = 1.0f / float(layer->getWidth());
1120 const float texY = 1.0f / float(layer->getHeight());
1121 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001122
Chris Craik3f0854292014-04-15 16:18:08 -07001123 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001124
Chris Craik3f0854292014-04-15 16:18:08 -07001125 // We must get (and therefore bind) the region mesh buffer
1126 // after we setup drawing in case we need to mess with the
1127 // stencil buffer in setupDraw()
1128 TextureVertex* mesh = mCaches.getRegionMesh();
1129 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001130
Chris Craik3f0854292014-04-15 16:18:08 -07001131 setupDrawWithTexture();
1132 setupDrawColor(alpha, alpha, alpha, alpha);
1133 setupDrawColorFilter(layer->getColorFilter());
1134 setupDrawBlending(layer);
1135 setupDrawProgram();
1136 setupDrawDirtyRegionsDisabled();
1137 setupDrawPureColorUniforms();
1138 setupDrawColorFilterUniforms(layer->getColorFilter());
1139 setupDrawTexture(layer->getTexture());
1140 if (currentTransform()->isPureTranslate()) {
1141 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1142 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001143
Chris Craik3f0854292014-04-15 16:18:08 -07001144 layer->setFilter(GL_NEAREST);
1145 setupDrawModelView(kModelViewMode_Translate, false,
1146 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1147 } else {
1148 layer->setFilter(GL_LINEAR);
1149 setupDrawModelView(kModelViewMode_Translate, false,
1150 rect.left, rect.top, rect.right, rect.bottom);
1151 }
1152 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001153
Chris Craik3f0854292014-04-15 16:18:08 -07001154 for (size_t i = 0; i < count; i++) {
1155 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001156
Chris Craik3f0854292014-04-15 16:18:08 -07001157 const float u1 = r->left * texX;
1158 const float v1 = (height - r->top) * texY;
1159 const float u2 = r->right * texX;
1160 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001161
Chris Craik3f0854292014-04-15 16:18:08 -07001162 // TODO: Reject quads outside of the clip
1163 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1164 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1165 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1166 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001167
Chris Craik3f0854292014-04-15 16:18:08 -07001168 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001169
Chris Craik3f0854292014-04-15 16:18:08 -07001170 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001171 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001172 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001173 numQuads = 0;
1174 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001175 }
Chris Craik3f0854292014-04-15 16:18:08 -07001176 }
1177
1178 if (numQuads > 0) {
1179 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
Chris Craike84a2082014-12-22 14:28:49 -08001180 GL_UNSIGNED_SHORT, nullptr));
Chris Craik3f0854292014-04-15 16:18:08 -07001181 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001182
Romain Guy5b3b3522010-10-27 18:57:51 -07001183#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001184 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001185#endif
1186
Chris Craik3f0854292014-04-15 16:18:08 -07001187 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001188}
1189
Romain Guy3a3133d2011-02-01 22:59:58 -08001190#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001191void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001192 size_t count;
1193 const android::Rect* rects = region.getArray(&count);
1194
1195 uint32_t colors[] = {
1196 0x7fff0000, 0x7f00ff00,
1197 0x7f0000ff, 0x7fff00ff,
1198 };
1199
1200 int offset = 0;
1201 int32_t top = rects[0].top;
1202
1203 for (size_t i = 0; i < count; i++) {
1204 if (top != rects[i].top) {
1205 offset ^= 0x2;
1206 top = rects[i].top;
1207 }
1208
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001209 SkPaint paint;
1210 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001211 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001212 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001213 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001214}
Chris Craike63f7c622013-10-17 10:30:55 -07001215#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001216
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001217void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001218 Vector<float> rects;
1219
1220 SkRegion::Iterator it(region);
1221 while (!it.done()) {
1222 const SkIRect& r = it.rect();
1223 rects.push(r.fLeft);
1224 rects.push(r.fTop);
1225 rects.push(r.fRight);
1226 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001227 it.next();
1228 }
1229
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001230 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001231}
1232
Romain Guy5b3b3522010-10-27 18:57:51 -07001233void OpenGLRenderer::dirtyLayer(const float left, const float top,
1234 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001235 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001236 Rect bounds(left, top, right, bottom);
1237 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001238 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001239 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001240}
1241
1242void OpenGLRenderer::dirtyLayer(const float left, const float top,
1243 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001244 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001245 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001246 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001247 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248}
1249
1250void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001251 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001252 bounds.snapToPixelBoundaries();
1253 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1254 if (!dirty.isEmpty()) {
1255 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 }
1257 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001258}
1259
Chris Craik4063a0e2013-11-15 16:06:56 -08001260void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001261 GLsizei elementsCount = quadsCount * 6;
1262 while (elementsCount > 0) {
1263 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1264
Romain Guy3380cfd2013-08-15 16:57:57 -07001265 setupDrawIndexedVertices(&mesh[0].x);
Chris Craike84a2082014-12-22 14:28:49 -08001266 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy448455f2013-07-22 13:57:50 -07001267
1268 elementsCount -= drawCount;
1269 // Though there are 4 vertices in a quad, we use 6 indices per
1270 // quad to draw with GL_TRIANGLES
1271 mesh += (drawCount / 6) * 4;
1272 }
1273}
1274
Romain Guy54be1cd2011-06-13 19:04:27 -07001275void OpenGLRenderer::clearLayerRegions() {
1276 const size_t count = mLayers.size();
1277 if (count == 0) return;
1278
Tom Hudson984162f2014-10-10 13:38:16 -04001279 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001280 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001281 // Doing several glScissor/glClear here can negatively impact
1282 // GPUs with a tiler architecture, instead we draw quads with
1283 // the Clear blending mode
1284
1285 // The list contains bounds that have already been clipped
1286 // against their initial clip rect, and the current clip
1287 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001288 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001289
Romain Guy448455f2013-07-22 13:57:50 -07001290 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001291 Vertex* vertex = mesh;
1292
1293 for (uint32_t i = 0; i < count; i++) {
Chris Craik51d6a3d2014-12-22 17:16:56 -08001294 const Rect& bounds = mLayers[i];
Romain Guy54be1cd2011-06-13 19:04:27 -07001295
Chris Craik51d6a3d2014-12-22 17:16:56 -08001296 Vertex::set(vertex++, bounds.left, bounds.top);
1297 Vertex::set(vertex++, bounds.right, bounds.top);
1298 Vertex::set(vertex++, bounds.left, bounds.bottom);
1299 Vertex::set(vertex++, bounds.right, bounds.bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001300 }
Romain Guye67307c2013-02-11 18:01:20 -08001301 // We must clear the list of dirty rects before we
1302 // call setupDraw() to prevent stencil setup to do
1303 // the same thing again
1304 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001305
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001306 SkPaint clearPaint;
1307 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1308
Romain Guy54be1cd2011-06-13 19:04:27 -07001309 setupDraw(false);
1310 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001311 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001312 setupDrawProgram();
1313 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001314 setupDrawModelView(kModelViewMode_Translate, false,
1315 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001316
Chris Craik4063a0e2013-11-15 16:06:56 -08001317 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001318
1319 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001320 } else {
Romain Guye67307c2013-02-11 18:01:20 -08001321 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001322 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001323}
1324
Romain Guybd6b79b2010-06-26 00:13:53 -07001325///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001326// State Deferral
1327///////////////////////////////////////////////////////////////////////////////
1328
Chris Craikff785832013-03-08 13:12:16 -08001329bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001330 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001331 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001332
Chris Craikff785832013-03-08 13:12:16 -08001333 if (stateDeferFlags & kStateDeferFlag_Draw) {
1334 // state has bounds initialized in local coordinates
1335 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001336 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001337 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001338 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1339 // is used, it should more closely duplicate the quickReject logic (in how it uses
1340 // snapToPixelBoundaries)
1341
Chris Craikd6b65f62014-01-01 14:45:21 -08001342 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001343 // quick rejected
1344 return true;
1345 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001346
Chris Craika02c4ed2013-06-14 13:43:58 -07001347 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001348 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001349 int& flags = state.mClipSideFlags;
1350 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001351 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1352 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1353 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1354 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001355 }
1356 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001357 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001358 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1359 // overdraw avoidance (since we don't know what it overlaps)
1360 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001361 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001362 }
1363 }
1364
Chris Craik527a3aa2013-03-04 10:19:31 -08001365 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1366 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001367 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001368 }
1369
Chris Craik7273daa2013-03-28 11:25:24 -07001370 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1371 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001372 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001373 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001374 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001375
1376 // always store/restore, since it's just a pointer
1377 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001378 return false;
1379}
1380
Chris Craik527a3aa2013-03-04 10:19:31 -08001381void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001382 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001383 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001384 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001385 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001386
Chris Craik527a3aa2013-03-04 10:19:31 -08001387 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001388 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001389 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001390 dirtyClip();
1391 }
Chris Craikc3566d02013-02-04 16:16:33 -08001392}
1393
Chris Craik28ce94a2013-05-31 11:38:03 -07001394/**
1395 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1396 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1397 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1398 *
1399 * This method should be called when restoreDisplayState() won't be restoring the clip
1400 */
1401void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
Chris Craike84a2082014-12-22 14:28:49 -08001402 if (clipRect != nullptr) {
Tom Hudson984162f2014-10-10 13:38:16 -04001403 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001404 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001405 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001406 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001407 dirtyClip();
Chris Craike84a2082014-12-22 14:28:49 -08001408 mCaches.setScissorEnabled(clipRect != nullptr || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001409}
1410
Chris Craikc3566d02013-02-04 16:16:33 -08001411///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001412// Clipping
1413///////////////////////////////////////////////////////////////////////////////
1414
Romain Guybb9524b2010-06-22 18:56:38 -07001415void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001416 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001417 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001418
Chris Craika64a2be2014-05-14 14:17:01 -07001419 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001420 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001421 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001422 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001423}
1424
Romain Guy8ce00302013-01-15 18:51:42 -08001425void OpenGLRenderer::ensureStencilBuffer() {
1426 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1427 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1428 // just hope we have one when hasLayer() returns false.
1429 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001430 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001431 }
1432}
1433
1434void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1435 // The layer's FBO is already bound when we reach this stage
1436 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001437 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1438 // is attached after we initiated tiling. We must turn it off,
1439 // attach the new render buffer then turn tiling back on
1440 endTiling();
1441
Romain Guy8d4aeb72013-02-12 16:08:55 -08001442 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001443 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001444 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001445
Romain Guyf735c8e2013-01-31 17:45:55 -08001446 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001447 }
1448}
1449
1450void OpenGLRenderer::setStencilFromClip() {
1451 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001452 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001453 EVENT_LOGD("setStencilFromClip - enabling");
1454
Romain Guy8ce00302013-01-15 18:51:42 -08001455 // NOTE: The order here is important, we must set dirtyClip to false
1456 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001457 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001458
1459 ensureStencilBuffer();
1460
1461 mCaches.stencil.enableWrite();
1462
Chris Craikdeeda3d2014-05-05 19:09:33 -07001463 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001464 // to the region's bounds
1465 bool resetScissor = mCaches.enableScissor();
1466 if (resetScissor) {
1467 // The scissor was not set so we now need to update it
1468 setScissorFromClip();
1469 }
1470 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001471
1472 // stash and disable the outline clip state, since stencil doesn't account for outline
1473 bool storedSkipOutlineClip = mSkipOutlineClip;
1474 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001475
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001476 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001477 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001478 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1479
Romain Guy8ce00302013-01-15 18:51:42 -08001480 // NOTE: We could use the region contour path to generate a smaller mesh
1481 // Since we are using the stencil we could use the red book path
1482 // drawing technique. It might increase bandwidth usage though.
1483
1484 // The last parameter is important: we are not drawing in the color buffer
1485 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001486 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001487 if (resetScissor) mCaches.disableScissor();
1488 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001489
1490 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001491
1492 // Draw the region used to generate the stencil if the appropriate debug
1493 // mode is enabled
1494 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001495 paint.setColor(0x7f0000ff);
1496 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1497 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001498 }
Romain Guy8ce00302013-01-15 18:51:42 -08001499 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001500 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001501 mCaches.stencil.disable();
1502 }
1503 }
1504}
1505
Chris Craikf0a59072013-11-19 18:00:46 -08001506/**
1507 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1508 *
1509 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1510 * style, and tessellated AA ramp
1511 */
1512bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001513 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001514 bool snapOut = paint && paint->isAntiAlias();
1515
1516 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1517 float outset = paint->getStrokeWidth() * 0.5f;
1518 left -= outset;
1519 top -= outset;
1520 right += outset;
1521 bottom += outset;
1522 }
1523
Chris Craikdeeda3d2014-05-05 19:09:33 -07001524 bool clipRequired = false;
1525 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001526 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001527 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001528 return true;
1529 }
1530
Chris Craikf23b25a2014-06-26 15:46:20 -07001531 // not quick rejected, so enable the scissor if clipRequired
1532 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1533 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001534 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001535}
1536
Romain Guy8ce00302013-01-15 18:51:42 -08001537void OpenGLRenderer::debugClip() {
1538#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001539 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001540 SkPaint paint;
1541 paint.setColor(0x7f00ff00);
1542 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1543
Romain Guy8ce00302013-01-15 18:51:42 -08001544 }
1545#endif
1546}
1547
Romain Guyf6a11b82010-06-23 17:47:49 -07001548///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001549// Drawing commands
1550///////////////////////////////////////////////////////////////////////////////
1551
Chris Craik62d307c2014-07-29 10:35:13 -07001552void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001553 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001554 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001555 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001556 // Make sure setScissor & setStencil happen at the beginning of
1557 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001558 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001559 if (mCaches.scissorEnabled) {
1560 setScissorFromClip();
1561 }
Chris Craik62d307c2014-07-29 10:35:13 -07001562
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001563 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001564 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001565
Romain Guy70ca14e2010-12-13 18:24:33 -08001566 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001567
Romain Guy70ca14e2010-12-13 18:24:33 -08001568 mSetShaderColor = false;
1569 mColorSet = false;
1570 mColorA = mColorR = mColorG = mColorB = 0.0f;
1571 mTextureUnit = 0;
1572 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001573
1574 // Enable debug highlight when what we're about to draw is tested against
1575 // the stencil buffer and if stencil highlight debugging is on
1576 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1577 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1578 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001579}
1580
1581void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1582 mDescription.hasTexture = true;
1583 mDescription.hasAlpha8Texture = isAlpha8;
1584}
1585
Romain Guyff316ec2013-02-13 18:39:43 -08001586void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1587 mDescription.hasTexture = true;
1588 mDescription.hasColors = true;
1589 mDescription.hasAlpha8Texture = isAlpha8;
1590}
1591
Romain Guyaa6c24c2011-04-28 18:40:04 -07001592void OpenGLRenderer::setupDrawWithExternalTexture() {
1593 mDescription.hasExternalTexture = true;
1594}
1595
Romain Guy15bc6432011-12-13 13:11:32 -08001596void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001597 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001598}
1599
Chris Craik91a8c7c2014-08-12 14:31:35 -07001600void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1601 mDescription.hasVertexAlpha = true;
1602 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001603}
1604
Romain Guy8d0d4782010-12-14 20:13:35 -08001605void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1606 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001607 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1608 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1609 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001610 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001611 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001612}
1613
Romain Guy86568192010-12-14 15:55:39 -08001614void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1615 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001616 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1617 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1618 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001619 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001620 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001621}
1622
Romain Guy41210632012-07-16 17:04:24 -07001623void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1624 mCaches.fontRenderer->describe(mDescription, paint);
1625}
1626
Romain Guy70ca14e2010-12-13 18:24:33 -08001627void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1628 mColorA = a;
1629 mColorR = r;
1630 mColorG = g;
1631 mColorB = b;
1632 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001633 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001634}
1635
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001636void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
Chris Craike84a2082014-12-22 14:28:49 -08001637 if (shader != nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001638 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001639 }
1640}
1641
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001642void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001643 if (filter == nullptr) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001644 return;
1645 }
1646
1647 SkXfermode::Mode mode;
Chris Craike84a2082014-12-22 14:28:49 -08001648 if (filter->asColorMode(nullptr, &mode)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001649 mDescription.colorOp = ProgramDescription::kColorBlend;
1650 mDescription.colorMode = mode;
Chris Craike84a2082014-12-22 14:28:49 -08001651 } else if (filter->asColorMatrix(nullptr)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001652 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001653 }
1654}
1655
Romain Guyf09ef512011-05-27 11:43:46 -07001656void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1657 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1658 mColorA = 1.0f;
1659 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001660 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001661 }
1662}
1663
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001664void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1665 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001666 // When the blending mode is kClear_Mode, we need to use a modulate color
1667 // argb=1,0,0,0
1668 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001669 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001670 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001671 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001672 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001673}
1674
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001675void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1676 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001677 // When the blending mode is kClear_Mode, we need to use a modulate color
1678 // argb=1,0,0,0
1679 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001680 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001681 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1682 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001683 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001684}
1685
1686void OpenGLRenderer::setupDrawProgram() {
1687 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001688 if (mDescription.hasRoundRectClip) {
1689 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001690 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001691 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001692 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001693 innerRect.left, innerRect.top,
1694 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001695 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1696 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001697
1698 // add half pixel to round out integer rect space to cover pixel centers
1699 float roundedOutRadius = state->radius + 0.5f;
1700 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1701 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001702 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001703}
1704
1705void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1706 mTrackDirtyRegions = false;
1707}
1708
Chris Craik4063a0e2013-11-15 16:06:56 -08001709void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1710 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001711 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001712 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001713 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001714 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001715
Romain Guy86568192010-12-14 15:55:39 -08001716 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001717 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Chris Craike84a2082014-12-22 14:28:49 -08001718 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(),
1719 mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001720 if (dirty && mTrackDirtyRegions) {
1721 if (!ignoreTransform) {
1722 dirtyLayer(left, top, right, bottom, *currentTransform());
1723 } else {
1724 dirtyLayer(left, top, right, bottom);
1725 }
Romain Guy86568192010-12-14 15:55:39 -08001726 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001727}
1728
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001729void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1730 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001731 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1732 }
1733}
1734
Romain Guy86568192010-12-14 15:55:39 -08001735void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001736 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001737 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001738 }
1739}
1740
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001741void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
Chris Craike84a2082014-12-22 14:28:49 -08001742 if (shader == nullptr) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001743 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001744 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001745
1746 if (ignoreTransform) {
1747 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1748 // because it was built into modelView / the geometry, and the description needs to
1749 // compensate.
1750 mat4 modelViewWithoutTransform;
1751 modelViewWithoutTransform.loadInverse(*currentTransform());
1752 modelViewWithoutTransform.multiply(mModelViewMatrix);
1753 mModelViewMatrix.load(modelViewWithoutTransform);
1754 }
1755
1756 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001757}
1758
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001759void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
Chris Craike84a2082014-12-22 14:28:49 -08001760 if (nullptr == filter) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001761 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001762 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001763
1764 SkColor color;
1765 SkXfermode::Mode mode;
1766 if (filter->asColorMode(&color, &mode)) {
1767 const int alpha = SkColorGetA(color);
1768 const GLfloat a = alpha / 255.0f;
1769 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1770 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1771 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1772 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1773 return;
1774 }
1775
1776 SkScalar srcColorMatrix[20];
1777 if (filter->asColorMatrix(srcColorMatrix)) {
1778
1779 float colorMatrix[16];
1780 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1781 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1782 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1783 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1784
1785 // Skia uses the range [0..255] for the addition vector, but we need
1786 // the [0..1] range to apply the vector in GLSL
1787 float colorVector[4];
1788 colorVector[0] = srcColorMatrix[4] / 255.0f;
1789 colorVector[1] = srcColorMatrix[9] / 255.0f;
1790 colorVector[2] = srcColorMatrix[14] / 255.0f;
1791 colorVector[3] = srcColorMatrix[19] / 255.0f;
1792
1793 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1794 GL_FALSE, colorMatrix);
1795 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1796 return;
1797 }
1798
1799 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001800}
1801
Romain Guy41210632012-07-16 17:04:24 -07001802void OpenGLRenderer::setupDrawTextGammaUniforms() {
1803 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1804}
1805
Romain Guy70ca14e2010-12-13 18:24:33 -08001806void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001807 bool force = mCaches.bindMeshBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08001808 mCaches.bindPositionVertexPointer(force, nullptr);
Romain Guy15bc6432011-12-13 13:11:32 -08001809 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001810}
1811
1812void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001813 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001814 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001815 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001816}
1817
Romain Guyaa6c24c2011-04-28 18:40:04 -07001818void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1819 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001820 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001821 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001822}
1823
Romain Guy8f0095c2011-05-02 17:24:22 -07001824void OpenGLRenderer::setupDrawTextureTransform() {
1825 mDescription.hasTextureTransform = true;
1826}
1827
1828void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001829 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1830 GL_FALSE, &transform.data[0]);
1831}
1832
Chris Craik564acf72014-01-02 16:46:18 -08001833void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1834 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001835 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001836 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001837 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001838 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001839 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001840 }
Romain Guyd71dd362011-12-12 19:03:35 -08001841
Chris Craikcb4d6002012-09-25 12:00:29 -07001842 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001843 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001844 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001845 }
1846
1847 mCaches.unbindIndicesBuffer();
1848}
1849
Chris Craik564acf72014-01-02 16:46:18 -08001850void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1851 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001852 bool force = mCaches.unbindMeshBuffer();
1853 GLsizei stride = sizeof(ColorTextureVertex);
1854
1855 mCaches.bindPositionVertexPointer(force, vertices, stride);
1856 if (mCaches.currentProgram->texCoords >= 0) {
1857 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1858 }
1859 int slot = mCaches.currentProgram->getAttrib("colors");
1860 if (slot >= 0) {
1861 glEnableVertexAttribArray(slot);
1862 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1863 }
1864
1865 mCaches.unbindIndicesBuffer();
1866}
1867
Chris Craik564acf72014-01-02 16:46:18 -08001868void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1869 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001870 bool force = false;
1871 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1872 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1873 // use the default VBO found in Caches
1874 if (!vertices || vbo) {
1875 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1876 } else {
1877 force = mCaches.unbindMeshBuffer();
1878 }
ztenghui63d41ab2014-02-14 13:13:41 -08001879 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001880
Chris Craikcb4d6002012-09-25 12:00:29 -07001881 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001882 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001883 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001884 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001885}
1886
Romain Guy448455f2013-07-22 13:57:50 -07001887void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001888 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001889 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001890 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001891}
1892
Romain Guy70ca14e2010-12-13 18:24:33 -08001893///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001894// Drawing
1895///////////////////////////////////////////////////////////////////////////////
1896
Tom Hudson107843d2014-09-08 11:26:26 -04001897void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001898 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1899 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001900 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001901 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001902 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001903 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001904 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001905 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001906 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001907 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001908 }
1909
Chris Craikef8d6f22014-12-17 11:10:28 -08001910 // Don't avoid overdraw when visualizing, since that makes it harder to
1911 // debug where it's coming from, and when the problem occurs.
1912 bool avoidOverdraw = !mCaches.debugOverdraw;
Chris Craika5f09182014-12-17 15:11:30 -08001913 DeferredDisplayList deferredList(*mState.currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001914 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001915 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001916
1917 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001918 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001919
Tom Hudson107843d2014-09-08 11:26:26 -04001920 deferredList.flush(*this, dirty);
1921 } else {
1922 // Even if there is no drawing command(Ex: invisible),
1923 // it still needs startFrame to clear buffer and start tiling.
1924 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001925 }
1926}
1927
Chris Craike84a2082014-12-22 14:28:49 -08001928void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top,
1929 const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001930 float x = left;
1931 float y = top;
1932
Romain Guy886b2752013-01-04 12:26:18 -08001933 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1934
Romain Guya168d732011-03-18 16:50:13 -07001935 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001936 if (currentTransform()->isPureTranslate()) {
1937 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1938 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001939 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001940
1941 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001942 } else {
Chris Craik678625242014-02-28 12:26:34 -08001943 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001944 }
1945
Romain Guy3b748a42013-04-17 18:54:38 -07001946 // No need to check for a UV mapper on the texture object, only ARGB_8888
1947 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001948 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Chris Craike84a2082014-12-22 14:28:49 -08001949 paint, (GLvoid*) nullptr, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001950 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001951}
1952
Romain Guy03c00b52013-06-20 18:30:28 -07001953/**
1954 * Important note: this method is intended to draw batches of bitmaps and
1955 * will not set the scissor enable or dirty the current layer, if any.
1956 * The caller is responsible for properly dirtying the current layer.
1957 */
Tom Hudson107843d2014-09-08 11:26:26 -04001958void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001959 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1960 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001961 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001962 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001963 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001964
Chris Craik527a3aa2013-03-04 10:19:31 -08001965 const AutoTexture autoCleanup(texture);
1966
Chris Craik527a3aa2013-03-04 10:19:31 -08001967 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001968 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001969
1970 const float x = (int) floorf(bounds.left + 0.5f);
1971 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001972 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001973 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001974 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001975 GL_TRIANGLES, bitmapCount * 6, true,
1976 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001977 } else {
1978 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001979 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001980 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1981 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001982 }
1983
Tom Hudson107843d2014-09-08 11:26:26 -04001984 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001985}
1986
Tom Hudson107843d2014-09-08 11:26:26 -04001987void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001988 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001989 return;
Romain Guy6926c722010-07-12 20:20:03 -07001990 }
1991
Romain Guya1d3c912011-12-13 14:55:06 -08001992 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001993 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001994 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001995 const AutoTexture autoCleanup(texture);
1996
Mike Reed1103b322014-07-08 12:36:44 -04001997 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07001998 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07001999 } else {
Chris Craik79647502014-08-06 13:42:24 -07002000 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002001 }
Chet Haase48659092012-05-31 15:21:51 -07002002
Tom Hudson107843d2014-09-08 11:26:26 -04002003 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002004}
2005
Tom Hudson107843d2014-09-08 11:26:26 -04002006void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002007 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002008 return;
Romain Guye651cc62012-05-14 19:44:40 -07002009 }
2010
2011 mCaches.activeTexture(0);
2012 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2013 const AutoTexture autoCleanup(texture);
2014
Mike Reed1103b322014-07-08 12:36:44 -04002015 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002016 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002017 } else {
Chris Craik79647502014-08-06 13:42:24 -07002018 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002019 }
Chet Haase48659092012-05-31 15:21:51 -07002020
Tom Hudson107843d2014-09-08 11:26:26 -04002021 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002022}
2023
Tom Hudson107843d2014-09-08 11:26:26 -04002024void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002025 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002026 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002027 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002028 }
2029
Chris Craik39a908c2013-06-13 14:39:01 -07002030 // TODO: use quickReject on bounds from vertices
2031 mCaches.enableScissor();
2032
Romain Guyb18d2d02011-02-10 15:52:54 -08002033 float left = FLT_MAX;
2034 float top = FLT_MAX;
2035 float right = FLT_MIN;
2036 float bottom = FLT_MIN;
2037
Romain Guya92bb4d2012-10-16 11:08:44 -07002038 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002039
Chris Craik51d6a3d2014-12-22 17:16:56 -08002040 std::unique_ptr<ColorTextureVertex[]> mesh(new ColorTextureVertex[count]);
2041 ColorTextureVertex* vertex = &mesh[0];
Romain Guyff316ec2013-02-13 18:39:43 -08002042
Chris Craik51d6a3d2014-12-22 17:16:56 -08002043 std::unique_ptr<int[]> tempColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002044 if (!colors) {
2045 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craik51d6a3d2014-12-22 17:16:56 -08002046 tempColors.reset(new int[colorsCount]);
2047 memset(tempColors.get(), 0xff, colorsCount * sizeof(int));
2048 colors = tempColors.get();
Romain Guyff316ec2013-02-13 18:39:43 -08002049 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002050
Romain Guy3b748a42013-04-17 18:54:38 -07002051 mCaches.activeTexture(0);
John Reckebd52612014-12-10 16:47:36 -08002052 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002053 const UvMapper& mapper(getMapper(texture));
2054
Romain Guy5a7b4662011-01-20 19:09:30 -08002055 for (int32_t y = 0; y < meshHeight; y++) {
2056 for (int32_t x = 0; x < meshWidth; x++) {
2057 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2058
2059 float u1 = float(x) / meshWidth;
2060 float u2 = float(x + 1) / meshWidth;
2061 float v1 = float(y) / meshHeight;
2062 float v2 = float(y + 1) / meshHeight;
2063
Romain Guy3b748a42013-04-17 18:54:38 -07002064 mapper.map(u1, v1, u2, v2);
2065
Romain Guy5a7b4662011-01-20 19:09:30 -08002066 int ax = i + (meshWidth + 1) * 2;
2067 int ay = ax + 1;
2068 int bx = i;
2069 int by = bx + 1;
2070 int cx = i + 2;
2071 int cy = cx + 1;
2072 int dx = i + (meshWidth + 1) * 2 + 2;
2073 int dy = dx + 1;
2074
Romain Guyff316ec2013-02-13 18:39:43 -08002075 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2076 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2077 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002078
Romain Guyff316ec2013-02-13 18:39:43 -08002079 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2080 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2081 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002082
Romain Guya92bb4d2012-10-16 11:08:44 -07002083 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2084 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2085 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2086 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002087 }
2088 }
2089
Chris Craikf0a59072013-11-19 18:00:46 -08002090 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002091 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002092 }
2093
Romain Guyff316ec2013-02-13 18:39:43 -08002094 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002095 texture = mCaches.textureCache.get(bitmap);
2096 if (!texture) {
Tom Hudson107843d2014-09-08 11:26:26 -04002097 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002098 }
Romain Guyff316ec2013-02-13 18:39:43 -08002099 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002100 const AutoTexture autoCleanup(texture);
2101
2102 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002103 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002104
2105 int alpha;
2106 SkXfermode::Mode mode;
2107 getAlphaAndMode(paint, &alpha, &mode);
2108
Romain Guyff316ec2013-02-13 18:39:43 -08002109 float a = alpha / 255.0f;
2110
Romain Guya92bb4d2012-10-16 11:08:44 -07002111 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002112 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002113 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002114
Romain Guyff316ec2013-02-13 18:39:43 -08002115 setupDraw();
2116 setupDrawWithTextureAndColor();
2117 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002118 setupDrawColorFilter(getColorFilter(paint));
2119 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002120 setupDrawProgram();
2121 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002122 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002123 setupDrawTexture(texture->id);
2124 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002125 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002126 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002127
2128 glDrawArrays(GL_TRIANGLES, 0, count);
2129
Romain Guyff316ec2013-02-13 18:39:43 -08002130 int slot = mCaches.currentProgram->getAttrib("colors");
2131 if (slot >= 0) {
2132 glDisableVertexAttribArray(slot);
2133 }
2134
Tom Hudson107843d2014-09-08 11:26:26 -04002135 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002136}
2137
Tom Hudson107843d2014-09-08 11:26:26 -04002138void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002139 float srcLeft, float srcTop, float srcRight, float srcBottom,
2140 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002141 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002142 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002143 return;
Romain Guy6926c722010-07-12 20:20:03 -07002144 }
2145
Romain Guya1d3c912011-12-13 14:55:06 -08002146 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002147 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002148 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002149 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002150
Romain Guy8ba548f2010-06-30 19:21:21 -07002151 const float width = texture->width;
2152 const float height = texture->height;
2153
Romain Guy3b748a42013-04-17 18:54:38 -07002154 float u1 = fmax(0.0f, srcLeft / width);
2155 float v1 = fmax(0.0f, srcTop / height);
2156 float u2 = fmin(1.0f, srcRight / width);
2157 float v2 = fmin(1.0f, srcBottom / height);
2158
2159 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002160
Romain Guy03750a02010-10-18 14:06:08 -07002161 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002162 resetDrawTextureTexCoords(u1, v1, u2, v2);
2163
Romain Guyd21b6e12011-11-30 20:21:23 -08002164 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2165
Romain Guy886b2752013-01-04 12:26:18 -08002166 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2167 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002168
Romain Guy886b2752013-01-04 12:26:18 -08002169 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2170 // Apply a scale transform on the canvas only when a shader is in use
2171 // Skia handles the ratio between the dst and src rects as a scale factor
2172 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002173 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002174 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002175
Chris Craikd6b65f62014-01-01 14:45:21 -08002176 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2177 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2178 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002179
2180 dstRight = x + (dstRight - dstLeft);
2181 dstBottom = y + (dstBottom - dstTop);
2182
2183 dstLeft = x;
2184 dstTop = y;
2185
Chris Craik678625242014-02-28 12:26:34 -08002186 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002187 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002188 } else {
Chris Craik678625242014-02-28 12:26:34 -08002189 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002190 }
2191
2192 if (CC_UNLIKELY(useScaleTransform)) {
2193 save(SkCanvas::kMatrix_SaveFlag);
2194 translate(dstLeft, dstTop);
2195 scale(scaleX, scaleY);
2196
2197 dstLeft = 0.0f;
2198 dstTop = 0.0f;
2199
2200 dstRight = srcRight - srcLeft;
2201 dstBottom = srcBottom - srcTop;
2202 }
2203
Mike Reed1103b322014-07-08 12:36:44 -04002204 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002205 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002206 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002207 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002208 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2209 } else {
2210 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002211 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002212 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002213 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2214 }
2215
2216 if (CC_UNLIKELY(useScaleTransform)) {
2217 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002218 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002219
2220 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002221
Tom Hudson107843d2014-09-08 11:26:26 -04002222 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002223}
2224
Tom Hudson107843d2014-09-08 11:26:26 -04002225void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002226 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002227 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002228 return;
Romain Guy6926c722010-07-12 20:20:03 -07002229 }
2230
John Reckebd52612014-12-10 16:47:36 -08002231 AssetAtlas::Entry* entry = mRenderState.assetAtlas().getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002232 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2233 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002234
Tom Hudson107843d2014-09-08 11:26:26 -04002235 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002236}
2237
Tom Hudson107843d2014-09-08 11:26:26 -04002238void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002239 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2240 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002241 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002242 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002243 }
2244
Romain Guy211370f2012-02-01 16:10:55 -08002245 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002246 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002247 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002248 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002249 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002250
Romain Guya4adcf02013-02-28 12:15:35 -08002251 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2252 texture->setFilter(GL_LINEAR, true);
2253
Chris Craikd6b65f62014-01-01 14:45:21 -08002254 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002255 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002256 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002257 const float offsetX = left + currentTransform()->getTranslateX();
2258 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002259 const size_t count = mesh->quads.size();
2260 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002261 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002262 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002263 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2264 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2265 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002266 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002267 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002268 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002269 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002270 }
2271 }
2272
Chris Craik4063a0e2013-11-15 16:06:56 -08002273 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002274 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002275 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2276 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002277
Romain Guy3b748a42013-04-17 18:54:38 -07002278 right = x + right - left;
2279 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002280 left = x;
2281 top = y;
2282 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002283 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002284 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2285 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002286 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2287 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002288 }
Chet Haase48659092012-05-31 15:21:51 -07002289
Tom Hudson107843d2014-09-08 11:26:26 -04002290 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002291}
2292
Romain Guy03c00b52013-06-20 18:30:28 -07002293/**
2294 * Important note: this method is intended to draw batches of 9-patch objects and
2295 * will not set the scissor enable or dirty the current layer, if any.
2296 * The caller is responsible for properly dirtying the current layer.
2297 */
Tom Hudson107843d2014-09-08 11:26:26 -04002298void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002299 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002300 mCaches.activeTexture(0);
2301 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002302 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002303 const AutoTexture autoCleanup(texture);
2304
2305 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2306 texture->setFilter(GL_LINEAR, true);
2307
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002308 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2309 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002310 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002311
Tom Hudson107843d2014-09-08 11:26:26 -04002312 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002313}
2314
Tom Hudson107843d2014-09-08 11:26:26 -04002315void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002316 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002317 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002318 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002319 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002320 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002321 }
2322
Chris Craik0d5ac952014-07-15 13:01:02 -07002323 Rect bounds(vertexBuffer.getBounds());
2324 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002325 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2326
Chris Craikcb4d6002012-09-25 12:00:29 -07002327 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002328 bool isAA = paint->isAntiAlias();
2329
Chris Craik710f46d2012-09-17 17:25:49 -07002330 setupDraw();
2331 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002332 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002333 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002334 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002335 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002336 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002337 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002338 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2339 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002340 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002341 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002342 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002343
ztenghui55bfb4e2013-12-03 10:38:55 -08002344 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002345 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002346 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002347 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002348
Chris Craik710f46d2012-09-17 17:25:49 -07002349 int alphaSlot = -1;
2350 if (isAA) {
2351 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2352 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002353 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002354 glEnableVertexAttribArray(alphaSlot);
2355 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002356 }
Romain Guy04299382012-07-18 17:15:41 -07002357
Chris Craik05f3d6e2014-06-02 16:27:04 -07002358 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2359 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002360 mCaches.unbindIndicesBuffer();
2361 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002362 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002363 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002364 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT,
2365 GL_UNSIGNED_SHORT, nullptr);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002366 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002367 mCaches.bindShadowIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002368 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT,
2369 GL_UNSIGNED_SHORT, nullptr);
ztenghuid5e8ade2014-08-13 15:48:02 -07002370 } else if (mode == VertexBuffer::kIndices) {
2371 mCaches.unbindIndicesBuffer();
Chris Craike84a2082014-12-22 14:28:49 -08002372 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(),
2373 GL_UNSIGNED_SHORT, vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002374 }
Romain Guy04299382012-07-18 17:15:41 -07002375
Chris Craik710f46d2012-09-17 17:25:49 -07002376 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002377 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002378 }
Chris Craik65cd6122012-12-10 17:56:27 -08002379
Tom Hudson107843d2014-09-08 11:26:26 -04002380 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002381}
2382
2383/**
Chris Craik65cd6122012-12-10 17:56:27 -08002384 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2385 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2386 * screen space in all directions. However, instead of using a fragment shader to compute the
2387 * translucency of the color from its position, we simply use a varying parameter to define how far
2388 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2389 *
2390 * Doesn't yet support joins, caps, or path effects.
2391 */
Tom Hudson107843d2014-09-08 11:26:26 -04002392void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002393 VertexBuffer vertexBuffer;
2394 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002395 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002396 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002397}
2398
2399/**
2400 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2401 * and additional geometry for defining an alpha slope perimeter.
2402 *
2403 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2404 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2405 * in-shader alpha region, but found it to be taxing on some GPUs.
2406 *
2407 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2408 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002409 */
Tom Hudson107843d2014-09-08 11:26:26 -04002410void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002411 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002412
Chris Craik65cd6122012-12-10 17:56:27 -08002413 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002414
Chris Craik65cd6122012-12-10 17:56:27 -08002415 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002416 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2417 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002418
Chris Craik05f3d6e2014-06-02 16:27:04 -07002419 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002420 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002421 }
2422
Chris Craikbf759452014-08-11 16:00:44 -07002423 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002424 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002425}
2426
Tom Hudson107843d2014-09-08 11:26:26 -04002427void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002428 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002429
Chris Craik6d29c8d2013-05-08 18:35:44 -07002430 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002431
Chris Craik6d29c8d2013-05-08 18:35:44 -07002432 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002433 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002434
Chris Craik05f3d6e2014-06-02 16:27:04 -07002435 const Rect& bounds = buffer.getBounds();
2436 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002437 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002438 }
2439
Chris Craikbf759452014-08-11 16:00:44 -07002440 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002441 drawVertexBuffer(buffer, paint, displayFlags);
2442
2443 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002444}
2445
Tom Hudson107843d2014-09-08 11:26:26 -04002446void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002447 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002448 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002449
Tom Hudson984162f2014-10-10 13:38:16 -04002450 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002451 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002452
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002453 SkPaint paint;
2454 paint.setColor(color);
2455 paint.setXfermodeMode(mode);
2456
2457 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002458
Tom Hudson107843d2014-09-08 11:26:26 -04002459 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002460}
Romain Guy9d5316e2010-06-24 19:30:36 -07002461
Tom Hudson107843d2014-09-08 11:26:26 -04002462void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002463 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002464 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002465 const AutoTexture autoCleanup(texture);
2466
2467 const float x = left + texture->left - texture->offset;
2468 const float y = top + texture->top - texture->offset;
2469
2470 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002471
Tom Hudson107843d2014-09-08 11:26:26 -04002472 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002473}
2474
Tom Hudson107843d2014-09-08 11:26:26 -04002475void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002476 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002477 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002478 || quickRejectSetupScissor(left, top, right, bottom, p)
2479 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002480 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002481 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002482
Chris Craike84a2082014-12-22 14:28:49 -08002483 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002484 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002485 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002486 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002487 drawShape(left, top, texture, p);
2488 } else {
2489 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2490 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2491 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002492 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002493}
2494
Tom Hudson107843d2014-09-08 11:26:26 -04002495void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002496 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002497 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2498 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002499 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002500 }
Chris Craike84a2082014-12-22 14:28:49 -08002501 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002502 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002503 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002504 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002505 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002506 SkPath path;
2507 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2508 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2509 } else {
2510 path.addCircle(x, y, radius);
2511 }
2512 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002513 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002514}
Romain Guy01d58e42011-01-19 21:54:02 -08002515
Tom Hudson107843d2014-09-08 11:26:26 -04002516void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002517 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002518 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002519 || quickRejectSetupScissor(left, top, right, bottom, p)
2520 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002521 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002522 }
Romain Guy01d58e42011-01-19 21:54:02 -08002523
Chris Craike84a2082014-12-22 14:28:49 -08002524 if (p->getPathEffect() != nullptr) {
Chris Craik710f46d2012-09-17 17:25:49 -07002525 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002526 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002527 drawShape(left, top, texture, p);
2528 } else {
2529 SkPath path;
2530 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2531 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2532 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2533 }
2534 path.addOval(rect);
2535 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002536 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002537}
2538
Tom Hudson107843d2014-09-08 11:26:26 -04002539void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002540 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002541 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002542 || quickRejectSetupScissor(left, top, right, bottom, p)
2543 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002544 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002545 }
2546
Chris Craik780c1282012-10-04 14:10:49 -07002547 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craike84a2082014-12-22 14:28:49 -08002548 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != nullptr || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002549 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002550 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002551 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002552 drawShape(left, top, texture, p);
2553 return;
Chris Craik780c1282012-10-04 14:10:49 -07002554 }
Chris Craik780c1282012-10-04 14:10:49 -07002555 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2556 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2557 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2558 }
2559
2560 SkPath path;
2561 if (useCenter) {
2562 path.moveTo(rect.centerX(), rect.centerY());
2563 }
2564 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2565 if (useCenter) {
2566 path.close();
2567 }
Tom Hudson107843d2014-09-08 11:26:26 -04002568 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002569}
2570
Romain Guycf8675e2012-10-02 12:32:25 -07002571// See SkPaintDefaults.h
2572#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2573
Tom Hudson107843d2014-09-08 11:26:26 -04002574void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002575 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002576 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002577 || quickRejectSetupScissor(left, top, right, bottom, p)
2578 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002579 return;
Romain Guy6926c722010-07-12 20:20:03 -07002580 }
2581
Chris Craik710f46d2012-09-17 17:25:49 -07002582 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002583 // only fill style is supported by drawConvexPath, since others have to handle joins
Chris Craike84a2082014-12-22 14:28:49 -08002584 if (p->getPathEffect() != nullptr || p->getStrokeJoin() != SkPaint::kMiter_Join ||
Romain Guycf8675e2012-10-02 12:32:25 -07002585 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2586 mCaches.activeTexture(0);
2587 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002588 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002589 drawShape(left, top, texture, p);
2590 } else {
2591 SkPath path;
2592 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2593 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2594 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2595 }
2596 path.addRect(rect);
2597 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002598 }
Chet Haase858aa932011-05-12 09:06:00 -07002599 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002600 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2601 SkPath path;
2602 path.addRect(left, top, right, bottom);
2603 drawConvexPath(path, p);
2604 } else {
2605 drawColorRect(left, top, right, bottom, p);
2606
2607 mDirty = true;
2608 }
Chet Haase858aa932011-05-12 09:06:00 -07002609 }
Romain Guyc7d53492010-06-25 13:41:57 -07002610}
Romain Guy9d5316e2010-06-24 19:30:36 -07002611
Chris Craikd218a922014-01-02 17:13:34 -08002612void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2613 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002614 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002615 mCaches.activeTexture(0);
2616
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002617 TextShadow textShadow;
2618 if (!getTextShadow(paint, &textShadow)) {
2619 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2620 }
2621
Raph Levien416a8472012-07-19 22:48:17 -07002622 // NOTE: The drop shadow will not perform gamma correction
2623 // if shader-based correction is enabled
2624 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2625 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002626 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002627 // If the drop shadow exceeds the max texture size or couldn't be
2628 // allocated, skip drawing
2629 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002630 const AutoTexture autoCleanup(shadow);
2631
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002632 const float sx = x - shadow->left + textShadow.dx;
2633 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002634
Tom Hudson984162f2014-10-10 13:38:16 -04002635 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002636 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002637 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002638 }
2639
2640 setupDraw();
2641 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002642 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002643 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002644 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002645 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002646 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002647 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2648 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002649 setupDrawTexture(shadow->id);
2650 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002651 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002652 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08002653 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Raph Levien416a8472012-07-19 22:48:17 -07002654
2655 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2656}
2657
Romain Guy768bffc2013-02-27 13:50:45 -08002658bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002659 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002660 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2661}
2662
Tom Hudson107843d2014-09-08 11:26:26 -04002663void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002664 const float* positions, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002665 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002666 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002667 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002668
Romain Guy671d6cf2012-01-18 12:39:17 -08002669 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002670 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002671 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002672 }
2673
Chris Craik39a908c2013-06-13 14:39:01 -07002674 mCaches.enableScissor();
2675
Romain Guy671d6cf2012-01-18 12:39:17 -08002676 float x = 0.0f;
2677 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002678 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002679 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002680 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2681 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002682 }
2683
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002684 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002685 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002686
2687 int alpha;
2688 SkXfermode::Mode mode;
2689 getAlphaAndMode(paint, &alpha, &mode);
2690
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002691 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002692 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002693 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002694 }
2695
Romain Guy671d6cf2012-01-18 12:39:17 -08002696 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002697 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002698 if (pureTranslate && !linearFilter) {
2699 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2700 }
Romain Guy257ae352013-03-20 16:31:12 -07002701 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002702
Tom Hudson984162f2014-10-10 13:38:16 -04002703 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002704 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2705
Romain Guy211370f2012-02-01 16:10:55 -08002706 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002707
Victoria Lease1e546812013-06-25 14:25:17 -07002708 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002710 positions, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 if (hasActiveLayer) {
2712 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002713 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 }
2715 dirtyLayerUnchecked(bounds, getRegion());
2716 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 }
Chet Haase48659092012-05-31 15:21:51 -07002718
Tom Hudson107843d2014-09-08 11:26:26 -04002719 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002720}
2721
Chris Craik59744b72014-07-01 17:56:52 -07002722bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002723 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002724 outMatrix->setIdentity();
2725 return false;
2726 } else if (CC_UNLIKELY(transform.isPerspective())) {
2727 outMatrix->setIdentity();
2728 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002729 }
Chris Craik59744b72014-07-01 17:56:52 -07002730
2731 /**
Chris Craika736cd92014-08-04 13:18:38 -07002732 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2733 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002734 */
2735 float sx, sy;
2736 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002737 outMatrix->setScale(
2738 roundf(fmaxf(1.0f, sx)),
2739 roundf(fmaxf(1.0f, sy)));
2740 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002741}
2742
Tom Hudson984162f2014-10-10 13:38:16 -04002743int OpenGLRenderer::getSaveCount() const {
2744 return mState.getSaveCount();
2745}
2746
2747int OpenGLRenderer::save(int flags) {
2748 return mState.save(flags);
2749}
2750
2751void OpenGLRenderer::restore() {
2752 return mState.restore();
2753}
2754
2755void OpenGLRenderer::restoreToCount(int saveCount) {
2756 return mState.restoreToCount(saveCount);
2757}
2758
2759void OpenGLRenderer::translate(float dx, float dy, float dz) {
2760 return mState.translate(dx, dy, dz);
2761}
2762
2763void OpenGLRenderer::rotate(float degrees) {
2764 return mState.rotate(degrees);
2765}
2766
2767void OpenGLRenderer::scale(float sx, float sy) {
2768 return mState.scale(sx, sy);
2769}
2770
2771void OpenGLRenderer::skew(float sx, float sy) {
2772 return mState.skew(sx, sy);
2773}
2774
2775void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2776 mState.setMatrix(matrix);
2777}
2778
2779void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2780 mState.concatMatrix(matrix);
2781}
2782
2783bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2784 return mState.clipRect(left, top, right, bottom, op);
2785}
2786
2787bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2788 return mState.clipPath(path, op);
2789}
2790
2791bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2792 return mState.clipRegion(region, op);
2793}
2794
2795void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2796 mState.setClippingOutline(allocator, outline);
2797}
2798
2799void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2800 const Rect& rect, float radius, bool highPriority) {
2801 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2802}
2803
2804
2805
Tom Hudson107843d2014-09-08 11:26:26 -04002806void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002807 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002808 DrawOpMode drawOpMode) {
2809
Chris Craik527a3aa2013-03-04 10:19:31 -08002810 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002811 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2812 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craike84a2082014-12-22 14:28:49 -08002813 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002814 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002815 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002816 }
Romain Guycac5fd32011-12-01 20:08:50 -08002817 }
2818
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002819 const float oldX = x;
2820 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002821
Chris Craikd6b65f62014-01-01 14:45:21 -08002822 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002823 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002824
Romain Guy211370f2012-02-01 16:10:55 -08002825 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002826 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2827 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002828 }
2829
Romain Guy86568192010-12-14 15:55:39 -08002830 int alpha;
2831 SkXfermode::Mode mode;
2832 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002833
Romain Guyc74f45a2013-02-26 19:10:14 -08002834 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2835
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002836 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002837 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002838 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002839 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002840 }
2841
Romain Guy19d4dd82013-03-04 11:14:26 -08002842 const bool hasActiveLayer = hasLayer();
2843
Romain Guy624234f2013-03-05 16:43:31 -08002844 // We only pass a partial transform to the font renderer. That partial
2845 // matrix defines how glyphs are rasterized. Typically we want glyphs
2846 // to be rasterized at their final size on screen, which means the partial
2847 // matrix needs to take the scale factor into account.
2848 // When a partial matrix is used to transform glyphs during rasterization,
2849 // the mesh is generated with the inverse transform (in the case of scale,
2850 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2851 // apply the full transform matrix at draw time in the vertex shader.
2852 // Applying the full matrix in the shader is the easiest way to handle
2853 // rotation and perspective and allows us to always generated quads in the
2854 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002855 SkMatrix fontTransform;
2856 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2857 || fabs(y - (int) y) > 0.0f
2858 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002859 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002860 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002861
Romain Guy624234f2013-03-05 16:43:31 -08002862 // TODO: Implement better clipping for scaled/rotated text
Chris Craike84a2082014-12-22 14:28:49 -08002863 const Rect* clip = !pureTranslate ? nullptr : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002864 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 -07002865
Raph Levien996e57c2012-07-23 15:22:52 -07002866 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002867 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002868
2869 // don't call issuedrawcommand, do it at end of batch
2870 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002871 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002872 SkPaint paintCopy(*paint);
2873 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2874 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002875 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002876 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002877 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craike84a2082014-12-22 14:28:49 -08002878 positions, hasActiveLayer ? &layerBounds : nullptr, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002879 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002880
Chris Craik527a3aa2013-03-04 10:19:31 -08002881 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002882 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002883 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002884 }
Chris Craik41541822013-05-03 16:35:54 -07002885 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002886 }
Romain Guy694b5192010-07-21 21:33:20 -07002887
Chris Craike63f7c622013-10-17 10:30:55 -07002888 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002889
Tom Hudson107843d2014-09-08 11:26:26 -04002890 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002891}
2892
Tom Hudson107843d2014-09-08 11:26:26 -04002893void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002894 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craike84a2082014-12-22 14:28:49 -08002895 if (text == nullptr || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002896 return;
Romain Guy03d58522012-02-24 17:54:07 -08002897 }
2898
Chris Craik39a908c2013-06-13 14:39:01 -07002899 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2900 mCaches.enableScissor();
2901
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002902 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002903 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002904 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002905
2906 int alpha;
2907 SkXfermode::Mode mode;
2908 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002909 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002910
Tom Hudson984162f2014-10-10 13:38:16 -04002911 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002912 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 -08002913
Romain Guy97771732012-02-28 18:17:02 -08002914 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002915
2916 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Chris Craike84a2082014-12-22 14:28:49 -08002917 hOffset, vOffset, hasActiveLayer ? &bounds : nullptr, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002918 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002919 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002920 dirtyLayerUnchecked(bounds, getRegion());
2921 }
Romain Guy97771732012-02-28 18:17:02 -08002922 }
Chet Haase48659092012-05-31 15:21:51 -07002923
Tom Hudson107843d2014-09-08 11:26:26 -04002924 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002925}
2926
Tom Hudson107843d2014-09-08 11:26:26 -04002927void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002928 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002929
Romain Guya1d3c912011-12-13 14:55:06 -08002930 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002931
Romain Guyfb8b7632010-08-23 21:05:08 -07002932 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002933 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002934 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002935
Romain Guy8b55f372010-08-18 17:10:07 -07002936 const float x = texture->left - texture->offset;
2937 const float y = texture->top - texture->offset;
2938
Romain Guy01d58e42011-01-19 21:54:02 -08002939 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002940 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002941}
2942
Tom Hudson107843d2014-09-08 11:26:26 -04002943void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002944 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002945 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002946 }
2947
Chris Craike84a2082014-12-22 14:28:49 -08002948 mat4* transform = nullptr;
Romain Guyb2e2f242012-10-17 18:18:35 -07002949 if (layer->isTextureLayer()) {
2950 transform = &layer->getTransform();
2951 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002952 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002953 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002954 }
2955 }
2956
Chris Craik39a908c2013-06-13 14:39:01 -07002957 bool clipRequired = false;
Chris Craike84a2082014-12-22 14:28:49 -08002958 const bool rejected = mState.calculateQuickRejectForScissor(
2959 x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
2960 &clipRequired, nullptr, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002961
2962 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002963 if (transform && !transform->isIdentity()) {
2964 restore();
2965 }
Tom Hudson107843d2014-09-08 11:26:26 -04002966 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002967 }
2968
Chris Craik62d307c2014-07-29 10:35:13 -07002969 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2970 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2971
Romain Guy5bb3c732012-11-29 17:52:58 -08002972 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002973
Chris Craik39a908c2013-06-13 14:39:01 -07002974 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002975 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002976
Romain Guy211370f2012-02-01 16:10:55 -08002977 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002978 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002979 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2980 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002981 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002982
Chris Craik16ecda52013-03-29 10:59:59 -07002983 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002984 setupDraw();
2985 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002986 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002987 setupDrawColorFilter(layer->getColorFilter());
2988 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002989 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002990 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002991 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002992 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002993 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2994 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2995 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002996
Romain Guyd21b6e12011-11-30 20:21:23 -08002997 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002998 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002999 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003000 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003001 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003002 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003003 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3004 }
Romain Guyf219da52011-01-16 12:54:25 -08003005
Romain Guy448455f2013-07-22 13:57:50 -07003006 TextureVertex* mesh = &layer->mesh[0];
3007 GLsizei elementsCount = layer->meshElementCount;
3008
3009 while (elementsCount > 0) {
3010 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3011
Romain Guy3380cfd2013-08-15 16:57:57 -07003012 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003013 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
Chris Craike84a2082014-12-22 14:28:49 -08003014 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, nullptr));
Romain Guy448455f2013-07-22 13:57:50 -07003015
3016 elementsCount -= drawCount;
3017 // Though there are 4 vertices in a quad, we use 6 indices per
3018 // quad to draw with GL_TRIANGLES
3019 mesh += (drawCount / 6) * 4;
3020 }
Romain Guyf219da52011-01-16 12:54:25 -08003021
Romain Guy3a3133d2011-02-01 22:59:58 -08003022#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003023 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003024#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003025 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003026
Romain Guy5bb3c732012-11-29 17:52:58 -08003027 if (layer->debugDrawUpdate) {
3028 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003029
3030 SkPaint paint;
3031 paint.setColor(0x7f00ff00);
3032 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003033 }
Romain Guyf219da52011-01-16 12:54:25 -08003034 }
Chris Craik34416ea2013-04-15 16:08:28 -07003035 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003036
Romain Guyb2e2f242012-10-17 18:18:35 -07003037 if (transform && !transform->isIdentity()) {
3038 restore();
3039 }
3040
Tom Hudson107843d2014-09-08 11:26:26 -04003041 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003042}
3043
Romain Guy6926c722010-07-12 20:20:03 -07003044///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003045// Draw filters
3046///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003047void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3048 // We should never get here since we apply the draw filter when stashing
3049 // the paints in the DisplayList.
3050 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003051}
3052
3053///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003054// Drawing implementation
3055///////////////////////////////////////////////////////////////////////////////
3056
Chris Craikd218a922014-01-02 17:13:34 -08003057Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
John Reckebd52612014-12-10 16:47:36 -08003058 Texture* texture = mRenderState.assetAtlas().getEntryTexture(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07003059 if (!texture) {
3060 return mCaches.textureCache.get(bitmap);
3061 }
3062 return texture;
3063}
3064
Romain Guy01d58e42011-01-19 21:54:02 -08003065void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003066 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003067 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003068 return;
3069 }
3070
3071 int alpha;
3072 SkXfermode::Mode mode;
3073 getAlphaAndMode(paint, &alpha, &mode);
3074
3075 setupDraw();
3076 setupDrawWithTexture(true);
3077 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003078 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003079 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003080 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003081 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003082 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3083 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003084 setupDrawTexture(texture->id);
3085 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003086 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003087 setupDrawShaderUniforms(getShader(paint));
Chris Craike84a2082014-12-22 14:28:49 -08003088 setupDrawMesh(nullptr, (GLvoid*) gMeshTextureOffset);
Romain Guy01d58e42011-01-19 21:54:02 -08003089
3090 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003091}
3092
Romain Guyf607bdc2010-09-10 19:20:06 -07003093// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003094#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3095#define kStdUnderline_Offset (1.0f / 9.0f)
3096#define kStdUnderline_Thickness (1.0f / 18.0f)
3097
Chris Craikd218a922014-01-02 17:13:34 -08003098void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3099 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003100 // Handle underline and strike-through
3101 uint32_t flags = paint->getFlags();
3102 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003103 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003104
Romain Guy211370f2012-02-01 16:10:55 -08003105 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003106 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003107 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003108
Raph Levien8b4072d2012-07-30 15:50:00 -07003109 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003110 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003111
Romain Guyf6834472011-01-23 13:32:12 -08003112 int linesCount = 0;
3113 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3114 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3115
3116 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003117 float points[pointsCount];
3118 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003119
3120 if (flags & SkPaint::kUnderlineText_Flag) {
3121 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003122 points[currentPoint++] = left;
3123 points[currentPoint++] = top;
3124 points[currentPoint++] = left + underlineWidth;
3125 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003126 }
3127
3128 if (flags & SkPaint::kStrikeThruText_Flag) {
3129 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003130 points[currentPoint++] = left;
3131 points[currentPoint++] = top;
3132 points[currentPoint++] = left + underlineWidth;
3133 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003134 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003135
Romain Guy726aeba2011-06-01 14:52:00 -07003136 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003137
Romain Guy726aeba2011-06-01 14:52:00 -07003138 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003139 }
3140 }
3141}
3142
Tom Hudson107843d2014-09-08 11:26:26 -04003143void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003144 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003145 return;
Romain Guy672433d2013-01-04 19:05:13 -08003146 }
3147
Tom Hudson107843d2014-09-08 11:26:26 -04003148 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003149}
3150
Tom Hudson107843d2014-09-08 11:26:26 -04003151void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003152 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003153 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003154
Chris Craik15a07a22014-01-26 13:43:53 -08003155 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003156 mCaches.enableScissor();
3157
3158 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003159 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003160
ztenghui14a4e352014-08-13 10:44:39 -07003161 // The caller has made sure casterAlpha > 0.
3162 float ambientShadowAlpha = mAmbientShadowAlpha;
3163 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3164 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3165 }
3166 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3167 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003168 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003169 }
ztenghui7b4516e2014-01-07 10:42:55 -08003170
ztenghui14a4e352014-08-13 10:44:39 -07003171 float spotShadowAlpha = mSpotShadowAlpha;
3172 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3173 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3174 }
3175 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3176 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003177 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003178 }
ztenghui7b4516e2014-01-07 10:42:55 -08003179
Tom Hudson107843d2014-09-08 11:26:26 -04003180 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003181}
3182
Tom Hudson107843d2014-09-08 11:26:26 -04003183void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003184 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003185 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003186 return;
Romain Guy3b753822013-03-05 10:27:35 -08003187 }
Romain Guy735738c2012-12-03 12:34:51 -08003188
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003189 int color = paint->getColor();
3190 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003191 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003192 color |= 0x00ffffff;
3193 }
3194
Romain Guy672433d2013-01-04 19:05:13 -08003195 float left = FLT_MAX;
3196 float top = FLT_MAX;
3197 float right = FLT_MIN;
3198 float bottom = FLT_MIN;
3199
Romain Guy448455f2013-07-22 13:57:50 -07003200 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003201 Vertex* vertex = mesh;
3202
Chris Craik2af46352012-11-26 18:30:17 -08003203 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003204 float l = rects[index + 0];
3205 float t = rects[index + 1];
3206 float r = rects[index + 2];
3207 float b = rects[index + 3];
3208
Romain Guy3b753822013-03-05 10:27:35 -08003209 Vertex::set(vertex++, l, t);
3210 Vertex::set(vertex++, r, t);
3211 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003212 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003213
Romain Guy3b753822013-03-05 10:27:35 -08003214 left = fminf(left, l);
3215 top = fminf(top, t);
3216 right = fmaxf(right, r);
3217 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003218 }
3219
Chris Craikf0a59072013-11-19 18:00:46 -08003220 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003221 return;
Romain Guya362c692013-02-04 13:50:16 -08003222 }
Romain Guy672433d2013-01-04 19:05:13 -08003223
Romain Guy672433d2013-01-04 19:05:13 -08003224 setupDraw();
3225 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003226 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003227 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003228 setupDrawColorFilter(getColorFilter(paint));
3229 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003230 setupDrawProgram();
3231 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003232 setupDrawModelView(kModelViewMode_Translate, false,
3233 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003234 setupDrawColorUniforms(getShader(paint));
3235 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003236 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003237
Romain Guy8ce00302013-01-15 18:51:42 -08003238 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003239 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003240 }
3241
Chris Craik4063a0e2013-11-15 16:06:56 -08003242 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003243
Tom Hudson107843d2014-09-08 11:26:26 -04003244 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003245}
3246
Romain Guy026c5e162010-06-28 17:12:22 -07003247void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003248 const SkPaint* paint, bool ignoreTransform) {
3249 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003250 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003251 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003252 color |= 0x00ffffff;
3253 }
3254
Romain Guy70ca14e2010-12-13 18:24:33 -08003255 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003256 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003257 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003258 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003259 setupDrawColorFilter(getColorFilter(paint));
3260 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003261 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003262 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3263 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 setupDrawColorUniforms(getShader(paint));
3265 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003266 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003268
Romain Guyc95c8d62010-09-17 15:31:32 -07003269 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3270}
3271
Romain Guy82ba8142010-07-09 13:25:56 -07003272void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003273 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003274 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003275
Chris Craike84a2082014-12-22 14:28:49 -08003276 GLvoid* vertices = (GLvoid*) nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -07003277 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3278
3279 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003280 vertices = &mMeshVertices[0].x;
3281 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003282
3283 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3284 texture->uvMapper->map(uvs);
3285
3286 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3287 }
3288
Chris Craikd6b65f62014-01-01 14:45:21 -08003289 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3290 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3291 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003292
Romain Guyd21b6e12011-11-30 20:21:23 -08003293 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003294 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003295 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003296 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003297 } else {
Chris Craik678625242014-02-28 12:26:34 -08003298 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003299 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003300 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3301 }
3302
3303 if (texture->uvMapper) {
3304 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003305 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003306}
3307
Romain Guyf7f93552010-07-08 19:17:03 -07003308void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003310 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003311 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3312 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003313
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003314 int a;
3315 SkXfermode::Mode mode;
3316 getAlphaAndMode(paint, &a, &mode);
3317 const float alpha = a / 255.0f;
3318
Romain Guy746b7402010-10-26 16:27:31 -07003319 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003320 setupDrawWithTexture();
3321 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003322 setupDrawColorFilter(getColorFilter(paint));
3323 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003324 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003325 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003326 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003327 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003328 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003329 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003331
Romain Guy6820ac82010-09-15 18:11:50 -07003332 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003333}
3334
Romain Guy3b748a42013-04-17 18:54:38 -07003335void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003336 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003337 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003338 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3339 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003340
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003341 int a;
3342 SkXfermode::Mode mode;
3343 getAlphaAndMode(paint, &a, &mode);
3344 const float alpha = a / 255.0f;
3345
Romain Guy3b748a42013-04-17 18:54:38 -07003346 setupDraw();
3347 setupDrawWithTexture();
3348 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003349 setupDrawColorFilter(getColorFilter(paint));
3350 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003351 setupDrawProgram();
3352 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003353 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003354 setupDrawTexture(texture);
3355 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003356 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003357 setupDrawMeshIndices(vertices, texCoords, vbo);
3358
Chris Craike84a2082014-12-22 14:28:49 -08003359 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, nullptr);
Romain Guy3b748a42013-04-17 18:54:38 -07003360}
3361
Romain Guy886b2752013-01-04 12:26:18 -08003362void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003363 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003364 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003365 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003366
Chris Craike84a2082014-12-22 14:28:49 -08003367 int color = paint != nullptr ? paint->getColor() : 0;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003368 int alpha;
3369 SkXfermode::Mode mode;
3370 getAlphaAndMode(paint, &alpha, &mode);
3371
Romain Guy886b2752013-01-04 12:26:18 -08003372 setupDraw();
3373 setupDrawWithTexture(true);
Chris Craike84a2082014-12-22 14:28:49 -08003374 if (paint != nullptr) {
Romain Guy886b2752013-01-04 12:26:18 -08003375 setupDrawAlpha8Color(color, alpha);
3376 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003377 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003378 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003379 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003380 setupDrawProgram();
3381 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003382 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003383 setupDrawTexture(texture);
3384 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003385 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003386 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003387 setupDrawMesh(vertices, texCoords);
3388
3389 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003390}
3391
Romain Guya5aed0d2010-09-09 14:42:43 -07003392void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003393 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003394
Chris Craike84a2082014-12-22 14:28:49 -08003395 if (writableSnapshot()->roundRectClipState != nullptr /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003396 blend = true;
3397 mDescription.hasRoundRectClip = true;
3398 }
3399 mSkipOutlineClip = true;
3400
Romain Guy82ba8142010-07-09 13:25:56 -07003401 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003402
Romain Guy82ba8142010-07-09 13:25:56 -07003403 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003404 // These blend modes are not supported by OpenGL directly and have
3405 // to be implemented using shaders. Since the shader will perform
3406 // the blending, turn blending off here
3407 // If the blend mode cannot be implemented using shaders, fall
3408 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003409 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003410 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003411 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003412 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003413
Romain Guy82bc7a72012-01-03 14:13:39 -08003414 if (mCaches.blend) {
3415 glDisable(GL_BLEND);
3416 mCaches.blend = false;
3417 }
3418
3419 return;
3420 } else {
3421 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003422 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003423 }
3424
3425 if (!mCaches.blend) {
3426 glEnable(GL_BLEND);
3427 }
3428
3429 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3430 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3431
3432 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3433 glBlendFunc(sourceMode, destMode);
3434 mCaches.lastSrcMode = sourceMode;
3435 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003436 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003437 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003438 glDisable(GL_BLEND);
3439 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003440 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003441}
3442
Romain Guy889f8d12010-07-29 14:37:42 -07003443bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003444 if (!program->isInUse()) {
Chris Craike84a2082014-12-22 14:28:49 -08003445 if (mCaches.currentProgram != nullptr) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003446 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003447 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003448 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003449 }
Romain Guy6926c722010-07-12 20:20:03 -07003450 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003451}
3452
Romain Guy026c5e162010-06-28 17:12:22 -07003453void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003454 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003455 TextureVertex::setUV(v++, u1, v1);
3456 TextureVertex::setUV(v++, u2, v1);
3457 TextureVertex::setUV(v++, u1, v2);
3458 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003459}
3460
Chris Craike84a2082014-12-22 14:28:49 -08003461void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha,
3462 SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003463 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003464 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3465 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003466 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003467 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003468 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003469}
3470
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003471float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003472 float alpha;
3473 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3474 alpha = mDrawModifiers.mOverrideLayerAlpha;
3475 } else {
3476 alpha = layer->getAlpha() / 255.0f;
3477 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003478 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003479}
3480
Romain Guy9d5316e2010-06-24 19:30:36 -07003481}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003482}; // namespace android