blob: 96a69f7e5cb5304440ac2a62a5faa4a055d28f10 [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 Craik797b95b22014-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
1571 if (clearLayer) {
1572 setStencilFromClip();
1573 } else {
1574 // While clearing layer, force disable stencil buffer, since
1575 // it's invalid to stencil-clip *during* the layer clear
1576 mCaches.stencil.disable();
1577 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001578 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001579
Romain Guy70ca14e2010-12-13 18:24:33 -08001580 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001581
Romain Guy70ca14e2010-12-13 18:24:33 -08001582 mSetShaderColor = false;
1583 mColorSet = false;
1584 mColorA = mColorR = mColorG = mColorB = 0.0f;
1585 mTextureUnit = 0;
1586 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001587
1588 // Enable debug highlight when what we're about to draw is tested against
1589 // the stencil buffer and if stencil highlight debugging is on
1590 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1591 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1592 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001593}
1594
1595void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1596 mDescription.hasTexture = true;
1597 mDescription.hasAlpha8Texture = isAlpha8;
1598}
1599
Romain Guyff316ec2013-02-13 18:39:43 -08001600void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1601 mDescription.hasTexture = true;
1602 mDescription.hasColors = true;
1603 mDescription.hasAlpha8Texture = isAlpha8;
1604}
1605
Romain Guyaa6c24c2011-04-28 18:40:04 -07001606void OpenGLRenderer::setupDrawWithExternalTexture() {
1607 mDescription.hasExternalTexture = true;
1608}
1609
Romain Guy15bc6432011-12-13 13:11:32 -08001610void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001611 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001612}
1613
Chris Craik91a8c7c2014-08-12 14:31:35 -07001614void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1615 mDescription.hasVertexAlpha = true;
1616 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001617}
1618
Romain Guy8d0d4782010-12-14 20:13:35 -08001619void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1620 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001621 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1622 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1623 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001624 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001625 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001626}
1627
Romain Guy86568192010-12-14 15:55:39 -08001628void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1629 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001630 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1631 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1632 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001633 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001634 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001635}
1636
Romain Guy41210632012-07-16 17:04:24 -07001637void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1638 mCaches.fontRenderer->describe(mDescription, paint);
1639}
1640
Romain Guy70ca14e2010-12-13 18:24:33 -08001641void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1642 mColorA = a;
1643 mColorR = r;
1644 mColorG = g;
1645 mColorB = b;
1646 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001647 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001648}
1649
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001650void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1651 if (shader != NULL) {
1652 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001653 }
1654}
1655
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001656void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1657 if (filter == NULL) {
1658 return;
1659 }
1660
1661 SkXfermode::Mode mode;
1662 if (filter->asColorMode(NULL, &mode)) {
1663 mDescription.colorOp = ProgramDescription::kColorBlend;
1664 mDescription.colorMode = mode;
1665 } else if (filter->asColorMatrix(NULL)) {
1666 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001667 }
1668}
1669
Romain Guyf09ef512011-05-27 11:43:46 -07001670void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1671 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1672 mColorA = 1.0f;
1673 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001674 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001675 }
1676}
1677
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001678void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1679 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001680 // When the blending mode is kClear_Mode, we need to use a modulate color
1681 // argb=1,0,0,0
1682 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001683 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001684 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001685 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001686 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001687}
1688
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001689void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1690 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001691 // When the blending mode is kClear_Mode, we need to use a modulate color
1692 // argb=1,0,0,0
1693 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001694 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001695 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1696 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001697 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001698}
1699
1700void OpenGLRenderer::setupDrawProgram() {
1701 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001702 if (mDescription.hasRoundRectClip) {
1703 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001704 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001705 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001706 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001707 innerRect.left, innerRect.top,
1708 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001709 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1710 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001711
1712 // add half pixel to round out integer rect space to cover pixel centers
1713 float roundedOutRadius = state->radius + 0.5f;
1714 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1715 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001716 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001717}
1718
1719void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1720 mTrackDirtyRegions = false;
1721}
1722
Chris Craik4063a0e2013-11-15 16:06:56 -08001723void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1724 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001725 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001726 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001727 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001728 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001729
Romain Guy86568192010-12-14 15:55:39 -08001730 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001731 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Tom Hudson984162f2014-10-10 13:38:16 -04001732 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001733 if (dirty && mTrackDirtyRegions) {
1734 if (!ignoreTransform) {
1735 dirtyLayer(left, top, right, bottom, *currentTransform());
1736 } else {
1737 dirtyLayer(left, top, right, bottom);
1738 }
Romain Guy86568192010-12-14 15:55:39 -08001739 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001740}
1741
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001742void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1743 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001744 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1745 }
1746}
1747
Romain Guy86568192010-12-14 15:55:39 -08001748void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001749 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001750 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001751 }
1752}
1753
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001754void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1755 if (shader == NULL) {
1756 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001757 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001758
1759 if (ignoreTransform) {
1760 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1761 // because it was built into modelView / the geometry, and the description needs to
1762 // compensate.
1763 mat4 modelViewWithoutTransform;
1764 modelViewWithoutTransform.loadInverse(*currentTransform());
1765 modelViewWithoutTransform.multiply(mModelViewMatrix);
1766 mModelViewMatrix.load(modelViewWithoutTransform);
1767 }
1768
1769 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001770}
1771
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001772void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1773 if (NULL == filter) {
1774 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001776
1777 SkColor color;
1778 SkXfermode::Mode mode;
1779 if (filter->asColorMode(&color, &mode)) {
1780 const int alpha = SkColorGetA(color);
1781 const GLfloat a = alpha / 255.0f;
1782 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1783 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1784 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1785 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1786 return;
1787 }
1788
1789 SkScalar srcColorMatrix[20];
1790 if (filter->asColorMatrix(srcColorMatrix)) {
1791
1792 float colorMatrix[16];
1793 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1794 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1795 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1796 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1797
1798 // Skia uses the range [0..255] for the addition vector, but we need
1799 // the [0..1] range to apply the vector in GLSL
1800 float colorVector[4];
1801 colorVector[0] = srcColorMatrix[4] / 255.0f;
1802 colorVector[1] = srcColorMatrix[9] / 255.0f;
1803 colorVector[2] = srcColorMatrix[14] / 255.0f;
1804 colorVector[3] = srcColorMatrix[19] / 255.0f;
1805
1806 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1807 GL_FALSE, colorMatrix);
1808 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1809 return;
1810 }
1811
1812 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001813}
1814
Romain Guy41210632012-07-16 17:04:24 -07001815void OpenGLRenderer::setupDrawTextGammaUniforms() {
1816 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1817}
1818
Romain Guy70ca14e2010-12-13 18:24:33 -08001819void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001820 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001821 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001822 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001823}
1824
1825void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001826 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001827 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001828 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001829}
1830
Romain Guyaa6c24c2011-04-28 18:40:04 -07001831void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1832 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001833 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001834 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001835}
1836
Romain Guy8f0095c2011-05-02 17:24:22 -07001837void OpenGLRenderer::setupDrawTextureTransform() {
1838 mDescription.hasTextureTransform = true;
1839}
1840
1841void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001842 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1843 GL_FALSE, &transform.data[0]);
1844}
1845
Chris Craik564acf72014-01-02 16:46:18 -08001846void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1847 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001848 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001849 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001850 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001852 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001853 }
Romain Guyd71dd362011-12-12 19:03:35 -08001854
Chris Craikcb4d6002012-09-25 12:00:29 -07001855 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001856 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001857 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001858 }
1859
1860 mCaches.unbindIndicesBuffer();
1861}
1862
Chris Craik564acf72014-01-02 16:46:18 -08001863void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1864 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001865 bool force = mCaches.unbindMeshBuffer();
1866 GLsizei stride = sizeof(ColorTextureVertex);
1867
1868 mCaches.bindPositionVertexPointer(force, vertices, stride);
1869 if (mCaches.currentProgram->texCoords >= 0) {
1870 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1871 }
1872 int slot = mCaches.currentProgram->getAttrib("colors");
1873 if (slot >= 0) {
1874 glEnableVertexAttribArray(slot);
1875 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1876 }
1877
1878 mCaches.unbindIndicesBuffer();
1879}
1880
Chris Craik564acf72014-01-02 16:46:18 -08001881void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1882 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001883 bool force = false;
1884 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1885 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1886 // use the default VBO found in Caches
1887 if (!vertices || vbo) {
1888 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1889 } else {
1890 force = mCaches.unbindMeshBuffer();
1891 }
ztenghui63d41ab2014-02-14 13:13:41 -08001892 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001893
Chris Craikcb4d6002012-09-25 12:00:29 -07001894 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001895 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001896 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001897 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001898}
1899
Romain Guy448455f2013-07-22 13:57:50 -07001900void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001901 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001902 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001903 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001904}
1905
Romain Guy70ca14e2010-12-13 18:24:33 -08001906///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001907// Drawing
1908///////////////////////////////////////////////////////////////////////////////
1909
Tom Hudson107843d2014-09-08 11:26:26 -04001910void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001911 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1912 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001913 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001914 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001915 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001916 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001917 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001918 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001919 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001920 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001921 }
1922
Tom Hudson984162f2014-10-10 13:38:16 -04001923 DeferredDisplayList deferredList(*mState.currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001924 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001925 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001926
1927 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001928 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001929
Tom Hudson107843d2014-09-08 11:26:26 -04001930 deferredList.flush(*this, dirty);
1931 } else {
1932 // Even if there is no drawing command(Ex: invisible),
1933 // it still needs startFrame to clear buffer and start tiling.
1934 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001935 }
1936}
1937
Chris Craikd218a922014-01-02 17:13:34 -08001938void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001939 float x = left;
1940 float y = top;
1941
Romain Guy886b2752013-01-04 12:26:18 -08001942 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1943
Romain Guya168d732011-03-18 16:50:13 -07001944 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001945 if (currentTransform()->isPureTranslate()) {
1946 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1947 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001948 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001949
1950 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001951 } else {
Chris Craik678625242014-02-28 12:26:34 -08001952 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001953 }
1954
Romain Guy3b748a42013-04-17 18:54:38 -07001955 // No need to check for a UV mapper on the texture object, only ARGB_8888
1956 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001957 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001958 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001959 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001960}
1961
Romain Guy03c00b52013-06-20 18:30:28 -07001962/**
1963 * Important note: this method is intended to draw batches of bitmaps and
1964 * will not set the scissor enable or dirty the current layer, if any.
1965 * The caller is responsible for properly dirtying the current layer.
1966 */
Tom Hudson107843d2014-09-08 11:26:26 -04001967void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001968 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1969 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001970 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001971 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001972 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001973
Chris Craik527a3aa2013-03-04 10:19:31 -08001974 const AutoTexture autoCleanup(texture);
1975
Chris Craik527a3aa2013-03-04 10:19:31 -08001976 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001977 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001978
1979 const float x = (int) floorf(bounds.left + 0.5f);
1980 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04001981 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001982 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001983 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001984 GL_TRIANGLES, bitmapCount * 6, true,
1985 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001986 } else {
1987 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001988 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001989 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1990 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001991 }
1992
Tom Hudson107843d2014-09-08 11:26:26 -04001993 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08001994}
1995
Tom Hudson107843d2014-09-08 11:26:26 -04001996void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07001997 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04001998 return;
Romain Guy6926c722010-07-12 20:20:03 -07001999 }
2000
Romain Guya1d3c912011-12-13 14:55:06 -08002001 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002002 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002003 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002004 const AutoTexture autoCleanup(texture);
2005
Mike Reed1103b322014-07-08 12:36:44 -04002006 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002007 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002008 } else {
Chris Craik79647502014-08-06 13:42:24 -07002009 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002010 }
Chet Haase48659092012-05-31 15:21:51 -07002011
Tom Hudson107843d2014-09-08 11:26:26 -04002012 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002013}
2014
Tom Hudson107843d2014-09-08 11:26:26 -04002015void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002016 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002017 return;
Romain Guye651cc62012-05-14 19:44:40 -07002018 }
2019
2020 mCaches.activeTexture(0);
2021 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2022 const AutoTexture autoCleanup(texture);
2023
Mike Reed1103b322014-07-08 12:36:44 -04002024 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002025 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002026 } else {
Chris Craik79647502014-08-06 13:42:24 -07002027 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002028 }
Chet Haase48659092012-05-31 15:21:51 -07002029
Tom Hudson107843d2014-09-08 11:26:26 -04002030 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002031}
2032
Tom Hudson107843d2014-09-08 11:26:26 -04002033void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002034 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002035 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002036 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002037 }
2038
Chris Craik39a908c2013-06-13 14:39:01 -07002039 // TODO: use quickReject on bounds from vertices
2040 mCaches.enableScissor();
2041
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 float left = FLT_MAX;
2043 float top = FLT_MAX;
2044 float right = FLT_MIN;
2045 float bottom = FLT_MIN;
2046
Romain Guya92bb4d2012-10-16 11:08:44 -07002047 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002048
Chris Craik564acf72014-01-02 16:46:18 -08002049 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2050 mesh.setCapacity(count);
2051 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002052
2053 bool cleanupColors = false;
2054 if (!colors) {
2055 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002056 int* newColors = new int[colorsCount];
2057 memset(newColors, 0xff, colorsCount * sizeof(int));
2058 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002059 cleanupColors = true;
2060 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002061
Romain Guy3b748a42013-04-17 18:54:38 -07002062 mCaches.activeTexture(0);
2063 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2064 const UvMapper& mapper(getMapper(texture));
2065
Romain Guy5a7b4662011-01-20 19:09:30 -08002066 for (int32_t y = 0; y < meshHeight; y++) {
2067 for (int32_t x = 0; x < meshWidth; x++) {
2068 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2069
2070 float u1 = float(x) / meshWidth;
2071 float u2 = float(x + 1) / meshWidth;
2072 float v1 = float(y) / meshHeight;
2073 float v2 = float(y + 1) / meshHeight;
2074
Romain Guy3b748a42013-04-17 18:54:38 -07002075 mapper.map(u1, v1, u2, v2);
2076
Romain Guy5a7b4662011-01-20 19:09:30 -08002077 int ax = i + (meshWidth + 1) * 2;
2078 int ay = ax + 1;
2079 int bx = i;
2080 int by = bx + 1;
2081 int cx = i + 2;
2082 int cy = cx + 1;
2083 int dx = i + (meshWidth + 1) * 2 + 2;
2084 int dy = dx + 1;
2085
Romain Guyff316ec2013-02-13 18:39:43 -08002086 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2087 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2088 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002089
Romain Guyff316ec2013-02-13 18:39:43 -08002090 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2091 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2092 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002093
Romain Guya92bb4d2012-10-16 11:08:44 -07002094 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2095 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2096 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2097 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002098 }
2099 }
2100
Chris Craikf0a59072013-11-19 18:00:46 -08002101 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002102 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002103 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002104 }
2105
Romain Guyff316ec2013-02-13 18:39:43 -08002106 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002107 texture = mCaches.textureCache.get(bitmap);
2108 if (!texture) {
2109 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002110 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002111 }
Romain Guyff316ec2013-02-13 18:39:43 -08002112 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 const AutoTexture autoCleanup(texture);
2114
2115 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002116 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002117
2118 int alpha;
2119 SkXfermode::Mode mode;
2120 getAlphaAndMode(paint, &alpha, &mode);
2121
Romain Guyff316ec2013-02-13 18:39:43 -08002122 float a = alpha / 255.0f;
2123
Romain Guya92bb4d2012-10-16 11:08:44 -07002124 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002125 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002126 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002127
Romain Guyff316ec2013-02-13 18:39:43 -08002128 setupDraw();
2129 setupDrawWithTextureAndColor();
2130 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002131 setupDrawColorFilter(getColorFilter(paint));
2132 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002133 setupDrawProgram();
2134 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002135 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002136 setupDrawTexture(texture->id);
2137 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002138 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002139 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002140
2141 glDrawArrays(GL_TRIANGLES, 0, count);
2142
Romain Guyff316ec2013-02-13 18:39:43 -08002143 int slot = mCaches.currentProgram->getAttrib("colors");
2144 if (slot >= 0) {
2145 glDisableVertexAttribArray(slot);
2146 }
2147
2148 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002149
Tom Hudson107843d2014-09-08 11:26:26 -04002150 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002151}
2152
Tom Hudson107843d2014-09-08 11:26:26 -04002153void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002154 float srcLeft, float srcTop, float srcRight, float srcBottom,
2155 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002156 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002157 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002158 return;
Romain Guy6926c722010-07-12 20:20:03 -07002159 }
2160
Romain Guya1d3c912011-12-13 14:55:06 -08002161 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002162 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002163 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002164 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002165
Romain Guy8ba548f2010-06-30 19:21:21 -07002166 const float width = texture->width;
2167 const float height = texture->height;
2168
Romain Guy3b748a42013-04-17 18:54:38 -07002169 float u1 = fmax(0.0f, srcLeft / width);
2170 float v1 = fmax(0.0f, srcTop / height);
2171 float u2 = fmin(1.0f, srcRight / width);
2172 float v2 = fmin(1.0f, srcBottom / height);
2173
2174 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002175
Romain Guy03750a02010-10-18 14:06:08 -07002176 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002177 resetDrawTextureTexCoords(u1, v1, u2, v2);
2178
Romain Guyd21b6e12011-11-30 20:21:23 -08002179 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2180
Romain Guy886b2752013-01-04 12:26:18 -08002181 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2182 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002183
Romain Guy886b2752013-01-04 12:26:18 -08002184 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2185 // Apply a scale transform on the canvas only when a shader is in use
2186 // Skia handles the ratio between the dst and src rects as a scale factor
2187 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002188 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002189 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002190
Chris Craikd6b65f62014-01-01 14:45:21 -08002191 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2192 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2193 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002194
2195 dstRight = x + (dstRight - dstLeft);
2196 dstBottom = y + (dstBottom - dstTop);
2197
2198 dstLeft = x;
2199 dstTop = y;
2200
Chris Craik678625242014-02-28 12:26:34 -08002201 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002202 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002203 } else {
Chris Craik678625242014-02-28 12:26:34 -08002204 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002205 }
2206
2207 if (CC_UNLIKELY(useScaleTransform)) {
2208 save(SkCanvas::kMatrix_SaveFlag);
2209 translate(dstLeft, dstTop);
2210 scale(scaleX, scaleY);
2211
2212 dstLeft = 0.0f;
2213 dstTop = 0.0f;
2214
2215 dstRight = srcRight - srcLeft;
2216 dstBottom = srcBottom - srcTop;
2217 }
2218
Mike Reed1103b322014-07-08 12:36:44 -04002219 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002220 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002221 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002222 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002223 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2224 } else {
2225 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002226 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002227 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002228 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2229 }
2230
2231 if (CC_UNLIKELY(useScaleTransform)) {
2232 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002233 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002234
2235 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002236
Tom Hudson107843d2014-09-08 11:26:26 -04002237 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002238}
2239
Tom Hudson107843d2014-09-08 11:26:26 -04002240void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002241 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002242 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002243 return;
Romain Guy6926c722010-07-12 20:20:03 -07002244 }
2245
Romain Guy4c2547f2013-06-11 16:19:24 -07002246 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002247 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2248 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002249
Tom Hudson107843d2014-09-08 11:26:26 -04002250 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002251}
2252
Tom Hudson107843d2014-09-08 11:26:26 -04002253void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002254 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2255 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002256 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002257 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002258 }
2259
Romain Guy211370f2012-02-01 16:10:55 -08002260 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002261 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002262 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002263 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002264 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002265
Romain Guya4adcf02013-02-28 12:15:35 -08002266 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2267 texture->setFilter(GL_LINEAR, true);
2268
Chris Craikd6b65f62014-01-01 14:45:21 -08002269 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002270 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002271 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002272 const float offsetX = left + currentTransform()->getTranslateX();
2273 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002274 const size_t count = mesh->quads.size();
2275 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002276 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002277 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002278 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2279 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2280 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002281 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002282 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002283 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 }
2286 }
2287
Chris Craik4063a0e2013-11-15 16:06:56 -08002288 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002289 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002290 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2291 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002292
Romain Guy3b748a42013-04-17 18:54:38 -07002293 right = x + right - left;
2294 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002295 left = x;
2296 top = y;
2297 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002298 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002299 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2300 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002301 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2302 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002303 }
Chet Haase48659092012-05-31 15:21:51 -07002304
Tom Hudson107843d2014-09-08 11:26:26 -04002305 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002306}
2307
Romain Guy03c00b52013-06-20 18:30:28 -07002308/**
2309 * Important note: this method is intended to draw batches of 9-patch objects and
2310 * will not set the scissor enable or dirty the current layer, if any.
2311 * The caller is responsible for properly dirtying the current layer.
2312 */
Tom Hudson107843d2014-09-08 11:26:26 -04002313void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002314 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002315 mCaches.activeTexture(0);
2316 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002317 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002318 const AutoTexture autoCleanup(texture);
2319
2320 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2321 texture->setFilter(GL_LINEAR, true);
2322
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002323 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2324 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002325 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002326
Tom Hudson107843d2014-09-08 11:26:26 -04002327 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002328}
2329
Tom Hudson107843d2014-09-08 11:26:26 -04002330void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002331 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002332 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002333 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002334 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002335 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002336 }
2337
Chris Craik0d5ac952014-07-15 13:01:02 -07002338 Rect bounds(vertexBuffer.getBounds());
2339 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002340 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2341
Chris Craikcb4d6002012-09-25 12:00:29 -07002342 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002343 bool isAA = paint->isAntiAlias();
2344
Chris Craik710f46d2012-09-17 17:25:49 -07002345 setupDraw();
2346 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002347 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002348 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002349 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002350 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002351 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002352 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002353 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2354 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002355 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002356 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002357 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002358
ztenghui55bfb4e2013-12-03 10:38:55 -08002359 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002360 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002361 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002362 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002363
Chris Craik710f46d2012-09-17 17:25:49 -07002364 int alphaSlot = -1;
2365 if (isAA) {
2366 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2367 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002368 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002369 glEnableVertexAttribArray(alphaSlot);
2370 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002371 }
Romain Guy04299382012-07-18 17:15:41 -07002372
Chris Craik05f3d6e2014-06-02 16:27:04 -07002373 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2374 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002375 mCaches.unbindIndicesBuffer();
2376 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002377 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002378 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002379 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002380 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002381 mCaches.bindShadowIndicesBuffer();
2382 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002383 } else if (mode == VertexBuffer::kIndices) {
2384 mCaches.unbindIndicesBuffer();
2385 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2386 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002387 }
Romain Guy04299382012-07-18 17:15:41 -07002388
Chris Craik710f46d2012-09-17 17:25:49 -07002389 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002390 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002391 }
Chris Craik65cd6122012-12-10 17:56:27 -08002392
Tom Hudson107843d2014-09-08 11:26:26 -04002393 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002394}
2395
2396/**
Chris Craik65cd6122012-12-10 17:56:27 -08002397 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2398 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2399 * screen space in all directions. However, instead of using a fragment shader to compute the
2400 * translucency of the color from its position, we simply use a varying parameter to define how far
2401 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2402 *
2403 * Doesn't yet support joins, caps, or path effects.
2404 */
Tom Hudson107843d2014-09-08 11:26:26 -04002405void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002406 VertexBuffer vertexBuffer;
2407 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002408 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002409 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002410}
2411
2412/**
2413 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2414 * and additional geometry for defining an alpha slope perimeter.
2415 *
2416 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2417 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2418 * in-shader alpha region, but found it to be taxing on some GPUs.
2419 *
2420 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2421 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002422 */
Tom Hudson107843d2014-09-08 11:26:26 -04002423void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002424 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002425
Chris Craik65cd6122012-12-10 17:56:27 -08002426 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002427
Chris Craik65cd6122012-12-10 17:56:27 -08002428 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002429 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2430 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002431
Chris Craik05f3d6e2014-06-02 16:27:04 -07002432 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002433 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002434 }
2435
Chris Craikbf759452014-08-11 16:00:44 -07002436 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002437 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002438}
2439
Tom Hudson107843d2014-09-08 11:26:26 -04002440void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002441 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002442
Chris Craik6d29c8d2013-05-08 18:35:44 -07002443 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002444
Chris Craik6d29c8d2013-05-08 18:35:44 -07002445 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002446 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002447
Chris Craik05f3d6e2014-06-02 16:27:04 -07002448 const Rect& bounds = buffer.getBounds();
2449 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002450 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002451 }
2452
Chris Craikbf759452014-08-11 16:00:44 -07002453 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002454 drawVertexBuffer(buffer, paint, displayFlags);
2455
2456 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002457}
2458
Tom Hudson107843d2014-09-08 11:26:26 -04002459void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002460 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002461 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002462
Tom Hudson984162f2014-10-10 13:38:16 -04002463 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002464 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002465
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002466 SkPaint paint;
2467 paint.setColor(color);
2468 paint.setXfermodeMode(mode);
2469
2470 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002471
Tom Hudson107843d2014-09-08 11:26:26 -04002472 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002473}
Romain Guy9d5316e2010-06-24 19:30:36 -07002474
Tom Hudson107843d2014-09-08 11:26:26 -04002475void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002476 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002477 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002478 const AutoTexture autoCleanup(texture);
2479
2480 const float x = left + texture->left - texture->offset;
2481 const float y = top + texture->top - texture->offset;
2482
2483 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002484
Tom Hudson107843d2014-09-08 11:26:26 -04002485 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002486}
2487
Tom Hudson107843d2014-09-08 11:26:26 -04002488void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002489 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002490 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002491 || quickRejectSetupScissor(left, top, right, bottom, p)
2492 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002493 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002494 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002495
Chris Craikcb4d6002012-09-25 12:00:29 -07002496 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002497 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002498 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002499 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002500 drawShape(left, top, texture, p);
2501 } else {
2502 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2503 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2504 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002505 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002506}
2507
Tom Hudson107843d2014-09-08 11:26:26 -04002508void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002509 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002510 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2511 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002512 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002513 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002514 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002515 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002516 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002517 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002518 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002519 SkPath path;
2520 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2521 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2522 } else {
2523 path.addCircle(x, y, radius);
2524 }
2525 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002526 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002527}
Romain Guy01d58e42011-01-19 21:54:02 -08002528
Tom Hudson107843d2014-09-08 11:26:26 -04002529void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002530 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002531 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002532 || quickRejectSetupScissor(left, top, right, bottom, p)
2533 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002534 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002535 }
Romain Guy01d58e42011-01-19 21:54:02 -08002536
Chris Craikcb4d6002012-09-25 12:00:29 -07002537 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002538 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002539 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002540 drawShape(left, top, texture, p);
2541 } else {
2542 SkPath path;
2543 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2544 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2545 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2546 }
2547 path.addOval(rect);
2548 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002549 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002550}
2551
Tom Hudson107843d2014-09-08 11:26:26 -04002552void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002553 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002554 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002555 || quickRejectSetupScissor(left, top, right, bottom, p)
2556 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002557 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002558 }
2559
Chris Craik780c1282012-10-04 14:10:49 -07002560 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002561 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002562 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002563 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002564 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002565 drawShape(left, top, texture, p);
2566 return;
Chris Craik780c1282012-10-04 14:10:49 -07002567 }
Chris Craik780c1282012-10-04 14:10:49 -07002568 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2569 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2570 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2571 }
2572
2573 SkPath path;
2574 if (useCenter) {
2575 path.moveTo(rect.centerX(), rect.centerY());
2576 }
2577 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2578 if (useCenter) {
2579 path.close();
2580 }
Tom Hudson107843d2014-09-08 11:26:26 -04002581 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002582}
2583
Romain Guycf8675e2012-10-02 12:32:25 -07002584// See SkPaintDefaults.h
2585#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2586
Tom Hudson107843d2014-09-08 11:26:26 -04002587void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002588 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002589 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002590 || quickRejectSetupScissor(left, top, right, bottom, p)
2591 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002592 return;
Romain Guy6926c722010-07-12 20:20:03 -07002593 }
2594
Chris Craik710f46d2012-09-17 17:25:49 -07002595 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002596 // only fill style is supported by drawConvexPath, since others have to handle joins
2597 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2598 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2599 mCaches.activeTexture(0);
2600 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002601 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002602 drawShape(left, top, texture, p);
2603 } else {
2604 SkPath path;
2605 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2606 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2607 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2608 }
2609 path.addRect(rect);
2610 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002611 }
Chet Haase858aa932011-05-12 09:06:00 -07002612 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002613 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2614 SkPath path;
2615 path.addRect(left, top, right, bottom);
2616 drawConvexPath(path, p);
2617 } else {
2618 drawColorRect(left, top, right, bottom, p);
2619
2620 mDirty = true;
2621 }
Chet Haase858aa932011-05-12 09:06:00 -07002622 }
Romain Guyc7d53492010-06-25 13:41:57 -07002623}
Romain Guy9d5316e2010-06-24 19:30:36 -07002624
Chris Craikd218a922014-01-02 17:13:34 -08002625void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2626 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002627 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002628 mCaches.activeTexture(0);
2629
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002630 TextShadow textShadow;
2631 if (!getTextShadow(paint, &textShadow)) {
2632 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2633 }
2634
Raph Levien416a8472012-07-19 22:48:17 -07002635 // NOTE: The drop shadow will not perform gamma correction
2636 // if shader-based correction is enabled
2637 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2638 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002639 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002640 // If the drop shadow exceeds the max texture size or couldn't be
2641 // allocated, skip drawing
2642 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002643 const AutoTexture autoCleanup(shadow);
2644
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002645 const float sx = x - shadow->left + textShadow.dx;
2646 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002647
Tom Hudson984162f2014-10-10 13:38:16 -04002648 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002649 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002650 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002651 }
2652
2653 setupDraw();
2654 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002655 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002656 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002657 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002658 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002659 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002660 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2661 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002662 setupDrawTexture(shadow->id);
2663 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002664 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002665 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002666 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2667
2668 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2669}
2670
Romain Guy768bffc2013-02-27 13:50:45 -08002671bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002672 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002673 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2674}
2675
Tom Hudson107843d2014-09-08 11:26:26 -04002676void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002677 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002678 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002679 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002680 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002681
Romain Guy671d6cf2012-01-18 12:39:17 -08002682 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002683 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002684 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002685 }
2686
Chris Craik39a908c2013-06-13 14:39:01 -07002687 mCaches.enableScissor();
2688
Romain Guy671d6cf2012-01-18 12:39:17 -08002689 float x = 0.0f;
2690 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002691 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002692 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002693 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2694 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002695 }
2696
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002697 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002698 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002699
2700 int alpha;
2701 SkXfermode::Mode mode;
2702 getAlphaAndMode(paint, &alpha, &mode);
2703
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002704 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002705 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002706 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002707 }
2708
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002710 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002711 if (pureTranslate && !linearFilter) {
2712 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2713 }
Romain Guy257ae352013-03-20 16:31:12 -07002714 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002715
Tom Hudson984162f2014-10-10 13:38:16 -04002716 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2718
Romain Guy211370f2012-02-01 16:10:55 -08002719 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002720
Victoria Lease1e546812013-06-25 14:25:17 -07002721 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002722 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002723 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002724 if (hasActiveLayer) {
2725 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002726 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002727 }
2728 dirtyLayerUnchecked(bounds, getRegion());
2729 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002730 }
Chet Haase48659092012-05-31 15:21:51 -07002731
Tom Hudson107843d2014-09-08 11:26:26 -04002732 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002733}
2734
Chris Craik59744b72014-07-01 17:56:52 -07002735bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002736 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002737 outMatrix->setIdentity();
2738 return false;
2739 } else if (CC_UNLIKELY(transform.isPerspective())) {
2740 outMatrix->setIdentity();
2741 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002742 }
Chris Craik59744b72014-07-01 17:56:52 -07002743
2744 /**
Chris Craika736cd92014-08-04 13:18:38 -07002745 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2746 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002747 */
2748 float sx, sy;
2749 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002750 outMatrix->setScale(
2751 roundf(fmaxf(1.0f, sx)),
2752 roundf(fmaxf(1.0f, sy)));
2753 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002754}
2755
Tom Hudson984162f2014-10-10 13:38:16 -04002756int OpenGLRenderer::getSaveCount() const {
2757 return mState.getSaveCount();
2758}
2759
2760int OpenGLRenderer::save(int flags) {
2761 return mState.save(flags);
2762}
2763
2764void OpenGLRenderer::restore() {
2765 return mState.restore();
2766}
2767
2768void OpenGLRenderer::restoreToCount(int saveCount) {
2769 return mState.restoreToCount(saveCount);
2770}
2771
2772void OpenGLRenderer::translate(float dx, float dy, float dz) {
2773 return mState.translate(dx, dy, dz);
2774}
2775
2776void OpenGLRenderer::rotate(float degrees) {
2777 return mState.rotate(degrees);
2778}
2779
2780void OpenGLRenderer::scale(float sx, float sy) {
2781 return mState.scale(sx, sy);
2782}
2783
2784void OpenGLRenderer::skew(float sx, float sy) {
2785 return mState.skew(sx, sy);
2786}
2787
2788void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2789 mState.setMatrix(matrix);
2790}
2791
2792void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2793 mState.concatMatrix(matrix);
2794}
2795
2796bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2797 return mState.clipRect(left, top, right, bottom, op);
2798}
2799
2800bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2801 return mState.clipPath(path, op);
2802}
2803
2804bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2805 return mState.clipRegion(region, op);
2806}
2807
2808void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2809 mState.setClippingOutline(allocator, outline);
2810}
2811
2812void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2813 const Rect& rect, float radius, bool highPriority) {
2814 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2815}
2816
2817
2818
Tom Hudson107843d2014-09-08 11:26:26 -04002819void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002820 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002821 DrawOpMode drawOpMode) {
2822
Chris Craik527a3aa2013-03-04 10:19:31 -08002823 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002824 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2825 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002826 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002827 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002828 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002829 }
Romain Guycac5fd32011-12-01 20:08:50 -08002830 }
2831
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002832 const float oldX = x;
2833 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002834
Chris Craikd6b65f62014-01-01 14:45:21 -08002835 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002836 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002837
Romain Guy211370f2012-02-01 16:10:55 -08002838 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002839 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2840 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002841 }
2842
Romain Guy86568192010-12-14 15:55:39 -08002843 int alpha;
2844 SkXfermode::Mode mode;
2845 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002846
Romain Guyc74f45a2013-02-26 19:10:14 -08002847 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2848
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002849 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002850 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002851 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002852 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002853 }
2854
Romain Guy19d4dd82013-03-04 11:14:26 -08002855 const bool hasActiveLayer = hasLayer();
2856
Romain Guy624234f2013-03-05 16:43:31 -08002857 // We only pass a partial transform to the font renderer. That partial
2858 // matrix defines how glyphs are rasterized. Typically we want glyphs
2859 // to be rasterized at their final size on screen, which means the partial
2860 // matrix needs to take the scale factor into account.
2861 // When a partial matrix is used to transform glyphs during rasterization,
2862 // the mesh is generated with the inverse transform (in the case of scale,
2863 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2864 // apply the full transform matrix at draw time in the vertex shader.
2865 // Applying the full matrix in the shader is the easiest way to handle
2866 // rotation and perspective and allows us to always generated quads in the
2867 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002868 SkMatrix fontTransform;
2869 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2870 || fabs(y - (int) y) > 0.0f
2871 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002872 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002873 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002874
Romain Guy624234f2013-03-05 16:43:31 -08002875 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002876 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002877 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 -07002878
Raph Levien996e57c2012-07-23 15:22:52 -07002879 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002880 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002881
2882 // don't call issuedrawcommand, do it at end of batch
2883 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002884 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002885 SkPaint paintCopy(*paint);
2886 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2887 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002888 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002889 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002890 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002891 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002892 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002893
Chris Craik527a3aa2013-03-04 10:19:31 -08002894 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002895 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002896 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002897 }
Chris Craik41541822013-05-03 16:35:54 -07002898 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002899 }
Romain Guy694b5192010-07-21 21:33:20 -07002900
Chris Craike63f7c622013-10-17 10:30:55 -07002901 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002902
Tom Hudson107843d2014-09-08 11:26:26 -04002903 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002904}
2905
Tom Hudson107843d2014-09-08 11:26:26 -04002906void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002907 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002908 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002909 return;
Romain Guy03d58522012-02-24 17:54:07 -08002910 }
2911
Chris Craik39a908c2013-06-13 14:39:01 -07002912 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2913 mCaches.enableScissor();
2914
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002915 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002916 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002917 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002918
2919 int alpha;
2920 SkXfermode::Mode mode;
2921 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002922 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002923
Tom Hudson984162f2014-10-10 13:38:16 -04002924 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002925 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 -08002926
Romain Guy97771732012-02-28 18:17:02 -08002927 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002928
2929 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002930 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002931 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002932 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002933 dirtyLayerUnchecked(bounds, getRegion());
2934 }
Romain Guy97771732012-02-28 18:17:02 -08002935 }
Chet Haase48659092012-05-31 15:21:51 -07002936
Tom Hudson107843d2014-09-08 11:26:26 -04002937 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002938}
2939
Tom Hudson107843d2014-09-08 11:26:26 -04002940void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002941 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002942
Romain Guya1d3c912011-12-13 14:55:06 -08002943 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002944
Romain Guyfb8b7632010-08-23 21:05:08 -07002945 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002946 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002947 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002948
Romain Guy8b55f372010-08-18 17:10:07 -07002949 const float x = texture->left - texture->offset;
2950 const float y = texture->top - texture->offset;
2951
Romain Guy01d58e42011-01-19 21:54:02 -08002952 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002953 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002954}
2955
Tom Hudson107843d2014-09-08 11:26:26 -04002956void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002957 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002958 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002959 }
2960
Romain Guyb2e2f242012-10-17 18:18:35 -07002961 mat4* transform = NULL;
2962 if (layer->isTextureLayer()) {
2963 transform = &layer->getTransform();
2964 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002965 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002966 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002967 }
2968 }
2969
Chris Craik39a908c2013-06-13 14:39:01 -07002970 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002971 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002972 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002973
2974 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002975 if (transform && !transform->isIdentity()) {
2976 restore();
2977 }
Tom Hudson107843d2014-09-08 11:26:26 -04002978 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002979 }
2980
Chris Craik62d307c2014-07-29 10:35:13 -07002981 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2982 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2983
Romain Guy5bb3c732012-11-29 17:52:58 -08002984 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002985
Chris Craik39a908c2013-06-13 14:39:01 -07002986 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002987 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002988
Romain Guy211370f2012-02-01 16:10:55 -08002989 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002990 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002991 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2992 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002993 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002994
Chris Craik16ecda52013-03-29 10:59:59 -07002995 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002996 setupDraw();
2997 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002998 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002999 setupDrawColorFilter(layer->getColorFilter());
3000 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003001 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003002 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003003 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003004 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003005 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3006 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3007 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003008
Romain Guyd21b6e12011-11-30 20:21:23 -08003009 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003010 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003011 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003012 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003013 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003014 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003015 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3016 }
Romain Guyf219da52011-01-16 12:54:25 -08003017
Romain Guy448455f2013-07-22 13:57:50 -07003018 TextureVertex* mesh = &layer->mesh[0];
3019 GLsizei elementsCount = layer->meshElementCount;
3020
3021 while (elementsCount > 0) {
3022 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3023
Romain Guy3380cfd2013-08-15 16:57:57 -07003024 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003025 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3026 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3027
3028 elementsCount -= drawCount;
3029 // Though there are 4 vertices in a quad, we use 6 indices per
3030 // quad to draw with GL_TRIANGLES
3031 mesh += (drawCount / 6) * 4;
3032 }
Romain Guyf219da52011-01-16 12:54:25 -08003033
Romain Guy3a3133d2011-02-01 22:59:58 -08003034#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003035 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003036#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003037 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003038
Romain Guy5bb3c732012-11-29 17:52:58 -08003039 if (layer->debugDrawUpdate) {
3040 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003041
3042 SkPaint paint;
3043 paint.setColor(0x7f00ff00);
3044 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003045 }
Romain Guyf219da52011-01-16 12:54:25 -08003046 }
Chris Craik34416ea2013-04-15 16:08:28 -07003047 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003048
Romain Guyb2e2f242012-10-17 18:18:35 -07003049 if (transform && !transform->isIdentity()) {
3050 restore();
3051 }
3052
Tom Hudson107843d2014-09-08 11:26:26 -04003053 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003054}
3055
Romain Guy6926c722010-07-12 20:20:03 -07003056///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003057// Draw filters
3058///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003059void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3060 // We should never get here since we apply the draw filter when stashing
3061 // the paints in the DisplayList.
3062 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003063}
3064
3065///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003066// Drawing implementation
3067///////////////////////////////////////////////////////////////////////////////
3068
Chris Craikd218a922014-01-02 17:13:34 -08003069Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003070 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3071 if (!texture) {
3072 return mCaches.textureCache.get(bitmap);
3073 }
3074 return texture;
3075}
3076
Romain Guy01d58e42011-01-19 21:54:02 -08003077void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003078 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003079 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003080 return;
3081 }
3082
3083 int alpha;
3084 SkXfermode::Mode mode;
3085 getAlphaAndMode(paint, &alpha, &mode);
3086
3087 setupDraw();
3088 setupDrawWithTexture(true);
3089 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003090 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003091 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003092 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003093 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003094 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3095 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003096 setupDrawTexture(texture->id);
3097 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003098 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003099 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003100 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3101
3102 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003103}
3104
Romain Guyf607bdc2010-09-10 19:20:06 -07003105// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003106#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3107#define kStdUnderline_Offset (1.0f / 9.0f)
3108#define kStdUnderline_Thickness (1.0f / 18.0f)
3109
Chris Craikd218a922014-01-02 17:13:34 -08003110void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3111 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003112 // Handle underline and strike-through
3113 uint32_t flags = paint->getFlags();
3114 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003115 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003116
Romain Guy211370f2012-02-01 16:10:55 -08003117 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003118 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003119 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003120
Raph Levien8b4072d2012-07-30 15:50:00 -07003121 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003122 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003123
Romain Guyf6834472011-01-23 13:32:12 -08003124 int linesCount = 0;
3125 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3126 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3127
3128 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003129 float points[pointsCount];
3130 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003131
3132 if (flags & SkPaint::kUnderlineText_Flag) {
3133 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003134 points[currentPoint++] = left;
3135 points[currentPoint++] = top;
3136 points[currentPoint++] = left + underlineWidth;
3137 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003138 }
3139
3140 if (flags & SkPaint::kStrikeThruText_Flag) {
3141 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003142 points[currentPoint++] = left;
3143 points[currentPoint++] = top;
3144 points[currentPoint++] = left + underlineWidth;
3145 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003146 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003147
Romain Guy726aeba2011-06-01 14:52:00 -07003148 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003149
Romain Guy726aeba2011-06-01 14:52:00 -07003150 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003151 }
3152 }
3153}
3154
Tom Hudson107843d2014-09-08 11:26:26 -04003155void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003156 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003157 return;
Romain Guy672433d2013-01-04 19:05:13 -08003158 }
3159
Tom Hudson107843d2014-09-08 11:26:26 -04003160 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003161}
3162
Tom Hudson107843d2014-09-08 11:26:26 -04003163void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003164 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003165 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003166
Chris Craik15a07a22014-01-26 13:43:53 -08003167 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003168 mCaches.enableScissor();
3169
3170 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003171 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003172
ztenghui14a4e352014-08-13 10:44:39 -07003173 // The caller has made sure casterAlpha > 0.
3174 float ambientShadowAlpha = mAmbientShadowAlpha;
3175 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3176 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3177 }
3178 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3179 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003180 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003181 }
ztenghui7b4516e2014-01-07 10:42:55 -08003182
ztenghui14a4e352014-08-13 10:44:39 -07003183 float spotShadowAlpha = mSpotShadowAlpha;
3184 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3185 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3186 }
3187 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3188 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003189 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003190 }
ztenghui7b4516e2014-01-07 10:42:55 -08003191
Tom Hudson107843d2014-09-08 11:26:26 -04003192 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003193}
3194
Tom Hudson107843d2014-09-08 11:26:26 -04003195void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003196 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003197 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003198 return;
Romain Guy3b753822013-03-05 10:27:35 -08003199 }
Romain Guy735738c2012-12-03 12:34:51 -08003200
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003201 int color = paint->getColor();
3202 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003203 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003204 color |= 0x00ffffff;
3205 }
3206
Romain Guy672433d2013-01-04 19:05:13 -08003207 float left = FLT_MAX;
3208 float top = FLT_MAX;
3209 float right = FLT_MIN;
3210 float bottom = FLT_MIN;
3211
Romain Guy448455f2013-07-22 13:57:50 -07003212 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003213 Vertex* vertex = mesh;
3214
Chris Craik2af46352012-11-26 18:30:17 -08003215 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003216 float l = rects[index + 0];
3217 float t = rects[index + 1];
3218 float r = rects[index + 2];
3219 float b = rects[index + 3];
3220
Romain Guy3b753822013-03-05 10:27:35 -08003221 Vertex::set(vertex++, l, t);
3222 Vertex::set(vertex++, r, t);
3223 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003224 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003225
Romain Guy3b753822013-03-05 10:27:35 -08003226 left = fminf(left, l);
3227 top = fminf(top, t);
3228 right = fmaxf(right, r);
3229 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003230 }
3231
Chris Craikf0a59072013-11-19 18:00:46 -08003232 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003233 return;
Romain Guya362c692013-02-04 13:50:16 -08003234 }
Romain Guy672433d2013-01-04 19:05:13 -08003235
Romain Guy672433d2013-01-04 19:05:13 -08003236 setupDraw();
3237 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003238 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003239 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003240 setupDrawColorFilter(getColorFilter(paint));
3241 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003242 setupDrawProgram();
3243 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003244 setupDrawModelView(kModelViewMode_Translate, false,
3245 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003246 setupDrawColorUniforms(getShader(paint));
3247 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003248 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003249
Romain Guy8ce00302013-01-15 18:51:42 -08003250 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003251 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003252 }
3253
Chris Craik4063a0e2013-11-15 16:06:56 -08003254 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003255
Tom Hudson107843d2014-09-08 11:26:26 -04003256 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003257}
3258
Romain Guy026c5e162010-06-28 17:12:22 -07003259void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003260 const SkPaint* paint, bool ignoreTransform) {
3261 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003262 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003263 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003264 color |= 0x00ffffff;
3265 }
3266
Romain Guy70ca14e2010-12-13 18:24:33 -08003267 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003268 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003269 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003270 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003271 setupDrawColorFilter(getColorFilter(paint));
3272 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003273 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003274 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3275 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003276 setupDrawColorUniforms(getShader(paint));
3277 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003278 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003279 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003280
Romain Guyc95c8d62010-09-17 15:31:32 -07003281 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3282}
3283
Romain Guy82ba8142010-07-09 13:25:56 -07003284void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003285 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003286 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003287
Romain Guy3b748a42013-04-17 18:54:38 -07003288 GLvoid* vertices = (GLvoid*) NULL;
3289 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3290
3291 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003292 vertices = &mMeshVertices[0].x;
3293 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003294
3295 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3296 texture->uvMapper->map(uvs);
3297
3298 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3299 }
3300
Chris Craikd6b65f62014-01-01 14:45:21 -08003301 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3302 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3303 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003304
Romain Guyd21b6e12011-11-30 20:21:23 -08003305 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003306 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003307 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003308 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003309 } else {
Chris Craik678625242014-02-28 12:26:34 -08003310 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003311 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003312 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3313 }
3314
3315 if (texture->uvMapper) {
3316 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003317 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003318}
3319
Romain Guyf7f93552010-07-08 19:17:03 -07003320void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003321 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003322 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003323 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3324 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003325
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003326 int a;
3327 SkXfermode::Mode mode;
3328 getAlphaAndMode(paint, &a, &mode);
3329 const float alpha = a / 255.0f;
3330
Romain Guy746b7402010-10-26 16:27:31 -07003331 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003332 setupDrawWithTexture();
3333 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003334 setupDrawColorFilter(getColorFilter(paint));
3335 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003336 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003337 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003338 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003339 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003340 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003341 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003342 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003343
Romain Guy6820ac82010-09-15 18:11:50 -07003344 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003345}
3346
Romain Guy3b748a42013-04-17 18:54:38 -07003347void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003348 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003349 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003350 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3351 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003352
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003353 int a;
3354 SkXfermode::Mode mode;
3355 getAlphaAndMode(paint, &a, &mode);
3356 const float alpha = a / 255.0f;
3357
Romain Guy3b748a42013-04-17 18:54:38 -07003358 setupDraw();
3359 setupDrawWithTexture();
3360 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003361 setupDrawColorFilter(getColorFilter(paint));
3362 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003363 setupDrawProgram();
3364 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003365 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003366 setupDrawTexture(texture);
3367 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003368 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003369 setupDrawMeshIndices(vertices, texCoords, vbo);
3370
3371 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003372}
3373
Romain Guy886b2752013-01-04 12:26:18 -08003374void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003375 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003376 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003377 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003378
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003379 int color = paint != NULL ? paint->getColor() : 0;
3380 int alpha;
3381 SkXfermode::Mode mode;
3382 getAlphaAndMode(paint, &alpha, &mode);
3383
Romain Guy886b2752013-01-04 12:26:18 -08003384 setupDraw();
3385 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003386 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003387 setupDrawAlpha8Color(color, alpha);
3388 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003389 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003390 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003392 setupDrawProgram();
3393 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003394 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003395 setupDrawTexture(texture);
3396 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003397 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003398 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003399 setupDrawMesh(vertices, texCoords);
3400
3401 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003402}
3403
Romain Guya5aed0d2010-09-09 14:42:43 -07003404void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003405 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003406
Tom Hudson984162f2014-10-10 13:38:16 -04003407 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003408 blend = true;
3409 mDescription.hasRoundRectClip = true;
3410 }
3411 mSkipOutlineClip = true;
3412
Romain Guy82ba8142010-07-09 13:25:56 -07003413 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003414
Romain Guy82ba8142010-07-09 13:25:56 -07003415 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003416 // These blend modes are not supported by OpenGL directly and have
3417 // to be implemented using shaders. Since the shader will perform
3418 // the blending, turn blending off here
3419 // If the blend mode cannot be implemented using shaders, fall
3420 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003421 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003422 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003423 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003424 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003425
Romain Guy82bc7a72012-01-03 14:13:39 -08003426 if (mCaches.blend) {
3427 glDisable(GL_BLEND);
3428 mCaches.blend = false;
3429 }
3430
3431 return;
3432 } else {
3433 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003434 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003435 }
3436
3437 if (!mCaches.blend) {
3438 glEnable(GL_BLEND);
3439 }
3440
3441 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3442 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3443
3444 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3445 glBlendFunc(sourceMode, destMode);
3446 mCaches.lastSrcMode = sourceMode;
3447 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003448 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003449 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003450 glDisable(GL_BLEND);
3451 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003452 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003453}
3454
Romain Guy889f8d12010-07-29 14:37:42 -07003455bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003456 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003457 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003458 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003459 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003460 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003461 }
Romain Guy6926c722010-07-12 20:20:03 -07003462 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003463}
3464
Romain Guy026c5e162010-06-28 17:12:22 -07003465void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003466 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003467 TextureVertex::setUV(v++, u1, v1);
3468 TextureVertex::setUV(v++, u2, v1);
3469 TextureVertex::setUV(v++, u1, v2);
3470 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003471}
3472
Chris Craikd218a922014-01-02 17:13:34 -08003473void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003474 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003475 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3476 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003477 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003478 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003479 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003480}
3481
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003482float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003483 float alpha;
3484 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3485 alpha = mDrawModifiers.mOverrideLayerAlpha;
3486 } else {
3487 alpha = layer->getAlpha() / 255.0f;
3488 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003489 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003490}
3491
Romain Guy9d5316e2010-06-24 19:30:36 -07003492}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003493}; // namespace android