blob: b96bc0d16f0c22aebc77bd6958e48d60caf17f3f [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();
316
Romain Guyca89e2a2013-03-08 17:44:20 -0800317 // When finish() is invoked on FBO 0 we've reached the end
318 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400319 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800320 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700321 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800322 }
323
Romain Guy11cb6422012-09-21 00:39:43 -0700324 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700325#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700326 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700327#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700328
Romain Guyc15008e2010-11-10 11:59:15 -0800329#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800330 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700331#else
332 if (mCaches.getDebugLevel() & kDebugMemory) {
333 mCaches.dumpMemoryUsage();
334 }
Romain Guyc15008e2010-11-10 11:59:15 -0800335#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700336 }
Romain Guy96885eb2013-03-26 15:05:58 -0700337
338 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400339
340 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700341}
342
Romain Guy35643dd2012-09-18 15:40:58 -0700343void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700344 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
345 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700346 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700347
348 mCaches.resetScissor();
349 dirtyClip();
350}
351
Andreas Gampe64bb4132014-11-22 00:35:09 +0000352void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400353 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700354
Tom Hudson984162f2014-10-10 13:38:16 -0400355 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700356 clip.snapToPixelBoundaries();
357
Romain Guyd643bb52011-03-01 14:55:21 -0800358 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800359 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800360 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800361 dirtyLayerUnchecked(clip, getRegion());
362 }
Romain Guyd643bb52011-03-01 14:55:21 -0800363
Romain Guy08aa2cb2011-03-17 11:06:57 -0700364 DrawGlInfo info;
365 info.clipLeft = clip.left;
366 info.clipTop = clip.top;
367 info.clipRight = clip.right;
368 info.clipBottom = clip.bottom;
369 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700370 info.width = getViewportWidth();
371 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800372 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700373
Tom Hudson984162f2014-10-10 13:38:16 -0400374 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700375 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400376 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700377 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
378 }
John Reck3b202512014-06-23 13:13:08 -0700379 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700380 setScissorFromClip();
381 }
Chris Craik54f574a2013-08-26 11:23:46 -0700382
John Reck3b202512014-06-23 13:13:08 -0700383 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
384 // Scissor may have been modified, reset dirty clip
385 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800386
Tom Hudson107843d2014-09-08 11:26:26 -0400387 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800388}
389
Romain Guyf6a11b82010-06-23 17:47:49 -0700390///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700391// Debug
392///////////////////////////////////////////////////////////////////////////////
393
Chris Craik62d307c2014-07-29 10:35:13 -0700394void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
395#if DEBUG_DETAILED_EVENTS
396 const int BUFFER_SIZE = 256;
397 va_list ap;
398 char buf[BUFFER_SIZE];
399
400 va_start(ap, fmt);
401 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
402 va_end(ap);
403
404 eventMark(buf);
405#endif
406}
407
408
Romain Guy0f6675332013-03-01 14:31:04 -0800409void OpenGLRenderer::eventMark(const char* name) const {
410 mCaches.eventMark(0, name);
411}
412
Romain Guy87e2f7572012-09-24 11:37:12 -0700413void OpenGLRenderer::startMark(const char* name) const {
414 mCaches.startMark(0, name);
415}
416
417void OpenGLRenderer::endMark() const {
418 mCaches.endMark();
419}
420
421void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700422 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700423}
424
425void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400426 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700427 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700428
429 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400430 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700431 clip->right - clip->left, clip->bottom - clip->top);
432
Romain Guy627c6fd2013-08-21 11:53:18 -0700433 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700434 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700435 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
436
437 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700438 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700439 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
440
441 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700442 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700443 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
444
445 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700446 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700447 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
448
Romain Guy87e2f7572012-09-24 11:37:12 -0700449 mCaches.stencil.disable();
450 }
451}
452
453///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700454// Layers
455///////////////////////////////////////////////////////////////////////////////
456
457bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700458 if (layer->deferredUpdateScheduled && layer->renderer
459 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700460
Romain Guy7c450aa2012-09-21 19:15:00 -0700461 if (inFrame) {
462 endTiling();
463 debugOverdraw(false, false);
464 }
Romain Guy11cb6422012-09-21 00:39:43 -0700465
Romain Guy96885eb2013-03-26 15:05:58 -0700466 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700467 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700468 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700469 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700470 }
Romain Guy11cb6422012-09-21 00:39:43 -0700471
472 if (inFrame) {
473 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800474 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700475 }
476
Romain Guy5bb3c732012-11-29 17:52:58 -0800477 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700478 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700479
480 return true;
481 }
482
483 return false;
484}
485
486void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700487 // If draw deferring is enabled this method will simply defer
488 // the display list of each individual layer. The layers remain
489 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700490 int count = mLayerUpdates.size();
491 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700492 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
493 startMark("Layer Updates");
494 } else {
495 startMark("Defer Layer Updates");
496 }
Romain Guy11cb6422012-09-21 00:39:43 -0700497
Chris Craik1206b9b2013-04-04 14:46:24 -0700498 // Note: it is very important to update the layers in order
499 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700500 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700501 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700502 }
Romain Guy11cb6422012-09-21 00:39:43 -0700503
Romain Guy96885eb2013-03-26 15:05:58 -0700504 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
505 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400506 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700507 }
508 endMark();
509 }
510}
511
512void OpenGLRenderer::flushLayers() {
513 int count = mLayerUpdates.size();
514 if (count > 0) {
515 startMark("Apply Layer Updates");
Romain Guy96885eb2013-03-26 15:05:58 -0700516
Chris Craik1206b9b2013-04-04 14:46:24 -0700517 // Note: it is very important to update the layers in order
518 for (int i = 0; i < count; i++) {
Chris Craik70850ea2014-11-18 10:49:23 -0800519 mLayerUpdates.itemAt(i)->flush();
Romain Guy96885eb2013-03-26 15:05:58 -0700520 }
521
522 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400523 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700524
Romain Guy11cb6422012-09-21 00:39:43 -0700525 endMark();
526 }
527}
528
529void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
530 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700531 // Make sure we don't introduce duplicates.
532 // SortedVector would do this automatically but we need to respect
533 // the insertion order. The linear search is not an issue since
534 // this list is usually very short (typically one item, at most a few)
535 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
536 if (mLayerUpdates.itemAt(i) == layer) {
537 return;
538 }
539 }
Romain Guy11cb6422012-09-21 00:39:43 -0700540 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700541 }
542}
543
Romain Guye93482f2013-06-17 13:14:51 -0700544void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
545 if (layer) {
546 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
547 if (mLayerUpdates.itemAt(i) == layer) {
548 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700549 break;
550 }
551 }
552 }
553}
554
Romain Guy40543602013-06-12 15:31:28 -0700555void OpenGLRenderer::flushLayerUpdates() {
Chris Craik70850ea2014-11-18 10:49:23 -0800556 ATRACE_NAME("Update HW Layers");
Romain Guy40543602013-06-12 15:31:28 -0700557 syncState();
558 updateLayers();
559 flushLayers();
560 // Wait for all the layer updates to be executed
561 AutoFence fence;
562}
563
John Reck443a7142014-09-04 17:40:05 -0700564void OpenGLRenderer::markLayersAsBuildLayers() {
565 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
566 mLayerUpdates[i]->wasBuildLayered = true;
567 }
568}
569
Romain Guy11cb6422012-09-21 00:39:43 -0700570///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700571// State management
572///////////////////////////////////////////////////////////////////////////////
573
Chris Craik14e51302013-12-30 15:32:54 -0800574void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700575 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800576 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
577 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700578
Chris Craika64a2be2014-05-14 14:17:01 -0700579 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700580 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700581 }
582
Romain Guy2542d192010-08-18 11:47:12 -0700583 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700584 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700585 }
Romain Guy2542d192010-08-18 11:47:12 -0700586
Romain Guy5ec99242010-11-03 16:19:08 -0700587 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700588 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700589 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700590 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800591 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700592 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700593 }
Romain Guybb9524b2010-06-22 18:56:38 -0700594}
595
Romain Guyf6a11b82010-06-23 17:47:49 -0700596///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700597// Layers
598///////////////////////////////////////////////////////////////////////////////
599
600int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700601 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700602 // force matrix/clip isolation for layer
603 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
604
Tom Hudson984162f2014-10-10 13:38:16 -0400605 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700606
Tom Hudson984162f2014-10-10 13:38:16 -0400607 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700608 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700609 }
Romain Guyd55a8612010-06-28 17:42:46 -0700610
611 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700612}
613
Chris Craikd90144d2013-03-19 15:03:48 -0700614void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
615 const Rect untransformedBounds(bounds);
616
Chris Craikd6b65f62014-01-01 14:45:21 -0800617 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700618
619 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400620 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700621 // We cannot work with sub-pixels in this case
622 bounds.snapToPixelBoundaries();
623
624 // When the layer is not an FBO, we may use glCopyTexImage so we
625 // need to make sure the layer does not extend outside the bounds
626 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700627 const Snapshot& previous = *(currentSnapshot()->previous);
628 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
629 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700630 bounds.setEmpty();
631 } else if (fboLayer) {
632 clip.set(bounds);
633 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800634 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700635 inverse.mapRect(clip);
636 clip.snapToPixelBoundaries();
637 if (clip.intersect(untransformedBounds)) {
638 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
639 bounds.set(untransformedBounds);
640 } else {
641 clip.setEmpty();
642 }
643 }
644 } else {
645 bounds.setEmpty();
646 }
647}
648
Chris Craik408eb122013-03-26 18:55:15 -0700649void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
650 bool fboLayer, int alpha) {
651 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
652 bounds.getHeight() > mCaches.maxTextureSize ||
653 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400654 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700655 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400656 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700657 }
658}
659
Chris Craikd90144d2013-03-19 15:03:48 -0700660int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500661 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400662 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700663
Tom Hudson984162f2014-10-10 13:38:16 -0400664 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700665 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
666 // operations will be able to store and restore the current clip and transform info, and
667 // quick rejection will be correct (for display lists)
668
669 Rect bounds(left, top, right, bottom);
670 Rect clip;
671 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500672 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700673
Tom Hudson984162f2014-10-10 13:38:16 -0400674 if (!mState.currentlyIgnored()) {
675 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
676 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
677 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
678 writableSnapshot()->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700679 }
680 }
681
682 return count;
683}
684
Romain Guy1c740bc2010-09-13 18:00:09 -0700685/**
686 * Layers are viewed by Skia are slightly different than layers in image editing
687 * programs (for instance.) When a layer is created, previously created layers
688 * and the frame buffer still receive every drawing command. For instance, if a
689 * layer is created and a shape intersecting the bounds of the layers and the
690 * framebuffer is draw, the shape will be drawn on both (unless the layer was
691 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
692 *
693 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
694 * texture. Unfortunately, this is inefficient as it requires every primitive to
695 * be drawn n + 1 times, where n is the number of active layers. In practice this
696 * means, for every primitive:
697 * - Switch active frame buffer
698 * - Change viewport, clip and projection matrix
699 * - Issue the drawing
700 *
701 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700702 * To avoid this, layers are implemented in a different way here, at least in the
703 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
704 * is set. When this flag is set we can redirect all drawing operations into a
705 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700706 *
707 * This implementation relies on the frame buffer being at least RGBA 8888. When
708 * a layer is created, only a texture is created, not an FBO. The content of the
709 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700710 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700711 * buffer and drawing continues as normal. This technique therefore treats the
712 * frame buffer as a scratch buffer for the layers.
713 *
714 * To compose the layers back onto the frame buffer, each layer texture
715 * (containing the original frame buffer data) is drawn as a simple quad over
716 * the frame buffer. The trick is that the quad is set as the composition
717 * destination in the blending equation, and the frame buffer becomes the source
718 * of the composition.
719 *
720 * Drawing layers with an alpha value requires an extra step before composition.
721 * An empty quad is drawn over the layer's region in the frame buffer. This quad
722 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
723 * quad is used to multiply the colors in the frame buffer. This is achieved by
724 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
725 * GL_ZERO, GL_SRC_ALPHA.
726 *
727 * Because glCopyTexImage2D() can be slow, an alternative implementation might
728 * be use to draw a single clipped layer. The implementation described above
729 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700730 *
731 * (1) The frame buffer is actually not cleared right away. To allow the GPU
732 * to potentially optimize series of calls to glCopyTexImage2D, the frame
733 * buffer is left untouched until the first drawing operation. Only when
734 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700735 */
Chet Haased48885a2012-08-28 17:43:28 -0700736bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700737 const SkPaint* paint, int flags, const SkPath* convexMask) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800738 if (kDebugLayers) {
739 ALOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
740 ALOGD("Layer cache size = %d", mCaches.layerCache.getSize());
741 }
Romain Guy8ba548f2010-06-30 19:21:21 -0700742
Romain Guyeb993562010-10-05 18:14:38 -0700743 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
744
Romain Guyf607bdc2010-09-10 19:20:06 -0700745 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700746 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700747 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700748 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000749 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700750
751 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400752 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700753 return false;
754 }
Romain Guyf18fd992010-07-08 11:45:51 -0700755
Romain Guya1d3c912011-12-13 14:55:06 -0800756 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700757 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700758 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700759 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700760 }
761
Derek Sollenberger674554f2014-02-19 16:47:32 +0000762 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700763 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700764 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
765 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500766
Chet Haasea23eed82012-04-12 15:19:04 -0700767 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700768 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700769 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700770
Romain Guy8fb95422010-08-17 18:38:51 -0700771 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400772 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
773 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700774
Chris Craika8bea8e2014-09-24 11:29:43 -0700775 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
776 fboLayer ? "" : "unclipped ",
777 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700778 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700779 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700780 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700781 } else {
782 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700783 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800784 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700785 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700786 // Workaround for some GL drivers. When reading pixels lying outside
787 // of the window we should get undefined values for those pixels.
788 // Unfortunately some drivers will turn the entire target texture black
789 // when reading outside of the window.
790 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
791 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700792 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800793 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700794
Chris Craika64a2be2014-05-14 14:17:01 -0700795 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
796 bounds.left, getViewportHeight() - bounds.bottom,
797 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700798
Romain Guy54be1cd2011-06-13 19:04:27 -0700799 // Enqueue the buffer coordinates to clear the corresponding region later
800 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700801 }
Romain Guyeb993562010-10-05 18:14:38 -0700802 }
Romain Guyf86ef572010-07-01 11:05:42 -0700803
Romain Guyd55a8612010-06-28 17:42:46 -0700804 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700805}
806
Chris Craike63f7c622013-10-17 10:30:55 -0700807bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800808 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700810
Tom Hudson984162f2014-10-10 13:38:16 -0400811 writableSnapshot()->region = &writableSnapshot()->layer->region;
812 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
813 writableSnapshot()->fbo = layer->getFbo();
814 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
815 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
816 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
817 writableSnapshot()->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700818
Romain Guy2b7028e2012-09-19 17:25:38 -0700819 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700820 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700821 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700822 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700824
825 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700826 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700827 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700828 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700829 }
830
831 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700832 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700833
henry.uh_chen33f5a592014-07-02 19:36:56 +0800834 // Expand the startTiling region by 1
835 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700836
837 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700838 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800839 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700840 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700841 glClear(GL_COLOR_BUFFER_BIT);
842
843 dirtyClip();
844
845 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700846 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847 return true;
848}
849
Romain Guy1c740bc2010-09-13 18:00:09 -0700850/**
851 * Read the documentation of createLayer() before doing anything in this method.
852 */
Chris Craik14e51302013-12-30 15:32:54 -0800853void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
854 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000855 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700856 return;
857 }
858
Chris Craik14e51302013-12-30 15:32:54 -0800859 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800860 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800861 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700862
Chris Craik39a908c2013-06-13 14:39:01 -0700863 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400864 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700865 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700866 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
867
Romain Guyeb993562010-10-05 18:14:38 -0700868 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700869 endTiling();
870
Romain Guye0aa84b2012-04-03 19:30:26 -0700871 // Detach the texture from the FBO
872 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800873
874 layer->removeFbo(false);
875
Romain Guyeb993562010-10-05 18:14:38 -0700876 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700877 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700878 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700879
Chris Craikd6b65f62014-01-01 14:45:21 -0800880 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700881 }
882
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500884 SkPaint layerPaint;
885 layerPaint.setAlpha(layer->getAlpha());
886 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
887 layerPaint.setColorFilter(layer->getColorFilter());
888
889 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700890 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700891 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700892 }
893
Romain Guy03750a02010-10-18 14:06:08 -0700894 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700895
Romain Guya1d3c912011-12-13 14:55:06 -0800896 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700897
Romain Guy5b3b3522010-10-27 18:57:51 -0700898 // When the layer is stored in an FBO, we can save a bit of fillrate by
899 // drawing only the dirty region
900 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800901 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700902 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 } else if (!rect.isEmpty()) {
904 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530905
906 save(0);
907 // the layer contains screen buffer content that shouldn't be alpha modulated
908 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400909 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700910 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530911 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700912 }
Romain Guy8b55f372010-08-18 17:10:07 -0700913
Romain Guy746b7402010-10-26 16:27:31 -0700914 dirtyClip();
915
Romain Guyeb993562010-10-05 18:14:38 -0700916 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700917 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700918 if (!mCaches.layerCache.put(layer)) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800919 if (kDebugLayers) {
920 ALOGD("Deleting layer");
921 }
John Reck0e89e2b2014-10-31 14:49:06 -0700922 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700923 }
924}
925
Romain Guyaa6c24c2011-04-28 18:40:04 -0700926void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700927 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700928
929 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700931 setupDrawWithTexture();
932 } else {
933 setupDrawWithExternalTexture();
934 }
935 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700936 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500937 setupDrawColorFilter(layer->getColorFilter());
938 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700939 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700940 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500941 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
943 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700944 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700946 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800947 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800948 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700949 layer->getWidth() == (uint32_t) rect.getWidth() &&
950 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800951 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
952 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700953
Romain Guyd21b6e12011-11-30 20:21:23 -0800954 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800955 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
956 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700957 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800958 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800959 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
960 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 }
962 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700963 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964
965 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700966}
967
Romain Guy5b3b3522010-10-27 18:57:51 -0700968void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700969 if (layer->isTextureLayer()) {
970 EVENT_LOGD("composeTextureLayerRect");
971 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
972 drawTextureLayer(layer, rect);
973 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
974 } else {
975 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700976 const Rect& texCoords = layer->texCoords;
977 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
978 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -0700979
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 float x = rect.left;
981 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -0800982 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700983 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -0700984 layer->getHeight() == (uint32_t) rect.getHeight();
985
986 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 // When we're swapping, the layer is already in screen coordinates
988 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800989 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
990 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 }
992
Romain Guyd21b6e12011-11-30 20:21:23 -0800993 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800995 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 }
997
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500998 SkPaint layerPaint;
999 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1000 layerPaint.setXfermodeMode(layer->getMode());
1001 layerPaint.setColorFilter(layer->getColorFilter());
1002
1003 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001004 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001005 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001006 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001007 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001008
Romain Guyaa6c24c2011-04-28 18:40:04 -07001009 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001010 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001011}
1012
Chris Craik34416ea2013-04-15 16:08:28 -07001013/**
1014 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1015 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1016 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1017 * by saveLayer's restore
1018 */
Tom Hudson984162f2014-10-10 13:38:16 -04001019#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1020 DRAW_COMMAND; \
1021 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1022 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1023 DRAW_COMMAND; \
1024 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1025 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001026 }
1027
1028#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1029
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001030// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1031// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1032class LayerShader : public SkShader {
1033public:
1034 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1035 : INHERITED(localMatrix)
1036 , mLayer(layer) {
1037 }
1038
1039 virtual bool asACustomShader(void** data) const {
1040 if (data) {
1041 *data = static_cast<void*>(mLayer);
1042 }
1043 return true;
1044 }
1045
1046 virtual bool isOpaque() const {
1047 return !mLayer->isBlend();
1048 }
1049
1050protected:
Andreas Gampe64bb4132014-11-22 00:35:09 +00001051 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001052 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1053 }
1054
1055 virtual void flatten(SkWriteBuffer&) const {
1056 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1057 }
1058
1059 virtual Factory getFactory() const {
1060 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1061 return NULL;
1062 }
1063private:
1064 // Unowned.
1065 Layer* mLayer;
1066 typedef SkShader INHERITED;
1067};
1068
Romain Guy5b3b3522010-10-27 18:57:51 -07001069void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001070 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1071
1072 if (layer->getConvexMask()) {
1073 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1074
1075 // clip to the area of the layer the mask can be larger
1076 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1077
1078 SkPaint paint;
1079 paint.setAntiAlias(true);
1080 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1081
Chris Craik3f0854292014-04-15 16:18:08 -07001082 // create LayerShader to map SaveLayer content into subsequent draw
1083 SkMatrix shaderMatrix;
1084 shaderMatrix.setTranslate(rect.left, rect.bottom);
1085 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001086 LayerShader layerShader(layer, &shaderMatrix);
1087 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001088
1089 // Since the drawing primitive is defined in local drawing space,
1090 // we don't need to modify the draw matrix
1091 const SkPath* maskPath = layer->getConvexMask();
1092 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1093
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001094 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001095 restore();
1096
1097 return;
1098 }
1099
Romain Guy5b3b3522010-10-27 18:57:51 -07001100 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001101 layer->setRegionAsRect();
1102
Chris Craik34416ea2013-04-15 16:08:28 -07001103 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001104
Romain Guy5b3b3522010-10-27 18:57:51 -07001105 layer->region.clear();
1106 return;
1107 }
1108
Chris Craik62d307c2014-07-29 10:35:13 -07001109 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001110 // standard Region based draw
1111 size_t count;
1112 const android::Rect* rects;
1113 Region safeRegion;
1114 if (CC_LIKELY(hasRectToRectTransform())) {
1115 rects = layer->region.getArray(&count);
1116 } else {
1117 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1118 rects = safeRegion.getArray(&count);
1119 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001120
Chris Craik3f0854292014-04-15 16:18:08 -07001121 const float alpha = getLayerAlpha(layer);
1122 const float texX = 1.0f / float(layer->getWidth());
1123 const float texY = 1.0f / float(layer->getHeight());
1124 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001125
Chris Craik3f0854292014-04-15 16:18:08 -07001126 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001127
Chris Craik3f0854292014-04-15 16:18:08 -07001128 // We must get (and therefore bind) the region mesh buffer
1129 // after we setup drawing in case we need to mess with the
1130 // stencil buffer in setupDraw()
1131 TextureVertex* mesh = mCaches.getRegionMesh();
1132 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001133
Chris Craik3f0854292014-04-15 16:18:08 -07001134 setupDrawWithTexture();
1135 setupDrawColor(alpha, alpha, alpha, alpha);
1136 setupDrawColorFilter(layer->getColorFilter());
1137 setupDrawBlending(layer);
1138 setupDrawProgram();
1139 setupDrawDirtyRegionsDisabled();
1140 setupDrawPureColorUniforms();
1141 setupDrawColorFilterUniforms(layer->getColorFilter());
1142 setupDrawTexture(layer->getTexture());
1143 if (currentTransform()->isPureTranslate()) {
1144 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1145 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001146
Chris Craik3f0854292014-04-15 16:18:08 -07001147 layer->setFilter(GL_NEAREST);
1148 setupDrawModelView(kModelViewMode_Translate, false,
1149 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1150 } else {
1151 layer->setFilter(GL_LINEAR);
1152 setupDrawModelView(kModelViewMode_Translate, false,
1153 rect.left, rect.top, rect.right, rect.bottom);
1154 }
1155 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001156
Chris Craik3f0854292014-04-15 16:18:08 -07001157 for (size_t i = 0; i < count; i++) {
1158 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001159
Chris Craik3f0854292014-04-15 16:18:08 -07001160 const float u1 = r->left * texX;
1161 const float v1 = (height - r->top) * texY;
1162 const float u2 = r->right * texX;
1163 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001164
Chris Craik3f0854292014-04-15 16:18:08 -07001165 // TODO: Reject quads outside of the clip
1166 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1167 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1168 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1169 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001170
Chris Craik3f0854292014-04-15 16:18:08 -07001171 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001172
Chris Craik3f0854292014-04-15 16:18:08 -07001173 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001174 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1175 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001176 numQuads = 0;
1177 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001178 }
Chris Craik3f0854292014-04-15 16:18:08 -07001179 }
1180
1181 if (numQuads > 0) {
1182 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1183 GL_UNSIGNED_SHORT, NULL));
1184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185
Romain Guy5b3b3522010-10-27 18:57:51 -07001186#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001187 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001188#endif
1189
Chris Craik3f0854292014-04-15 16:18:08 -07001190 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001191}
1192
Romain Guy3a3133d2011-02-01 22:59:58 -08001193#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001194void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001195 size_t count;
1196 const android::Rect* rects = region.getArray(&count);
1197
1198 uint32_t colors[] = {
1199 0x7fff0000, 0x7f00ff00,
1200 0x7f0000ff, 0x7fff00ff,
1201 };
1202
1203 int offset = 0;
1204 int32_t top = rects[0].top;
1205
1206 for (size_t i = 0; i < count; i++) {
1207 if (top != rects[i].top) {
1208 offset ^= 0x2;
1209 top = rects[i].top;
1210 }
1211
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001212 SkPaint paint;
1213 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001214 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001215 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001216 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001217}
Chris Craike63f7c622013-10-17 10:30:55 -07001218#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001219
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001220void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001221 Vector<float> rects;
1222
1223 SkRegion::Iterator it(region);
1224 while (!it.done()) {
1225 const SkIRect& r = it.rect();
1226 rects.push(r.fLeft);
1227 rects.push(r.fTop);
1228 rects.push(r.fRight);
1229 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001230 it.next();
1231 }
1232
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001233 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001234}
1235
Romain Guy5b3b3522010-10-27 18:57:51 -07001236void OpenGLRenderer::dirtyLayer(const float left, const float top,
1237 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001238 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001239 Rect bounds(left, top, right, bottom);
1240 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001241 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001242 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001243}
1244
1245void OpenGLRenderer::dirtyLayer(const float left, const float top,
1246 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001247 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001248 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001249 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001250 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001251}
1252
1253void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001254 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001255 bounds.snapToPixelBoundaries();
1256 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1257 if (!dirty.isEmpty()) {
1258 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001259 }
1260 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001261}
1262
Chris Craik4063a0e2013-11-15 16:06:56 -08001263void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001264 GLsizei elementsCount = quadsCount * 6;
1265 while (elementsCount > 0) {
1266 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1267
Romain Guy3380cfd2013-08-15 16:57:57 -07001268 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001269 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1270
1271 elementsCount -= drawCount;
1272 // Though there are 4 vertices in a quad, we use 6 indices per
1273 // quad to draw with GL_TRIANGLES
1274 mesh += (drawCount / 6) * 4;
1275 }
1276}
1277
Romain Guy54be1cd2011-06-13 19:04:27 -07001278void OpenGLRenderer::clearLayerRegions() {
1279 const size_t count = mLayers.size();
1280 if (count == 0) return;
1281
Tom Hudson984162f2014-10-10 13:38:16 -04001282 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001283 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001284 // Doing several glScissor/glClear here can negatively impact
1285 // GPUs with a tiler architecture, instead we draw quads with
1286 // the Clear blending mode
1287
1288 // The list contains bounds that have already been clipped
1289 // against their initial clip rect, and the current clip
1290 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001291 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001292
Romain Guy448455f2013-07-22 13:57:50 -07001293 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001294 Vertex* vertex = mesh;
1295
1296 for (uint32_t i = 0; i < count; i++) {
1297 Rect* bounds = mLayers.itemAt(i);
1298
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 Vertex::set(vertex++, bounds->left, bounds->top);
1300 Vertex::set(vertex++, bounds->right, bounds->top);
1301 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001302 Vertex::set(vertex++, bounds->right, bounds->bottom);
1303
1304 delete bounds;
1305 }
Romain Guye67307c2013-02-11 18:01:20 -08001306 // We must clear the list of dirty rects before we
1307 // call setupDraw() to prevent stencil setup to do
1308 // the same thing again
1309 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001310
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001311 SkPaint clearPaint;
1312 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1313
Romain Guy54be1cd2011-06-13 19:04:27 -07001314 setupDraw(false);
1315 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001316 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001317 setupDrawProgram();
1318 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001319 setupDrawModelView(kModelViewMode_Translate, false,
1320 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001321
Chris Craik4063a0e2013-11-15 16:06:56 -08001322 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001323
1324 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001325 } else {
1326 for (uint32_t i = 0; i < count; i++) {
1327 delete mLayers.itemAt(i);
1328 }
Romain Guye67307c2013-02-11 18:01:20 -08001329 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001330 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001331}
1332
Romain Guybd6b79b2010-06-26 00:13:53 -07001333///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001334// State Deferral
1335///////////////////////////////////////////////////////////////////////////////
1336
Chris Craikff785832013-03-08 13:12:16 -08001337bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001338 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001339 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001340
Chris Craikff785832013-03-08 13:12:16 -08001341 if (stateDeferFlags & kStateDeferFlag_Draw) {
1342 // state has bounds initialized in local coordinates
1343 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001344 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001345 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001346 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1347 // is used, it should more closely duplicate the quickReject logic (in how it uses
1348 // snapToPixelBoundaries)
1349
Chris Craikd6b65f62014-01-01 14:45:21 -08001350 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001351 // quick rejected
1352 return true;
1353 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001354
Chris Craika02c4ed2013-06-14 13:43:58 -07001355 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001356 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001357 int& flags = state.mClipSideFlags;
1358 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001359 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1360 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1361 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1362 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001363 }
1364 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001365 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001366 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1367 // overdraw avoidance (since we don't know what it overlaps)
1368 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001369 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001370 }
1371 }
1372
Chris Craik527a3aa2013-03-04 10:19:31 -08001373 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1374 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001376 }
1377
Chris Craik7273daa2013-03-28 11:25:24 -07001378 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1379 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001380 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001381 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001382 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001383
1384 // always store/restore, since it's just a pointer
1385 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001386 return false;
1387}
1388
Chris Craik527a3aa2013-03-04 10:19:31 -08001389void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001390 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001391 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001392 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001393 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001394
Chris Craik527a3aa2013-03-04 10:19:31 -08001395 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001396 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001397 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001398 dirtyClip();
1399 }
Chris Craikc3566d02013-02-04 16:16:33 -08001400}
1401
Chris Craik28ce94a2013-05-31 11:38:03 -07001402/**
1403 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1404 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1405 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1406 *
1407 * This method should be called when restoreDisplayState() won't be restoring the clip
1408 */
1409void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1410 if (clipRect != NULL) {
Tom Hudson984162f2014-10-10 13:38:16 -04001411 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001412 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001413 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001414 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001415 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001416 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001417}
1418
Chris Craikc3566d02013-02-04 16:16:33 -08001419///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001420// Clipping
1421///////////////////////////////////////////////////////////////////////////////
1422
Romain Guybb9524b2010-06-22 18:56:38 -07001423void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001424 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001425 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001426
Chris Craika64a2be2014-05-14 14:17:01 -07001427 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001428 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001429 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001430 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001431}
1432
Romain Guy8ce00302013-01-15 18:51:42 -08001433void OpenGLRenderer::ensureStencilBuffer() {
1434 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1435 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1436 // just hope we have one when hasLayer() returns false.
1437 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001438 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001439 }
1440}
1441
1442void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1443 // The layer's FBO is already bound when we reach this stage
1444 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001445 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1446 // is attached after we initiated tiling. We must turn it off,
1447 // attach the new render buffer then turn tiling back on
1448 endTiling();
1449
Romain Guy8d4aeb72013-02-12 16:08:55 -08001450 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001451 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001452 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001453
Romain Guyf735c8e2013-01-31 17:45:55 -08001454 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001455 }
1456}
1457
1458void OpenGLRenderer::setStencilFromClip() {
1459 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001460 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001461 EVENT_LOGD("setStencilFromClip - enabling");
1462
Romain Guy8ce00302013-01-15 18:51:42 -08001463 // NOTE: The order here is important, we must set dirtyClip to false
1464 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001465 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001466
1467 ensureStencilBuffer();
1468
1469 mCaches.stencil.enableWrite();
1470
Chris Craikdeeda3d2014-05-05 19:09:33 -07001471 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001472 // to the region's bounds
1473 bool resetScissor = mCaches.enableScissor();
1474 if (resetScissor) {
1475 // The scissor was not set so we now need to update it
1476 setScissorFromClip();
1477 }
1478 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001479
1480 // stash and disable the outline clip state, since stencil doesn't account for outline
1481 bool storedSkipOutlineClip = mSkipOutlineClip;
1482 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001483
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001484 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001485 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001486 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1487
Romain Guy8ce00302013-01-15 18:51:42 -08001488 // NOTE: We could use the region contour path to generate a smaller mesh
1489 // Since we are using the stencil we could use the red book path
1490 // drawing technique. It might increase bandwidth usage though.
1491
1492 // The last parameter is important: we are not drawing in the color buffer
1493 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001494 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001495 if (resetScissor) mCaches.disableScissor();
1496 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001497
1498 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001499
1500 // Draw the region used to generate the stencil if the appropriate debug
1501 // mode is enabled
1502 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001503 paint.setColor(0x7f0000ff);
1504 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1505 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001506 }
Romain Guy8ce00302013-01-15 18:51:42 -08001507 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001508 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001509 mCaches.stencil.disable();
1510 }
1511 }
1512}
1513
Chris Craikf0a59072013-11-19 18:00:46 -08001514/**
1515 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1516 *
1517 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1518 * style, and tessellated AA ramp
1519 */
1520bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001521 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001522 bool snapOut = paint && paint->isAntiAlias();
1523
1524 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1525 float outset = paint->getStrokeWidth() * 0.5f;
1526 left -= outset;
1527 top -= outset;
1528 right += outset;
1529 bottom += outset;
1530 }
1531
Chris Craikdeeda3d2014-05-05 19:09:33 -07001532 bool clipRequired = false;
1533 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001534 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001535 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001536 return true;
1537 }
1538
Chris Craikf23b25a2014-06-26 15:46:20 -07001539 // not quick rejected, so enable the scissor if clipRequired
1540 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1541 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001542 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001543}
1544
Romain Guy8ce00302013-01-15 18:51:42 -08001545void OpenGLRenderer::debugClip() {
1546#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001547 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001548 SkPaint paint;
1549 paint.setColor(0x7f00ff00);
1550 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1551
Romain Guy8ce00302013-01-15 18:51:42 -08001552 }
1553#endif
1554}
1555
Romain Guyf6a11b82010-06-23 17:47:49 -07001556///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001557// Drawing commands
1558///////////////////////////////////////////////////////////////////////////////
1559
Chris Craik62d307c2014-07-29 10:35:13 -07001560void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001561 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001562 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001563 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001564 // Make sure setScissor & setStencil happen at the beginning of
1565 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001566 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001567 if (mCaches.scissorEnabled) {
1568 setScissorFromClip();
1569 }
Chris Craik62d307c2014-07-29 10:35:13 -07001570
Dohyun Leeadc0d9d2014-11-24 21:08:15 +09001571 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001572 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001573
Romain Guy70ca14e2010-12-13 18:24:33 -08001574 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001575
Romain Guy70ca14e2010-12-13 18:24:33 -08001576 mSetShaderColor = false;
1577 mColorSet = false;
1578 mColorA = mColorR = mColorG = mColorB = 0.0f;
1579 mTextureUnit = 0;
1580 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001581
1582 // Enable debug highlight when what we're about to draw is tested against
1583 // the stencil buffer and if stencil highlight debugging is on
1584 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1585 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1586 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001587}
1588
1589void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1590 mDescription.hasTexture = true;
1591 mDescription.hasAlpha8Texture = isAlpha8;
1592}
1593
Romain Guyff316ec2013-02-13 18:39:43 -08001594void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1595 mDescription.hasTexture = true;
1596 mDescription.hasColors = true;
1597 mDescription.hasAlpha8Texture = isAlpha8;
1598}
1599
Romain Guyaa6c24c2011-04-28 18:40:04 -07001600void OpenGLRenderer::setupDrawWithExternalTexture() {
1601 mDescription.hasExternalTexture = true;
1602}
1603
Romain Guy15bc6432011-12-13 13:11:32 -08001604void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001605 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001606}
1607
Chris Craik91a8c7c2014-08-12 14:31:35 -07001608void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1609 mDescription.hasVertexAlpha = true;
1610 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001611}
1612
Romain Guy8d0d4782010-12-14 20:13:35 -08001613void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1614 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001615 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1616 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1617 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001618 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001619 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001620}
1621
Romain Guy86568192010-12-14 15:55:39 -08001622void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1623 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001624 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1625 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1626 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001627 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001628 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001629}
1630
Romain Guy41210632012-07-16 17:04:24 -07001631void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1632 mCaches.fontRenderer->describe(mDescription, paint);
1633}
1634
Romain Guy70ca14e2010-12-13 18:24:33 -08001635void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1636 mColorA = a;
1637 mColorR = r;
1638 mColorG = g;
1639 mColorB = b;
1640 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001641 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001642}
1643
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001644void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1645 if (shader != NULL) {
1646 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001647 }
1648}
1649
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001650void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1651 if (filter == NULL) {
1652 return;
1653 }
1654
1655 SkXfermode::Mode mode;
1656 if (filter->asColorMode(NULL, &mode)) {
1657 mDescription.colorOp = ProgramDescription::kColorBlend;
1658 mDescription.colorMode = mode;
1659 } else if (filter->asColorMatrix(NULL)) {
1660 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001661 }
1662}
1663
Romain Guyf09ef512011-05-27 11:43:46 -07001664void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1665 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1666 mColorA = 1.0f;
1667 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001668 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001669 }
1670}
1671
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001672void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1673 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001674 // When the blending mode is kClear_Mode, we need to use a modulate color
1675 // argb=1,0,0,0
1676 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001677 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001678 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001679 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001680 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001681}
1682
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001683void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1684 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001685 // When the blending mode is kClear_Mode, we need to use a modulate color
1686 // argb=1,0,0,0
1687 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001688 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001689 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1690 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001691 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001692}
1693
1694void OpenGLRenderer::setupDrawProgram() {
1695 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001696 if (mDescription.hasRoundRectClip) {
1697 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001698 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001699 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001700 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001701 innerRect.left, innerRect.top,
1702 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001703 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1704 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001705
1706 // add half pixel to round out integer rect space to cover pixel centers
1707 float roundedOutRadius = state->radius + 0.5f;
1708 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1709 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001710 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001711}
1712
1713void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1714 mTrackDirtyRegions = false;
1715}
1716
Chris Craik4063a0e2013-11-15 16:06:56 -08001717void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1718 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001719 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001720 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001721 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001722 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001723
Romain Guy86568192010-12-14 15:55:39 -08001724 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001725 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Tom Hudson984162f2014-10-10 13:38:16 -04001726 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001727 if (dirty && mTrackDirtyRegions) {
1728 if (!ignoreTransform) {
1729 dirtyLayer(left, top, right, bottom, *currentTransform());
1730 } else {
1731 dirtyLayer(left, top, right, bottom);
1732 }
Romain Guy86568192010-12-14 15:55:39 -08001733 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001734}
1735
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001736void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1737 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001738 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1739 }
1740}
1741
Romain Guy86568192010-12-14 15:55:39 -08001742void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001743 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001744 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001745 }
1746}
1747
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001748void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1749 if (shader == NULL) {
1750 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001752
1753 if (ignoreTransform) {
1754 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1755 // because it was built into modelView / the geometry, and the description needs to
1756 // compensate.
1757 mat4 modelViewWithoutTransform;
1758 modelViewWithoutTransform.loadInverse(*currentTransform());
1759 modelViewWithoutTransform.multiply(mModelViewMatrix);
1760 mModelViewMatrix.load(modelViewWithoutTransform);
1761 }
1762
1763 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001764}
1765
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001766void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1767 if (NULL == filter) {
1768 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001769 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001770
1771 SkColor color;
1772 SkXfermode::Mode mode;
1773 if (filter->asColorMode(&color, &mode)) {
1774 const int alpha = SkColorGetA(color);
1775 const GLfloat a = alpha / 255.0f;
1776 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1777 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1778 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1779 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1780 return;
1781 }
1782
1783 SkScalar srcColorMatrix[20];
1784 if (filter->asColorMatrix(srcColorMatrix)) {
1785
1786 float colorMatrix[16];
1787 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1788 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1789 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1790 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1791
1792 // Skia uses the range [0..255] for the addition vector, but we need
1793 // the [0..1] range to apply the vector in GLSL
1794 float colorVector[4];
1795 colorVector[0] = srcColorMatrix[4] / 255.0f;
1796 colorVector[1] = srcColorMatrix[9] / 255.0f;
1797 colorVector[2] = srcColorMatrix[14] / 255.0f;
1798 colorVector[3] = srcColorMatrix[19] / 255.0f;
1799
1800 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1801 GL_FALSE, colorMatrix);
1802 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1803 return;
1804 }
1805
1806 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001807}
1808
Romain Guy41210632012-07-16 17:04:24 -07001809void OpenGLRenderer::setupDrawTextGammaUniforms() {
1810 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1811}
1812
Romain Guy70ca14e2010-12-13 18:24:33 -08001813void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001814 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001815 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001816 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001817}
1818
1819void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001820 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001821 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001822 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001823}
1824
Romain Guyaa6c24c2011-04-28 18:40:04 -07001825void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1826 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001827 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001828 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001829}
1830
Romain Guy8f0095c2011-05-02 17:24:22 -07001831void OpenGLRenderer::setupDrawTextureTransform() {
1832 mDescription.hasTextureTransform = true;
1833}
1834
1835void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001836 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1837 GL_FALSE, &transform.data[0]);
1838}
1839
Chris Craik564acf72014-01-02 16:46:18 -08001840void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1841 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001842 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001843 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001844 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001845 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001846 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001847 }
Romain Guyd71dd362011-12-12 19:03:35 -08001848
Chris Craikcb4d6002012-09-25 12:00:29 -07001849 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001850 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001851 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001852 }
1853
1854 mCaches.unbindIndicesBuffer();
1855}
1856
Chris Craik564acf72014-01-02 16:46:18 -08001857void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1858 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001859 bool force = mCaches.unbindMeshBuffer();
1860 GLsizei stride = sizeof(ColorTextureVertex);
1861
1862 mCaches.bindPositionVertexPointer(force, vertices, stride);
1863 if (mCaches.currentProgram->texCoords >= 0) {
1864 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1865 }
1866 int slot = mCaches.currentProgram->getAttrib("colors");
1867 if (slot >= 0) {
1868 glEnableVertexAttribArray(slot);
1869 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1870 }
1871
1872 mCaches.unbindIndicesBuffer();
1873}
1874
Chris Craik564acf72014-01-02 16:46:18 -08001875void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1876 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001877 bool force = false;
1878 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1879 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1880 // use the default VBO found in Caches
1881 if (!vertices || vbo) {
1882 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1883 } else {
1884 force = mCaches.unbindMeshBuffer();
1885 }
ztenghui63d41ab2014-02-14 13:13:41 -08001886 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001887
Chris Craikcb4d6002012-09-25 12:00:29 -07001888 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001889 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001890 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001891 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001892}
1893
Romain Guy448455f2013-07-22 13:57:50 -07001894void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001895 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001896 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001897 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001898}
1899
Romain Guy70ca14e2010-12-13 18:24:33 -08001900///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001901// Drawing
1902///////////////////////////////////////////////////////////////////////////////
1903
Tom Hudson107843d2014-09-08 11:26:26 -04001904void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001905 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1906 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001907 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001908 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001909 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001910 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001911 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001912 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001913 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001914 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001915 }
1916
Tom Hudson984162f2014-10-10 13:38:16 -04001917 DeferredDisplayList deferredList(*mState.currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001918 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001919 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001920
1921 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001922 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001923
Tom Hudson107843d2014-09-08 11:26:26 -04001924 deferredList.flush(*this, dirty);
1925 } else {
1926 // Even if there is no drawing command(Ex: invisible),
1927 // it still needs startFrame to clear buffer and start tiling.
1928 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001929 }
1930}
1931
Chris Craikd218a922014-01-02 17:13:34 -08001932void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001933 float x = left;
1934 float y = top;
1935
Romain Guy886b2752013-01-04 12:26:18 -08001936 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1937
Romain Guya168d732011-03-18 16:50:13 -07001938 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001939 if (currentTransform()->isPureTranslate()) {
1940 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1941 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001942 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001943
1944 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001945 } else {
Chris Craik678625242014-02-28 12:26:34 -08001946 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001947 }
1948
Romain Guy3b748a42013-04-17 18:54:38 -07001949 // No need to check for a UV mapper on the texture object, only ARGB_8888
1950 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001951 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001952 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001953 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001954}
1955
Romain Guy03c00b52013-06-20 18:30:28 -07001956/**
1957 * Important note: this method is intended to draw batches of bitmaps and
1958 * will not set the scissor enable or dirty the current layer, if any.
1959 * The caller is responsible for properly dirtying the current layer.
1960 */
Tom Hudson107843d2014-09-08 11:26:26 -04001961void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001962 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1963 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001964 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001965 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001966 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001967
Chris Craik527a3aa2013-03-04 10:19:31 -08001968 const AutoTexture autoCleanup(texture);
1969
Chris Craik527a3aa2013-03-04 10:19:31 -08001970 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001971 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001972
1973 const float x = (int) floorf(bounds.left + 0.5f);
1974 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001975 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001977 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001978 GL_TRIANGLES, bitmapCount * 6, true,
1979 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001980 } else {
1981 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001982 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001983 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1984 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001985 }
1986
Tom Hudson107843d2014-09-08 11:26:26 -04001987 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001988}
1989
Tom Hudson107843d2014-09-08 11:26:26 -04001990void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001991 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001992 return;
Romain Guy6926c722010-07-12 20:20:03 -07001993 }
1994
Romain Guya1d3c912011-12-13 14:55:06 -08001995 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001996 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001997 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07001998 const AutoTexture autoCleanup(texture);
1999
Mike Reed1103b322014-07-08 12:36:44 -04002000 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002001 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002002 } else {
Chris Craik79647502014-08-06 13:42:24 -07002003 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002004 }
Chet Haase48659092012-05-31 15:21:51 -07002005
Tom Hudson107843d2014-09-08 11:26:26 -04002006 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002007}
2008
Tom Hudson107843d2014-09-08 11:26:26 -04002009void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002010 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002011 return;
Romain Guye651cc62012-05-14 19:44:40 -07002012 }
2013
2014 mCaches.activeTexture(0);
2015 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2016 const AutoTexture autoCleanup(texture);
2017
Mike Reed1103b322014-07-08 12:36:44 -04002018 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002019 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002020 } else {
Chris Craik79647502014-08-06 13:42:24 -07002021 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002022 }
Chet Haase48659092012-05-31 15:21:51 -07002023
Tom Hudson107843d2014-09-08 11:26:26 -04002024 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002025}
2026
Tom Hudson107843d2014-09-08 11:26:26 -04002027void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002028 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002029 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002030 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002031 }
2032
Chris Craik39a908c2013-06-13 14:39:01 -07002033 // TODO: use quickReject on bounds from vertices
2034 mCaches.enableScissor();
2035
Romain Guyb18d2d02011-02-10 15:52:54 -08002036 float left = FLT_MAX;
2037 float top = FLT_MAX;
2038 float right = FLT_MIN;
2039 float bottom = FLT_MIN;
2040
Romain Guya92bb4d2012-10-16 11:08:44 -07002041 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002042
Chris Craik564acf72014-01-02 16:46:18 -08002043 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2044 mesh.setCapacity(count);
2045 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002046
2047 bool cleanupColors = false;
2048 if (!colors) {
2049 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002050 int* newColors = new int[colorsCount];
2051 memset(newColors, 0xff, colorsCount * sizeof(int));
2052 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002053 cleanupColors = true;
2054 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002055
Romain Guy3b748a42013-04-17 18:54:38 -07002056 mCaches.activeTexture(0);
2057 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2058 const UvMapper& mapper(getMapper(texture));
2059
Romain Guy5a7b4662011-01-20 19:09:30 -08002060 for (int32_t y = 0; y < meshHeight; y++) {
2061 for (int32_t x = 0; x < meshWidth; x++) {
2062 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2063
2064 float u1 = float(x) / meshWidth;
2065 float u2 = float(x + 1) / meshWidth;
2066 float v1 = float(y) / meshHeight;
2067 float v2 = float(y + 1) / meshHeight;
2068
Romain Guy3b748a42013-04-17 18:54:38 -07002069 mapper.map(u1, v1, u2, v2);
2070
Romain Guy5a7b4662011-01-20 19:09:30 -08002071 int ax = i + (meshWidth + 1) * 2;
2072 int ay = ax + 1;
2073 int bx = i;
2074 int by = bx + 1;
2075 int cx = i + 2;
2076 int cy = cx + 1;
2077 int dx = i + (meshWidth + 1) * 2 + 2;
2078 int dy = dx + 1;
2079
Romain Guyff316ec2013-02-13 18:39:43 -08002080 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2081 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2082 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002083
Romain Guyff316ec2013-02-13 18:39:43 -08002084 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2085 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2086 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002087
Romain Guya92bb4d2012-10-16 11:08:44 -07002088 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2089 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2090 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2091 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002092 }
2093 }
2094
Chris Craikf0a59072013-11-19 18:00:46 -08002095 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002096 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002097 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002098 }
2099
Romain Guyff316ec2013-02-13 18:39:43 -08002100 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002101 texture = mCaches.textureCache.get(bitmap);
2102 if (!texture) {
2103 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002104 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002105 }
Romain Guyff316ec2013-02-13 18:39:43 -08002106 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002107 const AutoTexture autoCleanup(texture);
2108
2109 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002110 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002111
2112 int alpha;
2113 SkXfermode::Mode mode;
2114 getAlphaAndMode(paint, &alpha, &mode);
2115
Romain Guyff316ec2013-02-13 18:39:43 -08002116 float a = alpha / 255.0f;
2117
Romain Guya92bb4d2012-10-16 11:08:44 -07002118 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002119 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002120 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002121
Romain Guyff316ec2013-02-13 18:39:43 -08002122 setupDraw();
2123 setupDrawWithTextureAndColor();
2124 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002125 setupDrawColorFilter(getColorFilter(paint));
2126 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002127 setupDrawProgram();
2128 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002129 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002130 setupDrawTexture(texture->id);
2131 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002132 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002133 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002134
2135 glDrawArrays(GL_TRIANGLES, 0, count);
2136
Romain Guyff316ec2013-02-13 18:39:43 -08002137 int slot = mCaches.currentProgram->getAttrib("colors");
2138 if (slot >= 0) {
2139 glDisableVertexAttribArray(slot);
2140 }
2141
2142 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002143
Tom Hudson107843d2014-09-08 11:26:26 -04002144 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002145}
2146
Tom Hudson107843d2014-09-08 11:26:26 -04002147void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002148 float srcLeft, float srcTop, float srcRight, float srcBottom,
2149 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002150 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002151 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002152 return;
Romain Guy6926c722010-07-12 20:20:03 -07002153 }
2154
Romain Guya1d3c912011-12-13 14:55:06 -08002155 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002156 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002157 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002158 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002159
Romain Guy8ba548f2010-06-30 19:21:21 -07002160 const float width = texture->width;
2161 const float height = texture->height;
2162
Romain Guy3b748a42013-04-17 18:54:38 -07002163 float u1 = fmax(0.0f, srcLeft / width);
2164 float v1 = fmax(0.0f, srcTop / height);
2165 float u2 = fmin(1.0f, srcRight / width);
2166 float v2 = fmin(1.0f, srcBottom / height);
2167
2168 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002169
Romain Guy03750a02010-10-18 14:06:08 -07002170 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002171 resetDrawTextureTexCoords(u1, v1, u2, v2);
2172
Romain Guyd21b6e12011-11-30 20:21:23 -08002173 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2174
Romain Guy886b2752013-01-04 12:26:18 -08002175 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2176 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002177
Romain Guy886b2752013-01-04 12:26:18 -08002178 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2179 // Apply a scale transform on the canvas only when a shader is in use
2180 // Skia handles the ratio between the dst and src rects as a scale factor
2181 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002182 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002183 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002184
Chris Craikd6b65f62014-01-01 14:45:21 -08002185 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2186 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2187 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002188
2189 dstRight = x + (dstRight - dstLeft);
2190 dstBottom = y + (dstBottom - dstTop);
2191
2192 dstLeft = x;
2193 dstTop = y;
2194
Chris Craik678625242014-02-28 12:26:34 -08002195 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002196 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002197 } else {
Chris Craik678625242014-02-28 12:26:34 -08002198 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002199 }
2200
2201 if (CC_UNLIKELY(useScaleTransform)) {
2202 save(SkCanvas::kMatrix_SaveFlag);
2203 translate(dstLeft, dstTop);
2204 scale(scaleX, scaleY);
2205
2206 dstLeft = 0.0f;
2207 dstTop = 0.0f;
2208
2209 dstRight = srcRight - srcLeft;
2210 dstBottom = srcBottom - srcTop;
2211 }
2212
Mike Reed1103b322014-07-08 12:36:44 -04002213 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002214 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002215 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002216 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002217 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2218 } else {
2219 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002220 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002221 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002222 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2223 }
2224
2225 if (CC_UNLIKELY(useScaleTransform)) {
2226 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002227 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002228
2229 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002230
Tom Hudson107843d2014-09-08 11:26:26 -04002231 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002232}
2233
Tom Hudson107843d2014-09-08 11:26:26 -04002234void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002235 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002236 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002237 return;
Romain Guy6926c722010-07-12 20:20:03 -07002238 }
2239
Romain Guy4c2547f2013-06-11 16:19:24 -07002240 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002241 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2242 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002243
Tom Hudson107843d2014-09-08 11:26:26 -04002244 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002245}
2246
Tom Hudson107843d2014-09-08 11:26:26 -04002247void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002248 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2249 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002250 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002251 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002252 }
2253
Romain Guy211370f2012-02-01 16:10:55 -08002254 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002255 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002256 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002257 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002258 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002259
Romain Guya4adcf02013-02-28 12:15:35 -08002260 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2261 texture->setFilter(GL_LINEAR, true);
2262
Chris Craikd6b65f62014-01-01 14:45:21 -08002263 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002264 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002265 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002266 const float offsetX = left + currentTransform()->getTranslateX();
2267 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002268 const size_t count = mesh->quads.size();
2269 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002270 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002271 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002272 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2273 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2274 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002275 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002276 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002277 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002278 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002279 }
2280 }
2281
Chris Craik4063a0e2013-11-15 16:06:56 -08002282 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002283 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002284 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2285 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002286
Romain Guy3b748a42013-04-17 18:54:38 -07002287 right = x + right - left;
2288 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002289 left = x;
2290 top = y;
2291 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002292 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002293 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2294 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002295 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2296 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002297 }
Chet Haase48659092012-05-31 15:21:51 -07002298
Tom Hudson107843d2014-09-08 11:26:26 -04002299 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002300}
2301
Romain Guy03c00b52013-06-20 18:30:28 -07002302/**
2303 * Important note: this method is intended to draw batches of 9-patch objects and
2304 * will not set the scissor enable or dirty the current layer, if any.
2305 * The caller is responsible for properly dirtying the current layer.
2306 */
Tom Hudson107843d2014-09-08 11:26:26 -04002307void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002308 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002309 mCaches.activeTexture(0);
2310 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002311 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002312 const AutoTexture autoCleanup(texture);
2313
2314 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2315 texture->setFilter(GL_LINEAR, true);
2316
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002317 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2318 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002319 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002320
Tom Hudson107843d2014-09-08 11:26:26 -04002321 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002322}
2323
Tom Hudson107843d2014-09-08 11:26:26 -04002324void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002325 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002326 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002327 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002328 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002329 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002330 }
2331
Chris Craik0d5ac952014-07-15 13:01:02 -07002332 Rect bounds(vertexBuffer.getBounds());
2333 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002334 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2335
Chris Craikcb4d6002012-09-25 12:00:29 -07002336 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002337 bool isAA = paint->isAntiAlias();
2338
Chris Craik710f46d2012-09-17 17:25:49 -07002339 setupDraw();
2340 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002341 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002342 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002343 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002344 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002345 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002346 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002347 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2348 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002349 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002350 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002351 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002352
ztenghui55bfb4e2013-12-03 10:38:55 -08002353 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002354 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002355 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002356 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002357
Chris Craik710f46d2012-09-17 17:25:49 -07002358 int alphaSlot = -1;
2359 if (isAA) {
2360 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2361 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002362 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002363 glEnableVertexAttribArray(alphaSlot);
2364 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002365 }
Romain Guy04299382012-07-18 17:15:41 -07002366
Chris Craik05f3d6e2014-06-02 16:27:04 -07002367 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2368 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002369 mCaches.unbindIndicesBuffer();
2370 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002371 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002372 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002373 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002374 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002375 mCaches.bindShadowIndicesBuffer();
2376 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002377 } else if (mode == VertexBuffer::kIndices) {
2378 mCaches.unbindIndicesBuffer();
2379 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2380 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002381 }
Romain Guy04299382012-07-18 17:15:41 -07002382
Chris Craik710f46d2012-09-17 17:25:49 -07002383 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002384 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002385 }
Chris Craik65cd6122012-12-10 17:56:27 -08002386
Tom Hudson107843d2014-09-08 11:26:26 -04002387 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002388}
2389
2390/**
Chris Craik65cd6122012-12-10 17:56:27 -08002391 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2392 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2393 * screen space in all directions. However, instead of using a fragment shader to compute the
2394 * translucency of the color from its position, we simply use a varying parameter to define how far
2395 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2396 *
2397 * Doesn't yet support joins, caps, or path effects.
2398 */
Tom Hudson107843d2014-09-08 11:26:26 -04002399void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002400 VertexBuffer vertexBuffer;
2401 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002402 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002403 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002404}
2405
2406/**
2407 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2408 * and additional geometry for defining an alpha slope perimeter.
2409 *
2410 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2411 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2412 * in-shader alpha region, but found it to be taxing on some GPUs.
2413 *
2414 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2415 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002416 */
Tom Hudson107843d2014-09-08 11:26:26 -04002417void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002418 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002419
Chris Craik65cd6122012-12-10 17:56:27 -08002420 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002421
Chris Craik65cd6122012-12-10 17:56:27 -08002422 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002423 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2424 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002425
Chris Craik05f3d6e2014-06-02 16:27:04 -07002426 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002427 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002428 }
2429
Chris Craikbf759452014-08-11 16:00:44 -07002430 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002431 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002432}
2433
Tom Hudson107843d2014-09-08 11:26:26 -04002434void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002435 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002436
Chris Craik6d29c8d2013-05-08 18:35:44 -07002437 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002438
Chris Craik6d29c8d2013-05-08 18:35:44 -07002439 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002440 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002441
Chris Craik05f3d6e2014-06-02 16:27:04 -07002442 const Rect& bounds = buffer.getBounds();
2443 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002444 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002445 }
2446
Chris Craikbf759452014-08-11 16:00:44 -07002447 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002448 drawVertexBuffer(buffer, paint, displayFlags);
2449
2450 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002451}
2452
Tom Hudson107843d2014-09-08 11:26:26 -04002453void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002454 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002455 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002456
Tom Hudson984162f2014-10-10 13:38:16 -04002457 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002458 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002459
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002460 SkPaint paint;
2461 paint.setColor(color);
2462 paint.setXfermodeMode(mode);
2463
2464 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002465
Tom Hudson107843d2014-09-08 11:26:26 -04002466 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002467}
Romain Guy9d5316e2010-06-24 19:30:36 -07002468
Tom Hudson107843d2014-09-08 11:26:26 -04002469void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002470 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002471 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002472 const AutoTexture autoCleanup(texture);
2473
2474 const float x = left + texture->left - texture->offset;
2475 const float y = top + texture->top - texture->offset;
2476
2477 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002478
Tom Hudson107843d2014-09-08 11:26:26 -04002479 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002480}
2481
Tom Hudson107843d2014-09-08 11:26:26 -04002482void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002483 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002484 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002485 || quickRejectSetupScissor(left, top, right, bottom, p)
2486 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002487 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002488 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002489
Chris Craikcb4d6002012-09-25 12:00:29 -07002490 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002491 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002492 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002493 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002494 drawShape(left, top, texture, p);
2495 } else {
2496 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2497 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2498 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002499 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002500}
2501
Tom Hudson107843d2014-09-08 11:26:26 -04002502void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002503 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002504 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2505 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002506 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002507 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002508 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002509 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002510 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002511 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002512 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002513 SkPath path;
2514 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2515 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2516 } else {
2517 path.addCircle(x, y, radius);
2518 }
2519 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002520 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002521}
Romain Guy01d58e42011-01-19 21:54:02 -08002522
Tom Hudson107843d2014-09-08 11:26:26 -04002523void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002524 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002525 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002526 || quickRejectSetupScissor(left, top, right, bottom, p)
2527 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002528 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002529 }
Romain Guy01d58e42011-01-19 21:54:02 -08002530
Chris Craikcb4d6002012-09-25 12:00:29 -07002531 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002532 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002533 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002534 drawShape(left, top, texture, p);
2535 } else {
2536 SkPath path;
2537 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2538 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2539 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2540 }
2541 path.addOval(rect);
2542 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002543 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002544}
2545
Tom Hudson107843d2014-09-08 11:26:26 -04002546void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002547 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002548 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002549 || quickRejectSetupScissor(left, top, right, bottom, p)
2550 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002551 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002552 }
2553
Chris Craik780c1282012-10-04 14:10:49 -07002554 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002555 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002556 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002557 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002558 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002559 drawShape(left, top, texture, p);
2560 return;
Chris Craik780c1282012-10-04 14:10:49 -07002561 }
Chris Craik780c1282012-10-04 14:10:49 -07002562 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2563 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2564 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2565 }
2566
2567 SkPath path;
2568 if (useCenter) {
2569 path.moveTo(rect.centerX(), rect.centerY());
2570 }
2571 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2572 if (useCenter) {
2573 path.close();
2574 }
Tom Hudson107843d2014-09-08 11:26:26 -04002575 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002576}
2577
Romain Guycf8675e2012-10-02 12:32:25 -07002578// See SkPaintDefaults.h
2579#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2580
Tom Hudson107843d2014-09-08 11:26:26 -04002581void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002582 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002583 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002584 || quickRejectSetupScissor(left, top, right, bottom, p)
2585 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002586 return;
Romain Guy6926c722010-07-12 20:20:03 -07002587 }
2588
Chris Craik710f46d2012-09-17 17:25:49 -07002589 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002590 // only fill style is supported by drawConvexPath, since others have to handle joins
2591 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2592 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2593 mCaches.activeTexture(0);
2594 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002595 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002596 drawShape(left, top, texture, p);
2597 } else {
2598 SkPath path;
2599 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2600 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2601 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2602 }
2603 path.addRect(rect);
2604 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002605 }
Chet Haase858aa932011-05-12 09:06:00 -07002606 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002607 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2608 SkPath path;
2609 path.addRect(left, top, right, bottom);
2610 drawConvexPath(path, p);
2611 } else {
2612 drawColorRect(left, top, right, bottom, p);
2613
2614 mDirty = true;
2615 }
Chet Haase858aa932011-05-12 09:06:00 -07002616 }
Romain Guyc7d53492010-06-25 13:41:57 -07002617}
Romain Guy9d5316e2010-06-24 19:30:36 -07002618
Chris Craikd218a922014-01-02 17:13:34 -08002619void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2620 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002621 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002622 mCaches.activeTexture(0);
2623
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002624 TextShadow textShadow;
2625 if (!getTextShadow(paint, &textShadow)) {
2626 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2627 }
2628
Raph Levien416a8472012-07-19 22:48:17 -07002629 // NOTE: The drop shadow will not perform gamma correction
2630 // if shader-based correction is enabled
2631 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2632 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002633 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002634 // If the drop shadow exceeds the max texture size or couldn't be
2635 // allocated, skip drawing
2636 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002637 const AutoTexture autoCleanup(shadow);
2638
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002639 const float sx = x - shadow->left + textShadow.dx;
2640 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002641
Tom Hudson984162f2014-10-10 13:38:16 -04002642 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002643 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002644 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002645 }
2646
2647 setupDraw();
2648 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002649 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002650 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002651 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002652 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002653 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002654 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2655 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002656 setupDrawTexture(shadow->id);
2657 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002658 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002659 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002660 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2661
2662 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2663}
2664
Romain Guy768bffc2013-02-27 13:50:45 -08002665bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002666 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002667 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2668}
2669
Tom Hudson107843d2014-09-08 11:26:26 -04002670void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002671 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002672 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002673 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002674 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002675
Romain Guy671d6cf2012-01-18 12:39:17 -08002676 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002677 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002678 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002679 }
2680
Chris Craik39a908c2013-06-13 14:39:01 -07002681 mCaches.enableScissor();
2682
Romain Guy671d6cf2012-01-18 12:39:17 -08002683 float x = 0.0f;
2684 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002685 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002686 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002687 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2688 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002689 }
2690
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002691 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002692 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002693
2694 int alpha;
2695 SkXfermode::Mode mode;
2696 getAlphaAndMode(paint, &alpha, &mode);
2697
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002698 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002699 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002700 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002701 }
2702
Romain Guy671d6cf2012-01-18 12:39:17 -08002703 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002704 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002705 if (pureTranslate && !linearFilter) {
2706 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2707 }
Romain Guy257ae352013-03-20 16:31:12 -07002708 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002709
Tom Hudson984162f2014-10-10 13:38:16 -04002710 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2712
Romain Guy211370f2012-02-01 16:10:55 -08002713 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002714
Victoria Lease1e546812013-06-25 14:25:17 -07002715 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002716 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002717 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002718 if (hasActiveLayer) {
2719 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002720 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002721 }
2722 dirtyLayerUnchecked(bounds, getRegion());
2723 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002724 }
Chet Haase48659092012-05-31 15:21:51 -07002725
Tom Hudson107843d2014-09-08 11:26:26 -04002726 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002727}
2728
Chris Craik59744b72014-07-01 17:56:52 -07002729bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002730 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002731 outMatrix->setIdentity();
2732 return false;
2733 } else if (CC_UNLIKELY(transform.isPerspective())) {
2734 outMatrix->setIdentity();
2735 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002736 }
Chris Craik59744b72014-07-01 17:56:52 -07002737
2738 /**
Chris Craika736cd92014-08-04 13:18:38 -07002739 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2740 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002741 */
2742 float sx, sy;
2743 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002744 outMatrix->setScale(
2745 roundf(fmaxf(1.0f, sx)),
2746 roundf(fmaxf(1.0f, sy)));
2747 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002748}
2749
Tom Hudson984162f2014-10-10 13:38:16 -04002750int OpenGLRenderer::getSaveCount() const {
2751 return mState.getSaveCount();
2752}
2753
2754int OpenGLRenderer::save(int flags) {
2755 return mState.save(flags);
2756}
2757
2758void OpenGLRenderer::restore() {
2759 return mState.restore();
2760}
2761
2762void OpenGLRenderer::restoreToCount(int saveCount) {
2763 return mState.restoreToCount(saveCount);
2764}
2765
2766void OpenGLRenderer::translate(float dx, float dy, float dz) {
2767 return mState.translate(dx, dy, dz);
2768}
2769
2770void OpenGLRenderer::rotate(float degrees) {
2771 return mState.rotate(degrees);
2772}
2773
2774void OpenGLRenderer::scale(float sx, float sy) {
2775 return mState.scale(sx, sy);
2776}
2777
2778void OpenGLRenderer::skew(float sx, float sy) {
2779 return mState.skew(sx, sy);
2780}
2781
2782void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2783 mState.setMatrix(matrix);
2784}
2785
2786void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2787 mState.concatMatrix(matrix);
2788}
2789
2790bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2791 return mState.clipRect(left, top, right, bottom, op);
2792}
2793
2794bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2795 return mState.clipPath(path, op);
2796}
2797
2798bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2799 return mState.clipRegion(region, op);
2800}
2801
2802void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2803 mState.setClippingOutline(allocator, outline);
2804}
2805
2806void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2807 const Rect& rect, float radius, bool highPriority) {
2808 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2809}
2810
2811
2812
Tom Hudson107843d2014-09-08 11:26:26 -04002813void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002814 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002815 DrawOpMode drawOpMode) {
2816
Chris Craik527a3aa2013-03-04 10:19:31 -08002817 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002818 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2819 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002820 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002821 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002822 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002823 }
Romain Guycac5fd32011-12-01 20:08:50 -08002824 }
2825
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002826 const float oldX = x;
2827 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002828
Chris Craikd6b65f62014-01-01 14:45:21 -08002829 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002830 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002831
Romain Guy211370f2012-02-01 16:10:55 -08002832 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002833 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2834 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002835 }
2836
Romain Guy86568192010-12-14 15:55:39 -08002837 int alpha;
2838 SkXfermode::Mode mode;
2839 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002840
Romain Guyc74f45a2013-02-26 19:10:14 -08002841 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2842
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002843 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002844 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002845 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002846 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002847 }
2848
Romain Guy19d4dd82013-03-04 11:14:26 -08002849 const bool hasActiveLayer = hasLayer();
2850
Romain Guy624234f2013-03-05 16:43:31 -08002851 // We only pass a partial transform to the font renderer. That partial
2852 // matrix defines how glyphs are rasterized. Typically we want glyphs
2853 // to be rasterized at their final size on screen, which means the partial
2854 // matrix needs to take the scale factor into account.
2855 // When a partial matrix is used to transform glyphs during rasterization,
2856 // the mesh is generated with the inverse transform (in the case of scale,
2857 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2858 // apply the full transform matrix at draw time in the vertex shader.
2859 // Applying the full matrix in the shader is the easiest way to handle
2860 // rotation and perspective and allows us to always generated quads in the
2861 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002862 SkMatrix fontTransform;
2863 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2864 || fabs(y - (int) y) > 0.0f
2865 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002866 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002867 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002868
Romain Guy624234f2013-03-05 16:43:31 -08002869 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002870 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002871 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 -07002872
Raph Levien996e57c2012-07-23 15:22:52 -07002873 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002874 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002875
2876 // don't call issuedrawcommand, do it at end of batch
2877 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002878 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002879 SkPaint paintCopy(*paint);
2880 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2881 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002882 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002883 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002884 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002885 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002886 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002887
Chris Craik527a3aa2013-03-04 10:19:31 -08002888 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002889 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002890 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002891 }
Chris Craik41541822013-05-03 16:35:54 -07002892 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002893 }
Romain Guy694b5192010-07-21 21:33:20 -07002894
Chris Craike63f7c622013-10-17 10:30:55 -07002895 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002896
Tom Hudson107843d2014-09-08 11:26:26 -04002897 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002898}
2899
Tom Hudson107843d2014-09-08 11:26:26 -04002900void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002901 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002902 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002903 return;
Romain Guy03d58522012-02-24 17:54:07 -08002904 }
2905
Chris Craik39a908c2013-06-13 14:39:01 -07002906 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2907 mCaches.enableScissor();
2908
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002909 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002910 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002911 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002912
2913 int alpha;
2914 SkXfermode::Mode mode;
2915 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002916 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002917
Tom Hudson984162f2014-10-10 13:38:16 -04002918 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002919 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 -08002920
Romain Guy97771732012-02-28 18:17:02 -08002921 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002922
2923 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002924 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002925 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002926 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002927 dirtyLayerUnchecked(bounds, getRegion());
2928 }
Romain Guy97771732012-02-28 18:17:02 -08002929 }
Chet Haase48659092012-05-31 15:21:51 -07002930
Tom Hudson107843d2014-09-08 11:26:26 -04002931 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002932}
2933
Tom Hudson107843d2014-09-08 11:26:26 -04002934void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002935 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002936
Romain Guya1d3c912011-12-13 14:55:06 -08002937 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002938
Romain Guyfb8b7632010-08-23 21:05:08 -07002939 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002940 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002941 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002942
Romain Guy8b55f372010-08-18 17:10:07 -07002943 const float x = texture->left - texture->offset;
2944 const float y = texture->top - texture->offset;
2945
Romain Guy01d58e42011-01-19 21:54:02 -08002946 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002947 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002948}
2949
Tom Hudson107843d2014-09-08 11:26:26 -04002950void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002951 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002952 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002953 }
2954
Romain Guyb2e2f242012-10-17 18:18:35 -07002955 mat4* transform = NULL;
2956 if (layer->isTextureLayer()) {
2957 transform = &layer->getTransform();
2958 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002959 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002960 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002961 }
2962 }
2963
Chris Craik39a908c2013-06-13 14:39:01 -07002964 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002965 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002966 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002967
2968 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002969 if (transform && !transform->isIdentity()) {
2970 restore();
2971 }
Tom Hudson107843d2014-09-08 11:26:26 -04002972 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002973 }
2974
Chris Craik62d307c2014-07-29 10:35:13 -07002975 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2976 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2977
Romain Guy5bb3c732012-11-29 17:52:58 -08002978 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002979
Chris Craik39a908c2013-06-13 14:39:01 -07002980 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002981 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002982
Romain Guy211370f2012-02-01 16:10:55 -08002983 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002984 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002985 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2986 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002987 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002988
Chris Craik16ecda52013-03-29 10:59:59 -07002989 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002990 setupDraw();
2991 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002992 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002993 setupDrawColorFilter(layer->getColorFilter());
2994 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002995 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002996 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002997 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002998 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002999 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3000 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3001 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003002
Romain Guyd21b6e12011-11-30 20:21:23 -08003003 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003004 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003005 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003006 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003007 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003008 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003009 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3010 }
Romain Guyf219da52011-01-16 12:54:25 -08003011
Romain Guy448455f2013-07-22 13:57:50 -07003012 TextureVertex* mesh = &layer->mesh[0];
3013 GLsizei elementsCount = layer->meshElementCount;
3014
3015 while (elementsCount > 0) {
3016 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3017
Romain Guy3380cfd2013-08-15 16:57:57 -07003018 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003019 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3020 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3021
3022 elementsCount -= drawCount;
3023 // Though there are 4 vertices in a quad, we use 6 indices per
3024 // quad to draw with GL_TRIANGLES
3025 mesh += (drawCount / 6) * 4;
3026 }
Romain Guyf219da52011-01-16 12:54:25 -08003027
Romain Guy3a3133d2011-02-01 22:59:58 -08003028#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003029 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003030#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003031 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003032
Romain Guy5bb3c732012-11-29 17:52:58 -08003033 if (layer->debugDrawUpdate) {
3034 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003035
3036 SkPaint paint;
3037 paint.setColor(0x7f00ff00);
3038 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003039 }
Romain Guyf219da52011-01-16 12:54:25 -08003040 }
Chris Craik34416ea2013-04-15 16:08:28 -07003041 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003042
Romain Guyb2e2f242012-10-17 18:18:35 -07003043 if (transform && !transform->isIdentity()) {
3044 restore();
3045 }
3046
Tom Hudson107843d2014-09-08 11:26:26 -04003047 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003048}
3049
Romain Guy6926c722010-07-12 20:20:03 -07003050///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003051// Draw filters
3052///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003053void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3054 // We should never get here since we apply the draw filter when stashing
3055 // the paints in the DisplayList.
3056 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003057}
3058
3059///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003060// Drawing implementation
3061///////////////////////////////////////////////////////////////////////////////
3062
Chris Craikd218a922014-01-02 17:13:34 -08003063Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003064 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3065 if (!texture) {
3066 return mCaches.textureCache.get(bitmap);
3067 }
3068 return texture;
3069}
3070
Romain Guy01d58e42011-01-19 21:54:02 -08003071void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003072 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003073 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003074 return;
3075 }
3076
3077 int alpha;
3078 SkXfermode::Mode mode;
3079 getAlphaAndMode(paint, &alpha, &mode);
3080
3081 setupDraw();
3082 setupDrawWithTexture(true);
3083 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003084 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003085 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003086 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003087 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003088 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3089 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003090 setupDrawTexture(texture->id);
3091 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003092 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003093 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003094 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3095
3096 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003097}
3098
Romain Guyf607bdc2010-09-10 19:20:06 -07003099// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003100#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3101#define kStdUnderline_Offset (1.0f / 9.0f)
3102#define kStdUnderline_Thickness (1.0f / 18.0f)
3103
Chris Craikd218a922014-01-02 17:13:34 -08003104void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3105 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003106 // Handle underline and strike-through
3107 uint32_t flags = paint->getFlags();
3108 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003109 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003110
Romain Guy211370f2012-02-01 16:10:55 -08003111 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003112 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003113 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003114
Raph Levien8b4072d2012-07-30 15:50:00 -07003115 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003116 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003117
Romain Guyf6834472011-01-23 13:32:12 -08003118 int linesCount = 0;
3119 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3120 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3121
3122 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003123 float points[pointsCount];
3124 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003125
3126 if (flags & SkPaint::kUnderlineText_Flag) {
3127 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003128 points[currentPoint++] = left;
3129 points[currentPoint++] = top;
3130 points[currentPoint++] = left + underlineWidth;
3131 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003132 }
3133
3134 if (flags & SkPaint::kStrikeThruText_Flag) {
3135 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003136 points[currentPoint++] = left;
3137 points[currentPoint++] = top;
3138 points[currentPoint++] = left + underlineWidth;
3139 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003140 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003141
Romain Guy726aeba2011-06-01 14:52:00 -07003142 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003143
Romain Guy726aeba2011-06-01 14:52:00 -07003144 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003145 }
3146 }
3147}
3148
Tom Hudson107843d2014-09-08 11:26:26 -04003149void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003150 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003151 return;
Romain Guy672433d2013-01-04 19:05:13 -08003152 }
3153
Tom Hudson107843d2014-09-08 11:26:26 -04003154 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003155}
3156
Tom Hudson107843d2014-09-08 11:26:26 -04003157void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003158 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003159 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003160
Chris Craik15a07a22014-01-26 13:43:53 -08003161 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003162 mCaches.enableScissor();
3163
3164 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003165 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003166
ztenghui14a4e352014-08-13 10:44:39 -07003167 // The caller has made sure casterAlpha > 0.
3168 float ambientShadowAlpha = mAmbientShadowAlpha;
3169 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3170 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3171 }
3172 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3173 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003174 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003175 }
ztenghui7b4516e2014-01-07 10:42:55 -08003176
ztenghui14a4e352014-08-13 10:44:39 -07003177 float spotShadowAlpha = mSpotShadowAlpha;
3178 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3179 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3180 }
3181 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3182 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003183 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003184 }
ztenghui7b4516e2014-01-07 10:42:55 -08003185
Tom Hudson107843d2014-09-08 11:26:26 -04003186 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003187}
3188
Tom Hudson107843d2014-09-08 11:26:26 -04003189void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003190 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003191 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003192 return;
Romain Guy3b753822013-03-05 10:27:35 -08003193 }
Romain Guy735738c2012-12-03 12:34:51 -08003194
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003195 int color = paint->getColor();
3196 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003197 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003198 color |= 0x00ffffff;
3199 }
3200
Romain Guy672433d2013-01-04 19:05:13 -08003201 float left = FLT_MAX;
3202 float top = FLT_MAX;
3203 float right = FLT_MIN;
3204 float bottom = FLT_MIN;
3205
Romain Guy448455f2013-07-22 13:57:50 -07003206 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003207 Vertex* vertex = mesh;
3208
Chris Craik2af46352012-11-26 18:30:17 -08003209 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003210 float l = rects[index + 0];
3211 float t = rects[index + 1];
3212 float r = rects[index + 2];
3213 float b = rects[index + 3];
3214
Romain Guy3b753822013-03-05 10:27:35 -08003215 Vertex::set(vertex++, l, t);
3216 Vertex::set(vertex++, r, t);
3217 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003218 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003219
Romain Guy3b753822013-03-05 10:27:35 -08003220 left = fminf(left, l);
3221 top = fminf(top, t);
3222 right = fmaxf(right, r);
3223 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003224 }
3225
Chris Craikf0a59072013-11-19 18:00:46 -08003226 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003227 return;
Romain Guya362c692013-02-04 13:50:16 -08003228 }
Romain Guy672433d2013-01-04 19:05:13 -08003229
Romain Guy672433d2013-01-04 19:05:13 -08003230 setupDraw();
3231 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003232 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003233 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003234 setupDrawColorFilter(getColorFilter(paint));
3235 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003236 setupDrawProgram();
3237 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003238 setupDrawModelView(kModelViewMode_Translate, false,
3239 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003240 setupDrawColorUniforms(getShader(paint));
3241 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003242 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003243
Romain Guy8ce00302013-01-15 18:51:42 -08003244 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003245 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003246 }
3247
Chris Craik4063a0e2013-11-15 16:06:56 -08003248 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003249
Tom Hudson107843d2014-09-08 11:26:26 -04003250 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003251}
3252
Romain Guy026c5e162010-06-28 17:12:22 -07003253void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003254 const SkPaint* paint, bool ignoreTransform) {
3255 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003256 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003257 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003258 color |= 0x00ffffff;
3259 }
3260
Romain Guy70ca14e2010-12-13 18:24:33 -08003261 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003262 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003263 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003265 setupDrawColorFilter(getColorFilter(paint));
3266 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003268 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3269 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003270 setupDrawColorUniforms(getShader(paint));
3271 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003272 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003273 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003274
Romain Guyc95c8d62010-09-17 15:31:32 -07003275 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3276}
3277
Romain Guy82ba8142010-07-09 13:25:56 -07003278void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003279 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003280 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003281
Romain Guy3b748a42013-04-17 18:54:38 -07003282 GLvoid* vertices = (GLvoid*) NULL;
3283 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3284
3285 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003286 vertices = &mMeshVertices[0].x;
3287 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003288
3289 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3290 texture->uvMapper->map(uvs);
3291
3292 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3293 }
3294
Chris Craikd6b65f62014-01-01 14:45:21 -08003295 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3296 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3297 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003298
Romain Guyd21b6e12011-11-30 20:21:23 -08003299 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003300 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003301 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003302 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003303 } else {
Chris Craik678625242014-02-28 12:26:34 -08003304 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003305 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003306 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3307 }
3308
3309 if (texture->uvMapper) {
3310 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003311 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003312}
3313
Romain Guyf7f93552010-07-08 19:17:03 -07003314void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003315 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003316 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003317 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3318 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003319
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003320 int a;
3321 SkXfermode::Mode mode;
3322 getAlphaAndMode(paint, &a, &mode);
3323 const float alpha = a / 255.0f;
3324
Romain Guy746b7402010-10-26 16:27:31 -07003325 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003326 setupDrawWithTexture();
3327 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003328 setupDrawColorFilter(getColorFilter(paint));
3329 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003331 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003332 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003333 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003334 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003335 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003336 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003337
Romain Guy6820ac82010-09-15 18:11:50 -07003338 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003339}
3340
Romain Guy3b748a42013-04-17 18:54:38 -07003341void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003342 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003343 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003344 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3345 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003346
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003347 int a;
3348 SkXfermode::Mode mode;
3349 getAlphaAndMode(paint, &a, &mode);
3350 const float alpha = a / 255.0f;
3351
Romain Guy3b748a42013-04-17 18:54:38 -07003352 setupDraw();
3353 setupDrawWithTexture();
3354 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003355 setupDrawColorFilter(getColorFilter(paint));
3356 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003357 setupDrawProgram();
3358 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003359 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003360 setupDrawTexture(texture);
3361 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003362 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003363 setupDrawMeshIndices(vertices, texCoords, vbo);
3364
3365 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003366}
3367
Romain Guy886b2752013-01-04 12:26:18 -08003368void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003369 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003370 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003371 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003372
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003373 int color = paint != NULL ? paint->getColor() : 0;
3374 int alpha;
3375 SkXfermode::Mode mode;
3376 getAlphaAndMode(paint, &alpha, &mode);
3377
Romain Guy886b2752013-01-04 12:26:18 -08003378 setupDraw();
3379 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003380 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003381 setupDrawAlpha8Color(color, alpha);
3382 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003383 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003384 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003385 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003386 setupDrawProgram();
3387 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003388 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003389 setupDrawTexture(texture);
3390 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003392 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003393 setupDrawMesh(vertices, texCoords);
3394
3395 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003396}
3397
Romain Guya5aed0d2010-09-09 14:42:43 -07003398void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003399 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003400
Tom Hudson984162f2014-10-10 13:38:16 -04003401 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003402 blend = true;
3403 mDescription.hasRoundRectClip = true;
3404 }
3405 mSkipOutlineClip = true;
3406
Romain Guy82ba8142010-07-09 13:25:56 -07003407 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003408
Romain Guy82ba8142010-07-09 13:25:56 -07003409 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003410 // These blend modes are not supported by OpenGL directly and have
3411 // to be implemented using shaders. Since the shader will perform
3412 // the blending, turn blending off here
3413 // If the blend mode cannot be implemented using shaders, fall
3414 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003415 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003416 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003417 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003418 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003419
Romain Guy82bc7a72012-01-03 14:13:39 -08003420 if (mCaches.blend) {
3421 glDisable(GL_BLEND);
3422 mCaches.blend = false;
3423 }
3424
3425 return;
3426 } else {
3427 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003428 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003429 }
3430
3431 if (!mCaches.blend) {
3432 glEnable(GL_BLEND);
3433 }
3434
3435 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3436 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3437
3438 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3439 glBlendFunc(sourceMode, destMode);
3440 mCaches.lastSrcMode = sourceMode;
3441 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003442 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003443 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003444 glDisable(GL_BLEND);
3445 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003446 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003447}
3448
Romain Guy889f8d12010-07-29 14:37:42 -07003449bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003450 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003451 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003452 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003453 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003454 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003455 }
Romain Guy6926c722010-07-12 20:20:03 -07003456 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003457}
3458
Romain Guy026c5e162010-06-28 17:12:22 -07003459void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003460 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003461 TextureVertex::setUV(v++, u1, v1);
3462 TextureVertex::setUV(v++, u2, v1);
3463 TextureVertex::setUV(v++, u1, v2);
3464 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003465}
3466
Chris Craikd218a922014-01-02 17:13:34 -08003467void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003468 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003469 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3470 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003471 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003472 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003473 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003474}
3475
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003476float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003477 float alpha;
3478 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3479 alpha = mDrawModifiers.mOverrideLayerAlpha;
3480 } else {
3481 alpha = layer->getAlpha() / 255.0f;
3482 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003483 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003484}
3485
Romain Guy9d5316e2010-06-24 19:30:36 -07003486}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003487}; // namespace android