blob: c113164bf7cf7891ddd7f3185ad18aa3267b10a4 [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"
Romain Guye4d01122010-06-16 18:44:05 -070050
Chris Craik62d307c2014-07-29 10:35:13 -070051#if DEBUG_DETAILED_EVENTS
52 #define EVENT_LOGD(...) eventMarkDEBUG(__VA_ARGS__)
53#else
54 #define EVENT_LOGD(...)
55#endif
56
Chris Craika8bea8e2014-09-24 11:29:43 -070057static void atraceFormatBegin(const char* fmt, ...) {
58 const int BUFFER_SIZE = 256;
59 va_list ap;
60 char buf[BUFFER_SIZE];
61
62 va_start(ap, fmt);
63 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
64 va_end(ap);
65
66 ATRACE_BEGIN(buf);
67}
68
69#define ATRACE_FORMAT_BEGIN(fmt, ...) \
70 if (CC_UNLIKELY(ATRACE_ENABLED())) atraceFormatBegin(fmt, ##__VA_ARGS__)
71
Romain Guye4d01122010-06-16 18:44:05 -070072namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070073namespace uirenderer {
74
Chris Craik678625242014-02-28 12:26:34 -080075static GLenum getFilter(const SkPaint* paint) {
76 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
77 return GL_LINEAR;
78 }
79 return GL_NEAREST;
80}
Romain Guyd21b6e12011-11-30 20:21:23 -080081
Romain Guy9d5316e2010-06-24 19:30:36 -070082///////////////////////////////////////////////////////////////////////////////
83// Globals
84///////////////////////////////////////////////////////////////////////////////
85
Romain Guy889f8d12010-07-29 14:37:42 -070086/**
87 * Structure mapping Skia xfermodes to OpenGL blending factors.
88 */
89struct Blender {
90 SkXfermode::Mode mode;
91 GLenum src;
92 GLenum dst;
93}; // struct Blender
94
Romain Guy026c5e162010-06-28 17:12:22 -070095// In this array, the index of each Blender equals the value of the first
96// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
97static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070098 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
100 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
101 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
103 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
104 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
105 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
106 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
107 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
108 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
109 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
110 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500111 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -0700112 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -0700113};
Romain Guye4d01122010-06-16 18:44:05 -0700114
Romain Guy87a76572010-09-13 18:11:21 -0700115// This array contains the swapped version of each SkXfermode. For instance
116// this array's SrcOver blending mode is actually DstOver. You can refer to
117// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -0700118static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700119 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
120 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
121 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
122 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
123 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
124 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
125 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
126 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
127 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
128 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
129 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
130 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
131 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500132 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700133 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700134};
135
Romain Guyf6a11b82010-06-23 17:47:49 -0700136///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700137// Functions
138///////////////////////////////////////////////////////////////////////////////
139
140template<typename T>
141static inline T min(T a, T b) {
142 return a < b ? a : b;
143}
144
145///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700146// Constructors/destructor
147///////////////////////////////////////////////////////////////////////////////
148
John Reck3b202512014-06-23 13:13:08 -0700149OpenGLRenderer::OpenGLRenderer(RenderState& renderState)
Tom Hudson984162f2014-10-10 13:38:16 -0400150 : mState(*this)
151 , mFrameStarted(false)
Chris Craik058fc642014-07-23 18:19:28 -0700152 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700153 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700154 , mRenderState(renderState)
155 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700156 , mSuppressTiling(false)
157 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400158 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700159 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700160 , mLightRadius(FLT_MIN)
161 , mAmbientShadowAlpha(0)
162 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800163 // *set* draw modifiers to be 0
164 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700165 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700166
Romain Guyac670c02010-07-27 17:39:27 -0700167 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700168}
169
Romain Guy85bf02f2010-06-22 13:11:24 -0700170OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700171 // The context has already been destroyed at this point, do not call
172 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700173}
174
Romain Guy87e2f7572012-09-24 11:37:12 -0700175void OpenGLRenderer::initProperties() {
176 char property[PROPERTY_VALUE_MAX];
177 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
178 mScissorOptimizationDisabled = !strcasecmp(property, "true");
179 INIT_LOGD(" Scissor optimization %s",
180 mScissorOptimizationDisabled ? "disabled" : "enabled");
181 } else {
182 INIT_LOGD(" Scissor optimization enabled");
183 }
Romain Guy13631f32012-01-30 17:41:55 -0800184}
185
Chris Craik058fc642014-07-23 18:19:28 -0700186void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
187 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
188 mLightCenter = lightCenter;
189 mLightRadius = lightRadius;
190 mAmbientShadowAlpha = ambientShadowAlpha;
191 mSpotShadowAlpha = spotShadowAlpha;
192}
193
Romain Guy13631f32012-01-30 17:41:55 -0800194///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700195// Setup
196///////////////////////////////////////////////////////////////////////////////
197
Chris Craik797b95b22014-05-20 18:10:25 -0700198void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700199 glDisable(GL_DITHER);
200 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
201
202 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700203 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700204}
205
Romain Guy96885eb2013-03-26 15:05:58 -0700206void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800207 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800208 mCaches.clearGarbage();
Tom Hudson984162f2014-10-10 13:38:16 -0400209 mState.initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700210 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700211 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700212}
213
Tom Hudson107843d2014-09-08 11:26:26 -0400214void OpenGLRenderer::startFrame() {
215 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700216 mFrameStarted = true;
217
Tom Hudson984162f2014-10-10 13:38:16 -0400218 mState.setDirtyClip(true);
Romain Guyddf74372012-05-22 14:07:07 -0700219
Romain Guy96885eb2013-03-26 15:05:58 -0700220 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700221
Tom Hudson984162f2014-10-10 13:38:16 -0400222 mRenderState.setViewport(mState.getWidth(), mState.getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800223
Romain Guy54c1a642012-09-27 17:55:46 -0700224 // Functors break the tiling extension in pretty spectacular ways
225 // This ensures we don't use tiling when a functor is going to be
226 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700227 mSuppressTiling = mCaches.hasRegisteredFunctors()
228 || mFirstFrameAfterResize;
229 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700230
Chris Craikd6b65f62014-01-01 14:45:21 -0800231 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700232
Romain Guy7c450aa2012-09-21 19:15:00 -0700233 debugOverdraw(true, true);
234
Tom Hudson107843d2014-09-08 11:26:26 -0400235 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700236 mTilingClip.right, mTilingClip.bottom, mOpaque);
237}
238
Tom Hudson107843d2014-09-08 11:26:26 -0400239void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700240 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700241
Romain Guy96885eb2013-03-26 15:05:58 -0700242 setupFrameState(left, top, right, bottom, opaque);
243
244 // Layer renderers will start the frame immediately
245 // The framebuffer renderer will first defer the display list
246 // for each layer and wait until the first drawing command
247 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800248 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700249 syncState();
250 updateLayers();
251 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400252 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700253 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700254}
255
Romain Guydcfc8362013-01-03 13:08:57 -0800256void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
257 // If we know that we are going to redraw the entire framebuffer,
258 // perform a discard to let the driver know we don't need to preserve
259 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800260 if (mExtensions.hasDiscardFramebuffer() &&
Tom Hudson984162f2014-10-10 13:38:16 -0400261 left <= 0.0f && top <= 0.0f && right >= mState.getWidth() && bottom >= mState.getHeight()) {
262 const bool isFbo = onGetTargetFbo() == 0;
Romain Guyf1581982013-01-31 17:20:30 -0800263 const GLenum attachments[] = {
264 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
265 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800266 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
267 }
268}
269
Tom Hudson107843d2014-09-08 11:26:26 -0400270void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700271 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700272 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700273 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400275 mDirty = true;
276 return;
Romain Guyddf74372012-05-22 14:07:07 -0700277 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700278
Romain Guy7c25aab2012-10-18 15:05:02 -0700279 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700280}
281
282void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700283 if (mCaches.blend) {
284 glEnable(GL_BLEND);
285 } else {
286 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700287 }
Romain Guybb9524b2010-06-22 18:56:38 -0700288}
289
henry.uh_chen33f5a592014-07-02 19:36:56 +0800290void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700291 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800292 const Snapshot* snapshot = currentSnapshot();
293
Chris Craik14e51302013-12-30 15:32:54 -0800294 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800295 if (snapshot->flags & Snapshot::kFlagFboTarget) {
296 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700297 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700298
henry.uh_chen33f5a592014-07-02 19:36:56 +0800299 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800300 }
301}
302
henry.uh_chen33f5a592014-07-02 19:36:56 +0800303void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800304 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800305 if(expand) {
306 // Expand the startTiling region by 1
307 int leftNotZero = (clip.left > 0) ? 1 : 0;
308 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
309
310 mCaches.startTiling(
311 clip.left - leftNotZero,
312 windowHeight - clip.bottom - topNotZero,
313 clip.right - clip.left + leftNotZero + 1,
314 clip.bottom - clip.top + topNotZero + 1,
315 opaque);
316 } else {
317 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800318 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800319 }
Romain Guy54c1a642012-09-27 17:55:46 -0700320 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700321}
322
323void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700324 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700325}
326
Tom Hudson107843d2014-09-08 11:26:26 -0400327bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700328 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700329 endTiling();
330
Romain Guyca89e2a2013-03-08 17:44:20 -0800331 // When finish() is invoked on FBO 0 we've reached the end
332 // of the current frame
Tom Hudson984162f2014-10-10 13:38:16 -0400333 if (onGetTargetFbo() == 0) {
Romain Guyca89e2a2013-03-08 17:44:20 -0800334 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700335 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800336 }
337
Romain Guy11cb6422012-09-21 00:39:43 -0700338 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700339#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700340 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700341#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700342
Romain Guyc15008e2010-11-10 11:59:15 -0800343#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800344 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700345#else
346 if (mCaches.getDebugLevel() & kDebugMemory) {
347 mCaches.dumpMemoryUsage();
348 }
Romain Guyc15008e2010-11-10 11:59:15 -0800349#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700350 }
Romain Guy96885eb2013-03-26 15:05:58 -0700351
352 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400353
354 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700355}
356
Romain Guy35643dd2012-09-18 15:40:58 -0700357void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700358 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
359 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700360 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700361
362 mCaches.resetScissor();
363 dirtyClip();
364}
365
Tom Hudson107843d2014-09-08 11:26:26 -0400366void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Tom Hudson984162f2014-10-10 13:38:16 -0400367 if (mState.currentlyIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700368
Tom Hudson984162f2014-10-10 13:38:16 -0400369 Rect clip(*mState.currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700370 clip.snapToPixelBoundaries();
371
Romain Guyd643bb52011-03-01 14:55:21 -0800372 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800373 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800374 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800375 dirtyLayerUnchecked(clip, getRegion());
376 }
Romain Guyd643bb52011-03-01 14:55:21 -0800377
Romain Guy08aa2cb2011-03-17 11:06:57 -0700378 DrawGlInfo info;
379 info.clipLeft = clip.left;
380 info.clipTop = clip.top;
381 info.clipRight = clip.right;
382 info.clipBottom = clip.bottom;
383 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700384 info.width = getViewportWidth();
385 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800386 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700387
Tom Hudson984162f2014-10-10 13:38:16 -0400388 bool prevDirtyClip = mState.getDirtyClip();
Chris Craik54f574a2013-08-26 11:23:46 -0700389 // setup GL state for functor
Tom Hudson984162f2014-10-10 13:38:16 -0400390 if (mState.getDirtyClip()) {
Chris Craik54f574a2013-08-26 11:23:46 -0700391 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
392 }
John Reck3b202512014-06-23 13:13:08 -0700393 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700394 setScissorFromClip();
395 }
Chris Craik54f574a2013-08-26 11:23:46 -0700396
John Reck3b202512014-06-23 13:13:08 -0700397 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
398 // Scissor may have been modified, reset dirty clip
399 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800400
Tom Hudson107843d2014-09-08 11:26:26 -0400401 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800402}
403
Romain Guyf6a11b82010-06-23 17:47:49 -0700404///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700405// Debug
406///////////////////////////////////////////////////////////////////////////////
407
Chris Craik62d307c2014-07-29 10:35:13 -0700408void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
409#if DEBUG_DETAILED_EVENTS
410 const int BUFFER_SIZE = 256;
411 va_list ap;
412 char buf[BUFFER_SIZE];
413
414 va_start(ap, fmt);
415 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
416 va_end(ap);
417
418 eventMark(buf);
419#endif
420}
421
422
Romain Guy0f6675332013-03-01 14:31:04 -0800423void OpenGLRenderer::eventMark(const char* name) const {
424 mCaches.eventMark(0, name);
425}
426
Romain Guy87e2f7572012-09-24 11:37:12 -0700427void OpenGLRenderer::startMark(const char* name) const {
428 mCaches.startMark(0, name);
429}
430
431void OpenGLRenderer::endMark() const {
432 mCaches.endMark();
433}
434
435void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700436 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700437}
438
439void OpenGLRenderer::renderOverdraw() {
Tom Hudson984162f2014-10-10 13:38:16 -0400440 if (mCaches.debugOverdraw && onGetTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700441 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700442
443 mCaches.enableScissor();
Tom Hudson984162f2014-10-10 13:38:16 -0400444 mCaches.setScissor(clip->left, mState.firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700445 clip->right - clip->left, clip->bottom - clip->top);
446
Romain Guy627c6fd2013-08-21 11:53:18 -0700447 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700448 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700449 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
450
451 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700452 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700453 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
454
455 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700456 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700457 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
458
459 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700460 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700461 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
462
Romain Guy87e2f7572012-09-24 11:37:12 -0700463 mCaches.stencil.disable();
464 }
465}
466
467///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700468// Layers
469///////////////////////////////////////////////////////////////////////////////
470
471bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700472 if (layer->deferredUpdateScheduled && layer->renderer
473 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700474 ATRACE_CALL();
475
Romain Guy7c450aa2012-09-21 19:15:00 -0700476 if (inFrame) {
477 endTiling();
478 debugOverdraw(false, false);
479 }
Romain Guy11cb6422012-09-21 00:39:43 -0700480
Romain Guy96885eb2013-03-26 15:05:58 -0700481 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700482 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700483 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700484 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700485 }
Romain Guy11cb6422012-09-21 00:39:43 -0700486
487 if (inFrame) {
488 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800489 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700490 }
491
Romain Guy5bb3c732012-11-29 17:52:58 -0800492 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700493 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700494
495 return true;
496 }
497
498 return false;
499}
500
501void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700502 // If draw deferring is enabled this method will simply defer
503 // the display list of each individual layer. The layers remain
504 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700505 int count = mLayerUpdates.size();
506 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700507 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
508 startMark("Layer Updates");
509 } else {
510 startMark("Defer Layer Updates");
511 }
Romain Guy11cb6422012-09-21 00:39:43 -0700512
Chris Craik1206b9b2013-04-04 14:46:24 -0700513 // Note: it is very important to update the layers in order
514 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700515 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700516 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700517 }
Romain Guy11cb6422012-09-21 00:39:43 -0700518
Romain Guy96885eb2013-03-26 15:05:58 -0700519 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
520 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400521 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700522 }
523 endMark();
524 }
525}
526
527void OpenGLRenderer::flushLayers() {
528 int count = mLayerUpdates.size();
529 if (count > 0) {
530 startMark("Apply Layer Updates");
531 char layerName[12];
532
Chris Craik1206b9b2013-04-04 14:46:24 -0700533 // Note: it is very important to update the layers in order
534 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700535 Layer* layer = mLayerUpdates.itemAt(i).get();
Chris Craika8bea8e2014-09-24 11:29:43 -0700536
Romain Guy96885eb2013-03-26 15:05:58 -0700537 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700538 startMark(layerName);
Chris Craika8bea8e2014-09-24 11:29:43 -0700539 ATRACE_FORMAT_BEGIN("flushLayer %ux%u", layer->getWidth(), layer->getHeight());
Romain Guy02b49b72013-03-29 12:37:16 -0700540
Romain Guy02b49b72013-03-29 12:37:16 -0700541 layer->flush();
Chris Craika8bea8e2014-09-24 11:29:43 -0700542
Romain Guy40543602013-06-12 15:31:28 -0700543 ATRACE_END();
Chris Craika8bea8e2014-09-24 11:29:43 -0700544 endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700545 }
546
547 mLayerUpdates.clear();
Tom Hudson984162f2014-10-10 13:38:16 -0400548 mRenderState.bindFramebuffer(onGetTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700549
Romain Guy11cb6422012-09-21 00:39:43 -0700550 endMark();
551 }
552}
553
554void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
555 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700556 // Make sure we don't introduce duplicates.
557 // SortedVector would do this automatically but we need to respect
558 // the insertion order. The linear search is not an issue since
559 // this list is usually very short (typically one item, at most a few)
560 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
561 if (mLayerUpdates.itemAt(i) == layer) {
562 return;
563 }
564 }
Romain Guy11cb6422012-09-21 00:39:43 -0700565 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700566 }
567}
568
Romain Guye93482f2013-06-17 13:14:51 -0700569void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
570 if (layer) {
571 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
572 if (mLayerUpdates.itemAt(i) == layer) {
573 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700574 break;
575 }
576 }
577 }
578}
579
Romain Guy40543602013-06-12 15:31:28 -0700580void OpenGLRenderer::flushLayerUpdates() {
John Recka7c2ea22014-08-08 13:21:00 -0700581 ATRACE_CALL();
Romain Guy40543602013-06-12 15:31:28 -0700582 syncState();
583 updateLayers();
584 flushLayers();
585 // Wait for all the layer updates to be executed
586 AutoFence fence;
587}
588
John Reck443a7142014-09-04 17:40:05 -0700589void OpenGLRenderer::markLayersAsBuildLayers() {
590 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
591 mLayerUpdates[i]->wasBuildLayered = true;
592 }
593}
594
Romain Guy11cb6422012-09-21 00:39:43 -0700595///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700596// State management
597///////////////////////////////////////////////////////////////////////////////
598
Chris Craik14e51302013-12-30 15:32:54 -0800599void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700600 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800601 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
602 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700603
Chris Craika64a2be2014-05-14 14:17:01 -0700604 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700605 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700606 }
607
Romain Guy2542d192010-08-18 11:47:12 -0700608 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700609 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700610 }
Romain Guy2542d192010-08-18 11:47:12 -0700611
Romain Guy5ec99242010-11-03 16:19:08 -0700612 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700613 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700614 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700615 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800616 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700617 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700618 }
Romain Guybb9524b2010-06-22 18:56:38 -0700619}
620
Romain Guyf6a11b82010-06-23 17:47:49 -0700621///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700622// Layers
623///////////////////////////////////////////////////////////////////////////////
624
625int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700626 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700627 // force matrix/clip isolation for layer
628 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
629
Tom Hudson984162f2014-10-10 13:38:16 -0400630 const int count = mState.saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700631
Tom Hudson984162f2014-10-10 13:38:16 -0400632 if (!mState.currentlyIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700633 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700634 }
Romain Guyd55a8612010-06-28 17:42:46 -0700635
636 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700637}
638
Chris Craikd90144d2013-03-19 15:03:48 -0700639void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
640 const Rect untransformedBounds(bounds);
641
Chris Craikd6b65f62014-01-01 14:45:21 -0800642 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700643
644 // Layers only make sense if they are in the framebuffer's bounds
Tom Hudson984162f2014-10-10 13:38:16 -0400645 if (bounds.intersect(*mState.currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700646 // We cannot work with sub-pixels in this case
647 bounds.snapToPixelBoundaries();
648
649 // When the layer is not an FBO, we may use glCopyTexImage so we
650 // need to make sure the layer does not extend outside the bounds
651 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700652 const Snapshot& previous = *(currentSnapshot()->previous);
653 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
654 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700655 bounds.setEmpty();
656 } else if (fboLayer) {
657 clip.set(bounds);
658 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800659 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700660 inverse.mapRect(clip);
661 clip.snapToPixelBoundaries();
662 if (clip.intersect(untransformedBounds)) {
663 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
664 bounds.set(untransformedBounds);
665 } else {
666 clip.setEmpty();
667 }
668 }
669 } else {
670 bounds.setEmpty();
671 }
672}
673
Chris Craik408eb122013-03-26 18:55:15 -0700674void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
675 bool fboLayer, int alpha) {
676 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
677 bounds.getHeight() > mCaches.maxTextureSize ||
678 (fboLayer && clip.isEmpty())) {
Tom Hudson984162f2014-10-10 13:38:16 -0400679 writableSnapshot()->empty = fboLayer;
Chris Craik408eb122013-03-26 18:55:15 -0700680 } else {
Tom Hudson984162f2014-10-10 13:38:16 -0400681 writableSnapshot()->invisible = writableSnapshot()->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700682 }
683}
684
Chris Craikd90144d2013-03-19 15:03:48 -0700685int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500686 const SkPaint* paint, int flags) {
Tom Hudson984162f2014-10-10 13:38:16 -0400687 const int count = mState.saveSnapshot(flags);
Chris Craikd90144d2013-03-19 15:03:48 -0700688
Tom Hudson984162f2014-10-10 13:38:16 -0400689 if (!mState.currentlyIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700690 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
691 // operations will be able to store and restore the current clip and transform info, and
692 // quick rejection will be correct (for display lists)
693
694 Rect bounds(left, top, right, bottom);
695 Rect clip;
696 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500697 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700698
Tom Hudson984162f2014-10-10 13:38:16 -0400699 if (!mState.currentlyIgnored()) {
700 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
701 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
702 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
703 writableSnapshot()->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700704 }
705 }
706
707 return count;
708}
709
Romain Guy1c740bc2010-09-13 18:00:09 -0700710/**
711 * Layers are viewed by Skia are slightly different than layers in image editing
712 * programs (for instance.) When a layer is created, previously created layers
713 * and the frame buffer still receive every drawing command. For instance, if a
714 * layer is created and a shape intersecting the bounds of the layers and the
715 * framebuffer is draw, the shape will be drawn on both (unless the layer was
716 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
717 *
718 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
719 * texture. Unfortunately, this is inefficient as it requires every primitive to
720 * be drawn n + 1 times, where n is the number of active layers. In practice this
721 * means, for every primitive:
722 * - Switch active frame buffer
723 * - Change viewport, clip and projection matrix
724 * - Issue the drawing
725 *
726 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700727 * To avoid this, layers are implemented in a different way here, at least in the
728 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
729 * is set. When this flag is set we can redirect all drawing operations into a
730 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700731 *
732 * This implementation relies on the frame buffer being at least RGBA 8888. When
733 * a layer is created, only a texture is created, not an FBO. The content of the
734 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700735 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700736 * buffer and drawing continues as normal. This technique therefore treats the
737 * frame buffer as a scratch buffer for the layers.
738 *
739 * To compose the layers back onto the frame buffer, each layer texture
740 * (containing the original frame buffer data) is drawn as a simple quad over
741 * the frame buffer. The trick is that the quad is set as the composition
742 * destination in the blending equation, and the frame buffer becomes the source
743 * of the composition.
744 *
745 * Drawing layers with an alpha value requires an extra step before composition.
746 * An empty quad is drawn over the layer's region in the frame buffer. This quad
747 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
748 * quad is used to multiply the colors in the frame buffer. This is achieved by
749 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
750 * GL_ZERO, GL_SRC_ALPHA.
751 *
752 * Because glCopyTexImage2D() can be slow, an alternative implementation might
753 * be use to draw a single clipped layer. The implementation described above
754 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700755 *
756 * (1) The frame buffer is actually not cleared right away. To allow the GPU
757 * to potentially optimize series of calls to glCopyTexImage2D, the frame
758 * buffer is left untouched until the first drawing operation. Only when
759 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700760 */
Chet Haased48885a2012-08-28 17:43:28 -0700761bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700762 const SkPaint* paint, int flags, const SkPath* convexMask) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800763 if (kDebugLayers) {
764 ALOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
765 ALOGD("Layer cache size = %d", mCaches.layerCache.getSize());
766 }
Romain Guy8ba548f2010-06-30 19:21:21 -0700767
Romain Guyeb993562010-10-05 18:14:38 -0700768 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
769
Romain Guyf607bdc2010-09-10 19:20:06 -0700770 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700771 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700772 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700773 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000774 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700775
776 // Bail out if we won't draw in this snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400777 if (mState.currentlyIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700778 return false;
779 }
Romain Guyf18fd992010-07-08 11:45:51 -0700780
Romain Guya1d3c912011-12-13 14:55:06 -0800781 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700782 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700783 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700784 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700785 }
786
Derek Sollenberger674554f2014-02-19 16:47:32 +0000787 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700788 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700789 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
790 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500791
Chet Haasea23eed82012-04-12 15:19:04 -0700792 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700793 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700794 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700795
Romain Guy8fb95422010-08-17 18:38:51 -0700796 // Save the layer in the snapshot
Tom Hudson984162f2014-10-10 13:38:16 -0400797 writableSnapshot()->flags |= Snapshot::kFlagIsLayer;
798 writableSnapshot()->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700799
Chris Craika8bea8e2014-09-24 11:29:43 -0700800 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
801 fboLayer ? "" : "unclipped ",
802 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700803 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700804 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700805 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700806 } else {
807 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700808 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800809 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700810 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700811 // Workaround for some GL drivers. When reading pixels lying outside
812 // of the window we should get undefined values for those pixels.
813 // Unfortunately some drivers will turn the entire target texture black
814 // when reading outside of the window.
815 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
816 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700817 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800818 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700819
Chris Craika64a2be2014-05-14 14:17:01 -0700820 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
821 bounds.left, getViewportHeight() - bounds.bottom,
822 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700823
Romain Guy54be1cd2011-06-13 19:04:27 -0700824 // Enqueue the buffer coordinates to clear the corresponding region later
825 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700826 }
Romain Guyeb993562010-10-05 18:14:38 -0700827 }
Romain Guyf86ef572010-07-01 11:05:42 -0700828
Romain Guyd55a8612010-06-28 17:42:46 -0700829 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700830}
831
Chris Craike63f7c622013-10-17 10:30:55 -0700832bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800833 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700834 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700835
Tom Hudson984162f2014-10-10 13:38:16 -0400836 writableSnapshot()->region = &writableSnapshot()->layer->region;
837 writableSnapshot()->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
838 writableSnapshot()->fbo = layer->getFbo();
839 writableSnapshot()->resetTransform(-bounds.left, -bounds.top, 0.0f);
840 writableSnapshot()->resetClip(clip.left, clip.top, clip.right, clip.bottom);
841 writableSnapshot()->initializeViewport(bounds.getWidth(), bounds.getHeight());
842 writableSnapshot()->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700843
Romain Guy2b7028e2012-09-19 17:25:38 -0700844 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700845 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700846 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700847 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700848 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700849
850 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700851 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700852 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700853 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700854 }
855
856 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700857 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700858
henry.uh_chen33f5a592014-07-02 19:36:56 +0800859 // Expand the startTiling region by 1
860 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700861
862 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700863 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800864 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 glClear(GL_COLOR_BUFFER_BIT);
867
868 dirtyClip();
869
870 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700871 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700872 return true;
873}
874
Romain Guy1c740bc2010-09-13 18:00:09 -0700875/**
876 * Read the documentation of createLayer() before doing anything in this method.
877 */
Chris Craik14e51302013-12-30 15:32:54 -0800878void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
879 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000880 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700881 return;
882 }
883
Chris Craik14e51302013-12-30 15:32:54 -0800884 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800885 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800886 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700887
Chris Craik39a908c2013-06-13 14:39:01 -0700888 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -0400889 mState.calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700890 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700891 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
892
Romain Guyeb993562010-10-05 18:14:38 -0700893 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700894 endTiling();
895
Romain Guye0aa84b2012-04-03 19:30:26 -0700896 // Detach the texture from the FBO
897 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800898
899 layer->removeFbo(false);
900
Romain Guyeb993562010-10-05 18:14:38 -0700901 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700902 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700903 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700904
Chris Craikd6b65f62014-01-01 14:45:21 -0800905 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700906 }
907
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500909 SkPaint layerPaint;
910 layerPaint.setAlpha(layer->getAlpha());
911 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
912 layerPaint.setColorFilter(layer->getColorFilter());
913
914 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700915 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700916 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700917 }
918
Romain Guy03750a02010-10-18 14:06:08 -0700919 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700920
Romain Guya1d3c912011-12-13 14:55:06 -0800921 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700922
Romain Guy5b3b3522010-10-27 18:57:51 -0700923 // When the layer is stored in an FBO, we can save a bit of fillrate by
924 // drawing only the dirty region
925 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800926 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700927 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 } else if (!rect.isEmpty()) {
929 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530930
931 save(0);
932 // the layer contains screen buffer content that shouldn't be alpha modulated
933 // (and any necessary alpha modulation was handled drawing into the layer)
Tom Hudson984162f2014-10-10 13:38:16 -0400934 writableSnapshot()->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530936 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 }
Romain Guy8b55f372010-08-18 17:10:07 -0700938
Romain Guy746b7402010-10-26 16:27:31 -0700939 dirtyClip();
940
Romain Guyeb993562010-10-05 18:14:38 -0700941 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700942 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700943 if (!mCaches.layerCache.put(layer)) {
Andreas Gampeedaecc12014-11-10 20:54:07 -0800944 if (kDebugLayers) {
945 ALOGD("Deleting layer");
946 }
John Reck0e89e2b2014-10-31 14:49:06 -0700947 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700948 }
949}
950
Romain Guyaa6c24c2011-04-28 18:40:04 -0700951void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700952 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700953
954 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700955 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700956 setupDrawWithTexture();
957 } else {
958 setupDrawWithExternalTexture();
959 }
960 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700961 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500962 setupDrawColorFilter(layer->getColorFilter());
963 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700965 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500966 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700967 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
968 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700969 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700970 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700971 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800972 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800973 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700974 layer->getWidth() == (uint32_t) rect.getWidth() &&
975 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800976 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
977 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700978
Romain Guyd21b6e12011-11-30 20:21:23 -0800979 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800980 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
981 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700982 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800983 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800984 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
985 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700986 }
987 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700988 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700989
990 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700991}
992
Romain Guy5b3b3522010-10-27 18:57:51 -0700993void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700994 if (layer->isTextureLayer()) {
995 EVENT_LOGD("composeTextureLayerRect");
996 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
997 drawTextureLayer(layer, rect);
998 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
999 } else {
1000 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -07001001 const Rect& texCoords = layer->texCoords;
1002 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1003 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001004
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 float x = rect.left;
1006 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001007 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001008 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001009 layer->getHeight() == (uint32_t) rect.getHeight();
1010
1011 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 // When we're swapping, the layer is already in screen coordinates
1013 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001014 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1015 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 }
1017
Romain Guyd21b6e12011-11-30 20:21:23 -08001018 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001020 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001021 }
1022
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001023 SkPaint layerPaint;
1024 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1025 layerPaint.setXfermodeMode(layer->getMode());
1026 layerPaint.setColorFilter(layer->getColorFilter());
1027
1028 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001029 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001030 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001031 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001032 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001033
Romain Guyaa6c24c2011-04-28 18:40:04 -07001034 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001035 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001036}
1037
Chris Craik34416ea2013-04-15 16:08:28 -07001038/**
1039 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1040 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1041 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1042 * by saveLayer's restore
1043 */
Tom Hudson984162f2014-10-10 13:38:16 -04001044#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1045 DRAW_COMMAND; \
1046 if (CC_UNLIKELY(mCaches.debugOverdraw && onGetTargetFbo() == 0 && COND)) { \
1047 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1048 DRAW_COMMAND; \
1049 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1050 } \
Chris Craik34416ea2013-04-15 16:08:28 -07001051 }
1052
1053#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1054
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001055// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1056// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1057class LayerShader : public SkShader {
1058public:
1059 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1060 : INHERITED(localMatrix)
1061 , mLayer(layer) {
1062 }
1063
1064 virtual bool asACustomShader(void** data) const {
1065 if (data) {
1066 *data = static_cast<void*>(mLayer);
1067 }
1068 return true;
1069 }
1070
1071 virtual bool isOpaque() const {
1072 return !mLayer->isBlend();
1073 }
1074
1075protected:
1076 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
1077 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1078 }
1079
1080 virtual void flatten(SkWriteBuffer&) const {
1081 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1082 }
1083
1084 virtual Factory getFactory() const {
1085 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1086 return NULL;
1087 }
1088private:
1089 // Unowned.
1090 Layer* mLayer;
1091 typedef SkShader INHERITED;
1092};
1093
Romain Guy5b3b3522010-10-27 18:57:51 -07001094void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001095 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1096
1097 if (layer->getConvexMask()) {
1098 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1099
1100 // clip to the area of the layer the mask can be larger
1101 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1102
1103 SkPaint paint;
1104 paint.setAntiAlias(true);
1105 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1106
Chris Craik3f0854292014-04-15 16:18:08 -07001107 // create LayerShader to map SaveLayer content into subsequent draw
1108 SkMatrix shaderMatrix;
1109 shaderMatrix.setTranslate(rect.left, rect.bottom);
1110 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001111 LayerShader layerShader(layer, &shaderMatrix);
1112 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001113
1114 // Since the drawing primitive is defined in local drawing space,
1115 // we don't need to modify the draw matrix
1116 const SkPath* maskPath = layer->getConvexMask();
1117 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1118
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001119 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001120 restore();
1121
1122 return;
1123 }
1124
Romain Guy5b3b3522010-10-27 18:57:51 -07001125 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001126 layer->setRegionAsRect();
1127
Chris Craik34416ea2013-04-15 16:08:28 -07001128 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001129
Romain Guy5b3b3522010-10-27 18:57:51 -07001130 layer->region.clear();
1131 return;
1132 }
1133
Chris Craik62d307c2014-07-29 10:35:13 -07001134 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001135 // standard Region based draw
1136 size_t count;
1137 const android::Rect* rects;
1138 Region safeRegion;
1139 if (CC_LIKELY(hasRectToRectTransform())) {
1140 rects = layer->region.getArray(&count);
1141 } else {
1142 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1143 rects = safeRegion.getArray(&count);
1144 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001145
Chris Craik3f0854292014-04-15 16:18:08 -07001146 const float alpha = getLayerAlpha(layer);
1147 const float texX = 1.0f / float(layer->getWidth());
1148 const float texY = 1.0f / float(layer->getHeight());
1149 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001150
Chris Craik3f0854292014-04-15 16:18:08 -07001151 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001152
Chris Craik3f0854292014-04-15 16:18:08 -07001153 // We must get (and therefore bind) the region mesh buffer
1154 // after we setup drawing in case we need to mess with the
1155 // stencil buffer in setupDraw()
1156 TextureVertex* mesh = mCaches.getRegionMesh();
1157 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001158
Chris Craik3f0854292014-04-15 16:18:08 -07001159 setupDrawWithTexture();
1160 setupDrawColor(alpha, alpha, alpha, alpha);
1161 setupDrawColorFilter(layer->getColorFilter());
1162 setupDrawBlending(layer);
1163 setupDrawProgram();
1164 setupDrawDirtyRegionsDisabled();
1165 setupDrawPureColorUniforms();
1166 setupDrawColorFilterUniforms(layer->getColorFilter());
1167 setupDrawTexture(layer->getTexture());
1168 if (currentTransform()->isPureTranslate()) {
1169 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1170 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001171
Chris Craik3f0854292014-04-15 16:18:08 -07001172 layer->setFilter(GL_NEAREST);
1173 setupDrawModelView(kModelViewMode_Translate, false,
1174 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1175 } else {
1176 layer->setFilter(GL_LINEAR);
1177 setupDrawModelView(kModelViewMode_Translate, false,
1178 rect.left, rect.top, rect.right, rect.bottom);
1179 }
1180 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001181
Chris Craik3f0854292014-04-15 16:18:08 -07001182 for (size_t i = 0; i < count; i++) {
1183 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001184
Chris Craik3f0854292014-04-15 16:18:08 -07001185 const float u1 = r->left * texX;
1186 const float v1 = (height - r->top) * texY;
1187 const float u2 = r->right * texX;
1188 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001189
Chris Craik3f0854292014-04-15 16:18:08 -07001190 // TODO: Reject quads outside of the clip
1191 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1192 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1193 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1194 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001195
Chris Craik3f0854292014-04-15 16:18:08 -07001196 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001197
Chris Craik3f0854292014-04-15 16:18:08 -07001198 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001199 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1200 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001201 numQuads = 0;
1202 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001203 }
Chris Craik3f0854292014-04-15 16:18:08 -07001204 }
1205
1206 if (numQuads > 0) {
1207 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1208 GL_UNSIGNED_SHORT, NULL));
1209 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001210
Romain Guy5b3b3522010-10-27 18:57:51 -07001211#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001212 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001213#endif
1214
Chris Craik3f0854292014-04-15 16:18:08 -07001215 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001216}
1217
Romain Guy3a3133d2011-02-01 22:59:58 -08001218#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001219void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001220 size_t count;
1221 const android::Rect* rects = region.getArray(&count);
1222
1223 uint32_t colors[] = {
1224 0x7fff0000, 0x7f00ff00,
1225 0x7f0000ff, 0x7fff00ff,
1226 };
1227
1228 int offset = 0;
1229 int32_t top = rects[0].top;
1230
1231 for (size_t i = 0; i < count; i++) {
1232 if (top != rects[i].top) {
1233 offset ^= 0x2;
1234 top = rects[i].top;
1235 }
1236
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001237 SkPaint paint;
1238 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001239 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001240 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001241 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001242}
Chris Craike63f7c622013-10-17 10:30:55 -07001243#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001244
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001245void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001246 Vector<float> rects;
1247
1248 SkRegion::Iterator it(region);
1249 while (!it.done()) {
1250 const SkIRect& r = it.rect();
1251 rects.push(r.fLeft);
1252 rects.push(r.fTop);
1253 rects.push(r.fRight);
1254 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001255 it.next();
1256 }
1257
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001258 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001259}
1260
Romain Guy5b3b3522010-10-27 18:57:51 -07001261void OpenGLRenderer::dirtyLayer(const float left, const float top,
1262 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001263 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001264 Rect bounds(left, top, right, bottom);
1265 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001266 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001267 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001268}
1269
1270void OpenGLRenderer::dirtyLayer(const float left, const float top,
1271 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001272 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001273 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001274 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001275 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001276}
1277
1278void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Tom Hudson984162f2014-10-10 13:38:16 -04001279 if (bounds.intersect(*mState.currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001280 bounds.snapToPixelBoundaries();
1281 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1282 if (!dirty.isEmpty()) {
1283 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001284 }
1285 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001286}
1287
Chris Craik4063a0e2013-11-15 16:06:56 -08001288void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001289 GLsizei elementsCount = quadsCount * 6;
1290 while (elementsCount > 0) {
1291 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1292
Romain Guy3380cfd2013-08-15 16:57:57 -07001293 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001294 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1295
1296 elementsCount -= drawCount;
1297 // Though there are 4 vertices in a quad, we use 6 indices per
1298 // quad to draw with GL_TRIANGLES
1299 mesh += (drawCount / 6) * 4;
1300 }
1301}
1302
Romain Guy54be1cd2011-06-13 19:04:27 -07001303void OpenGLRenderer::clearLayerRegions() {
1304 const size_t count = mLayers.size();
1305 if (count == 0) return;
1306
Tom Hudson984162f2014-10-10 13:38:16 -04001307 if (!mState.currentlyIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001308 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001309 // Doing several glScissor/glClear here can negatively impact
1310 // GPUs with a tiler architecture, instead we draw quads with
1311 // the Clear blending mode
1312
1313 // The list contains bounds that have already been clipped
1314 // against their initial clip rect, and the current clip
1315 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001316 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001317
Romain Guy448455f2013-07-22 13:57:50 -07001318 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001319 Vertex* vertex = mesh;
1320
1321 for (uint32_t i = 0; i < count; i++) {
1322 Rect* bounds = mLayers.itemAt(i);
1323
Romain Guy54be1cd2011-06-13 19:04:27 -07001324 Vertex::set(vertex++, bounds->left, bounds->top);
1325 Vertex::set(vertex++, bounds->right, bounds->top);
1326 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001327 Vertex::set(vertex++, bounds->right, bounds->bottom);
1328
1329 delete bounds;
1330 }
Romain Guye67307c2013-02-11 18:01:20 -08001331 // We must clear the list of dirty rects before we
1332 // call setupDraw() to prevent stencil setup to do
1333 // the same thing again
1334 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001335
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001336 SkPaint clearPaint;
1337 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1338
Romain Guy54be1cd2011-06-13 19:04:27 -07001339 setupDraw(false);
1340 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001341 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001342 setupDrawProgram();
1343 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001344 setupDrawModelView(kModelViewMode_Translate, false,
1345 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001346
Chris Craik4063a0e2013-11-15 16:06:56 -08001347 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001348
1349 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001350 } else {
1351 for (uint32_t i = 0; i < count; i++) {
1352 delete mLayers.itemAt(i);
1353 }
Romain Guye67307c2013-02-11 18:01:20 -08001354 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001355 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001356}
1357
Romain Guybd6b79b2010-06-26 00:13:53 -07001358///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001359// State Deferral
1360///////////////////////////////////////////////////////////////////////////////
1361
Chris Craikff785832013-03-08 13:12:16 -08001362bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Tom Hudson984162f2014-10-10 13:38:16 -04001363 const Rect* currentClip = mState.currentClipRect();
Chris Craikd6b65f62014-01-01 14:45:21 -08001364 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001365
Chris Craikff785832013-03-08 13:12:16 -08001366 if (stateDeferFlags & kStateDeferFlag_Draw) {
1367 // state has bounds initialized in local coordinates
1368 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001369 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001370 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001371 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1372 // is used, it should more closely duplicate the quickReject logic (in how it uses
1373 // snapToPixelBoundaries)
1374
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001376 // quick rejected
1377 return true;
1378 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001379
Chris Craika02c4ed2013-06-14 13:43:58 -07001380 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001381 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001382 int& flags = state.mClipSideFlags;
1383 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001384 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1385 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1386 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1387 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001388 }
1389 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001390 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001391 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1392 // overdraw avoidance (since we don't know what it overlaps)
1393 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001394 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001395 }
1396 }
1397
Chris Craik527a3aa2013-03-04 10:19:31 -08001398 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1399 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001400 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001401 }
1402
Chris Craik7273daa2013-03-28 11:25:24 -07001403 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1404 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001405 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001406 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001407 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001408
1409 // always store/restore, since it's just a pointer
1410 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001411 return false;
1412}
1413
Chris Craik527a3aa2013-03-04 10:19:31 -08001414void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001415 setMatrix(state.mMatrix);
Tom Hudson984162f2014-10-10 13:38:16 -04001416 writableSnapshot()->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001417 mDrawModifiers = state.mDrawModifiers;
Tom Hudson984162f2014-10-10 13:38:16 -04001418 writableSnapshot()->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001419
Chris Craik527a3aa2013-03-04 10:19:31 -08001420 if (state.mClipValid && !skipClipRestore) {
Tom Hudson984162f2014-10-10 13:38:16 -04001421 writableSnapshot()->setClip(state.mClip.left, state.mClip.top,
Chris Craik28ce94a2013-05-31 11:38:03 -07001422 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001423 dirtyClip();
1424 }
Chris Craikc3566d02013-02-04 16:16:33 -08001425}
1426
Chris Craik28ce94a2013-05-31 11:38:03 -07001427/**
1428 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1429 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1430 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1431 *
1432 * This method should be called when restoreDisplayState() won't be restoring the clip
1433 */
1434void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1435 if (clipRect != NULL) {
Tom Hudson984162f2014-10-10 13:38:16 -04001436 writableSnapshot()->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
Chris Craik28ce94a2013-05-31 11:38:03 -07001437 } else {
Tom Hudson984162f2014-10-10 13:38:16 -04001438 writableSnapshot()->setClip(0, 0, mState.getWidth(), mState.getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001439 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001440 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001441 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001442}
1443
Chris Craikc3566d02013-02-04 16:16:33 -08001444///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001445// Clipping
1446///////////////////////////////////////////////////////////////////////////////
1447
Romain Guybb9524b2010-06-22 18:56:38 -07001448void OpenGLRenderer::setScissorFromClip() {
Tom Hudson984162f2014-10-10 13:38:16 -04001449 Rect clip(*mState.currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001450 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001451
Chris Craika64a2be2014-05-14 14:17:01 -07001452 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001453 clip.getWidth(), clip.getHeight())) {
Tom Hudson984162f2014-10-10 13:38:16 -04001454 mState.setDirtyClip(false);
Romain Guy8a4ac612012-07-17 17:32:48 -07001455 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001456}
1457
Romain Guy8ce00302013-01-15 18:51:42 -08001458void OpenGLRenderer::ensureStencilBuffer() {
1459 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1460 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1461 // just hope we have one when hasLayer() returns false.
1462 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001463 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001464 }
1465}
1466
1467void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1468 // The layer's FBO is already bound when we reach this stage
1469 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001470 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1471 // is attached after we initiated tiling. We must turn it off,
1472 // attach the new render buffer then turn tiling back on
1473 endTiling();
1474
Romain Guy8d4aeb72013-02-12 16:08:55 -08001475 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001476 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001477 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001478
Romain Guyf735c8e2013-01-31 17:45:55 -08001479 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001480 }
1481}
1482
1483void OpenGLRenderer::setStencilFromClip() {
1484 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001485 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001486 EVENT_LOGD("setStencilFromClip - enabling");
1487
Romain Guy8ce00302013-01-15 18:51:42 -08001488 // NOTE: The order here is important, we must set dirtyClip to false
1489 // before any draw call to avoid calling back into this method
Tom Hudson984162f2014-10-10 13:38:16 -04001490 mState.setDirtyClip(false);
Romain Guy8ce00302013-01-15 18:51:42 -08001491
1492 ensureStencilBuffer();
1493
1494 mCaches.stencil.enableWrite();
1495
Chris Craikdeeda3d2014-05-05 19:09:33 -07001496 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001497 // to the region's bounds
1498 bool resetScissor = mCaches.enableScissor();
1499 if (resetScissor) {
1500 // The scissor was not set so we now need to update it
1501 setScissorFromClip();
1502 }
1503 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001504
1505 // stash and disable the outline clip state, since stencil doesn't account for outline
1506 bool storedSkipOutlineClip = mSkipOutlineClip;
1507 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001508
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001509 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001510 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001511 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1512
Romain Guy8ce00302013-01-15 18:51:42 -08001513 // NOTE: We could use the region contour path to generate a smaller mesh
1514 // Since we are using the stencil we could use the red book path
1515 // drawing technique. It might increase bandwidth usage though.
1516
1517 // The last parameter is important: we are not drawing in the color buffer
1518 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001519 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001520 if (resetScissor) mCaches.disableScissor();
1521 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001522
1523 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001524
1525 // Draw the region used to generate the stencil if the appropriate debug
1526 // mode is enabled
1527 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001528 paint.setColor(0x7f0000ff);
1529 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1530 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001531 }
Romain Guy8ce00302013-01-15 18:51:42 -08001532 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001533 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001534 mCaches.stencil.disable();
1535 }
1536 }
1537}
1538
Chris Craikf0a59072013-11-19 18:00:46 -08001539/**
1540 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1541 *
1542 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1543 * style, and tessellated AA ramp
1544 */
1545bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001546 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001547 bool snapOut = paint && paint->isAntiAlias();
1548
1549 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1550 float outset = paint->getStrokeWidth() * 0.5f;
1551 left -= outset;
1552 top -= outset;
1553 right += outset;
1554 bottom += outset;
1555 }
1556
Chris Craikdeeda3d2014-05-05 19:09:33 -07001557 bool clipRequired = false;
1558 bool roundRectClipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04001559 if (mState.calculateQuickRejectForScissor(left, top, right, bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -07001560 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001561 return true;
1562 }
1563
Chris Craikf23b25a2014-06-26 15:46:20 -07001564 // not quick rejected, so enable the scissor if clipRequired
1565 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1566 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001567 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001568}
1569
Romain Guy8ce00302013-01-15 18:51:42 -08001570void OpenGLRenderer::debugClip() {
1571#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001572 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001573 SkPaint paint;
1574 paint.setColor(0x7f00ff00);
1575 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1576
Romain Guy8ce00302013-01-15 18:51:42 -08001577 }
1578#endif
1579}
1580
Romain Guyf6a11b82010-06-23 17:47:49 -07001581///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001582// Drawing commands
1583///////////////////////////////////////////////////////////////////////////////
1584
Chris Craik62d307c2014-07-29 10:35:13 -07001585void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001586 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001587 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001588 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001589 // Make sure setScissor & setStencil happen at the beginning of
1590 // this method
Tom Hudson984162f2014-10-10 13:38:16 -04001591 if (mState.getDirtyClip()) {
Chris Craikb98a0162013-02-21 11:30:22 -08001592 if (mCaches.scissorEnabled) {
1593 setScissorFromClip();
1594 }
Chris Craik62d307c2014-07-29 10:35:13 -07001595
1596 if (clearLayer) {
1597 setStencilFromClip();
1598 } else {
1599 // While clearing layer, force disable stencil buffer, since
1600 // it's invalid to stencil-clip *during* the layer clear
1601 mCaches.stencil.disable();
1602 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001603 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001604
Romain Guy70ca14e2010-12-13 18:24:33 -08001605 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001606
Romain Guy70ca14e2010-12-13 18:24:33 -08001607 mSetShaderColor = false;
1608 mColorSet = false;
1609 mColorA = mColorR = mColorG = mColorB = 0.0f;
1610 mTextureUnit = 0;
1611 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001612
1613 // Enable debug highlight when what we're about to draw is tested against
1614 // the stencil buffer and if stencil highlight debugging is on
1615 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1616 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1617 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001618}
1619
1620void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1621 mDescription.hasTexture = true;
1622 mDescription.hasAlpha8Texture = isAlpha8;
1623}
1624
Romain Guyff316ec2013-02-13 18:39:43 -08001625void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1626 mDescription.hasTexture = true;
1627 mDescription.hasColors = true;
1628 mDescription.hasAlpha8Texture = isAlpha8;
1629}
1630
Romain Guyaa6c24c2011-04-28 18:40:04 -07001631void OpenGLRenderer::setupDrawWithExternalTexture() {
1632 mDescription.hasExternalTexture = true;
1633}
1634
Romain Guy15bc6432011-12-13 13:11:32 -08001635void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001636 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001637}
1638
Chris Craik91a8c7c2014-08-12 14:31:35 -07001639void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1640 mDescription.hasVertexAlpha = true;
1641 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001642}
1643
Romain Guy8d0d4782010-12-14 20:13:35 -08001644void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1645 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001646 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1647 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1648 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001649 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001650 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001651}
1652
Romain Guy86568192010-12-14 15:55:39 -08001653void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1654 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001655 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1656 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1657 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001658 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001659 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001660}
1661
Romain Guy41210632012-07-16 17:04:24 -07001662void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1663 mCaches.fontRenderer->describe(mDescription, paint);
1664}
1665
Romain Guy70ca14e2010-12-13 18:24:33 -08001666void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1667 mColorA = a;
1668 mColorR = r;
1669 mColorG = g;
1670 mColorB = b;
1671 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001672 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001673}
1674
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001675void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1676 if (shader != NULL) {
1677 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001678 }
1679}
1680
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001681void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1682 if (filter == NULL) {
1683 return;
1684 }
1685
1686 SkXfermode::Mode mode;
1687 if (filter->asColorMode(NULL, &mode)) {
1688 mDescription.colorOp = ProgramDescription::kColorBlend;
1689 mDescription.colorMode = mode;
1690 } else if (filter->asColorMatrix(NULL)) {
1691 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001692 }
1693}
1694
Romain Guyf09ef512011-05-27 11:43:46 -07001695void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1696 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1697 mColorA = 1.0f;
1698 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001699 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001700 }
1701}
1702
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001703void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1704 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001705 // When the blending mode is kClear_Mode, we need to use a modulate color
1706 // argb=1,0,0,0
1707 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001708 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001709 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001710 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001711 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001712}
1713
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001714void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1715 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001716 // When the blending mode is kClear_Mode, we need to use a modulate color
1717 // argb=1,0,0,0
1718 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001719 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001720 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1721 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001722 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001723}
1724
1725void OpenGLRenderer::setupDrawProgram() {
1726 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001727 if (mDescription.hasRoundRectClip) {
1728 // TODO: avoid doing this repeatedly, stashing state pointer in program
Tom Hudson984162f2014-10-10 13:38:16 -04001729 const RoundRectClipState* state = writableSnapshot()->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001730 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001731 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001732 innerRect.left, innerRect.top,
1733 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001734 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1735 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001736
1737 // add half pixel to round out integer rect space to cover pixel centers
1738 float roundedOutRadius = state->radius + 0.5f;
1739 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1740 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001741 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001742}
1743
1744void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1745 mTrackDirtyRegions = false;
1746}
1747
Chris Craik4063a0e2013-11-15 16:06:56 -08001748void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1749 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001750 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001751 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001752 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001754
Romain Guy86568192010-12-14 15:55:39 -08001755 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001756 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
Tom Hudson984162f2014-10-10 13:38:16 -04001757 mCaches.currentProgram->set(writableSnapshot()->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
Chris Craika64a2be2014-05-14 14:17:01 -07001758 if (dirty && mTrackDirtyRegions) {
1759 if (!ignoreTransform) {
1760 dirtyLayer(left, top, right, bottom, *currentTransform());
1761 } else {
1762 dirtyLayer(left, top, right, bottom);
1763 }
Romain Guy86568192010-12-14 15:55:39 -08001764 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001765}
1766
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001767void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1768 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001769 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1770 }
1771}
1772
Romain Guy86568192010-12-14 15:55:39 -08001773void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001774 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001775 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001776 }
1777}
1778
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001779void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1780 if (shader == NULL) {
1781 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001782 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001783
1784 if (ignoreTransform) {
1785 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1786 // because it was built into modelView / the geometry, and the description needs to
1787 // compensate.
1788 mat4 modelViewWithoutTransform;
1789 modelViewWithoutTransform.loadInverse(*currentTransform());
1790 modelViewWithoutTransform.multiply(mModelViewMatrix);
1791 mModelViewMatrix.load(modelViewWithoutTransform);
1792 }
1793
1794 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001795}
1796
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001797void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1798 if (NULL == filter) {
1799 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001800 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001801
1802 SkColor color;
1803 SkXfermode::Mode mode;
1804 if (filter->asColorMode(&color, &mode)) {
1805 const int alpha = SkColorGetA(color);
1806 const GLfloat a = alpha / 255.0f;
1807 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1808 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1809 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1810 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1811 return;
1812 }
1813
1814 SkScalar srcColorMatrix[20];
1815 if (filter->asColorMatrix(srcColorMatrix)) {
1816
1817 float colorMatrix[16];
1818 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1819 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1820 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1821 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1822
1823 // Skia uses the range [0..255] for the addition vector, but we need
1824 // the [0..1] range to apply the vector in GLSL
1825 float colorVector[4];
1826 colorVector[0] = srcColorMatrix[4] / 255.0f;
1827 colorVector[1] = srcColorMatrix[9] / 255.0f;
1828 colorVector[2] = srcColorMatrix[14] / 255.0f;
1829 colorVector[3] = srcColorMatrix[19] / 255.0f;
1830
1831 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1832 GL_FALSE, colorMatrix);
1833 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1834 return;
1835 }
1836
1837 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001838}
1839
Romain Guy41210632012-07-16 17:04:24 -07001840void OpenGLRenderer::setupDrawTextGammaUniforms() {
1841 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1842}
1843
Romain Guy70ca14e2010-12-13 18:24:33 -08001844void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001845 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001846 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001847 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001848}
1849
1850void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001851 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001852 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001853 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001854}
1855
Romain Guyaa6c24c2011-04-28 18:40:04 -07001856void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1857 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001858 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001859 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001860}
1861
Romain Guy8f0095c2011-05-02 17:24:22 -07001862void OpenGLRenderer::setupDrawTextureTransform() {
1863 mDescription.hasTextureTransform = true;
1864}
1865
1866void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001867 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1868 GL_FALSE, &transform.data[0]);
1869}
1870
Chris Craik564acf72014-01-02 16:46:18 -08001871void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1872 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001873 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001874 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001875 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001876 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001877 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001878 }
Romain Guyd71dd362011-12-12 19:03:35 -08001879
Chris Craikcb4d6002012-09-25 12:00:29 -07001880 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001881 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001882 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001883 }
1884
1885 mCaches.unbindIndicesBuffer();
1886}
1887
Chris Craik564acf72014-01-02 16:46:18 -08001888void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1889 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001890 bool force = mCaches.unbindMeshBuffer();
1891 GLsizei stride = sizeof(ColorTextureVertex);
1892
1893 mCaches.bindPositionVertexPointer(force, vertices, stride);
1894 if (mCaches.currentProgram->texCoords >= 0) {
1895 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1896 }
1897 int slot = mCaches.currentProgram->getAttrib("colors");
1898 if (slot >= 0) {
1899 glEnableVertexAttribArray(slot);
1900 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1901 }
1902
1903 mCaches.unbindIndicesBuffer();
1904}
1905
Chris Craik564acf72014-01-02 16:46:18 -08001906void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1907 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001908 bool force = false;
1909 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1910 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1911 // use the default VBO found in Caches
1912 if (!vertices || vbo) {
1913 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1914 } else {
1915 force = mCaches.unbindMeshBuffer();
1916 }
ztenghui63d41ab2014-02-14 13:13:41 -08001917 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001918
Chris Craikcb4d6002012-09-25 12:00:29 -07001919 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001920 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001921 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001922 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001923}
1924
Romain Guy448455f2013-07-22 13:57:50 -07001925void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001926 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001927 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001928 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001929}
1930
Romain Guy70ca14e2010-12-13 18:24:33 -08001931///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001932// Drawing
1933///////////////////////////////////////////////////////////////////////////////
1934
Tom Hudson107843d2014-09-08 11:26:26 -04001935void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001936 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1937 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001938 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001939 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001940 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001941 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001942 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001943 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001944 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001945 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001946 }
1947
Tom Hudson984162f2014-10-10 13:38:16 -04001948 DeferredDisplayList deferredList(*mState.currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001949 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001950 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001951
1952 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001953 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001954
Tom Hudson107843d2014-09-08 11:26:26 -04001955 deferredList.flush(*this, dirty);
1956 } else {
1957 // Even if there is no drawing command(Ex: invisible),
1958 // it still needs startFrame to clear buffer and start tiling.
1959 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001960 }
1961}
1962
Chris Craikd218a922014-01-02 17:13:34 -08001963void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guya168d732011-03-18 16:50:13 -07001964 float x = left;
1965 float y = top;
1966
Romain Guy886b2752013-01-04 12:26:18 -08001967 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1968
Romain Guya168d732011-03-18 16:50:13 -07001969 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001970 if (currentTransform()->isPureTranslate()) {
1971 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1972 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001973 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001974
1975 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001976 } else {
Chris Craik678625242014-02-28 12:26:34 -08001977 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001978 }
1979
Romain Guy3b748a42013-04-17 18:54:38 -07001980 // No need to check for a UV mapper on the texture object, only ARGB_8888
1981 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001982 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001983 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001984 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001985}
1986
Romain Guy03c00b52013-06-20 18:30:28 -07001987/**
1988 * Important note: this method is intended to draw batches of bitmaps and
1989 * will not set the scissor enable or dirty the current layer, if any.
1990 * The caller is responsible for properly dirtying the current layer.
1991 */
Tom Hudson107843d2014-09-08 11:26:26 -04001992void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001993 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1994 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001995 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001996 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001997 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001998
Chris Craik527a3aa2013-03-04 10:19:31 -08001999 const AutoTexture autoCleanup(texture);
2000
Chris Craik527a3aa2013-03-04 10:19:31 -08002001 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002002 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002003
2004 const float x = (int) floorf(bounds.left + 0.5f);
2005 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002006 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002007 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002008 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002009 GL_TRIANGLES, bitmapCount * 6, true,
2010 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002011 } else {
2012 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002013 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002014 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2015 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002016 }
2017
Tom Hudson107843d2014-09-08 11:26:26 -04002018 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002019}
2020
Tom Hudson107843d2014-09-08 11:26:26 -04002021void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002022 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002023 return;
Romain Guy6926c722010-07-12 20:20:03 -07002024 }
2025
Romain Guya1d3c912011-12-13 14:55:06 -08002026 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002027 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002028 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002029 const AutoTexture autoCleanup(texture);
2030
Mike Reed1103b322014-07-08 12:36:44 -04002031 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002032 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002033 } else {
Chris Craik79647502014-08-06 13:42:24 -07002034 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002035 }
Chet Haase48659092012-05-31 15:21:51 -07002036
Tom Hudson107843d2014-09-08 11:26:26 -04002037 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002038}
2039
Tom Hudson107843d2014-09-08 11:26:26 -04002040void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002041 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002042 return;
Romain Guye651cc62012-05-14 19:44:40 -07002043 }
2044
2045 mCaches.activeTexture(0);
2046 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2047 const AutoTexture autoCleanup(texture);
2048
Mike Reed1103b322014-07-08 12:36:44 -04002049 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002050 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002051 } else {
Chris Craik79647502014-08-06 13:42:24 -07002052 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002053 }
Chet Haase48659092012-05-31 15:21:51 -07002054
Tom Hudson107843d2014-09-08 11:26:26 -04002055 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002056}
2057
Tom Hudson107843d2014-09-08 11:26:26 -04002058void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002059 const float* vertices, const int* colors, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002060 if (!vertices || mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002061 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002062 }
2063
Chris Craik39a908c2013-06-13 14:39:01 -07002064 // TODO: use quickReject on bounds from vertices
2065 mCaches.enableScissor();
2066
Romain Guyb18d2d02011-02-10 15:52:54 -08002067 float left = FLT_MAX;
2068 float top = FLT_MAX;
2069 float right = FLT_MIN;
2070 float bottom = FLT_MIN;
2071
Romain Guya92bb4d2012-10-16 11:08:44 -07002072 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002073
Chris Craik564acf72014-01-02 16:46:18 -08002074 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2075 mesh.setCapacity(count);
2076 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002077
2078 bool cleanupColors = false;
2079 if (!colors) {
2080 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002081 int* newColors = new int[colorsCount];
2082 memset(newColors, 0xff, colorsCount * sizeof(int));
2083 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002084 cleanupColors = true;
2085 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002086
Romain Guy3b748a42013-04-17 18:54:38 -07002087 mCaches.activeTexture(0);
2088 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2089 const UvMapper& mapper(getMapper(texture));
2090
Romain Guy5a7b4662011-01-20 19:09:30 -08002091 for (int32_t y = 0; y < meshHeight; y++) {
2092 for (int32_t x = 0; x < meshWidth; x++) {
2093 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2094
2095 float u1 = float(x) / meshWidth;
2096 float u2 = float(x + 1) / meshWidth;
2097 float v1 = float(y) / meshHeight;
2098 float v2 = float(y + 1) / meshHeight;
2099
Romain Guy3b748a42013-04-17 18:54:38 -07002100 mapper.map(u1, v1, u2, v2);
2101
Romain Guy5a7b4662011-01-20 19:09:30 -08002102 int ax = i + (meshWidth + 1) * 2;
2103 int ay = ax + 1;
2104 int bx = i;
2105 int by = bx + 1;
2106 int cx = i + 2;
2107 int cy = cx + 1;
2108 int dx = i + (meshWidth + 1) * 2 + 2;
2109 int dy = dx + 1;
2110
Romain Guyff316ec2013-02-13 18:39:43 -08002111 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2112 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2113 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002114
Romain Guyff316ec2013-02-13 18:39:43 -08002115 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2116 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2117 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002118
Romain Guya92bb4d2012-10-16 11:08:44 -07002119 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2120 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2121 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2122 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002123 }
2124 }
2125
Chris Craikf0a59072013-11-19 18:00:46 -08002126 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002127 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002128 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002129 }
2130
Romain Guyff316ec2013-02-13 18:39:43 -08002131 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002132 texture = mCaches.textureCache.get(bitmap);
2133 if (!texture) {
2134 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002135 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002136 }
Romain Guyff316ec2013-02-13 18:39:43 -08002137 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002138 const AutoTexture autoCleanup(texture);
2139
2140 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002141 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002142
2143 int alpha;
2144 SkXfermode::Mode mode;
2145 getAlphaAndMode(paint, &alpha, &mode);
2146
Romain Guyff316ec2013-02-13 18:39:43 -08002147 float a = alpha / 255.0f;
2148
Romain Guya92bb4d2012-10-16 11:08:44 -07002149 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002150 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002151 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002152
Romain Guyff316ec2013-02-13 18:39:43 -08002153 setupDraw();
2154 setupDrawWithTextureAndColor();
2155 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002156 setupDrawColorFilter(getColorFilter(paint));
2157 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002158 setupDrawProgram();
2159 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002160 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002161 setupDrawTexture(texture->id);
2162 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002163 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002164 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002165
2166 glDrawArrays(GL_TRIANGLES, 0, count);
2167
Romain Guyff316ec2013-02-13 18:39:43 -08002168 int slot = mCaches.currentProgram->getAttrib("colors");
2169 if (slot >= 0) {
2170 glDisableVertexAttribArray(slot);
2171 }
2172
2173 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002174
Tom Hudson107843d2014-09-08 11:26:26 -04002175 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002176}
2177
Tom Hudson107843d2014-09-08 11:26:26 -04002178void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002179 float srcLeft, float srcTop, float srcRight, float srcBottom,
2180 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002181 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002182 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002183 return;
Romain Guy6926c722010-07-12 20:20:03 -07002184 }
2185
Romain Guya1d3c912011-12-13 14:55:06 -08002186 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002187 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002188 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002189 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002190
Romain Guy8ba548f2010-06-30 19:21:21 -07002191 const float width = texture->width;
2192 const float height = texture->height;
2193
Romain Guy3b748a42013-04-17 18:54:38 -07002194 float u1 = fmax(0.0f, srcLeft / width);
2195 float v1 = fmax(0.0f, srcTop / height);
2196 float u2 = fmin(1.0f, srcRight / width);
2197 float v2 = fmin(1.0f, srcBottom / height);
2198
2199 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002200
Romain Guy03750a02010-10-18 14:06:08 -07002201 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002202 resetDrawTextureTexCoords(u1, v1, u2, v2);
2203
Romain Guyd21b6e12011-11-30 20:21:23 -08002204 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2205
Romain Guy886b2752013-01-04 12:26:18 -08002206 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2207 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002208
Romain Guy886b2752013-01-04 12:26:18 -08002209 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2210 // Apply a scale transform on the canvas only when a shader is in use
2211 // Skia handles the ratio between the dst and src rects as a scale factor
2212 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002213 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002214 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002215
Chris Craikd6b65f62014-01-01 14:45:21 -08002216 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2217 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2218 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002219
2220 dstRight = x + (dstRight - dstLeft);
2221 dstBottom = y + (dstBottom - dstTop);
2222
2223 dstLeft = x;
2224 dstTop = y;
2225
Chris Craik678625242014-02-28 12:26:34 -08002226 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002227 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002228 } else {
Chris Craik678625242014-02-28 12:26:34 -08002229 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002230 }
2231
2232 if (CC_UNLIKELY(useScaleTransform)) {
2233 save(SkCanvas::kMatrix_SaveFlag);
2234 translate(dstLeft, dstTop);
2235 scale(scaleX, scaleY);
2236
2237 dstLeft = 0.0f;
2238 dstTop = 0.0f;
2239
2240 dstRight = srcRight - srcLeft;
2241 dstBottom = srcBottom - srcTop;
2242 }
2243
Mike Reed1103b322014-07-08 12:36:44 -04002244 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002245 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002246 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002247 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002248 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2249 } else {
2250 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002251 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002252 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002253 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2254 }
2255
2256 if (CC_UNLIKELY(useScaleTransform)) {
2257 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002258 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002259
2260 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002261
Tom Hudson107843d2014-09-08 11:26:26 -04002262 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002263}
2264
Tom Hudson107843d2014-09-08 11:26:26 -04002265void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002266 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002267 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002268 return;
Romain Guy6926c722010-07-12 20:20:03 -07002269 }
2270
Romain Guy4c2547f2013-06-11 16:19:24 -07002271 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002272 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2273 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002274
Tom Hudson107843d2014-09-08 11:26:26 -04002275 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002276}
2277
Tom Hudson107843d2014-09-08 11:26:26 -04002278void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002279 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2280 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002281 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002282 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002283 }
2284
Romain Guy211370f2012-02-01 16:10:55 -08002285 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002286 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002287 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002288 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002289 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002290
Romain Guya4adcf02013-02-28 12:15:35 -08002291 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2292 texture->setFilter(GL_LINEAR, true);
2293
Chris Craikd6b65f62014-01-01 14:45:21 -08002294 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002295 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002296 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002297 const float offsetX = left + currentTransform()->getTranslateX();
2298 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002299 const size_t count = mesh->quads.size();
2300 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002301 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002302 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002303 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2304 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2305 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002306 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002307 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002308 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002309 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002310 }
2311 }
2312
Chris Craik4063a0e2013-11-15 16:06:56 -08002313 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002314 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002315 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2316 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002317
Romain Guy3b748a42013-04-17 18:54:38 -07002318 right = x + right - left;
2319 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002320 left = x;
2321 top = y;
2322 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002323 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002324 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2325 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002326 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2327 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002328 }
Chet Haase48659092012-05-31 15:21:51 -07002329
Tom Hudson107843d2014-09-08 11:26:26 -04002330 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002331}
2332
Romain Guy03c00b52013-06-20 18:30:28 -07002333/**
2334 * Important note: this method is intended to draw batches of 9-patch objects and
2335 * will not set the scissor enable or dirty the current layer, if any.
2336 * The caller is responsible for properly dirtying the current layer.
2337 */
Tom Hudson107843d2014-09-08 11:26:26 -04002338void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002339 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002340 mCaches.activeTexture(0);
2341 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002342 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002343 const AutoTexture autoCleanup(texture);
2344
2345 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2346 texture->setFilter(GL_LINEAR, true);
2347
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002348 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2349 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002350 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002351
Tom Hudson107843d2014-09-08 11:26:26 -04002352 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002353}
2354
Tom Hudson107843d2014-09-08 11:26:26 -04002355void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002356 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002357 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002358 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002359 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002360 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002361 }
2362
Chris Craik0d5ac952014-07-15 13:01:02 -07002363 Rect bounds(vertexBuffer.getBounds());
2364 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002365 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2366
Chris Craikcb4d6002012-09-25 12:00:29 -07002367 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002368 bool isAA = paint->isAntiAlias();
2369
Chris Craik710f46d2012-09-17 17:25:49 -07002370 setupDraw();
2371 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002372 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Tom Hudson984162f2014-10-10 13:38:16 -04002373 setupDrawColor(color, ((color >> 24) & 0xFF) * writableSnapshot()->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002374 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002375 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002376 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002377 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002378 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2379 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002380 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002381 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002382 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002383
ztenghui55bfb4e2013-12-03 10:38:55 -08002384 const void* vertices = vertexBuffer.getBuffer();
Andreas Gampeedaecc12014-11-10 20:54:07 -08002385 mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002386 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002387 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002388
Chris Craik710f46d2012-09-17 17:25:49 -07002389 int alphaSlot = -1;
2390 if (isAA) {
2391 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2392 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002393 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002394 glEnableVertexAttribArray(alphaSlot);
2395 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002396 }
Romain Guy04299382012-07-18 17:15:41 -07002397
Chris Craik05f3d6e2014-06-02 16:27:04 -07002398 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2399 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002400 mCaches.unbindIndicesBuffer();
2401 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002402 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002403 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002404 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002405 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002406 mCaches.bindShadowIndicesBuffer();
2407 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002408 } else if (mode == VertexBuffer::kIndices) {
2409 mCaches.unbindIndicesBuffer();
2410 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2411 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002412 }
Romain Guy04299382012-07-18 17:15:41 -07002413
Chris Craik710f46d2012-09-17 17:25:49 -07002414 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002415 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002416 }
Chris Craik65cd6122012-12-10 17:56:27 -08002417
Tom Hudson107843d2014-09-08 11:26:26 -04002418 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002419}
2420
2421/**
Chris Craik65cd6122012-12-10 17:56:27 -08002422 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2423 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2424 * screen space in all directions. However, instead of using a fragment shader to compute the
2425 * translucency of the color from its position, we simply use a varying parameter to define how far
2426 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2427 *
2428 * Doesn't yet support joins, caps, or path effects.
2429 */
Tom Hudson107843d2014-09-08 11:26:26 -04002430void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002431 VertexBuffer vertexBuffer;
2432 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002433 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002434 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002435}
2436
2437/**
2438 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2439 * and additional geometry for defining an alpha slope perimeter.
2440 *
2441 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2442 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2443 * in-shader alpha region, but found it to be taxing on some GPUs.
2444 *
2445 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2446 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002447 */
Tom Hudson107843d2014-09-08 11:26:26 -04002448void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002449 if (mState.currentlyIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002450
Chris Craik65cd6122012-12-10 17:56:27 -08002451 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002452
Chris Craik65cd6122012-12-10 17:56:27 -08002453 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002454 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2455 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002456
Chris Craik05f3d6e2014-06-02 16:27:04 -07002457 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002458 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002459 }
2460
Chris Craikbf759452014-08-11 16:00:44 -07002461 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002462 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002463}
2464
Tom Hudson107843d2014-09-08 11:26:26 -04002465void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002466 if (mState.currentlyIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002467
Chris Craik6d29c8d2013-05-08 18:35:44 -07002468 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002469
Chris Craik6d29c8d2013-05-08 18:35:44 -07002470 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002471 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002472
Chris Craik05f3d6e2014-06-02 16:27:04 -07002473 const Rect& bounds = buffer.getBounds();
2474 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002475 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002476 }
2477
Chris Craikbf759452014-08-11 16:00:44 -07002478 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002479 drawVertexBuffer(buffer, paint, displayFlags);
2480
2481 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002482}
2483
Tom Hudson107843d2014-09-08 11:26:26 -04002484void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002485 // No need to check against the clip, we fill the clip region
Tom Hudson984162f2014-10-10 13:38:16 -04002486 if (mState.currentlyIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002487
Tom Hudson984162f2014-10-10 13:38:16 -04002488 Rect clip(*mState.currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002489 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002490
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002491 SkPaint paint;
2492 paint.setColor(color);
2493 paint.setXfermodeMode(mode);
2494
2495 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002496
Tom Hudson107843d2014-09-08 11:26:26 -04002497 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002498}
Romain Guy9d5316e2010-06-24 19:30:36 -07002499
Tom Hudson107843d2014-09-08 11:26:26 -04002500void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002501 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002502 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002503 const AutoTexture autoCleanup(texture);
2504
2505 const float x = left + texture->left - texture->offset;
2506 const float y = top + texture->top - texture->offset;
2507
2508 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002509
Tom Hudson107843d2014-09-08 11:26:26 -04002510 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002511}
2512
Tom Hudson107843d2014-09-08 11:26:26 -04002513void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002514 float rx, float ry, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002515 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002516 || quickRejectSetupScissor(left, top, right, bottom, p)
2517 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002518 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002519 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002520
Chris Craikcb4d6002012-09-25 12:00:29 -07002521 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002522 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002523 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002524 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002525 drawShape(left, top, texture, p);
2526 } else {
2527 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2528 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2529 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002530 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002531}
2532
Tom Hudson107843d2014-09-08 11:26:26 -04002533void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002534 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002535 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2536 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002537 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002538 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002539 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002540 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002541 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002542 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002543 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002544 SkPath path;
2545 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2546 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2547 } else {
2548 path.addCircle(x, y, radius);
2549 }
2550 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002551 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002552}
Romain Guy01d58e42011-01-19 21:54:02 -08002553
Tom Hudson107843d2014-09-08 11:26:26 -04002554void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002555 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002556 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002557 || quickRejectSetupScissor(left, top, right, bottom, p)
2558 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002559 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002560 }
Romain Guy01d58e42011-01-19 21:54:02 -08002561
Chris Craikcb4d6002012-09-25 12:00:29 -07002562 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002563 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002564 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002565 drawShape(left, top, texture, p);
2566 } else {
2567 SkPath path;
2568 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 path.addOval(rect);
2573 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002574 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002575}
2576
Tom Hudson107843d2014-09-08 11:26:26 -04002577void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002578 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002579 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002580 || quickRejectSetupScissor(left, top, right, bottom, p)
2581 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002582 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002583 }
2584
Chris Craik780c1282012-10-04 14:10:49 -07002585 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002586 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002587 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002588 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002589 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002590 drawShape(left, top, texture, p);
2591 return;
Chris Craik780c1282012-10-04 14:10:49 -07002592 }
Chris Craik780c1282012-10-04 14:10:49 -07002593 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2594 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2595 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2596 }
2597
2598 SkPath path;
2599 if (useCenter) {
2600 path.moveTo(rect.centerX(), rect.centerY());
2601 }
2602 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2603 if (useCenter) {
2604 path.close();
2605 }
Tom Hudson107843d2014-09-08 11:26:26 -04002606 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002607}
2608
Romain Guycf8675e2012-10-02 12:32:25 -07002609// See SkPaintDefaults.h
2610#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2611
Tom Hudson107843d2014-09-08 11:26:26 -04002612void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002613 const SkPaint* p) {
Tom Hudson984162f2014-10-10 13:38:16 -04002614 if (mState.currentlyIgnored()
Chris Craik947eabf2014-08-19 10:21:12 -07002615 || quickRejectSetupScissor(left, top, right, bottom, p)
2616 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002617 return;
Romain Guy6926c722010-07-12 20:20:03 -07002618 }
2619
Chris Craik710f46d2012-09-17 17:25:49 -07002620 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002621 // only fill style is supported by drawConvexPath, since others have to handle joins
2622 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2623 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2624 mCaches.activeTexture(0);
2625 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002626 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002627 drawShape(left, top, texture, p);
2628 } else {
2629 SkPath path;
2630 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2631 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2632 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2633 }
2634 path.addRect(rect);
2635 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002636 }
Chet Haase858aa932011-05-12 09:06:00 -07002637 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002638 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2639 SkPath path;
2640 path.addRect(left, top, right, bottom);
2641 drawConvexPath(path, p);
2642 } else {
2643 drawColorRect(left, top, right, bottom, p);
2644
2645 mDirty = true;
2646 }
Chet Haase858aa932011-05-12 09:06:00 -07002647 }
Romain Guyc7d53492010-06-25 13:41:57 -07002648}
Romain Guy9d5316e2010-06-24 19:30:36 -07002649
Chris Craikd218a922014-01-02 17:13:34 -08002650void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2651 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002652 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002653 mCaches.activeTexture(0);
2654
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002655 TextShadow textShadow;
2656 if (!getTextShadow(paint, &textShadow)) {
2657 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2658 }
2659
Raph Levien416a8472012-07-19 22:48:17 -07002660 // NOTE: The drop shadow will not perform gamma correction
2661 // if shader-based correction is enabled
2662 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2663 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002664 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002665 // If the drop shadow exceeds the max texture size or couldn't be
2666 // allocated, skip drawing
2667 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002668 const AutoTexture autoCleanup(shadow);
2669
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002670 const float sx = x - shadow->left + textShadow.dx;
2671 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002672
Tom Hudson984162f2014-10-10 13:38:16 -04002673 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * writableSnapshot()->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002674 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002675 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002676 }
2677
2678 setupDraw();
2679 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002680 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002681 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002682 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002683 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002684 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002685 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2686 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002687 setupDrawTexture(shadow->id);
2688 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002689 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002690 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002691 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2692
2693 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2694}
2695
Romain Guy768bffc2013-02-27 13:50:45 -08002696bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Tom Hudson984162f2014-10-10 13:38:16 -04002697 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * currentSnapshot()->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002698 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2699}
2700
Tom Hudson107843d2014-09-08 11:26:26 -04002701void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002702 const float* positions, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002703 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002704 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002705 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002706
Romain Guy671d6cf2012-01-18 12:39:17 -08002707 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002708 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002709 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002710 }
2711
Chris Craik39a908c2013-06-13 14:39:01 -07002712 mCaches.enableScissor();
2713
Romain Guy671d6cf2012-01-18 12:39:17 -08002714 float x = 0.0f;
2715 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002716 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002718 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2719 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002720 }
2721
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002722 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002723 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002724
2725 int alpha;
2726 SkXfermode::Mode mode;
2727 getAlphaAndMode(paint, &alpha, &mode);
2728
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002729 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002730 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002731 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002732 }
2733
Romain Guy671d6cf2012-01-18 12:39:17 -08002734 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002735 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 if (pureTranslate && !linearFilter) {
2737 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2738 }
Romain Guy257ae352013-03-20 16:31:12 -07002739 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002740
Tom Hudson984162f2014-10-10 13:38:16 -04002741 const Rect* clip = pureTranslate ? writableSnapshot()->clipRect : &writableSnapshot()->getLocalClip();
Romain Guy671d6cf2012-01-18 12:39:17 -08002742 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2743
Romain Guy211370f2012-02-01 16:10:55 -08002744 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002745
Victoria Lease1e546812013-06-25 14:25:17 -07002746 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002747 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002748 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 if (hasActiveLayer) {
2750 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002751 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002752 }
2753 dirtyLayerUnchecked(bounds, getRegion());
2754 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002755 }
Chet Haase48659092012-05-31 15:21:51 -07002756
Tom Hudson107843d2014-09-08 11:26:26 -04002757 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002758}
2759
Chris Craik59744b72014-07-01 17:56:52 -07002760bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002761 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002762 outMatrix->setIdentity();
2763 return false;
2764 } else if (CC_UNLIKELY(transform.isPerspective())) {
2765 outMatrix->setIdentity();
2766 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002767 }
Chris Craik59744b72014-07-01 17:56:52 -07002768
2769 /**
Chris Craika736cd92014-08-04 13:18:38 -07002770 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2771 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002772 */
2773 float sx, sy;
2774 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002775 outMatrix->setScale(
2776 roundf(fmaxf(1.0f, sx)),
2777 roundf(fmaxf(1.0f, sy)));
2778 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002779}
2780
Tom Hudson984162f2014-10-10 13:38:16 -04002781int OpenGLRenderer::getSaveCount() const {
2782 return mState.getSaveCount();
2783}
2784
2785int OpenGLRenderer::save(int flags) {
2786 return mState.save(flags);
2787}
2788
2789void OpenGLRenderer::restore() {
2790 return mState.restore();
2791}
2792
2793void OpenGLRenderer::restoreToCount(int saveCount) {
2794 return mState.restoreToCount(saveCount);
2795}
2796
2797void OpenGLRenderer::translate(float dx, float dy, float dz) {
2798 return mState.translate(dx, dy, dz);
2799}
2800
2801void OpenGLRenderer::rotate(float degrees) {
2802 return mState.rotate(degrees);
2803}
2804
2805void OpenGLRenderer::scale(float sx, float sy) {
2806 return mState.scale(sx, sy);
2807}
2808
2809void OpenGLRenderer::skew(float sx, float sy) {
2810 return mState.skew(sx, sy);
2811}
2812
2813void OpenGLRenderer::setMatrix(const Matrix4& matrix) {
2814 mState.setMatrix(matrix);
2815}
2816
2817void OpenGLRenderer::concatMatrix(const Matrix4& matrix) {
2818 mState.concatMatrix(matrix);
2819}
2820
2821bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
2822 return mState.clipRect(left, top, right, bottom, op);
2823}
2824
2825bool OpenGLRenderer::clipPath(const SkPath* path, SkRegion::Op op) {
2826 return mState.clipPath(path, op);
2827}
2828
2829bool OpenGLRenderer::clipRegion(const SkRegion* region, SkRegion::Op op) {
2830 return mState.clipRegion(region, op);
2831}
2832
2833void OpenGLRenderer::setClippingOutline(LinearAllocator& allocator, const Outline* outline) {
2834 mState.setClippingOutline(allocator, outline);
2835}
2836
2837void OpenGLRenderer::setClippingRoundRect(LinearAllocator& allocator,
2838 const Rect& rect, float radius, bool highPriority) {
2839 mState.setClippingRoundRect(allocator, rect, radius, highPriority);
2840}
2841
2842
2843
Tom Hudson107843d2014-09-08 11:26:26 -04002844void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002845 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002846 DrawOpMode drawOpMode) {
2847
Chris Craik527a3aa2013-03-04 10:19:31 -08002848 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002849 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2850 // drawing as ops from DeferredDisplayList are already filtered for these
Tom Hudson984162f2014-10-10 13:38:16 -04002851 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002852 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002853 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002854 }
Romain Guycac5fd32011-12-01 20:08:50 -08002855 }
2856
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002857 const float oldX = x;
2858 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002859
Chris Craikd6b65f62014-01-01 14:45:21 -08002860 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002861 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002862
Romain Guy211370f2012-02-01 16:10:55 -08002863 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002864 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2865 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002866 }
2867
Romain Guy86568192010-12-14 15:55:39 -08002868 int alpha;
2869 SkXfermode::Mode mode;
2870 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002871
Romain Guyc74f45a2013-02-26 19:10:14 -08002872 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2873
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002874 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002875 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002876 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002877 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002878 }
2879
Romain Guy19d4dd82013-03-04 11:14:26 -08002880 const bool hasActiveLayer = hasLayer();
2881
Romain Guy624234f2013-03-05 16:43:31 -08002882 // We only pass a partial transform to the font renderer. That partial
2883 // matrix defines how glyphs are rasterized. Typically we want glyphs
2884 // to be rasterized at their final size on screen, which means the partial
2885 // matrix needs to take the scale factor into account.
2886 // When a partial matrix is used to transform glyphs during rasterization,
2887 // the mesh is generated with the inverse transform (in the case of scale,
2888 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2889 // apply the full transform matrix at draw time in the vertex shader.
2890 // Applying the full matrix in the shader is the easiest way to handle
2891 // rotation and perspective and allows us to always generated quads in the
2892 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002893 SkMatrix fontTransform;
2894 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2895 || fabs(y - (int) y) > 0.0f
2896 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002897 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002898 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002899
Romain Guy624234f2013-03-05 16:43:31 -08002900 // TODO: Implement better clipping for scaled/rotated text
Tom Hudson984162f2014-10-10 13:38:16 -04002901 const Rect* clip = !pureTranslate ? NULL : mState.currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002902 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 -07002903
Raph Levien996e57c2012-07-23 15:22:52 -07002904 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002905 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002906
2907 // don't call issuedrawcommand, do it at end of batch
2908 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002909 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002910 SkPaint paintCopy(*paint);
2911 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2912 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002913 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002914 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002915 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002916 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002917 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002918
Chris Craik527a3aa2013-03-04 10:19:31 -08002919 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002920 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002921 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002922 }
Chris Craik41541822013-05-03 16:35:54 -07002923 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002924 }
Romain Guy694b5192010-07-21 21:33:20 -07002925
Chris Craike63f7c622013-10-17 10:30:55 -07002926 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002927
Tom Hudson107843d2014-09-08 11:26:26 -04002928 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002929}
2930
Tom Hudson107843d2014-09-08 11:26:26 -04002931void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002932 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002933 if (text == NULL || count == 0 || mState.currentlyIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002934 return;
Romain Guy03d58522012-02-24 17:54:07 -08002935 }
2936
Chris Craik39a908c2013-06-13 14:39:01 -07002937 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2938 mCaches.enableScissor();
2939
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002940 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002941 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002942 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002943
2944 int alpha;
2945 SkXfermode::Mode mode;
2946 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002947 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002948
Tom Hudson984162f2014-10-10 13:38:16 -04002949 const Rect* clip = &writableSnapshot()->getLocalClip();
Romain Guy97771732012-02-28 18:17:02 -08002950 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 -08002951
Romain Guy97771732012-02-28 18:17:02 -08002952 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002953
2954 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002955 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002956 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002957 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002958 dirtyLayerUnchecked(bounds, getRegion());
2959 }
Romain Guy97771732012-02-28 18:17:02 -08002960 }
Chet Haase48659092012-05-31 15:21:51 -07002961
Tom Hudson107843d2014-09-08 11:26:26 -04002962 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002963}
2964
Tom Hudson107843d2014-09-08 11:26:26 -04002965void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04002966 if (mState.currentlyIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002967
Romain Guya1d3c912011-12-13 14:55:06 -08002968 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002969
Romain Guyfb8b7632010-08-23 21:05:08 -07002970 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002971 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002972 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002973
Romain Guy8b55f372010-08-18 17:10:07 -07002974 const float x = texture->left - texture->offset;
2975 const float y = texture->top - texture->offset;
2976
Romain Guy01d58e42011-01-19 21:54:02 -08002977 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002978 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002979}
2980
Tom Hudson107843d2014-09-08 11:26:26 -04002981void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002982 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002983 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002984 }
2985
Romain Guyb2e2f242012-10-17 18:18:35 -07002986 mat4* transform = NULL;
2987 if (layer->isTextureLayer()) {
2988 transform = &layer->getTransform();
2989 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002990 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002991 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002992 }
2993 }
2994
Chris Craik39a908c2013-06-13 14:39:01 -07002995 bool clipRequired = false;
Tom Hudson984162f2014-10-10 13:38:16 -04002996 const bool rejected = mState.calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002997 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002998
2999 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003000 if (transform && !transform->isIdentity()) {
3001 restore();
3002 }
Tom Hudson107843d2014-09-08 11:26:26 -04003003 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08003004 }
3005
Chris Craik62d307c2014-07-29 10:35:13 -07003006 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
3007 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
3008
Romain Guy5bb3c732012-11-29 17:52:58 -08003009 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003010
Chris Craik39a908c2013-06-13 14:39:01 -07003011 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003012 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003013
Romain Guy211370f2012-02-01 16:10:55 -08003014 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08003015 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003016 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3017 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003018 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003019
Chris Craik16ecda52013-03-29 10:59:59 -07003020 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003021 setupDraw();
3022 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003023 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003024 setupDrawColorFilter(layer->getColorFilter());
3025 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003026 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003027 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003028 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07003029 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08003030 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3031 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
3032 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003033
Romain Guyd21b6e12011-11-30 20:21:23 -08003034 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003035 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003036 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003037 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003038 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003039 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003040 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3041 }
Romain Guyf219da52011-01-16 12:54:25 -08003042
Romain Guy448455f2013-07-22 13:57:50 -07003043 TextureVertex* mesh = &layer->mesh[0];
3044 GLsizei elementsCount = layer->meshElementCount;
3045
3046 while (elementsCount > 0) {
3047 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3048
Romain Guy3380cfd2013-08-15 16:57:57 -07003049 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003050 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3051 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3052
3053 elementsCount -= drawCount;
3054 // Though there are 4 vertices in a quad, we use 6 indices per
3055 // quad to draw with GL_TRIANGLES
3056 mesh += (drawCount / 6) * 4;
3057 }
Romain Guyf219da52011-01-16 12:54:25 -08003058
Romain Guy3a3133d2011-02-01 22:59:58 -08003059#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003060 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003061#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003062 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003063
Romain Guy5bb3c732012-11-29 17:52:58 -08003064 if (layer->debugDrawUpdate) {
3065 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003066
3067 SkPaint paint;
3068 paint.setColor(0x7f00ff00);
3069 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003070 }
Romain Guyf219da52011-01-16 12:54:25 -08003071 }
Chris Craik34416ea2013-04-15 16:08:28 -07003072 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003073
Romain Guyb2e2f242012-10-17 18:18:35 -07003074 if (transform && !transform->isIdentity()) {
3075 restore();
3076 }
3077
Tom Hudson107843d2014-09-08 11:26:26 -04003078 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003079}
3080
Romain Guy6926c722010-07-12 20:20:03 -07003081///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003082// Draw filters
3083///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003084void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3085 // We should never get here since we apply the draw filter when stashing
3086 // the paints in the DisplayList.
3087 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003088}
3089
3090///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003091// Drawing implementation
3092///////////////////////////////////////////////////////////////////////////////
3093
Chris Craikd218a922014-01-02 17:13:34 -08003094Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003095 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3096 if (!texture) {
3097 return mCaches.textureCache.get(bitmap);
3098 }
3099 return texture;
3100}
3101
Romain Guy01d58e42011-01-19 21:54:02 -08003102void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003103 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003104 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003105 return;
3106 }
3107
3108 int alpha;
3109 SkXfermode::Mode mode;
3110 getAlphaAndMode(paint, &alpha, &mode);
3111
3112 setupDraw();
3113 setupDrawWithTexture(true);
3114 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003115 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003116 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003117 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003118 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003119 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3120 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003121 setupDrawTexture(texture->id);
3122 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003123 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003124 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003125 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3126
3127 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003128}
3129
Romain Guyf607bdc2010-09-10 19:20:06 -07003130// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003131#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3132#define kStdUnderline_Offset (1.0f / 9.0f)
3133#define kStdUnderline_Thickness (1.0f / 18.0f)
3134
Chris Craikd218a922014-01-02 17:13:34 -08003135void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3136 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003137 // Handle underline and strike-through
3138 uint32_t flags = paint->getFlags();
3139 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003140 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003141
Romain Guy211370f2012-02-01 16:10:55 -08003142 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003143 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003144 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003145
Raph Levien8b4072d2012-07-30 15:50:00 -07003146 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003147 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003148
Romain Guyf6834472011-01-23 13:32:12 -08003149 int linesCount = 0;
3150 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3151 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3152
3153 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003154 float points[pointsCount];
3155 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003156
3157 if (flags & SkPaint::kUnderlineText_Flag) {
3158 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003159 points[currentPoint++] = left;
3160 points[currentPoint++] = top;
3161 points[currentPoint++] = left + underlineWidth;
3162 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003163 }
3164
3165 if (flags & SkPaint::kStrikeThruText_Flag) {
3166 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003167 points[currentPoint++] = left;
3168 points[currentPoint++] = top;
3169 points[currentPoint++] = left + underlineWidth;
3170 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003171 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003172
Romain Guy726aeba2011-06-01 14:52:00 -07003173 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003174
Romain Guy726aeba2011-06-01 14:52:00 -07003175 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003176 }
3177 }
3178}
3179
Tom Hudson107843d2014-09-08 11:26:26 -04003180void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Tom Hudson984162f2014-10-10 13:38:16 -04003181 if (mState.currentlyIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003182 return;
Romain Guy672433d2013-01-04 19:05:13 -08003183 }
3184
Tom Hudson107843d2014-09-08 11:26:26 -04003185 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003186}
3187
Tom Hudson107843d2014-09-08 11:26:26 -04003188void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003189 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson984162f2014-10-10 13:38:16 -04003190 if (mState.currentlyIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003191
Chris Craik15a07a22014-01-26 13:43:53 -08003192 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003193 mCaches.enableScissor();
3194
3195 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003196 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003197
ztenghui14a4e352014-08-13 10:44:39 -07003198 // The caller has made sure casterAlpha > 0.
3199 float ambientShadowAlpha = mAmbientShadowAlpha;
3200 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3201 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3202 }
3203 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3204 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003205 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003206 }
ztenghui7b4516e2014-01-07 10:42:55 -08003207
ztenghui14a4e352014-08-13 10:44:39 -07003208 float spotShadowAlpha = mSpotShadowAlpha;
3209 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3210 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3211 }
3212 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3213 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003214 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003215 }
ztenghui7b4516e2014-01-07 10:42:55 -08003216
Tom Hudson107843d2014-09-08 11:26:26 -04003217 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003218}
3219
Tom Hudson107843d2014-09-08 11:26:26 -04003220void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003221 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003222 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003223 return;
Romain Guy3b753822013-03-05 10:27:35 -08003224 }
Romain Guy735738c2012-12-03 12:34:51 -08003225
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003226 int color = paint->getColor();
3227 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003228 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003229 color |= 0x00ffffff;
3230 }
3231
Romain Guy672433d2013-01-04 19:05:13 -08003232 float left = FLT_MAX;
3233 float top = FLT_MAX;
3234 float right = FLT_MIN;
3235 float bottom = FLT_MIN;
3236
Romain Guy448455f2013-07-22 13:57:50 -07003237 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003238 Vertex* vertex = mesh;
3239
Chris Craik2af46352012-11-26 18:30:17 -08003240 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003241 float l = rects[index + 0];
3242 float t = rects[index + 1];
3243 float r = rects[index + 2];
3244 float b = rects[index + 3];
3245
Romain Guy3b753822013-03-05 10:27:35 -08003246 Vertex::set(vertex++, l, t);
3247 Vertex::set(vertex++, r, t);
3248 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003249 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003250
Romain Guy3b753822013-03-05 10:27:35 -08003251 left = fminf(left, l);
3252 top = fminf(top, t);
3253 right = fmaxf(right, r);
3254 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003255 }
3256
Chris Craikf0a59072013-11-19 18:00:46 -08003257 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003258 return;
Romain Guya362c692013-02-04 13:50:16 -08003259 }
Romain Guy672433d2013-01-04 19:05:13 -08003260
Romain Guy672433d2013-01-04 19:05:13 -08003261 setupDraw();
3262 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003263 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003264 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003265 setupDrawColorFilter(getColorFilter(paint));
3266 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003267 setupDrawProgram();
3268 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003269 setupDrawModelView(kModelViewMode_Translate, false,
3270 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003271 setupDrawColorUniforms(getShader(paint));
3272 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003273 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003274
Romain Guy8ce00302013-01-15 18:51:42 -08003275 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003276 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003277 }
3278
Chris Craik4063a0e2013-11-15 16:06:56 -08003279 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003280
Tom Hudson107843d2014-09-08 11:26:26 -04003281 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003282}
3283
Romain Guy026c5e162010-06-28 17:12:22 -07003284void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003285 const SkPaint* paint, bool ignoreTransform) {
3286 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003287 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003288 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003289 color |= 0x00ffffff;
3290 }
3291
Romain Guy70ca14e2010-12-13 18:24:33 -08003292 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003293 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003294 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003295 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003296 setupDrawColorFilter(getColorFilter(paint));
3297 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003298 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003299 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3300 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003301 setupDrawColorUniforms(getShader(paint));
3302 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003303 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003304 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003305
Romain Guyc95c8d62010-09-17 15:31:32 -07003306 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3307}
3308
Romain Guy82ba8142010-07-09 13:25:56 -07003309void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003310 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003311 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003312
Romain Guy3b748a42013-04-17 18:54:38 -07003313 GLvoid* vertices = (GLvoid*) NULL;
3314 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3315
3316 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003317 vertices = &mMeshVertices[0].x;
3318 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003319
3320 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3321 texture->uvMapper->map(uvs);
3322
3323 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3324 }
3325
Chris Craikd6b65f62014-01-01 14:45:21 -08003326 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3327 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3328 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003329
Romain Guyd21b6e12011-11-30 20:21:23 -08003330 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003331 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003332 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003333 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003334 } else {
Chris Craik678625242014-02-28 12:26:34 -08003335 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003336 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003337 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3338 }
3339
3340 if (texture->uvMapper) {
3341 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003342 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003343}
3344
Romain Guyf7f93552010-07-08 19:17:03 -07003345void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003346 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003347 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003348 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3349 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003350
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003351 int a;
3352 SkXfermode::Mode mode;
3353 getAlphaAndMode(paint, &a, &mode);
3354 const float alpha = a / 255.0f;
3355
Romain Guy746b7402010-10-26 16:27:31 -07003356 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003357 setupDrawWithTexture();
3358 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003359 setupDrawColorFilter(getColorFilter(paint));
3360 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003361 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003362 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003363 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003364 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003365 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003367 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003368
Romain Guy6820ac82010-09-15 18:11:50 -07003369 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003370}
3371
Romain Guy3b748a42013-04-17 18:54:38 -07003372void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003373 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003374 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003375 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3376 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003377
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003378 int a;
3379 SkXfermode::Mode mode;
3380 getAlphaAndMode(paint, &a, &mode);
3381 const float alpha = a / 255.0f;
3382
Romain Guy3b748a42013-04-17 18:54:38 -07003383 setupDraw();
3384 setupDrawWithTexture();
3385 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003386 setupDrawColorFilter(getColorFilter(paint));
3387 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003388 setupDrawProgram();
3389 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003390 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003391 setupDrawTexture(texture);
3392 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003393 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003394 setupDrawMeshIndices(vertices, texCoords, vbo);
3395
3396 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003397}
3398
Romain Guy886b2752013-01-04 12:26:18 -08003399void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003400 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003401 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003402 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003403
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003404 int color = paint != NULL ? paint->getColor() : 0;
3405 int alpha;
3406 SkXfermode::Mode mode;
3407 getAlphaAndMode(paint, &alpha, &mode);
3408
Romain Guy886b2752013-01-04 12:26:18 -08003409 setupDraw();
3410 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003411 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003412 setupDrawAlpha8Color(color, alpha);
3413 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003414 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003415 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003416 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003417 setupDrawProgram();
3418 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003419 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003420 setupDrawTexture(texture);
3421 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003422 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003423 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003424 setupDrawMesh(vertices, texCoords);
3425
3426 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003427}
3428
Romain Guya5aed0d2010-09-09 14:42:43 -07003429void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003430 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003431
Tom Hudson984162f2014-10-10 13:38:16 -04003432 if (writableSnapshot()->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003433 blend = true;
3434 mDescription.hasRoundRectClip = true;
3435 }
3436 mSkipOutlineClip = true;
3437
Romain Guy82ba8142010-07-09 13:25:56 -07003438 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003439
Romain Guy82ba8142010-07-09 13:25:56 -07003440 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003441 // These blend modes are not supported by OpenGL directly and have
3442 // to be implemented using shaders. Since the shader will perform
3443 // the blending, turn blending off here
3444 // If the blend mode cannot be implemented using shaders, fall
3445 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003446 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003447 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003448 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003449 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003450
Romain Guy82bc7a72012-01-03 14:13:39 -08003451 if (mCaches.blend) {
3452 glDisable(GL_BLEND);
3453 mCaches.blend = false;
3454 }
3455
3456 return;
3457 } else {
3458 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003459 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003460 }
3461
3462 if (!mCaches.blend) {
3463 glEnable(GL_BLEND);
3464 }
3465
3466 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3467 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3468
3469 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3470 glBlendFunc(sourceMode, destMode);
3471 mCaches.lastSrcMode = sourceMode;
3472 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003473 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003474 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003475 glDisable(GL_BLEND);
3476 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003477 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003478}
3479
Romain Guy889f8d12010-07-29 14:37:42 -07003480bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003481 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003482 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003483 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003484 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003485 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003486 }
Romain Guy6926c722010-07-12 20:20:03 -07003487 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003488}
3489
Romain Guy026c5e162010-06-28 17:12:22 -07003490void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003491 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003492 TextureVertex::setUV(v++, u1, v1);
3493 TextureVertex::setUV(v++, u2, v1);
3494 TextureVertex::setUV(v++, u1, v2);
3495 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003496}
3497
Chris Craikd218a922014-01-02 17:13:34 -08003498void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003499 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003500 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3501 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003502 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003503 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003504 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003505}
3506
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003507float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003508 float alpha;
3509 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3510 alpha = mDrawModifiers.mOverrideLayerAlpha;
3511 } else {
3512 alpha = layer->getAlpha() / 255.0f;
3513 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003514 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003515}
3516
Romain Guy9d5316e2010-06-24 19:30:36 -07003517}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003518}; // namespace android