blob: 1ac9e7bdf0ba0845466f231ed0eca417d8fe4e3e [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)
Chris Craik058fc642014-07-23 18:19:28 -0700150 : mFrameStarted(false)
151 , mCaches(Caches::getInstance())
John Reck3b202512014-06-23 13:13:08 -0700152 , mExtensions(Extensions::getInstance())
Chris Craik058fc642014-07-23 18:19:28 -0700153 , mRenderState(renderState)
154 , mScissorOptimizationDisabled(false)
Chris Craik284b2432014-09-18 16:05:35 -0700155 , mSuppressTiling(false)
156 , mFirstFrameAfterResize(true)
Tom Hudson107843d2014-09-08 11:26:26 -0400157 , mDirty(false)
John Reck1aa5d2d2014-07-24 13:38:28 -0700158 , mLightCenter((Vector3){FLT_MIN, FLT_MIN, FLT_MIN})
Chris Craik058fc642014-07-23 18:19:28 -0700159 , mLightRadius(FLT_MIN)
160 , mAmbientShadowAlpha(0)
161 , mSpotShadowAlpha(0) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800162 // *set* draw modifiers to be 0
163 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700164 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700165
Romain Guyac670c02010-07-27 17:39:27 -0700166 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
Romain Guye4d01122010-06-16 18:44:05 -0700167}
168
Romain Guy85bf02f2010-06-22 13:11:24 -0700169OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700170 // The context has already been destroyed at this point, do not call
171 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700172}
173
Romain Guy87e2f7572012-09-24 11:37:12 -0700174void OpenGLRenderer::initProperties() {
175 char property[PROPERTY_VALUE_MAX];
176 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
177 mScissorOptimizationDisabled = !strcasecmp(property, "true");
178 INIT_LOGD(" Scissor optimization %s",
179 mScissorOptimizationDisabled ? "disabled" : "enabled");
180 } else {
181 INIT_LOGD(" Scissor optimization enabled");
182 }
Romain Guy13631f32012-01-30 17:41:55 -0800183}
184
Chris Craik058fc642014-07-23 18:19:28 -0700185void OpenGLRenderer::initLight(const Vector3& lightCenter, float lightRadius,
186 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
187 mLightCenter = lightCenter;
188 mLightRadius = lightRadius;
189 mAmbientShadowAlpha = ambientShadowAlpha;
190 mSpotShadowAlpha = spotShadowAlpha;
191}
192
Romain Guy13631f32012-01-30 17:41:55 -0800193///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700194// Setup
195///////////////////////////////////////////////////////////////////////////////
196
Chris Craik797b95b2014-05-20 18:10:25 -0700197void OpenGLRenderer::onViewportInitialized() {
Romain Guy35643dd2012-09-18 15:40:58 -0700198 glDisable(GL_DITHER);
199 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
200
201 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik284b2432014-09-18 16:05:35 -0700202 mFirstFrameAfterResize = true;
Romain Guy35643dd2012-09-18 15:40:58 -0700203}
204
Romain Guy96885eb2013-03-26 15:05:58 -0700205void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800206 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800207 mCaches.clearGarbage();
Chris Craik69e5adf2014-08-14 13:34:01 -0700208 initializeSaveStack(left, top, right, bottom, mLightCenter);
Romain Guy96885eb2013-03-26 15:05:58 -0700209 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700210 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700211}
212
Tom Hudson107843d2014-09-08 11:26:26 -0400213void OpenGLRenderer::startFrame() {
214 if (mFrameStarted) return;
Romain Guy96885eb2013-03-26 15:05:58 -0700215 mFrameStarted = true;
216
Romain Guy41308e22012-10-22 20:02:43 -0700217 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700218
Romain Guy96885eb2013-03-26 15:05:58 -0700219 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700220
John Reck3b202512014-06-23 13:13:08 -0700221 mRenderState.setViewport(getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800222
Romain Guy54c1a642012-09-27 17:55:46 -0700223 // Functors break the tiling extension in pretty spectacular ways
224 // This ensures we don't use tiling when a functor is going to be
225 // invoked during the frame
Chris Craik284b2432014-09-18 16:05:35 -0700226 mSuppressTiling = mCaches.hasRegisteredFunctors()
227 || mFirstFrameAfterResize;
228 mFirstFrameAfterResize = false;
Romain Guy54c1a642012-09-27 17:55:46 -0700229
Chris Craikd6b65f62014-01-01 14:45:21 -0800230 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700231
Romain Guy7c450aa2012-09-21 19:15:00 -0700232 debugOverdraw(true, true);
233
Tom Hudson107843d2014-09-08 11:26:26 -0400234 clear(mTilingClip.left, mTilingClip.top,
Romain Guy96885eb2013-03-26 15:05:58 -0700235 mTilingClip.right, mTilingClip.bottom, mOpaque);
236}
237
Tom Hudson107843d2014-09-08 11:26:26 -0400238void OpenGLRenderer::prepareDirty(float left, float top,
Romain Guy96885eb2013-03-26 15:05:58 -0700239 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700240
Romain Guy96885eb2013-03-26 15:05:58 -0700241 setupFrameState(left, top, right, bottom, opaque);
242
243 // Layer renderers will start the frame immediately
244 // The framebuffer renderer will first defer the display list
245 // for each layer and wait until the first drawing command
246 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800247 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700248 syncState();
249 updateLayers();
250 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400251 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -0700252 }
Romain Guy7c25aab2012-10-18 15:05:02 -0700253}
254
Romain Guydcfc8362013-01-03 13:08:57 -0800255void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
256 // If we know that we are going to redraw the entire framebuffer,
257 // perform a discard to let the driver know we don't need to preserve
258 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800259 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800260 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800261 const bool isFbo = getTargetFbo() == 0;
262 const GLenum attachments[] = {
263 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
264 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800265 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
266 }
267}
268
Tom Hudson107843d2014-09-08 11:26:26 -0400269void OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
John Reck23d307c2014-10-27 12:38:48 -0700270 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700271 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700272 mCaches.setScissor(left, getViewportHeight() - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700273 glClear(GL_COLOR_BUFFER_BIT);
Tom Hudson107843d2014-09-08 11:26:26 -0400274 mDirty = true;
275 return;
Romain Guyddf74372012-05-22 14:07:07 -0700276 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700277
Romain Guy7c25aab2012-10-18 15:05:02 -0700278 mCaches.resetScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700279}
280
281void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700282 if (mCaches.blend) {
283 glEnable(GL_BLEND);
284 } else {
285 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700286 }
Romain Guybb9524b2010-06-22 18:56:38 -0700287}
288
henry.uh_chen33f5a592014-07-02 19:36:56 +0800289void OpenGLRenderer::startTilingCurrentClip(bool opaque, bool expand) {
Romain Guy54c1a642012-09-27 17:55:46 -0700290 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800291 const Snapshot* snapshot = currentSnapshot();
292
Chris Craik14e51302013-12-30 15:32:54 -0800293 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800294 if (snapshot->flags & Snapshot::kFlagFboTarget) {
295 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700296 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700297
henry.uh_chen33f5a592014-07-02 19:36:56 +0800298 startTiling(*clip, getViewportHeight(), opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800299 }
300}
301
henry.uh_chen33f5a592014-07-02 19:36:56 +0800302void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800303 if (!mSuppressTiling) {
henry.uh_chen33f5a592014-07-02 19:36:56 +0800304 if(expand) {
305 // Expand the startTiling region by 1
306 int leftNotZero = (clip.left > 0) ? 1 : 0;
307 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
308
309 mCaches.startTiling(
310 clip.left - leftNotZero,
311 windowHeight - clip.bottom - topNotZero,
312 clip.right - clip.left + leftNotZero + 1,
313 clip.bottom - clip.top + topNotZero + 1,
314 opaque);
315 } else {
316 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800317 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen33f5a592014-07-02 19:36:56 +0800318 }
Romain Guy54c1a642012-09-27 17:55:46 -0700319 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700320}
321
322void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700323 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700324}
325
Tom Hudson107843d2014-09-08 11:26:26 -0400326bool OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700327 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700328 endTiling();
329
Romain Guyca89e2a2013-03-08 17:44:20 -0800330 // When finish() is invoked on FBO 0 we've reached the end
331 // of the current frame
332 if (getTargetFbo() == 0) {
333 mCaches.pathCache.trim();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700334 mCaches.tessellationCache.trim();
Romain Guyca89e2a2013-03-08 17:44:20 -0800335 }
336
Romain Guy11cb6422012-09-21 00:39:43 -0700337 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700339 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700340#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700341
Romain Guyc15008e2010-11-10 11:59:15 -0800342#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800343 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700344#else
345 if (mCaches.getDebugLevel() & kDebugMemory) {
346 mCaches.dumpMemoryUsage();
347 }
Romain Guyc15008e2010-11-10 11:59:15 -0800348#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700349 }
Romain Guy96885eb2013-03-26 15:05:58 -0700350
351 mFrameStarted = false;
Tom Hudson107843d2014-09-08 11:26:26 -0400352
353 return reportAndClearDirty();
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy35643dd2012-09-18 15:40:58 -0700356void OpenGLRenderer::resumeAfterLayer() {
John Reck3b202512014-06-23 13:13:08 -0700357 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
358 mRenderState.bindFramebuffer(currentSnapshot()->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700359 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700360
361 mCaches.resetScissor();
362 dirtyClip();
363}
364
Tom Hudson107843d2014-09-08 11:26:26 -0400365void OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
366 if (currentSnapshot()->isIgnored()) return;
Chris Craik408eb122013-03-26 18:55:15 -0700367
Chris Craikd6b65f62014-01-01 14:45:21 -0800368 Rect clip(*currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700369 clip.snapToPixelBoundaries();
370
Romain Guyd643bb52011-03-01 14:55:21 -0800371 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800372 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800373 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800374 dirtyLayerUnchecked(clip, getRegion());
375 }
Romain Guyd643bb52011-03-01 14:55:21 -0800376
Romain Guy08aa2cb2011-03-17 11:06:57 -0700377 DrawGlInfo info;
378 info.clipLeft = clip.left;
379 info.clipTop = clip.top;
380 info.clipRight = clip.right;
381 info.clipBottom = clip.bottom;
382 info.isLayer = hasLayer();
Chris Craika64a2be2014-05-14 14:17:01 -0700383 info.width = getViewportWidth();
384 info.height = getViewportHeight();
Chris Craikd6b65f62014-01-01 14:45:21 -0800385 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700386
John Reck3b202512014-06-23 13:13:08 -0700387 bool prevDirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700388 // setup GL state for functor
389 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700390 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
391 }
John Reck3b202512014-06-23 13:13:08 -0700392 if (mCaches.enableScissor() || prevDirtyClip) {
John Reck25d2f7b2013-09-10 13:12:09 -0700393 setScissorFromClip();
394 }
Chris Craik54f574a2013-08-26 11:23:46 -0700395
John Reck3b202512014-06-23 13:13:08 -0700396 mRenderState.invokeFunctor(functor, DrawGlInfo::kModeDraw, &info);
397 // Scissor may have been modified, reset dirty clip
398 dirtyClip();
Romain Guycabfcc12011-03-07 18:06:46 -0800399
Tom Hudson107843d2014-09-08 11:26:26 -0400400 mDirty = true;
Chet Haasedaf98e92011-01-10 14:10:36 -0800401}
402
Romain Guyf6a11b82010-06-23 17:47:49 -0700403///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700404// Debug
405///////////////////////////////////////////////////////////////////////////////
406
Chris Craik62d307c2014-07-29 10:35:13 -0700407void OpenGLRenderer::eventMarkDEBUG(const char* fmt, ...) const {
408#if DEBUG_DETAILED_EVENTS
409 const int BUFFER_SIZE = 256;
410 va_list ap;
411 char buf[BUFFER_SIZE];
412
413 va_start(ap, fmt);
414 vsnprintf(buf, BUFFER_SIZE, fmt, ap);
415 va_end(ap);
416
417 eventMark(buf);
418#endif
419}
420
421
Romain Guy0f667532013-03-01 14:31:04 -0800422void OpenGLRenderer::eventMark(const char* name) const {
423 mCaches.eventMark(0, name);
424}
425
Romain Guy87e2f7572012-09-24 11:37:12 -0700426void OpenGLRenderer::startMark(const char* name) const {
427 mCaches.startMark(0, name);
428}
429
430void OpenGLRenderer::endMark() const {
431 mCaches.endMark();
432}
433
434void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
John Reck3b202512014-06-23 13:13:08 -0700435 mRenderState.debugOverdraw(enable, clear);
Romain Guy87e2f7572012-09-24 11:37:12 -0700436}
437
438void OpenGLRenderer::renderOverdraw() {
439 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700440 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700441
442 mCaches.enableScissor();
Chris Craika64a2be2014-05-14 14:17:01 -0700443 mCaches.setScissor(clip->left, firstSnapshot()->getViewportHeight() - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700444 clip->right - clip->left, clip->bottom - clip->top);
445
Romain Guy627c6fd2013-08-21 11:53:18 -0700446 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700447 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700448 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
449
450 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700451 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700452 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
453
454 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700455 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700456 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
457
458 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700459 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700460 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
461
Romain Guy87e2f7572012-09-24 11:37:12 -0700462 mCaches.stencil.disable();
463 }
464}
465
466///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700467// Layers
468///////////////////////////////////////////////////////////////////////////////
469
470bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Chris Craika7090e02014-06-20 16:01:00 -0700471 if (layer->deferredUpdateScheduled && layer->renderer
472 && layer->renderNode.get() && layer->renderNode->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700473 ATRACE_CALL();
474
Romain Guy11cb6422012-09-21 00:39:43 -0700475 Rect& dirty = layer->dirtyRect;
476
Romain Guy7c450aa2012-09-21 19:15:00 -0700477 if (inFrame) {
478 endTiling();
479 debugOverdraw(false, false);
480 }
Romain Guy11cb6422012-09-21 00:39:43 -0700481
Romain Guy96885eb2013-03-26 15:05:58 -0700482 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Chris Craik69e5adf2014-08-14 13:34:01 -0700483 layer->render(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700484 } else {
Chris Craik69e5adf2014-08-14 13:34:01 -0700485 layer->defer(*this);
Romain Guy96885eb2013-03-26 15:05:58 -0700486 }
Romain Guy11cb6422012-09-21 00:39:43 -0700487
488 if (inFrame) {
489 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800490 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700491 }
492
Romain Guy5bb3c732012-11-29 17:52:58 -0800493 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700494 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700495
496 return true;
497 }
498
499 return false;
500}
501
502void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700503 // If draw deferring is enabled this method will simply defer
504 // the display list of each individual layer. The layers remain
505 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700506 int count = mLayerUpdates.size();
507 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700508 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
509 startMark("Layer Updates");
510 } else {
511 startMark("Defer Layer Updates");
512 }
Romain Guy11cb6422012-09-21 00:39:43 -0700513
Chris Craik1206b9b2013-04-04 14:46:24 -0700514 // Note: it is very important to update the layers in order
515 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700516 Layer* layer = mLayerUpdates.itemAt(i).get();
Romain Guy11cb6422012-09-21 00:39:43 -0700517 updateLayer(layer, false);
Romain Guy11cb6422012-09-21 00:39:43 -0700518 }
Romain Guy11cb6422012-09-21 00:39:43 -0700519
Romain Guy96885eb2013-03-26 15:05:58 -0700520 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
521 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700522 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700523 }
524 endMark();
525 }
526}
527
528void OpenGLRenderer::flushLayers() {
529 int count = mLayerUpdates.size();
530 if (count > 0) {
531 startMark("Apply Layer Updates");
532 char layerName[12];
533
Chris Craik1206b9b2013-04-04 14:46:24 -0700534 // Note: it is very important to update the layers in order
535 for (int i = 0; i < count; i++) {
John Reck0e89e2b2014-10-31 14:49:06 -0700536 Layer* layer = mLayerUpdates.itemAt(i).get();
Chris Craika8bea8e2014-09-24 11:29:43 -0700537
Romain Guy96885eb2013-03-26 15:05:58 -0700538 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700539 startMark(layerName);
Chris Craika8bea8e2014-09-24 11:29:43 -0700540 ATRACE_FORMAT_BEGIN("flushLayer %ux%u", layer->getWidth(), layer->getHeight());
Romain Guy02b49b72013-03-29 12:37:16 -0700541
Romain Guy02b49b72013-03-29 12:37:16 -0700542 layer->flush();
Chris Craika8bea8e2014-09-24 11:29:43 -0700543
Romain Guy40543602013-06-12 15:31:28 -0700544 ATRACE_END();
Chris Craika8bea8e2014-09-24 11:29:43 -0700545 endMark();
Romain Guy96885eb2013-03-26 15:05:58 -0700546 }
547
548 mLayerUpdates.clear();
John Reck3b202512014-06-23 13:13:08 -0700549 mRenderState.bindFramebuffer(getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700550
Romain Guy11cb6422012-09-21 00:39:43 -0700551 endMark();
552 }
553}
554
555void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
556 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700557 // Make sure we don't introduce duplicates.
558 // SortedVector would do this automatically but we need to respect
559 // the insertion order. The linear search is not an issue since
560 // this list is usually very short (typically one item, at most a few)
561 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
562 if (mLayerUpdates.itemAt(i) == layer) {
563 return;
564 }
565 }
Romain Guy11cb6422012-09-21 00:39:43 -0700566 mLayerUpdates.push_back(layer);
Romain Guy11cb6422012-09-21 00:39:43 -0700567 }
568}
569
Romain Guye93482f2013-06-17 13:14:51 -0700570void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
571 if (layer) {
572 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
573 if (mLayerUpdates.itemAt(i) == layer) {
574 mLayerUpdates.removeAt(i);
Romain Guye93482f2013-06-17 13:14:51 -0700575 break;
576 }
577 }
578 }
579}
580
Romain Guy40543602013-06-12 15:31:28 -0700581void OpenGLRenderer::flushLayerUpdates() {
John Recka7c2ea22014-08-08 13:21:00 -0700582 ATRACE_CALL();
Romain Guy40543602013-06-12 15:31:28 -0700583 syncState();
584 updateLayers();
585 flushLayers();
586 // Wait for all the layer updates to be executed
587 AutoFence fence;
588}
589
John Reck443a7142014-09-04 17:40:05 -0700590void OpenGLRenderer::markLayersAsBuildLayers() {
591 for (size_t i = 0; i < mLayerUpdates.size(); i++) {
592 mLayerUpdates[i]->wasBuildLayered = true;
593 }
594}
595
Romain Guy11cb6422012-09-21 00:39:43 -0700596///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700597// State management
598///////////////////////////////////////////////////////////////////////////////
599
Chris Craik14e51302013-12-30 15:32:54 -0800600void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craika64a2be2014-05-14 14:17:01 -0700601 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800602 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
603 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700604
Chris Craika64a2be2014-05-14 14:17:01 -0700605 if (restoreViewport) {
John Reck3b202512014-06-23 13:13:08 -0700606 mRenderState.setViewport(getViewportWidth(), getViewportHeight());
Romain Guyeb993562010-10-05 18:14:38 -0700607 }
608
Romain Guy2542d192010-08-18 11:47:12 -0700609 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700610 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700611 }
Romain Guy2542d192010-08-18 11:47:12 -0700612
Romain Guy5ec99242010-11-03 16:19:08 -0700613 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700614 endMark(); // Savelayer
Chris Craika8bea8e2014-09-24 11:29:43 -0700615 ATRACE_END(); // SaveLayer
Chris Craik7273daa2013-03-28 11:25:24 -0700616 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800617 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700618 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700619 }
Romain Guybb9524b2010-06-22 18:56:38 -0700620}
621
Romain Guyf6a11b82010-06-23 17:47:49 -0700622///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700623// Layers
624///////////////////////////////////////////////////////////////////////////////
625
626int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700627 const SkPaint* paint, int flags, const SkPath* convexMask) {
Chris Craik4ace7302014-09-14 15:49:54 -0700628 // force matrix/clip isolation for layer
629 flags |= SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag;
630
Romain Guyeb993562010-10-05 18:14:38 -0700631 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700632
Chris Craikd6b65f62014-01-01 14:45:21 -0800633 if (!currentSnapshot()->isIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700634 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700635 }
Romain Guyd55a8612010-06-28 17:42:46 -0700636
637 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700638}
639
Chris Craikd90144d2013-03-19 15:03:48 -0700640void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
641 const Rect untransformedBounds(bounds);
642
Chris Craikd6b65f62014-01-01 14:45:21 -0800643 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700644
645 // Layers only make sense if they are in the framebuffer's bounds
Chris Craikd6b65f62014-01-01 14:45:21 -0800646 if (bounds.intersect(*currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700647 // We cannot work with sub-pixels in this case
648 bounds.snapToPixelBoundaries();
649
650 // When the layer is not an FBO, we may use glCopyTexImage so we
651 // need to make sure the layer does not extend outside the bounds
652 // of the framebuffer
Chris Craik92419752014-05-15 13:21:28 -0700653 const Snapshot& previous = *(currentSnapshot()->previous);
654 Rect previousViewport(0, 0, previous.getViewportWidth(), previous.getViewportHeight());
655 if (!bounds.intersect(previousViewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700656 bounds.setEmpty();
657 } else if (fboLayer) {
658 clip.set(bounds);
659 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800660 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700661 inverse.mapRect(clip);
662 clip.snapToPixelBoundaries();
663 if (clip.intersect(untransformedBounds)) {
664 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
665 bounds.set(untransformedBounds);
666 } else {
667 clip.setEmpty();
668 }
669 }
670 } else {
671 bounds.setEmpty();
672 }
673}
674
Chris Craik408eb122013-03-26 18:55:15 -0700675void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
676 bool fboLayer, int alpha) {
677 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
678 bounds.getHeight() > mCaches.maxTextureSize ||
679 (fboLayer && clip.isEmpty())) {
680 mSnapshot->empty = fboLayer;
681 } else {
Chris Craik74cf7e62014-08-07 14:34:46 -0700682 mSnapshot->invisible = mSnapshot->invisible || (alpha <= 0 && fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700683 }
684}
685
Chris Craikd90144d2013-03-19 15:03:48 -0700686int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500687 const SkPaint* paint, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700688 const int count = saveSnapshot(flags);
689
Chris Craikd6b65f62014-01-01 14:45:21 -0800690 if (!currentSnapshot()->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700691 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
692 // operations will be able to store and restore the current clip and transform info, and
693 // quick rejection will be correct (for display lists)
694
695 Rect bounds(left, top, right, bottom);
696 Rect clip;
697 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500698 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700699
Chris Craikd6b65f62014-01-01 14:45:21 -0800700 if (!currentSnapshot()->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700701 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
702 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700703 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700704 mSnapshot->roundRectClipState = NULL;
Chris Craikd90144d2013-03-19 15:03:48 -0700705 }
706 }
707
708 return count;
709}
710
Romain Guy1c740bc2010-09-13 18:00:09 -0700711/**
712 * Layers are viewed by Skia are slightly different than layers in image editing
713 * programs (for instance.) When a layer is created, previously created layers
714 * and the frame buffer still receive every drawing command. For instance, if a
715 * layer is created and a shape intersecting the bounds of the layers and the
716 * framebuffer is draw, the shape will be drawn on both (unless the layer was
717 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
718 *
719 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
720 * texture. Unfortunately, this is inefficient as it requires every primitive to
721 * be drawn n + 1 times, where n is the number of active layers. In practice this
722 * means, for every primitive:
723 * - Switch active frame buffer
724 * - Change viewport, clip and projection matrix
725 * - Issue the drawing
726 *
727 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700728 * To avoid this, layers are implemented in a different way here, at least in the
729 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
730 * is set. When this flag is set we can redirect all drawing operations into a
731 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700732 *
733 * This implementation relies on the frame buffer being at least RGBA 8888. When
734 * a layer is created, only a texture is created, not an FBO. The content of the
735 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700736 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700737 * buffer and drawing continues as normal. This technique therefore treats the
738 * frame buffer as a scratch buffer for the layers.
739 *
740 * To compose the layers back onto the frame buffer, each layer texture
741 * (containing the original frame buffer data) is drawn as a simple quad over
742 * the frame buffer. The trick is that the quad is set as the composition
743 * destination in the blending equation, and the frame buffer becomes the source
744 * of the composition.
745 *
746 * Drawing layers with an alpha value requires an extra step before composition.
747 * An empty quad is drawn over the layer's region in the frame buffer. This quad
748 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
749 * quad is used to multiply the colors in the frame buffer. This is achieved by
750 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
751 * GL_ZERO, GL_SRC_ALPHA.
752 *
753 * Because glCopyTexImage2D() can be slow, an alternative implementation might
754 * be use to draw a single clipped layer. The implementation described above
755 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700756 *
757 * (1) The frame buffer is actually not cleared right away. To allow the GPU
758 * to potentially optimize series of calls to glCopyTexImage2D, the frame
759 * buffer is left untouched until the first drawing operation. Only when
760 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700761 */
Chet Haased48885a2012-08-28 17:43:28 -0700762bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700763 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700764 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700765 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700766
Romain Guyeb993562010-10-05 18:14:38 -0700767 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
768
Romain Guyf607bdc2010-09-10 19:20:06 -0700769 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700770 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700771 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700772 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000773 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700774
775 // Bail out if we won't draw in this snapshot
Chris Craikd6b65f62014-01-01 14:45:21 -0800776 if (currentSnapshot()->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700777 return false;
778 }
Romain Guyf18fd992010-07-08 11:45:51 -0700779
Romain Guya1d3c912011-12-13 14:55:06 -0800780 mCaches.activeTexture(0);
John Reck3b202512014-06-23 13:13:08 -0700781 Layer* layer = mCaches.layerCache.get(mRenderState, bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700782 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700783 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700784 }
785
Derek Sollenberger674554f2014-02-19 16:47:32 +0000786 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700787 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700788 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
789 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500790
Chet Haasea23eed82012-04-12 15:19:04 -0700791 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700792 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700793 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda57022010-07-06 11:39:32 -0700794
Romain Guy8fb95422010-08-17 18:38:51 -0700795 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700796 mSnapshot->flags |= Snapshot::kFlagIsLayer;
797 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700798
Chris Craika8bea8e2014-09-24 11:29:43 -0700799 ATRACE_FORMAT_BEGIN("%ssaveLayer %ux%u",
800 fboLayer ? "" : "unclipped ",
801 layer->getWidth(), layer->getHeight());
Chris Craik7273daa2013-03-28 11:25:24 -0700802 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700803 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700804 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700805 } else {
806 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700807 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800808 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700809 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700810 // Workaround for some GL drivers. When reading pixels lying outside
811 // of the window we should get undefined values for those pixels.
812 // Unfortunately some drivers will turn the entire target texture black
813 // when reading outside of the window.
814 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
815 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700816 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800817 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700818
Chris Craika64a2be2014-05-14 14:17:01 -0700819 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
820 bounds.left, getViewportHeight() - bounds.bottom,
821 bounds.getWidth(), bounds.getHeight());
Romain Guyb254c242013-06-27 17:15:24 -0700822
Romain Guy54be1cd2011-06-13 19:04:27 -0700823 // Enqueue the buffer coordinates to clear the corresponding region later
824 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700825 }
Romain Guyeb993562010-10-05 18:14:38 -0700826 }
Romain Guyf86ef572010-07-01 11:05:42 -0700827
Romain Guyd55a8612010-06-28 17:42:46 -0700828 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700829}
830
Chris Craike63f7c622013-10-17 10:30:55 -0700831bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800832 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700833 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700834
Chet Haased48885a2012-08-28 17:43:28 -0700835 mSnapshot->region = &mSnapshot->layer->region;
Chris Craika64a2be2014-05-14 14:17:01 -0700836 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
Chet Haased48885a2012-08-28 17:43:28 -0700837 mSnapshot->fbo = layer->getFbo();
838 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
839 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craika64a2be2014-05-14 14:17:01 -0700840 mSnapshot->initializeViewport(bounds.getWidth(), bounds.getHeight());
Chris Craikdeeda3d2014-05-05 19:09:33 -0700841 mSnapshot->roundRectClipState = NULL;
Romain Guy5b3b3522010-10-27 18:57:51 -0700842
Romain Guy2b7028e2012-09-19 17:25:38 -0700843 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700844 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700845 // Bind texture to FBO
John Reck3b202512014-06-23 13:13:08 -0700846 mRenderState.bindFramebuffer(layer->getFbo());
Romain Guy9ace8f52011-07-07 20:50:11 -0700847 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700848
849 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700850 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700851 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700852 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700853 }
854
855 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700856 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700857
henry.uh_chen33f5a592014-07-02 19:36:56 +0800858 // Expand the startTiling region by 1
859 startTilingCurrentClip(true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700860
861 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700862 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800863 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700864 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700865 glClear(GL_COLOR_BUFFER_BIT);
866
867 dirtyClip();
868
869 // Change the ortho projection
John Reck3b202512014-06-23 13:13:08 -0700870 mRenderState.setViewport(bounds.getWidth(), bounds.getHeight());
Romain Guy5b3b3522010-10-27 18:57:51 -0700871 return true;
872}
873
Romain Guy1c740bc2010-09-13 18:00:09 -0700874/**
875 * Read the documentation of createLayer() before doing anything in this method.
876 */
Chris Craik14e51302013-12-30 15:32:54 -0800877void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
878 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000879 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700880 return;
881 }
882
Chris Craik14e51302013-12-30 15:32:54 -0800883 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800884 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800885 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700886
Chris Craik39a908c2013-06-13 14:39:01 -0700887 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800888 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
Chris Craikdeeda3d2014-05-05 19:09:33 -0700889 &clipRequired, NULL, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700890 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
891
Romain Guyeb993562010-10-05 18:14:38 -0700892 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700893 endTiling();
894
Romain Guye0aa84b2012-04-03 19:30:26 -0700895 // Detach the texture from the FBO
896 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800897
898 layer->removeFbo(false);
899
Romain Guyeb993562010-10-05 18:14:38 -0700900 // Unbind current FBO and restore previous one
John Reck3b202512014-06-23 13:13:08 -0700901 mRenderState.bindFramebuffer(restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700902 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700903
Chris Craikd6b65f62014-01-01 14:45:21 -0800904 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700905 }
906
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500908 SkPaint layerPaint;
909 layerPaint.setAlpha(layer->getAlpha());
910 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
911 layerPaint.setColorFilter(layer->getColorFilter());
912
913 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700914 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700915 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700916 }
917
Romain Guy03750a02010-10-18 14:06:08 -0700918 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700919
Romain Guya1d3c912011-12-13 14:55:06 -0800920 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700921
Romain Guy5b3b3522010-10-27 18:57:51 -0700922 // When the layer is stored in an FBO, we can save a bit of fillrate by
923 // drawing only the dirty region
924 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800925 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700926 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700927 } else if (!rect.isEmpty()) {
928 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530929
930 save(0);
931 // the layer contains screen buffer content that shouldn't be alpha modulated
932 // (and any necessary alpha modulation was handled drawing into the layer)
933 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530935 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700936 }
Romain Guy8b55f372010-08-18 17:10:07 -0700937
Romain Guy746b7402010-10-26 16:27:31 -0700938 dirtyClip();
939
Romain Guyeb993562010-10-05 18:14:38 -0700940 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700941 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700942 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700943 LAYER_LOGD("Deleting layer");
John Reck0e89e2b2014-10-31 14:49:06 -0700944 layer->decStrong(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700945 }
946}
947
Romain Guyaa6c24c2011-04-28 18:40:04 -0700948void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700949 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700950
951 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700953 setupDrawWithTexture();
954 } else {
955 setupDrawWithExternalTexture();
956 }
957 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700958 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500959 setupDrawColorFilter(layer->getColorFilter());
960 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700961 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500963 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700964 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
965 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700966 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700967 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700968 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800969 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800970 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700971 layer->getWidth() == (uint32_t) rect.getWidth() &&
972 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800973 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
974 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700975
Romain Guyd21b6e12011-11-30 20:21:23 -0800976 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800977 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
978 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800980 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800981 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
982 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700983 }
984 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700985 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700986
987 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700988}
989
Romain Guy5b3b3522010-10-27 18:57:51 -0700990void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Chris Craik62d307c2014-07-29 10:35:13 -0700991 if (layer->isTextureLayer()) {
992 EVENT_LOGD("composeTextureLayerRect");
993 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
994 drawTextureLayer(layer, rect);
995 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
996 } else {
997 EVENT_LOGD("composeHardwareLayerRect");
Romain Guyaa6c24c2011-04-28 18:40:04 -0700998 const Rect& texCoords = layer->texCoords;
999 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1000 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001001
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 float x = rect.left;
1003 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001004 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001005 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001006 layer->getHeight() == (uint32_t) rect.getHeight();
1007
1008 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 // When we're swapping, the layer is already in screen coordinates
1010 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001011 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1012 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001013 }
1014
Romain Guyd21b6e12011-11-30 20:21:23 -08001015 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001017 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 }
1019
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001020 SkPaint layerPaint;
1021 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1022 layerPaint.setXfermodeMode(layer->getMode());
1023 layerPaint.setColorFilter(layer->getColorFilter());
1024
1025 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001027 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001028 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001029 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001030
Romain Guyaa6c24c2011-04-28 18:40:04 -07001031 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001032 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001033}
1034
Chris Craik34416ea2013-04-15 16:08:28 -07001035/**
1036 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1037 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1038 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1039 * by saveLayer's restore
1040 */
1041#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1042 DRAW_COMMAND; \
1043 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1044 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1045 DRAW_COMMAND; \
1046 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1047 } \
1048 }
1049
1050#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1051
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001052// This class is purely for inspection. It inherits from SkShader, but Skia does not know how to
1053// use it. The OpenGLRenderer will look at it to find its Layer and whether it is opaque.
1054class LayerShader : public SkShader {
1055public:
1056 LayerShader(Layer* layer, const SkMatrix* localMatrix)
1057 : INHERITED(localMatrix)
1058 , mLayer(layer) {
1059 }
1060
1061 virtual bool asACustomShader(void** data) const {
1062 if (data) {
1063 *data = static_cast<void*>(mLayer);
1064 }
1065 return true;
1066 }
1067
1068 virtual bool isOpaque() const {
1069 return !mLayer->isBlend();
1070 }
1071
1072protected:
1073 virtual void shadeSpan(int x, int y, SkPMColor[], int count) {
1074 LOG_ALWAYS_FATAL("LayerShader should never be drawn with raster backend.");
1075 }
1076
1077 virtual void flatten(SkWriteBuffer&) const {
1078 LOG_ALWAYS_FATAL("LayerShader should never be flattened.");
1079 }
1080
1081 virtual Factory getFactory() const {
1082 LOG_ALWAYS_FATAL("LayerShader should never be created from a stream.");
1083 return NULL;
1084 }
1085private:
1086 // Unowned.
1087 Layer* mLayer;
1088 typedef SkShader INHERITED;
1089};
1090
Romain Guy5b3b3522010-10-27 18:57:51 -07001091void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001092 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1093
1094 if (layer->getConvexMask()) {
1095 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1096
1097 // clip to the area of the layer the mask can be larger
1098 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1099
1100 SkPaint paint;
1101 paint.setAntiAlias(true);
1102 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1103
Chris Craik3f0854292014-04-15 16:18:08 -07001104 // create LayerShader to map SaveLayer content into subsequent draw
1105 SkMatrix shaderMatrix;
1106 shaderMatrix.setTranslate(rect.left, rect.bottom);
1107 shaderMatrix.preScale(1, -1);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001108 LayerShader layerShader(layer, &shaderMatrix);
1109 paint.setShader(&layerShader);
Chris Craik3f0854292014-04-15 16:18:08 -07001110
1111 // Since the drawing primitive is defined in local drawing space,
1112 // we don't need to modify the draw matrix
1113 const SkPath* maskPath = layer->getConvexMask();
1114 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1115
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001116 paint.setShader(NULL);
Chris Craik3f0854292014-04-15 16:18:08 -07001117 restore();
1118
1119 return;
1120 }
1121
Romain Guy5b3b3522010-10-27 18:57:51 -07001122 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001123 layer->setRegionAsRect();
1124
Chris Craik34416ea2013-04-15 16:08:28 -07001125 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001126
Romain Guy5b3b3522010-10-27 18:57:51 -07001127 layer->region.clear();
1128 return;
1129 }
1130
Chris Craik62d307c2014-07-29 10:35:13 -07001131 EVENT_LOGD("composeLayerRegion");
Chris Craik3f0854292014-04-15 16:18:08 -07001132 // standard Region based draw
1133 size_t count;
1134 const android::Rect* rects;
1135 Region safeRegion;
1136 if (CC_LIKELY(hasRectToRectTransform())) {
1137 rects = layer->region.getArray(&count);
1138 } else {
1139 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1140 rects = safeRegion.getArray(&count);
1141 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001142
Chris Craik3f0854292014-04-15 16:18:08 -07001143 const float alpha = getLayerAlpha(layer);
1144 const float texX = 1.0f / float(layer->getWidth());
1145 const float texY = 1.0f / float(layer->getHeight());
1146 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001147
Chris Craik3f0854292014-04-15 16:18:08 -07001148 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001149
Chris Craik3f0854292014-04-15 16:18:08 -07001150 // We must get (and therefore bind) the region mesh buffer
1151 // after we setup drawing in case we need to mess with the
1152 // stencil buffer in setupDraw()
1153 TextureVertex* mesh = mCaches.getRegionMesh();
1154 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001155
Chris Craik3f0854292014-04-15 16:18:08 -07001156 setupDrawWithTexture();
1157 setupDrawColor(alpha, alpha, alpha, alpha);
1158 setupDrawColorFilter(layer->getColorFilter());
1159 setupDrawBlending(layer);
1160 setupDrawProgram();
1161 setupDrawDirtyRegionsDisabled();
1162 setupDrawPureColorUniforms();
1163 setupDrawColorFilterUniforms(layer->getColorFilter());
1164 setupDrawTexture(layer->getTexture());
1165 if (currentTransform()->isPureTranslate()) {
1166 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1167 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001168
Chris Craik3f0854292014-04-15 16:18:08 -07001169 layer->setFilter(GL_NEAREST);
1170 setupDrawModelView(kModelViewMode_Translate, false,
1171 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1172 } else {
1173 layer->setFilter(GL_LINEAR);
1174 setupDrawModelView(kModelViewMode_Translate, false,
1175 rect.left, rect.top, rect.right, rect.bottom);
1176 }
1177 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001178
Chris Craik3f0854292014-04-15 16:18:08 -07001179 for (size_t i = 0; i < count; i++) {
1180 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001181
Chris Craik3f0854292014-04-15 16:18:08 -07001182 const float u1 = r->left * texX;
1183 const float v1 = (height - r->top) * texY;
1184 const float u2 = r->right * texX;
1185 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001186
Chris Craik3f0854292014-04-15 16:18:08 -07001187 // TODO: Reject quads outside of the clip
1188 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1189 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1190 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1191 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001192
Chris Craik3f0854292014-04-15 16:18:08 -07001193 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001194
Chris Craik3f0854292014-04-15 16:18:08 -07001195 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001196 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1197 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001198 numQuads = 0;
1199 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001200 }
Chris Craik3f0854292014-04-15 16:18:08 -07001201 }
1202
1203 if (numQuads > 0) {
1204 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1205 GL_UNSIGNED_SHORT, NULL));
1206 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001207
Romain Guy5b3b3522010-10-27 18:57:51 -07001208#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001209 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001210#endif
1211
Chris Craik3f0854292014-04-15 16:18:08 -07001212 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001213}
1214
Romain Guy3a3133d2011-02-01 22:59:58 -08001215#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001216void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001217 size_t count;
1218 const android::Rect* rects = region.getArray(&count);
1219
1220 uint32_t colors[] = {
1221 0x7fff0000, 0x7f00ff00,
1222 0x7f0000ff, 0x7fff00ff,
1223 };
1224
1225 int offset = 0;
1226 int32_t top = rects[0].top;
1227
1228 for (size_t i = 0; i < count; i++) {
1229 if (top != rects[i].top) {
1230 offset ^= 0x2;
1231 top = rects[i].top;
1232 }
1233
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001234 SkPaint paint;
1235 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001236 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001237 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001238 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001239}
Chris Craike63f7c622013-10-17 10:30:55 -07001240#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001241
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001242void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001243 Vector<float> rects;
1244
1245 SkRegion::Iterator it(region);
1246 while (!it.done()) {
1247 const SkIRect& r = it.rect();
1248 rects.push(r.fLeft);
1249 rects.push(r.fTop);
1250 rects.push(r.fRight);
1251 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001252 it.next();
1253 }
1254
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001255 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001256}
1257
Romain Guy5b3b3522010-10-27 18:57:51 -07001258void OpenGLRenderer::dirtyLayer(const float left, const float top,
1259 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001260 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001261 Rect bounds(left, top, right, bottom);
1262 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001263 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001264 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001265}
1266
1267void OpenGLRenderer::dirtyLayer(const float left, const float top,
1268 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001269 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001270 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001271 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001272 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001273}
1274
1275void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001276 if (bounds.intersect(*currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001277 bounds.snapToPixelBoundaries();
1278 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1279 if (!dirty.isEmpty()) {
1280 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001281 }
1282 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001283}
1284
Chris Craik4063a0e2013-11-15 16:06:56 -08001285void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001286 GLsizei elementsCount = quadsCount * 6;
1287 while (elementsCount > 0) {
1288 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1289
Romain Guy3380cfd2013-08-15 16:57:57 -07001290 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001291 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1292
1293 elementsCount -= drawCount;
1294 // Though there are 4 vertices in a quad, we use 6 indices per
1295 // quad to draw with GL_TRIANGLES
1296 mesh += (drawCount / 6) * 4;
1297 }
1298}
1299
Romain Guy54be1cd2011-06-13 19:04:27 -07001300void OpenGLRenderer::clearLayerRegions() {
1301 const size_t count = mLayers.size();
1302 if (count == 0) return;
1303
Chris Craikd6b65f62014-01-01 14:45:21 -08001304 if (!currentSnapshot()->isIgnored()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001305 EVENT_LOGD("clearLayerRegions");
Romain Guy54be1cd2011-06-13 19:04:27 -07001306 // Doing several glScissor/glClear here can negatively impact
1307 // GPUs with a tiler architecture, instead we draw quads with
1308 // the Clear blending mode
1309
1310 // The list contains bounds that have already been clipped
1311 // against their initial clip rect, and the current clip
1312 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001313 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001314
Romain Guy448455f2013-07-22 13:57:50 -07001315 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001316 Vertex* vertex = mesh;
1317
1318 for (uint32_t i = 0; i < count; i++) {
1319 Rect* bounds = mLayers.itemAt(i);
1320
Romain Guy54be1cd2011-06-13 19:04:27 -07001321 Vertex::set(vertex++, bounds->left, bounds->top);
1322 Vertex::set(vertex++, bounds->right, bounds->top);
1323 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001324 Vertex::set(vertex++, bounds->right, bounds->bottom);
1325
1326 delete bounds;
1327 }
Romain Guye67307c2013-02-11 18:01:20 -08001328 // We must clear the list of dirty rects before we
1329 // call setupDraw() to prevent stencil setup to do
1330 // the same thing again
1331 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001332
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001333 SkPaint clearPaint;
1334 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1335
Romain Guy54be1cd2011-06-13 19:04:27 -07001336 setupDraw(false);
1337 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001338 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001339 setupDrawProgram();
1340 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001341 setupDrawModelView(kModelViewMode_Translate, false,
1342 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001343
Chris Craik4063a0e2013-11-15 16:06:56 -08001344 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001345
1346 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001347 } else {
1348 for (uint32_t i = 0; i < count; i++) {
1349 delete mLayers.itemAt(i);
1350 }
Romain Guye67307c2013-02-11 18:01:20 -08001351 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001352 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001353}
1354
Romain Guybd6b79b2010-06-26 00:13:53 -07001355///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001356// State Deferral
1357///////////////////////////////////////////////////////////////////////////////
1358
Chris Craikff785832013-03-08 13:12:16 -08001359bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001360 const Rect* currentClip = currentClipRect();
1361 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001362
Chris Craikff785832013-03-08 13:12:16 -08001363 if (stateDeferFlags & kStateDeferFlag_Draw) {
1364 // state has bounds initialized in local coordinates
1365 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001366 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001367 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001368 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1369 // is used, it should more closely duplicate the quickReject logic (in how it uses
1370 // snapToPixelBoundaries)
1371
Chris Craikd6b65f62014-01-01 14:45:21 -08001372 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001373 // quick rejected
1374 return true;
1375 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001376
Chris Craika02c4ed2013-06-14 13:43:58 -07001377 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001378 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001379 int& flags = state.mClipSideFlags;
1380 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001381 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1382 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1383 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1384 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001385 }
1386 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001387 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001388 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1389 // overdraw avoidance (since we don't know what it overlaps)
1390 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001391 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001392 }
1393 }
1394
Chris Craik527a3aa2013-03-04 10:19:31 -08001395 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1396 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001397 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001398 }
1399
Chris Craik7273daa2013-03-28 11:25:24 -07001400 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1401 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001402 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001403 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001404 state.mAlpha = currentSnapshot()->alpha;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001405
1406 // always store/restore, since it's just a pointer
1407 state.mRoundRectClipState = currentSnapshot()->roundRectClipState;
Chris Craikc3566d02013-02-04 16:16:33 -08001408 return false;
1409}
1410
Chris Craik527a3aa2013-03-04 10:19:31 -08001411void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001412 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001413 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001414 mDrawModifiers = state.mDrawModifiers;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001415 mSnapshot->roundRectClipState = state.mRoundRectClipState;
Chris Craikff785832013-03-08 13:12:16 -08001416
Chris Craik527a3aa2013-03-04 10:19:31 -08001417 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001418 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1419 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001420 dirtyClip();
1421 }
Chris Craikc3566d02013-02-04 16:16:33 -08001422}
1423
Chris Craik28ce94a2013-05-31 11:38:03 -07001424/**
1425 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1426 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1427 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1428 *
1429 * This method should be called when restoreDisplayState() won't be restoring the clip
1430 */
1431void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1432 if (clipRect != NULL) {
1433 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1434 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001435 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001436 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001437 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001438 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001439}
1440
Chris Craikc3566d02013-02-04 16:16:33 -08001441///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001442// Clipping
1443///////////////////////////////////////////////////////////////////////////////
1444
Romain Guybb9524b2010-06-22 18:56:38 -07001445void OpenGLRenderer::setScissorFromClip() {
Chris Craikd6b65f62014-01-01 14:45:21 -08001446 Rect clip(*currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001447 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001448
Chris Craika64a2be2014-05-14 14:17:01 -07001449 if (mCaches.setScissor(clip.left, getViewportHeight() - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001450 clip.getWidth(), clip.getHeight())) {
1451 mDirtyClip = false;
1452 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001453}
1454
Romain Guy8ce00302013-01-15 18:51:42 -08001455void OpenGLRenderer::ensureStencilBuffer() {
1456 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1457 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1458 // just hope we have one when hasLayer() returns false.
1459 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001460 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001461 }
1462}
1463
1464void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1465 // The layer's FBO is already bound when we reach this stage
1466 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001467 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1468 // is attached after we initiated tiling. We must turn it off,
1469 // attach the new render buffer then turn tiling back on
1470 endTiling();
1471
Romain Guy8d4aeb72013-02-12 16:08:55 -08001472 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001473 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001474 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001475
Romain Guyf735c8e2013-01-31 17:45:55 -08001476 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001477 }
1478}
1479
1480void OpenGLRenderer::setStencilFromClip() {
1481 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001482 if (!currentSnapshot()->clipRegion->isEmpty()) {
Chris Craik62d307c2014-07-29 10:35:13 -07001483 EVENT_LOGD("setStencilFromClip - enabling");
1484
Romain Guy8ce00302013-01-15 18:51:42 -08001485 // NOTE: The order here is important, we must set dirtyClip to false
1486 // before any draw call to avoid calling back into this method
1487 mDirtyClip = false;
1488
1489 ensureStencilBuffer();
1490
1491 mCaches.stencil.enableWrite();
1492
Chris Craikdeeda3d2014-05-05 19:09:33 -07001493 // Clear and update the stencil, but first make sure we restrict drawing
Romain Guy8ce00302013-01-15 18:51:42 -08001494 // to the region's bounds
1495 bool resetScissor = mCaches.enableScissor();
1496 if (resetScissor) {
1497 // The scissor was not set so we now need to update it
1498 setScissorFromClip();
1499 }
1500 mCaches.stencil.clear();
Chris Craikdeeda3d2014-05-05 19:09:33 -07001501
1502 // stash and disable the outline clip state, since stencil doesn't account for outline
1503 bool storedSkipOutlineClip = mSkipOutlineClip;
1504 mSkipOutlineClip = true;
Romain Guy8ce00302013-01-15 18:51:42 -08001505
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001506 SkPaint paint;
Chris Craik98d608d2014-07-17 12:25:11 -07001507 paint.setColor(SK_ColorBLACK);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001508 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1509
Romain Guy8ce00302013-01-15 18:51:42 -08001510 // NOTE: We could use the region contour path to generate a smaller mesh
1511 // Since we are using the stencil we could use the red book path
1512 // drawing technique. It might increase bandwidth usage though.
1513
1514 // The last parameter is important: we are not drawing in the color buffer
1515 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001516 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001517 if (resetScissor) mCaches.disableScissor();
1518 mSkipOutlineClip = storedSkipOutlineClip;
Romain Guy8ce00302013-01-15 18:51:42 -08001519
1520 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001521
1522 // Draw the region used to generate the stencil if the appropriate debug
1523 // mode is enabled
1524 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001525 paint.setColor(0x7f0000ff);
1526 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1527 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001528 }
Romain Guy8ce00302013-01-15 18:51:42 -08001529 } else {
Chris Craik62d307c2014-07-29 10:35:13 -07001530 EVENT_LOGD("setStencilFromClip - disabling");
Romain Guy8ce00302013-01-15 18:51:42 -08001531 mCaches.stencil.disable();
1532 }
1533 }
1534}
1535
Chris Craikf0a59072013-11-19 18:00:46 -08001536/**
1537 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1538 *
1539 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1540 * style, and tessellated AA ramp
1541 */
1542bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001543 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08001544 bool snapOut = paint && paint->isAntiAlias();
1545
1546 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1547 float outset = paint->getStrokeWidth() * 0.5f;
1548 left -= outset;
1549 top -= outset;
1550 right += outset;
1551 bottom += outset;
1552 }
1553
Chris Craikdeeda3d2014-05-05 19:09:33 -07001554 bool clipRequired = false;
1555 bool roundRectClipRequired = false;
1556 if (calculateQuickRejectForScissor(left, top, right, bottom,
1557 &clipRequired, &roundRectClipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001558 return true;
1559 }
1560
Chris Craikf23b25a2014-06-26 15:46:20 -07001561 // not quick rejected, so enable the scissor if clipRequired
1562 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1563 mSkipOutlineClip = !roundRectClipRequired;
Chris Craik39a908c2013-06-13 14:39:01 -07001564 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001565}
1566
Romain Guy8ce00302013-01-15 18:51:42 -08001567void OpenGLRenderer::debugClip() {
1568#if DEBUG_CLIP_REGIONS
Chris Craikf23b25a2014-06-26 15:46:20 -07001569 if (!currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001570 SkPaint paint;
1571 paint.setColor(0x7f00ff00);
1572 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1573
Romain Guy8ce00302013-01-15 18:51:42 -08001574 }
1575#endif
1576}
1577
Romain Guyf6a11b82010-06-23 17:47:49 -07001578///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001579// Drawing commands
1580///////////////////////////////////////////////////////////////////////////////
1581
Chris Craik62d307c2014-07-29 10:35:13 -07001582void OpenGLRenderer::setupDraw(bool clearLayer) {
Chris Craikf0a59072013-11-19 18:00:46 -08001583 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001584 // changes the scissor test state
Chris Craik62d307c2014-07-29 10:35:13 -07001585 if (clearLayer) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001586 // Make sure setScissor & setStencil happen at the beginning of
1587 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001588 if (mDirtyClip) {
1589 if (mCaches.scissorEnabled) {
1590 setScissorFromClip();
1591 }
Chris Craik62d307c2014-07-29 10:35:13 -07001592
1593 if (clearLayer) {
1594 setStencilFromClip();
1595 } else {
1596 // While clearing layer, force disable stencil buffer, since
1597 // it's invalid to stencil-clip *during* the layer clear
1598 mCaches.stencil.disable();
1599 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001600 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001601
Romain Guy70ca14e2010-12-13 18:24:33 -08001602 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001603
Romain Guy70ca14e2010-12-13 18:24:33 -08001604 mSetShaderColor = false;
1605 mColorSet = false;
1606 mColorA = mColorR = mColorG = mColorB = 0.0f;
1607 mTextureUnit = 0;
1608 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001609
1610 // Enable debug highlight when what we're about to draw is tested against
1611 // the stencil buffer and if stencil highlight debugging is on
1612 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1613 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1614 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001615}
1616
1617void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1618 mDescription.hasTexture = true;
1619 mDescription.hasAlpha8Texture = isAlpha8;
1620}
1621
Romain Guyff316ec2013-02-13 18:39:43 -08001622void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1623 mDescription.hasTexture = true;
1624 mDescription.hasColors = true;
1625 mDescription.hasAlpha8Texture = isAlpha8;
1626}
1627
Romain Guyaa6c24c2011-04-28 18:40:04 -07001628void OpenGLRenderer::setupDrawWithExternalTexture() {
1629 mDescription.hasExternalTexture = true;
1630}
1631
Romain Guy15bc6432011-12-13 13:11:32 -08001632void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001633 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001634}
1635
Chris Craik91a8c7c2014-08-12 14:31:35 -07001636void OpenGLRenderer::setupDrawVertexAlpha(bool useShadowAlphaInterp) {
1637 mDescription.hasVertexAlpha = true;
1638 mDescription.useShadowAlphaInterp = useShadowAlphaInterp;
Chet Haase5b0200b2011-04-13 17:58:08 -07001639}
1640
Romain Guy8d0d4782010-12-14 20:13:35 -08001641void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1642 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001643 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1644 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1645 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001646 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001647 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001648}
1649
Romain Guy86568192010-12-14 15:55:39 -08001650void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1651 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001652 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1653 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1654 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001655 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001656 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001657}
1658
Romain Guy41210632012-07-16 17:04:24 -07001659void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1660 mCaches.fontRenderer->describe(mDescription, paint);
1661}
1662
Romain Guy70ca14e2010-12-13 18:24:33 -08001663void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1664 mColorA = a;
1665 mColorR = r;
1666 mColorG = g;
1667 mColorB = b;
1668 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001669 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001670}
1671
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001672void OpenGLRenderer::setupDrawShader(const SkShader* shader) {
1673 if (shader != NULL) {
1674 SkiaShader::describe(&mCaches, mDescription, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001675 }
1676}
1677
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001678void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1679 if (filter == NULL) {
1680 return;
1681 }
1682
1683 SkXfermode::Mode mode;
1684 if (filter->asColorMode(NULL, &mode)) {
1685 mDescription.colorOp = ProgramDescription::kColorBlend;
1686 mDescription.colorMode = mode;
1687 } else if (filter->asColorMatrix(NULL)) {
1688 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001689 }
1690}
1691
Romain Guyf09ef512011-05-27 11:43:46 -07001692void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1693 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1694 mColorA = 1.0f;
1695 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001696 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001697 }
1698}
1699
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001700void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1701 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001702 // When the blending mode is kClear_Mode, we need to use a modulate color
1703 // argb=1,0,0,0
1704 accountForClear(mode);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001705 // TODO: check shader blending, once we have shader drawing support for layers.
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001706 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001707 (mColorSet && mColorA < 1.0f) || isBlendedColorFilter(layer->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001708 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001709}
1710
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001711void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1712 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001713 // When the blending mode is kClear_Mode, we need to use a modulate color
1714 // argb=1,0,0,0
1715 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001716 blend |= (mColorSet && mColorA < 1.0f) ||
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001717 (getShader(paint) && !getShader(paint)->isOpaque()) ||
1718 isBlendedColorFilter(getColorFilter(paint));
Chris Craikc3566d02013-02-04 16:16:33 -08001719 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001720}
1721
1722void OpenGLRenderer::setupDrawProgram() {
1723 useProgram(mCaches.programCache.get(mDescription));
Chris Craikdeeda3d2014-05-05 19:09:33 -07001724 if (mDescription.hasRoundRectClip) {
1725 // TODO: avoid doing this repeatedly, stashing state pointer in program
1726 const RoundRectClipState* state = mSnapshot->roundRectClipState;
Chris Craikaf4d04c2014-07-29 12:50:14 -07001727 const Rect& innerRect = state->innerRect;
Chris Craikdeeda3d2014-05-05 19:09:33 -07001728 glUniform4f(mCaches.currentProgram->getUniform("roundRectInnerRectLTRB"),
Chris Craikaf4d04c2014-07-29 12:50:14 -07001729 innerRect.left, innerRect.top,
1730 innerRect.right, innerRect.bottom);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001731 glUniformMatrix4fv(mCaches.currentProgram->getUniform("roundRectInvTransform"),
1732 1, GL_FALSE, &state->matrix.data[0]);
Chris Craik4340c262014-09-11 18:58:45 -07001733
1734 // add half pixel to round out integer rect space to cover pixel centers
1735 float roundedOutRadius = state->radius + 0.5f;
1736 glUniform1f(mCaches.currentProgram->getUniform("roundRectRadius"),
1737 roundedOutRadius);
Chris Craikdeeda3d2014-05-05 19:09:33 -07001738 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001739}
1740
1741void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1742 mTrackDirtyRegions = false;
1743}
1744
Chris Craik4063a0e2013-11-15 16:06:56 -08001745void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1746 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001747 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001748 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001749 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001750 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001751
Romain Guy86568192010-12-14 15:55:39 -08001752 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craika64a2be2014-05-14 14:17:01 -07001753 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
1754 mCaches.currentProgram->set(mSnapshot->getOrthoMatrix(), mModelViewMatrix, transformMatrix, offset);
1755 if (dirty && mTrackDirtyRegions) {
1756 if (!ignoreTransform) {
1757 dirtyLayer(left, top, right, bottom, *currentTransform());
1758 } else {
1759 dirtyLayer(left, top, right, bottom);
1760 }
Romain Guy86568192010-12-14 15:55:39 -08001761 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001762}
1763
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001764void OpenGLRenderer::setupDrawColorUniforms(bool hasShader) {
1765 if ((mColorSet && !hasShader) || (hasShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001766 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1767 }
1768}
1769
Romain Guy86568192010-12-14 15:55:39 -08001770void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001771 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001772 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001773 }
1774}
1775
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001776void OpenGLRenderer::setupDrawShaderUniforms(const SkShader* shader, bool ignoreTransform) {
1777 if (shader == NULL) {
1778 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001779 }
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04001780
1781 if (ignoreTransform) {
1782 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1783 // because it was built into modelView / the geometry, and the description needs to
1784 // compensate.
1785 mat4 modelViewWithoutTransform;
1786 modelViewWithoutTransform.loadInverse(*currentTransform());
1787 modelViewWithoutTransform.multiply(mModelViewMatrix);
1788 mModelViewMatrix.load(modelViewWithoutTransform);
1789 }
1790
1791 SkiaShader::setupProgram(&mCaches, mModelViewMatrix, &mTextureUnit, mExtensions, *shader);
Romain Guy70ca14e2010-12-13 18:24:33 -08001792}
1793
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001794void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1795 if (NULL == filter) {
1796 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001797 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001798
1799 SkColor color;
1800 SkXfermode::Mode mode;
1801 if (filter->asColorMode(&color, &mode)) {
1802 const int alpha = SkColorGetA(color);
1803 const GLfloat a = alpha / 255.0f;
1804 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1805 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1806 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1807 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1808 return;
1809 }
1810
1811 SkScalar srcColorMatrix[20];
1812 if (filter->asColorMatrix(srcColorMatrix)) {
1813
1814 float colorMatrix[16];
1815 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1816 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1817 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1818 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1819
1820 // Skia uses the range [0..255] for the addition vector, but we need
1821 // the [0..1] range to apply the vector in GLSL
1822 float colorVector[4];
1823 colorVector[0] = srcColorMatrix[4] / 255.0f;
1824 colorVector[1] = srcColorMatrix[9] / 255.0f;
1825 colorVector[2] = srcColorMatrix[14] / 255.0f;
1826 colorVector[3] = srcColorMatrix[19] / 255.0f;
1827
1828 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1829 GL_FALSE, colorMatrix);
1830 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1831 return;
1832 }
1833
1834 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001835}
1836
Romain Guy41210632012-07-16 17:04:24 -07001837void OpenGLRenderer::setupDrawTextGammaUniforms() {
1838 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1839}
1840
Romain Guy70ca14e2010-12-13 18:24:33 -08001841void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001842 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001843 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001844 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001845}
1846
1847void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001848 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001849 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001850 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001851}
1852
Romain Guyaa6c24c2011-04-28 18:40:04 -07001853void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1854 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001855 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001856 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001857}
1858
Romain Guy8f0095c2011-05-02 17:24:22 -07001859void OpenGLRenderer::setupDrawTextureTransform() {
1860 mDescription.hasTextureTransform = true;
1861}
1862
1863void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001864 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1865 GL_FALSE, &transform.data[0]);
1866}
1867
Chris Craik564acf72014-01-02 16:46:18 -08001868void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1869 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001870 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001871 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001872 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001873 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001874 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001875 }
Romain Guyd71dd362011-12-12 19:03:35 -08001876
Chris Craikcb4d6002012-09-25 12:00:29 -07001877 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001878 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001879 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001880 }
1881
1882 mCaches.unbindIndicesBuffer();
1883}
1884
Chris Craik564acf72014-01-02 16:46:18 -08001885void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1886 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001887 bool force = mCaches.unbindMeshBuffer();
1888 GLsizei stride = sizeof(ColorTextureVertex);
1889
1890 mCaches.bindPositionVertexPointer(force, vertices, stride);
1891 if (mCaches.currentProgram->texCoords >= 0) {
1892 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1893 }
1894 int slot = mCaches.currentProgram->getAttrib("colors");
1895 if (slot >= 0) {
1896 glEnableVertexAttribArray(slot);
1897 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1898 }
1899
1900 mCaches.unbindIndicesBuffer();
1901}
1902
Chris Craik564acf72014-01-02 16:46:18 -08001903void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1904 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001905 bool force = false;
1906 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1907 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1908 // use the default VBO found in Caches
1909 if (!vertices || vbo) {
1910 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1911 } else {
1912 force = mCaches.unbindMeshBuffer();
1913 }
ztenghui63d41ab2014-02-14 13:13:41 -08001914 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001915
Chris Craikcb4d6002012-09-25 12:00:29 -07001916 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001917 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001918 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001919 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001920}
1921
Romain Guy448455f2013-07-22 13:57:50 -07001922void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001923 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001924 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001925 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001926}
1927
Romain Guy70ca14e2010-12-13 18:24:33 -08001928///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001929// Drawing
1930///////////////////////////////////////////////////////////////////////////////
1931
Tom Hudson107843d2014-09-08 11:26:26 -04001932void OpenGLRenderer::drawRenderNode(RenderNode* renderNode, Rect& dirty, int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001933 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1934 // will be performed by the display list itself
Chris Craika7090e02014-06-20 16:01:00 -07001935 if (renderNode && renderNode->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001936 // compute 3d ordering
Chris Craika7090e02014-06-20 16:01:00 -07001937 renderNode->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001938 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Tom Hudson107843d2014-09-08 11:26:26 -04001939 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001940 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001941 renderNode->replay(replayStruct, 0);
Tom Hudson107843d2014-09-08 11:26:26 -04001942 return;
Chris Craikc3566d02013-02-04 16:16:33 -08001943 }
1944
John Reck23d307c2014-10-27 12:38:48 -07001945 DeferredDisplayList deferredList(*currentClipRect());
Chris Craikff785832013-03-08 13:12:16 -08001946 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craika7090e02014-06-20 16:01:00 -07001947 renderNode->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001948
1949 flushLayers();
Tom Hudson107843d2014-09-08 11:26:26 -04001950 startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001951
Tom Hudson107843d2014-09-08 11:26:26 -04001952 deferredList.flush(*this, dirty);
1953 } else {
1954 // Even if there is no drawing command(Ex: invisible),
1955 // it still needs startFrame to clear buffer and start tiling.
1956 startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08001957 }
1958}
1959
Chris Craikd218a922014-01-02 17:13:34 -08001960void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001961 int color = paint != NULL ? paint->getColor() : 0;
1962
Romain Guya168d732011-03-18 16:50:13 -07001963 float x = left;
1964 float y = top;
1965
Romain Guy886b2752013-01-04 12:26:18 -08001966 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1967
Romain Guya168d732011-03-18 16:50:13 -07001968 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001969 if (currentTransform()->isPureTranslate()) {
1970 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1971 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001972 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001973
1974 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001975 } else {
Chris Craik678625242014-02-28 12:26:34 -08001976 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001977 }
1978
Romain Guy3b748a42013-04-17 18:54:38 -07001979 // No need to check for a UV mapper on the texture object, only ARGB_8888
1980 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001981 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001982 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001983 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001984}
1985
Romain Guy03c00b52013-06-20 18:30:28 -07001986/**
1987 * Important note: this method is intended to draw batches of bitmaps and
1988 * will not set the scissor enable or dirty the current layer, if any.
1989 * The caller is responsible for properly dirtying the current layer.
1990 */
Tom Hudson107843d2014-09-08 11:26:26 -04001991void OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08001992 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1993 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001994 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001995 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04001996 if (!texture) return;
Romain Guy3b748a42013-04-17 18:54:38 -07001997
Chris Craik527a3aa2013-03-04 10:19:31 -08001998 const AutoTexture autoCleanup(texture);
1999
Chris Craik527a3aa2013-03-04 10:19:31 -08002000 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002001 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002002
2003 const float x = (int) floorf(bounds.left + 0.5f);
2004 const float y = (int) floorf(bounds.top + 0.5f);
Mike Reed1103b322014-07-08 12:36:44 -04002005 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002006 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002007 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002008 GL_TRIANGLES, bitmapCount * 6, true,
2009 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002010 } else {
2011 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002012 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002013 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2014 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002015 }
2016
Tom Hudson107843d2014-09-08 11:26:26 -04002017 mDirty = true;
Chris Craik527a3aa2013-03-04 10:19:31 -08002018}
2019
Tom Hudson107843d2014-09-08 11:26:26 -04002020void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002021 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002022 return;
Romain Guy6926c722010-07-12 20:20:03 -07002023 }
2024
Romain Guya1d3c912011-12-13 14:55:06 -08002025 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002026 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002027 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002028 const AutoTexture autoCleanup(texture);
2029
Mike Reed1103b322014-07-08 12:36:44 -04002030 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002031 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guya168d732011-03-18 16:50:13 -07002032 } else {
Chris Craik79647502014-08-06 13:42:24 -07002033 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guya168d732011-03-18 16:50:13 -07002034 }
Chet Haase48659092012-05-31 15:21:51 -07002035
Tom Hudson107843d2014-09-08 11:26:26 -04002036 mDirty = true;
Romain Guyce0537b2010-06-29 21:05:21 -07002037}
2038
Tom Hudson107843d2014-09-08 11:26:26 -04002039void OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, const SkPaint* paint) {
Chris Craik79647502014-08-06 13:42:24 -07002040 if (quickRejectSetupScissor(0, 0, bitmap->width(), bitmap->height())) {
Tom Hudson107843d2014-09-08 11:26:26 -04002041 return;
Romain Guye651cc62012-05-14 19:44:40 -07002042 }
2043
2044 mCaches.activeTexture(0);
2045 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2046 const AutoTexture autoCleanup(texture);
2047
Mike Reed1103b322014-07-08 12:36:44 -04002048 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Chris Craik79647502014-08-06 13:42:24 -07002049 drawAlphaBitmap(texture, 0, 0, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002050 } else {
Chris Craik79647502014-08-06 13:42:24 -07002051 drawTextureRect(0, 0, bitmap->width(), bitmap->height(), texture, paint);
Romain Guy886b2752013-01-04 12:26:18 -08002052 }
Chet Haase48659092012-05-31 15:21:51 -07002053
Tom Hudson107843d2014-09-08 11:26:26 -04002054 mDirty = true;
Romain Guye651cc62012-05-14 19:44:40 -07002055}
2056
Tom Hudson107843d2014-09-08 11:26:26 -04002057void OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
Chris Craikd218a922014-01-02 17:13:34 -08002058 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002059 if (!vertices || currentSnapshot()->isIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002060 return;
Romain Guy5a7b4662011-01-20 19:09:30 -08002061 }
2062
Chris Craik39a908c2013-06-13 14:39:01 -07002063 // TODO: use quickReject on bounds from vertices
2064 mCaches.enableScissor();
2065
Romain Guyb18d2d02011-02-10 15:52:54 -08002066 float left = FLT_MAX;
2067 float top = FLT_MAX;
2068 float right = FLT_MIN;
2069 float bottom = FLT_MIN;
2070
Romain Guya92bb4d2012-10-16 11:08:44 -07002071 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002072
Chris Craik564acf72014-01-02 16:46:18 -08002073 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2074 mesh.setCapacity(count);
2075 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002076
2077 bool cleanupColors = false;
2078 if (!colors) {
2079 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002080 int* newColors = new int[colorsCount];
2081 memset(newColors, 0xff, colorsCount * sizeof(int));
2082 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002083 cleanupColors = true;
2084 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002085
Romain Guy3b748a42013-04-17 18:54:38 -07002086 mCaches.activeTexture(0);
2087 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2088 const UvMapper& mapper(getMapper(texture));
2089
Romain Guy5a7b4662011-01-20 19:09:30 -08002090 for (int32_t y = 0; y < meshHeight; y++) {
2091 for (int32_t x = 0; x < meshWidth; x++) {
2092 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2093
2094 float u1 = float(x) / meshWidth;
2095 float u2 = float(x + 1) / meshWidth;
2096 float v1 = float(y) / meshHeight;
2097 float v2 = float(y + 1) / meshHeight;
2098
Romain Guy3b748a42013-04-17 18:54:38 -07002099 mapper.map(u1, v1, u2, v2);
2100
Romain Guy5a7b4662011-01-20 19:09:30 -08002101 int ax = i + (meshWidth + 1) * 2;
2102 int ay = ax + 1;
2103 int bx = i;
2104 int by = bx + 1;
2105 int cx = i + 2;
2106 int cy = cx + 1;
2107 int dx = i + (meshWidth + 1) * 2 + 2;
2108 int dy = dx + 1;
2109
Romain Guyff316ec2013-02-13 18:39:43 -08002110 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2111 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2112 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002113
Romain Guyff316ec2013-02-13 18:39:43 -08002114 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2115 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2116 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002117
Romain Guya92bb4d2012-10-16 11:08:44 -07002118 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2119 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2120 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2121 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002122 }
2123 }
2124
Chris Craikf0a59072013-11-19 18:00:46 -08002125 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002126 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002127 return;
Romain Guya92bb4d2012-10-16 11:08:44 -07002128 }
2129
Romain Guyff316ec2013-02-13 18:39:43 -08002130 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002131 texture = mCaches.textureCache.get(bitmap);
2132 if (!texture) {
2133 if (cleanupColors) delete[] colors;
Tom Hudson107843d2014-09-08 11:26:26 -04002134 return;
Romain Guy3b748a42013-04-17 18:54:38 -07002135 }
Romain Guyff316ec2013-02-13 18:39:43 -08002136 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002137 const AutoTexture autoCleanup(texture);
2138
2139 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002140 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002141
2142 int alpha;
2143 SkXfermode::Mode mode;
2144 getAlphaAndMode(paint, &alpha, &mode);
2145
Romain Guyff316ec2013-02-13 18:39:43 -08002146 float a = alpha / 255.0f;
2147
Romain Guya92bb4d2012-10-16 11:08:44 -07002148 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002149 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002150 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002151
Romain Guyff316ec2013-02-13 18:39:43 -08002152 setupDraw();
2153 setupDrawWithTextureAndColor();
2154 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002155 setupDrawColorFilter(getColorFilter(paint));
2156 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002157 setupDrawProgram();
2158 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002159 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002160 setupDrawTexture(texture->id);
2161 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002162 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002163 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002164
2165 glDrawArrays(GL_TRIANGLES, 0, count);
2166
Romain Guyff316ec2013-02-13 18:39:43 -08002167 int slot = mCaches.currentProgram->getAttrib("colors");
2168 if (slot >= 0) {
2169 glDisableVertexAttribArray(slot);
2170 }
2171
2172 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002173
Tom Hudson107843d2014-09-08 11:26:26 -04002174 mDirty = true;
Romain Guy5a7b4662011-01-20 19:09:30 -08002175}
2176
Tom Hudson107843d2014-09-08 11:26:26 -04002177void OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002178 float srcLeft, float srcTop, float srcRight, float srcBottom,
2179 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002180 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002181 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002182 return;
Romain Guy6926c722010-07-12 20:20:03 -07002183 }
2184
Romain Guya1d3c912011-12-13 14:55:06 -08002185 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002186 Texture* texture = getTexture(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002187 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002188 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002189
Romain Guy8ba548f2010-06-30 19:21:21 -07002190 const float width = texture->width;
2191 const float height = texture->height;
2192
Romain Guy3b748a42013-04-17 18:54:38 -07002193 float u1 = fmax(0.0f, srcLeft / width);
2194 float v1 = fmax(0.0f, srcTop / height);
2195 float u2 = fmin(1.0f, srcRight / width);
2196 float v2 = fmin(1.0f, srcBottom / height);
2197
2198 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002199
Romain Guy03750a02010-10-18 14:06:08 -07002200 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002201 resetDrawTextureTexCoords(u1, v1, u2, v2);
2202
Romain Guyd21b6e12011-11-30 20:21:23 -08002203 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2204
Romain Guy886b2752013-01-04 12:26:18 -08002205 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2206 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002207
Romain Guy886b2752013-01-04 12:26:18 -08002208 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2209 // Apply a scale transform on the canvas only when a shader is in use
2210 // Skia handles the ratio between the dst and src rects as a scale factor
2211 // when a shader is set
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002212 bool useScaleTransform = getShader(paint) && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002213 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002214
Chris Craikd6b65f62014-01-01 14:45:21 -08002215 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2216 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2217 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002218
2219 dstRight = x + (dstRight - dstLeft);
2220 dstBottom = y + (dstBottom - dstTop);
2221
2222 dstLeft = x;
2223 dstTop = y;
2224
Chris Craik678625242014-02-28 12:26:34 -08002225 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002226 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002227 } else {
Chris Craik678625242014-02-28 12:26:34 -08002228 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002229 }
2230
2231 if (CC_UNLIKELY(useScaleTransform)) {
2232 save(SkCanvas::kMatrix_SaveFlag);
2233 translate(dstLeft, dstTop);
2234 scale(scaleX, scaleY);
2235
2236 dstLeft = 0.0f;
2237 dstTop = 0.0f;
2238
2239 dstRight = srcRight - srcLeft;
2240 dstBottom = srcBottom - srcTop;
2241 }
2242
Mike Reed1103b322014-07-08 12:36:44 -04002243 if (CC_UNLIKELY(bitmap->colorType() == kAlpha_8_SkColorType)) {
Romain Guy886b2752013-01-04 12:26:18 -08002244 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002245 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002246 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002247 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2248 } else {
2249 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002250 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002251 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002252 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2253 }
2254
2255 if (CC_UNLIKELY(useScaleTransform)) {
2256 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002257 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002258
2259 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002260
Tom Hudson107843d2014-09-08 11:26:26 -04002261 mDirty = true;
Romain Guy8ba548f2010-06-30 19:21:21 -07002262}
2263
Tom Hudson107843d2014-09-08 11:26:26 -04002264void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
Chris Craikd218a922014-01-02 17:13:34 -08002265 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002266 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002267 return;
Romain Guy6926c722010-07-12 20:20:03 -07002268 }
2269
Romain Guy4c2547f2013-06-11 16:19:24 -07002270 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002271 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2272 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002273
Tom Hudson107843d2014-09-08 11:26:26 -04002274 drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002275}
2276
Tom Hudson107843d2014-09-08 11:26:26 -04002277void OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
Chris Craikd218a922014-01-02 17:13:34 -08002278 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2279 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002280 if (quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002281 return;
Romain Guy4c2547f2013-06-11 16:19:24 -07002282 }
2283
Romain Guy211370f2012-02-01 16:10:55 -08002284 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002285 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002286 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002287 if (!texture) return;
Romain Guya4adcf02013-02-28 12:15:35 -08002288 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002289
Romain Guya4adcf02013-02-28 12:15:35 -08002290 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2291 texture->setFilter(GL_LINEAR, true);
2292
Chris Craikd6b65f62014-01-01 14:45:21 -08002293 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002294 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002295 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002296 const float offsetX = left + currentTransform()->getTranslateX();
2297 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002298 const size_t count = mesh->quads.size();
2299 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002300 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002301 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002302 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2303 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2304 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002305 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002306 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002307 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002308 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002309 }
2310 }
2311
Chris Craik4063a0e2013-11-15 16:06:56 -08002312 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002313 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002314 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2315 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002316
Romain Guy3b748a42013-04-17 18:54:38 -07002317 right = x + right - left;
2318 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002319 left = x;
2320 top = y;
2321 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002322 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002323 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2324 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002325 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2326 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002327 }
Chet Haase48659092012-05-31 15:21:51 -07002328
Tom Hudson107843d2014-09-08 11:26:26 -04002329 mDirty = true;
Romain Guyf7f93552010-07-08 19:17:03 -07002330}
2331
Romain Guy03c00b52013-06-20 18:30:28 -07002332/**
2333 * Important note: this method is intended to draw batches of 9-patch objects and
2334 * will not set the scissor enable or dirty the current layer, if any.
2335 * The caller is responsible for properly dirtying the current layer.
2336 */
Tom Hudson107843d2014-09-08 11:26:26 -04002337void OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
Chris Craikd218a922014-01-02 17:13:34 -08002338 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002339 mCaches.activeTexture(0);
2340 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Tom Hudson107843d2014-09-08 11:26:26 -04002341 if (!texture) return;
Romain Guy03c00b52013-06-20 18:30:28 -07002342 const AutoTexture autoCleanup(texture);
2343
2344 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2345 texture->setFilter(GL_LINEAR, true);
2346
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002347 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2348 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002349 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002350
Tom Hudson107843d2014-09-08 11:26:26 -04002351 mDirty = true;
Romain Guy03c00b52013-06-20 18:30:28 -07002352}
2353
Tom Hudson107843d2014-09-08 11:26:26 -04002354void OpenGLRenderer::drawVertexBuffer(float translateX, float translateY,
Chris Craikbf759452014-08-11 16:00:44 -07002355 const VertexBuffer& vertexBuffer, const SkPaint* paint, int displayFlags) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002356 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002357 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002358 // no vertices to draw
Tom Hudson107843d2014-09-08 11:26:26 -04002359 return;
Chris Craik65cd6122012-12-10 17:56:27 -08002360 }
2361
Chris Craik0d5ac952014-07-15 13:01:02 -07002362 Rect bounds(vertexBuffer.getBounds());
2363 bounds.translate(translateX, translateY);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002364 dirtyLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, *currentTransform());
2365
Chris Craikcb4d6002012-09-25 12:00:29 -07002366 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002367 bool isAA = paint->isAntiAlias();
2368
Chris Craik710f46d2012-09-17 17:25:49 -07002369 setupDraw();
2370 setupDrawNoTexture();
Chris Craik91a8c7c2014-08-12 14:31:35 -07002371 if (isAA) setupDrawVertexAlpha((displayFlags & kVertexBuffer_ShadowInterp));
Chris Craik710f46d2012-09-17 17:25:49 -07002372 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002373 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002374 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002375 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002376 setupDrawProgram();
Chris Craikbf759452014-08-11 16:00:44 -07002377 setupDrawModelView(kModelViewMode_Translate, (displayFlags & kVertexBuffer_Offset),
2378 translateX, translateY, 0, 0);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002379 setupDrawColorUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002380 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002381 setupDrawShaderUniforms(getShader(paint));
Chet Haase858aa932011-05-12 09:06:00 -07002382
ztenghui55bfb4e2013-12-03 10:38:55 -08002383 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002384 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002385 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002386 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002387
Chris Craik710f46d2012-09-17 17:25:49 -07002388 int alphaSlot = -1;
2389 if (isAA) {
2390 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2391 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002392 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002393 glEnableVertexAttribArray(alphaSlot);
2394 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002395 }
Romain Guy04299382012-07-18 17:15:41 -07002396
Chris Craik05f3d6e2014-06-02 16:27:04 -07002397 const VertexBuffer::Mode mode = vertexBuffer.getMode();
2398 if (mode == VertexBuffer::kStandard) {
ztenghui63d41ab2014-02-14 13:13:41 -08002399 mCaches.unbindIndicesBuffer();
2400 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Chris Craik05f3d6e2014-06-02 16:27:04 -07002401 } else if (mode == VertexBuffer::kOnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002402 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002403 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
Chris Craik05f3d6e2014-06-02 16:27:04 -07002404 } else if (mode == VertexBuffer::kTwoPolyRingShadow) {
ztenghui50ecf842014-03-11 16:52:30 -07002405 mCaches.bindShadowIndicesBuffer();
2406 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghuid5e8ade2014-08-13 15:48:02 -07002407 } else if (mode == VertexBuffer::kIndices) {
2408 mCaches.unbindIndicesBuffer();
2409 glDrawElements(GL_TRIANGLE_STRIP, vertexBuffer.getIndexCount(), GL_UNSIGNED_SHORT,
2410 vertexBuffer.getIndices());
ztenghui63d41ab2014-02-14 13:13:41 -08002411 }
Romain Guy04299382012-07-18 17:15:41 -07002412
Chris Craik710f46d2012-09-17 17:25:49 -07002413 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002414 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002415 }
Chris Craik65cd6122012-12-10 17:56:27 -08002416
Tom Hudson107843d2014-09-08 11:26:26 -04002417 mDirty = true;
Chet Haase858aa932011-05-12 09:06:00 -07002418}
2419
2420/**
Chris Craik65cd6122012-12-10 17:56:27 -08002421 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2422 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2423 * screen space in all directions. However, instead of using a fragment shader to compute the
2424 * translucency of the color from its position, we simply use a varying parameter to define how far
2425 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2426 *
2427 * Doesn't yet support joins, caps, or path effects.
2428 */
Tom Hudson107843d2014-09-08 11:26:26 -04002429void OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002430 VertexBuffer vertexBuffer;
2431 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002432 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Tom Hudson107843d2014-09-08 11:26:26 -04002433 drawVertexBuffer(vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002434}
2435
2436/**
2437 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2438 * and additional geometry for defining an alpha slope perimeter.
2439 *
2440 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2441 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2442 * in-shader alpha region, but found it to be taxing on some GPUs.
2443 *
2444 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2445 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002446 */
Tom Hudson107843d2014-09-08 11:26:26 -04002447void OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
2448 if (currentSnapshot()->isIgnored() || count < 4) return;
Chet Haase8a5cc922011-04-26 07:28:09 -07002449
Chris Craik65cd6122012-12-10 17:56:27 -08002450 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002451
Chris Craik65cd6122012-12-10 17:56:27 -08002452 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002453 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), buffer);
2454 const Rect& bounds = buffer.getBounds();
Romain Guyd71ff91d2013-02-08 13:46:40 -08002455
Chris Craik05f3d6e2014-06-02 16:27:04 -07002456 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002457 return;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002458 }
2459
Chris Craikbf759452014-08-11 16:00:44 -07002460 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002461 drawVertexBuffer(buffer, paint, displayFlags);
Chet Haase5b0200b2011-04-13 17:58:08 -07002462}
2463
Tom Hudson107843d2014-09-08 11:26:26 -04002464void OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
2465 if (currentSnapshot()->isIgnored() || count < 2) return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002466
Chris Craik6d29c8d2013-05-08 18:35:44 -07002467 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002468
Chris Craik6d29c8d2013-05-08 18:35:44 -07002469 VertexBuffer buffer;
Chris Craik05f3d6e2014-06-02 16:27:04 -07002470 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002471
Chris Craik05f3d6e2014-06-02 16:27:04 -07002472 const Rect& bounds = buffer.getBounds();
2473 if (quickRejectSetupScissor(bounds.left, bounds.top, bounds.right, bounds.bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002474 return;
Romain Guyed6fcb02011-03-21 13:11:28 -07002475 }
2476
Chris Craikbf759452014-08-11 16:00:44 -07002477 int displayFlags = paint->isAntiAlias() ? 0 : kVertexBuffer_Offset;
Tom Hudson107843d2014-09-08 11:26:26 -04002478 drawVertexBuffer(buffer, paint, displayFlags);
2479
2480 mDirty = true;
Romain Guyed6fcb02011-03-21 13:11:28 -07002481}
2482
Tom Hudson107843d2014-09-08 11:26:26 -04002483void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002484 // No need to check against the clip, we fill the clip region
Tom Hudson107843d2014-09-08 11:26:26 -04002485 if (currentSnapshot()->isIgnored()) return;
Romain Guye45362c2010-11-03 19:58:32 -07002486
Chris Craikd6b65f62014-01-01 14:45:21 -08002487 Rect clip(*currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002488 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002489
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002490 SkPaint paint;
2491 paint.setColor(color);
2492 paint.setXfermodeMode(mode);
2493
2494 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002495
Tom Hudson107843d2014-09-08 11:26:26 -04002496 mDirty = true;
Romain Guyc7d53492010-06-25 13:41:57 -07002497}
Romain Guy9d5316e2010-06-24 19:30:36 -07002498
Tom Hudson107843d2014-09-08 11:26:26 -04002499void OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002500 const SkPaint* paint) {
Tom Hudson107843d2014-09-08 11:26:26 -04002501 if (!texture) return;
Romain Guy01d58e42011-01-19 21:54:02 -08002502 const AutoTexture autoCleanup(texture);
2503
2504 const float x = left + texture->left - texture->offset;
2505 const float y = top + texture->top - texture->offset;
2506
2507 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002508
Tom Hudson107843d2014-09-08 11:26:26 -04002509 mDirty = true;
Romain Guy01d58e42011-01-19 21:54:02 -08002510}
2511
Tom Hudson107843d2014-09-08 11:26:26 -04002512void OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002513 float rx, float ry, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002514 if (currentSnapshot()->isIgnored()
2515 || quickRejectSetupScissor(left, top, right, bottom, p)
2516 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002517 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002518 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002519
Chris Craikcb4d6002012-09-25 12:00:29 -07002520 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002521 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002522 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002523 right - left, bottom - top, rx, ry, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002524 drawShape(left, top, texture, p);
2525 } else {
2526 const VertexBuffer* vertexBuffer = mCaches.tessellationCache.getRoundRect(
2527 *currentTransform(), *p, right - left, bottom - top, rx, ry);
2528 drawVertexBuffer(left, top, *vertexBuffer, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002529 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002530}
2531
Tom Hudson107843d2014-09-08 11:26:26 -04002532void OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002533 if (currentSnapshot()->isIgnored()
2534 || quickRejectSetupScissor(x - radius, y - radius, x + radius, y + radius, p)
2535 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002536 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002537 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002538 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002539 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002540 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002541 drawShape(x - radius, y - radius, texture, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002542 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002543 SkPath path;
2544 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2545 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2546 } else {
2547 path.addCircle(x, y, radius);
2548 }
2549 drawConvexPath(path, p);
Chris Craikcb4d6002012-09-25 12:00:29 -07002550 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002551}
Romain Guy01d58e42011-01-19 21:54:02 -08002552
Tom Hudson107843d2014-09-08 11:26:26 -04002553void OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002554 const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002555 if (currentSnapshot()->isIgnored()
2556 || quickRejectSetupScissor(left, top, right, bottom, p)
2557 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002558 return;
Chris Craik710f46d2012-09-17 17:25:49 -07002559 }
Romain Guy01d58e42011-01-19 21:54:02 -08002560
Chris Craikcb4d6002012-09-25 12:00:29 -07002561 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002562 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002563 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002564 drawShape(left, top, texture, p);
2565 } else {
2566 SkPath path;
2567 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2568 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2569 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2570 }
2571 path.addOval(rect);
2572 drawConvexPath(path, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002573 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002574}
2575
Tom Hudson107843d2014-09-08 11:26:26 -04002576void OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002577 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002578 if (currentSnapshot()->isIgnored()
2579 || quickRejectSetupScissor(left, top, right, bottom, p)
2580 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002581 return;
Romain Guy8b2f5262011-01-23 16:15:02 -08002582 }
2583
Chris Craik780c1282012-10-04 14:10:49 -07002584 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002585 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002586 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002587 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002588 startAngle, sweepAngle, useCenter, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002589 drawShape(left, top, texture, p);
2590 return;
Chris Craik780c1282012-10-04 14:10:49 -07002591 }
Chris Craik780c1282012-10-04 14:10:49 -07002592 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2593 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2594 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2595 }
2596
2597 SkPath path;
2598 if (useCenter) {
2599 path.moveTo(rect.centerX(), rect.centerY());
2600 }
2601 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2602 if (useCenter) {
2603 path.close();
2604 }
Tom Hudson107843d2014-09-08 11:26:26 -04002605 drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002606}
2607
Romain Guycf8675e2012-10-02 12:32:25 -07002608// See SkPaintDefaults.h
2609#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2610
Tom Hudson107843d2014-09-08 11:26:26 -04002611void OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002612 const SkPaint* p) {
Chris Craik947eabf2014-08-19 10:21:12 -07002613 if (currentSnapshot()->isIgnored()
2614 || quickRejectSetupScissor(left, top, right, bottom, p)
2615 || paintWillNotDraw(*p)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002616 return;
Romain Guy6926c722010-07-12 20:20:03 -07002617 }
2618
Chris Craik710f46d2012-09-17 17:25:49 -07002619 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002620 // only fill style is supported by drawConvexPath, since others have to handle joins
2621 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2622 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2623 mCaches.activeTexture(0);
2624 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002625 mCaches.pathCache.getRect(right - left, bottom - top, p);
Tom Hudson107843d2014-09-08 11:26:26 -04002626 drawShape(left, top, texture, p);
2627 } else {
2628 SkPath path;
2629 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2630 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2631 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2632 }
2633 path.addRect(rect);
2634 drawConvexPath(path, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002635 }
Chet Haase858aa932011-05-12 09:06:00 -07002636 } else {
Tom Hudson107843d2014-09-08 11:26:26 -04002637 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
2638 SkPath path;
2639 path.addRect(left, top, right, bottom);
2640 drawConvexPath(path, p);
2641 } else {
2642 drawColorRect(left, top, right, bottom, p);
2643
2644 mDirty = true;
2645 }
Chet Haase858aa932011-05-12 09:06:00 -07002646 }
Romain Guyc7d53492010-06-25 13:41:57 -07002647}
Romain Guy9d5316e2010-06-24 19:30:36 -07002648
Chris Craikd218a922014-01-02 17:13:34 -08002649void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2650 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002651 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002652 mCaches.activeTexture(0);
2653
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002654 TextShadow textShadow;
2655 if (!getTextShadow(paint, &textShadow)) {
2656 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2657 }
2658
Raph Levien416a8472012-07-19 22:48:17 -07002659 // NOTE: The drop shadow will not perform gamma correction
2660 // if shader-based correction is enabled
2661 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2662 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002663 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002664 // If the drop shadow exceeds the max texture size or couldn't be
2665 // allocated, skip drawing
2666 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002667 const AutoTexture autoCleanup(shadow);
2668
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002669 const float sx = x - shadow->left + textShadow.dx;
2670 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002671
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002672 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * mSnapshot->alpha;
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002673 if (getShader(paint)) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002674 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002675 }
2676
2677 setupDraw();
2678 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002679 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002680 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002681 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002682 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002683 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002684 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2685 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002686 setupDrawTexture(shadow->id);
2687 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002688 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04002689 setupDrawShaderUniforms(getShader(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002690 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2691
2692 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2693}
2694
Romain Guy768bffc2013-02-27 13:50:45 -08002695bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002696 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002697 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2698}
2699
Tom Hudson107843d2014-09-08 11:26:26 -04002700void OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002701 const float* positions, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002702 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002703 return;
Romain Guyeb9a5362012-01-17 17:39:26 -08002704 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002705
Romain Guy671d6cf2012-01-18 12:39:17 -08002706 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002707 if (!currentTransform()->isSimple()) {
Tom Hudson107843d2014-09-08 11:26:26 -04002708 return;
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 }
2710
Chris Craik39a908c2013-06-13 14:39:01 -07002711 mCaches.enableScissor();
2712
Romain Guy671d6cf2012-01-18 12:39:17 -08002713 float x = 0.0f;
2714 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002715 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002716 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002717 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2718 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 }
2720
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002721 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002722 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy671d6cf2012-01-18 12:39:17 -08002723
2724 int alpha;
2725 SkXfermode::Mode mode;
2726 getAlphaAndMode(paint, &alpha, &mode);
2727
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002728 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002729 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002730 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002731 }
2732
Romain Guy671d6cf2012-01-18 12:39:17 -08002733 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002734 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002735 if (pureTranslate && !linearFilter) {
2736 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2737 }
Romain Guy257ae352013-03-20 16:31:12 -07002738 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002739
2740 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2741 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2742
Romain Guy211370f2012-02-01 16:10:55 -08002743 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002744
Victoria Lease1e546812013-06-25 14:25:17 -07002745 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002746 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002747 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002748 if (hasActiveLayer) {
2749 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002750 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 }
2752 dirtyLayerUnchecked(bounds, getRegion());
2753 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 }
Chet Haase48659092012-05-31 15:21:51 -07002755
Tom Hudson107843d2014-09-08 11:26:26 -04002756 mDirty = true;
Romain Guyeb9a5362012-01-17 17:39:26 -08002757}
2758
Chris Craik59744b72014-07-01 17:56:52 -07002759bool OpenGLRenderer::findBestFontTransform(const mat4& transform, SkMatrix* outMatrix) const {
Romain Guy624234f2013-03-05 16:43:31 -08002760 if (CC_LIKELY(transform.isPureTranslate())) {
Chris Craik59744b72014-07-01 17:56:52 -07002761 outMatrix->setIdentity();
2762 return false;
2763 } else if (CC_UNLIKELY(transform.isPerspective())) {
2764 outMatrix->setIdentity();
2765 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002766 }
Chris Craik59744b72014-07-01 17:56:52 -07002767
2768 /**
Chris Craika736cd92014-08-04 13:18:38 -07002769 * Input is a non-perspective, scaling transform. Generate a scale-only transform,
2770 * with values rounded to the nearest int.
Chris Craik59744b72014-07-01 17:56:52 -07002771 */
2772 float sx, sy;
2773 transform.decomposeScale(sx, sy);
Chris Craika736cd92014-08-04 13:18:38 -07002774 outMatrix->setScale(
2775 roundf(fmaxf(1.0f, sx)),
2776 roundf(fmaxf(1.0f, sy)));
2777 return true;
Romain Guy624234f2013-03-05 16:43:31 -08002778}
2779
Tom Hudson107843d2014-09-08 11:26:26 -04002780void OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002781 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002782 DrawOpMode drawOpMode) {
2783
Chris Craik527a3aa2013-03-04 10:19:31 -08002784 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002785 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2786 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craikd6b65f62014-01-01 14:45:21 -08002787 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002788 quickRejectSetupScissor(bounds)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002789 return;
Chris Craik28ce94a2013-05-31 11:38:03 -07002790 }
Romain Guycac5fd32011-12-01 20:08:50 -08002791 }
2792
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002793 const float oldX = x;
2794 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002795
Chris Craikd6b65f62014-01-01 14:45:21 -08002796 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002797 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002798
Romain Guy211370f2012-02-01 16:10:55 -08002799 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002800 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2801 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002802 }
2803
Romain Guy86568192010-12-14 15:55:39 -08002804 int alpha;
2805 SkXfermode::Mode mode;
2806 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002807
Romain Guyc74f45a2013-02-26 19:10:14 -08002808 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2809
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002810 if (CC_UNLIKELY(hasTextShadow(paint))) {
Chris Craik59744b72014-07-01 17:56:52 -07002811 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guyc74f45a2013-02-26 19:10:14 -08002812 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002813 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002814 }
2815
Romain Guy19d4dd82013-03-04 11:14:26 -08002816 const bool hasActiveLayer = hasLayer();
2817
Romain Guy624234f2013-03-05 16:43:31 -08002818 // We only pass a partial transform to the font renderer. That partial
2819 // matrix defines how glyphs are rasterized. Typically we want glyphs
2820 // to be rasterized at their final size on screen, which means the partial
2821 // matrix needs to take the scale factor into account.
2822 // When a partial matrix is used to transform glyphs during rasterization,
2823 // the mesh is generated with the inverse transform (in the case of scale,
2824 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2825 // apply the full transform matrix at draw time in the vertex shader.
2826 // Applying the full matrix in the shader is the easiest way to handle
2827 // rotation and perspective and allows us to always generated quads in the
2828 // font renderer which greatly simplifies the code, clipping in particular.
Chris Craik59744b72014-07-01 17:56:52 -07002829 SkMatrix fontTransform;
2830 bool linearFilter = findBestFontTransform(transform, &fontTransform)
2831 || fabs(y - (int) y) > 0.0f
2832 || fabs(x - (int) x) > 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002833 fontRenderer.setFont(paint, fontTransform);
Romain Guy257ae352013-03-20 16:31:12 -07002834 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002835
Romain Guy624234f2013-03-05 16:43:31 -08002836 // TODO: Implement better clipping for scaled/rotated text
Chris Craikd6b65f62014-01-01 14:45:21 -08002837 const Rect* clip = !pureTranslate ? NULL : currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002838 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 -07002839
Raph Levien996e57c2012-07-23 15:22:52 -07002840 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002841 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002842
2843 // don't call issuedrawcommand, do it at end of batch
2844 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002845 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002846 SkPaint paintCopy(*paint);
2847 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2848 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002849 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002850 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002851 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002852 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002853 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002854
Chris Craik527a3aa2013-03-04 10:19:31 -08002855 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002856 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002857 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002858 }
Chris Craik41541822013-05-03 16:35:54 -07002859 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002860 }
Romain Guy694b5192010-07-21 21:33:20 -07002861
Chris Craike63f7c622013-10-17 10:30:55 -07002862 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002863
Tom Hudson107843d2014-09-08 11:26:26 -04002864 mDirty = true;
Romain Guy694b5192010-07-21 21:33:20 -07002865}
2866
Tom Hudson107843d2014-09-08 11:26:26 -04002867void OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002868 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002869 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Tom Hudson107843d2014-09-08 11:26:26 -04002870 return;
Romain Guy03d58522012-02-24 17:54:07 -08002871 }
2872
Chris Craik39a908c2013-06-13 14:39:01 -07002873 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2874 mCaches.enableScissor();
2875
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002876 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Chris Craik59744b72014-07-01 17:56:52 -07002877 fontRenderer.setFont(paint, SkMatrix::I());
Romain Guy257ae352013-03-20 16:31:12 -07002878 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002879
2880 int alpha;
2881 SkXfermode::Mode mode;
2882 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002883 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002884
Romain Guy97771732012-02-28 18:17:02 -08002885 const Rect* clip = &mSnapshot->getLocalClip();
2886 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 -08002887
Romain Guy97771732012-02-28 18:17:02 -08002888 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002889
2890 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002891 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002892 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002893 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002894 dirtyLayerUnchecked(bounds, getRegion());
2895 }
Romain Guy97771732012-02-28 18:17:02 -08002896 }
Chet Haase48659092012-05-31 15:21:51 -07002897
Tom Hudson107843d2014-09-08 11:26:26 -04002898 mDirty = true;
Romain Guy325740f2012-02-24 16:48:34 -08002899}
2900
Tom Hudson107843d2014-09-08 11:26:26 -04002901void OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
2902 if (currentSnapshot()->isIgnored()) return;
Romain Guydbc26d22010-10-11 17:58:29 -07002903
Romain Guya1d3c912011-12-13 14:55:06 -08002904 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002905
Romain Guyfb8b7632010-08-23 21:05:08 -07002906 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002907 if (!texture) return;
Romain Guy22158e12010-08-06 11:18:34 -07002908 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002909
Romain Guy8b55f372010-08-18 17:10:07 -07002910 const float x = texture->left - texture->offset;
2911 const float y = texture->top - texture->offset;
2912
Romain Guy01d58e42011-01-19 21:54:02 -08002913 drawPathTexture(texture, x, y, paint);
Tom Hudson107843d2014-09-08 11:26:26 -04002914 mDirty = true;
Romain Guy7fbcc042010-08-04 15:40:07 -07002915}
2916
Tom Hudson107843d2014-09-08 11:26:26 -04002917void OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002918 if (!layer) {
Tom Hudson107843d2014-09-08 11:26:26 -04002919 return;
Romain Guy35643dd2012-09-18 15:40:58 -07002920 }
2921
Romain Guyb2e2f242012-10-17 18:18:35 -07002922 mat4* transform = NULL;
2923 if (layer->isTextureLayer()) {
2924 transform = &layer->getTransform();
2925 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002926 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002927 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002928 }
2929 }
2930
Chris Craik39a908c2013-06-13 14:39:01 -07002931 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002932 const bool rejected = calculateQuickRejectForScissor(x, y,
Chris Craikdeeda3d2014-05-05 19:09:33 -07002933 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, NULL, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002934
2935 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002936 if (transform && !transform->isIdentity()) {
2937 restore();
2938 }
Tom Hudson107843d2014-09-08 11:26:26 -04002939 return;
Romain Guy6c319ca2011-01-11 14:29:25 -08002940 }
2941
Chris Craik62d307c2014-07-29 10:35:13 -07002942 EVENT_LOGD("drawLayer," RECT_STRING ", clipRequired %d", x, y,
2943 x + layer->layer.getWidth(), y + layer->layer.getHeight(), clipRequired);
2944
Romain Guy5bb3c732012-11-29 17:52:58 -08002945 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002946
Chris Craik39a908c2013-06-13 14:39:01 -07002947 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002948 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002949
Romain Guy211370f2012-02-01 16:10:55 -08002950 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002951 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002952 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2953 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002954 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002955
Chris Craik16ecda52013-03-29 10:59:59 -07002956 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002957 setupDraw();
2958 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002959 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002960 setupDrawColorFilter(layer->getColorFilter());
2961 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002962 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002963 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002964 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002965 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002966 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2967 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2968 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002969
Romain Guyd21b6e12011-11-30 20:21:23 -08002970 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002971 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002972 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002973 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002974 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08002975 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07002976 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2977 }
Romain Guyf219da52011-01-16 12:54:25 -08002978
Romain Guy448455f2013-07-22 13:57:50 -07002979 TextureVertex* mesh = &layer->mesh[0];
2980 GLsizei elementsCount = layer->meshElementCount;
2981
2982 while (elementsCount > 0) {
2983 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
2984
Romain Guy3380cfd2013-08-15 16:57:57 -07002985 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07002986 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2987 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
2988
2989 elementsCount -= drawCount;
2990 // Though there are 4 vertices in a quad, we use 6 indices per
2991 // quad to draw with GL_TRIANGLES
2992 mesh += (drawCount / 6) * 4;
2993 }
Romain Guyf219da52011-01-16 12:54:25 -08002994
Romain Guy3a3133d2011-02-01 22:59:58 -08002995#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002996 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002997#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002998 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002999
Romain Guy5bb3c732012-11-29 17:52:58 -08003000 if (layer->debugDrawUpdate) {
3001 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003002
3003 SkPaint paint;
3004 paint.setColor(0x7f00ff00);
3005 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003006 }
Romain Guyf219da52011-01-16 12:54:25 -08003007 }
Chris Craik34416ea2013-04-15 16:08:28 -07003008 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003009
Romain Guyb2e2f242012-10-17 18:18:35 -07003010 if (transform && !transform->isIdentity()) {
3011 restore();
3012 }
3013
Tom Hudson107843d2014-09-08 11:26:26 -04003014 mDirty = true;
Romain Guy6c319ca2011-01-11 14:29:25 -08003015}
3016
Romain Guy6926c722010-07-12 20:20:03 -07003017///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003018// Draw filters
3019///////////////////////////////////////////////////////////////////////////////
Derek Sollenberger09c2d4f2014-10-15 09:21:10 -04003020void OpenGLRenderer::setDrawFilter(SkDrawFilter* filter) {
3021 // We should never get here since we apply the draw filter when stashing
3022 // the paints in the DisplayList.
3023 LOG_ALWAYS_FATAL("OpenGLRenderer does not directly support DrawFilters");
Romain Guy5ff9df62012-01-23 17:09:05 -08003024}
3025
3026///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003027// Drawing implementation
3028///////////////////////////////////////////////////////////////////////////////
3029
Chris Craikd218a922014-01-02 17:13:34 -08003030Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003031 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3032 if (!texture) {
3033 return mCaches.textureCache.get(bitmap);
3034 }
3035 return texture;
3036}
3037
Romain Guy01d58e42011-01-19 21:54:02 -08003038void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003039 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003040 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003041 return;
3042 }
3043
3044 int alpha;
3045 SkXfermode::Mode mode;
3046 getAlphaAndMode(paint, &alpha, &mode);
3047
3048 setupDraw();
3049 setupDrawWithTexture(true);
3050 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003051 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003052 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003053 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003054 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003055 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3056 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003057 setupDrawTexture(texture->id);
3058 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003059 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003060 setupDrawShaderUniforms(getShader(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003061 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3062
3063 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003064}
3065
Romain Guyf607bdc2010-09-10 19:20:06 -07003066// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003067#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3068#define kStdUnderline_Offset (1.0f / 9.0f)
3069#define kStdUnderline_Thickness (1.0f / 18.0f)
3070
Chris Craikd218a922014-01-02 17:13:34 -08003071void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3072 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003073 // Handle underline and strike-through
3074 uint32_t flags = paint->getFlags();
3075 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003076 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003077
Romain Guy211370f2012-02-01 16:10:55 -08003078 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003079 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003080 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003081
Raph Levien8b4072d2012-07-30 15:50:00 -07003082 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003083 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003084
Romain Guyf6834472011-01-23 13:32:12 -08003085 int linesCount = 0;
3086 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3087 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3088
3089 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003090 float points[pointsCount];
3091 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003092
3093 if (flags & SkPaint::kUnderlineText_Flag) {
3094 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003095 points[currentPoint++] = left;
3096 points[currentPoint++] = top;
3097 points[currentPoint++] = left + underlineWidth;
3098 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003099 }
3100
3101 if (flags & SkPaint::kStrikeThruText_Flag) {
3102 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003103 points[currentPoint++] = left;
3104 points[currentPoint++] = top;
3105 points[currentPoint++] = left + underlineWidth;
3106 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003107 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003108
Romain Guy726aeba2011-06-01 14:52:00 -07003109 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003110
Romain Guy726aeba2011-06-01 14:52:00 -07003111 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003112 }
3113 }
3114}
3115
Tom Hudson107843d2014-09-08 11:26:26 -04003116void OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003117 if (currentSnapshot()->isIgnored()) {
Tom Hudson107843d2014-09-08 11:26:26 -04003118 return;
Romain Guy672433d2013-01-04 19:05:13 -08003119 }
3120
Tom Hudson107843d2014-09-08 11:26:26 -04003121 drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003122}
3123
Chris Craikb79a3e32014-03-11 12:20:17 -07003124static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3125 // map z coordinate with true 3d matrix
3126 point.z = transformZ.mapZ(point);
3127
3128 // map x,y coordinates with draw/Skia matrix
3129 transformXY.mapPoint(point.x, point.y);
3130}
3131
Tom Hudson107843d2014-09-08 11:26:26 -04003132void OpenGLRenderer::drawShadow(float casterAlpha,
Chris Craik05f3d6e2014-06-02 16:27:04 -07003133 const VertexBuffer* ambientShadowVertexBuffer, const VertexBuffer* spotShadowVertexBuffer) {
Tom Hudson107843d2014-09-08 11:26:26 -04003134 if (currentSnapshot()->isIgnored()) return;
Chris Craikf57776b2013-10-25 18:30:17 -07003135
Chris Craik15a07a22014-01-26 13:43:53 -08003136 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003137 mCaches.enableScissor();
3138
3139 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003140 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003141
ztenghui14a4e352014-08-13 10:44:39 -07003142 // The caller has made sure casterAlpha > 0.
3143 float ambientShadowAlpha = mAmbientShadowAlpha;
3144 if (CC_UNLIKELY(mCaches.propertyAmbientShadowStrength >= 0)) {
3145 ambientShadowAlpha = mCaches.propertyAmbientShadowStrength;
3146 }
3147 if (ambientShadowVertexBuffer && ambientShadowAlpha > 0) {
3148 paint.setARGB(casterAlpha * ambientShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003149 drawVertexBuffer(*ambientShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003150 }
ztenghui7b4516e2014-01-07 10:42:55 -08003151
ztenghui14a4e352014-08-13 10:44:39 -07003152 float spotShadowAlpha = mSpotShadowAlpha;
3153 if (CC_UNLIKELY(mCaches.propertySpotShadowStrength >= 0)) {
3154 spotShadowAlpha = mCaches.propertySpotShadowStrength;
3155 }
3156 if (spotShadowVertexBuffer && spotShadowAlpha > 0) {
3157 paint.setARGB(casterAlpha * spotShadowAlpha, 0, 0, 0);
Chris Craik91a8c7c2014-08-12 14:31:35 -07003158 drawVertexBuffer(*spotShadowVertexBuffer, &paint, kVertexBuffer_ShadowInterp);
ztenghuief94c6f2014-02-13 17:09:45 -08003159 }
ztenghui7b4516e2014-01-07 10:42:55 -08003160
Tom Hudson107843d2014-09-08 11:26:26 -04003161 mDirty=true;
Chris Craikf57776b2013-10-25 18:30:17 -07003162}
3163
Tom Hudson107843d2014-09-08 11:26:26 -04003164void OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003165 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003166 if (count == 0) {
Tom Hudson107843d2014-09-08 11:26:26 -04003167 return;
Romain Guy3b753822013-03-05 10:27:35 -08003168 }
Romain Guy735738c2012-12-03 12:34:51 -08003169
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003170 int color = paint->getColor();
3171 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003172 if (getShader(paint)) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003173 color |= 0x00ffffff;
3174 }
3175
Romain Guy672433d2013-01-04 19:05:13 -08003176 float left = FLT_MAX;
3177 float top = FLT_MAX;
3178 float right = FLT_MIN;
3179 float bottom = FLT_MIN;
3180
Romain Guy448455f2013-07-22 13:57:50 -07003181 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003182 Vertex* vertex = mesh;
3183
Chris Craik2af46352012-11-26 18:30:17 -08003184 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003185 float l = rects[index + 0];
3186 float t = rects[index + 1];
3187 float r = rects[index + 2];
3188 float b = rects[index + 3];
3189
Romain Guy3b753822013-03-05 10:27:35 -08003190 Vertex::set(vertex++, l, t);
3191 Vertex::set(vertex++, r, t);
3192 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003193 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003194
Romain Guy3b753822013-03-05 10:27:35 -08003195 left = fminf(left, l);
3196 top = fminf(top, t);
3197 right = fmaxf(right, r);
3198 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003199 }
3200
Chris Craikf0a59072013-11-19 18:00:46 -08003201 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Tom Hudson107843d2014-09-08 11:26:26 -04003202 return;
Romain Guya362c692013-02-04 13:50:16 -08003203 }
Romain Guy672433d2013-01-04 19:05:13 -08003204
Romain Guy672433d2013-01-04 19:05:13 -08003205 setupDraw();
3206 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003207 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003208 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003209 setupDrawColorFilter(getColorFilter(paint));
3210 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003211 setupDrawProgram();
3212 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003213 setupDrawModelView(kModelViewMode_Translate, false,
3214 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003215 setupDrawColorUniforms(getShader(paint));
3216 setupDrawShaderUniforms(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003217 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003218
Romain Guy8ce00302013-01-15 18:51:42 -08003219 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003220 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003221 }
3222
Chris Craik4063a0e2013-11-15 16:06:56 -08003223 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003224
Tom Hudson107843d2014-09-08 11:26:26 -04003225 mDirty = true;
Romain Guy672433d2013-01-04 19:05:13 -08003226}
3227
Romain Guy026c5e162010-06-28 17:12:22 -07003228void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003229 const SkPaint* paint, bool ignoreTransform) {
3230 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003231 // If a shader is set, preserve only the alpha
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003232 if (getShader(paint)) {
Romain Guyd27977d2010-07-14 19:18:51 -07003233 color |= 0x00ffffff;
3234 }
3235
Romain Guy70ca14e2010-12-13 18:24:33 -08003236 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003237 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003238 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003239 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003240 setupDrawColorFilter(getColorFilter(paint));
3241 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003242 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003243 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3244 left, top, right, bottom, ignoreTransform);
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003245 setupDrawColorUniforms(getShader(paint));
3246 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003247 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003248 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003249
Romain Guyc95c8d62010-09-17 15:31:32 -07003250 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3251}
3252
Romain Guy82ba8142010-07-09 13:25:56 -07003253void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003254 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003255 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003256
Romain Guy3b748a42013-04-17 18:54:38 -07003257 GLvoid* vertices = (GLvoid*) NULL;
3258 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3259
3260 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003261 vertices = &mMeshVertices[0].x;
3262 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003263
3264 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3265 texture->uvMapper->map(uvs);
3266
3267 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3268 }
3269
Chris Craikd6b65f62014-01-01 14:45:21 -08003270 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3271 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3272 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003273
Romain Guyd21b6e12011-11-30 20:21:23 -08003274 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003275 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003276 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003277 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003278 } else {
Chris Craik678625242014-02-28 12:26:34 -08003279 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003280 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003281 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3282 }
3283
3284 if (texture->uvMapper) {
3285 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003286 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003287}
3288
Romain Guyf7f93552010-07-08 19:17:03 -07003289void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003290 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003291 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003292 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3293 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003294
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003295 int a;
3296 SkXfermode::Mode mode;
3297 getAlphaAndMode(paint, &a, &mode);
3298 const float alpha = a / 255.0f;
3299
Romain Guy746b7402010-10-26 16:27:31 -07003300 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003301 setupDrawWithTexture();
3302 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003303 setupDrawColorFilter(getColorFilter(paint));
3304 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003305 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003306 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003307 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003308 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003309 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003310 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003311 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003312
Romain Guy6820ac82010-09-15 18:11:50 -07003313 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003314}
3315
Romain Guy3b748a42013-04-17 18:54:38 -07003316void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003317 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003318 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003319 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3320 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003321
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003322 int a;
3323 SkXfermode::Mode mode;
3324 getAlphaAndMode(paint, &a, &mode);
3325 const float alpha = a / 255.0f;
3326
Romain Guy3b748a42013-04-17 18:54:38 -07003327 setupDraw();
3328 setupDrawWithTexture();
3329 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003330 setupDrawColorFilter(getColorFilter(paint));
3331 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003332 setupDrawProgram();
3333 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003334 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003335 setupDrawTexture(texture);
3336 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003337 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003338 setupDrawMeshIndices(vertices, texCoords, vbo);
3339
3340 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003341}
3342
Romain Guy886b2752013-01-04 12:26:18 -08003343void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003344 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003345 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003346 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003347
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003348 int color = paint != NULL ? paint->getColor() : 0;
3349 int alpha;
3350 SkXfermode::Mode mode;
3351 getAlphaAndMode(paint, &alpha, &mode);
3352
Romain Guy886b2752013-01-04 12:26:18 -08003353 setupDraw();
3354 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003355 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003356 setupDrawAlpha8Color(color, alpha);
3357 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003358 setupDrawColorFilter(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003359 setupDrawShader(getShader(paint));
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003360 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003361 setupDrawProgram();
3362 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);
3365 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003366 setupDrawColorFilterUniforms(getColorFilter(paint));
Leon Scroggins IIId1ad5e62014-05-05 12:50:38 -04003367 setupDrawShaderUniforms(getShader(paint), ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003368 setupDrawMesh(vertices, texCoords);
3369
3370 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003371}
3372
Romain Guya5aed0d2010-09-09 14:42:43 -07003373void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003374 ProgramDescription& description, bool swapSrcDst) {
Chris Craikdeeda3d2014-05-05 19:09:33 -07003375
3376 if (mSnapshot->roundRectClipState != NULL /*&& !mSkipOutlineClip*/) {
3377 blend = true;
3378 mDescription.hasRoundRectClip = true;
3379 }
3380 mSkipOutlineClip = true;
3381
Romain Guy82ba8142010-07-09 13:25:56 -07003382 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003383
Romain Guy82ba8142010-07-09 13:25:56 -07003384 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003385 // These blend modes are not supported by OpenGL directly and have
3386 // to be implemented using shaders. Since the shader will perform
3387 // the blending, turn blending off here
3388 // If the blend mode cannot be implemented using shaders, fall
3389 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003390 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003391 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003392 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003393 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003394
Romain Guy82bc7a72012-01-03 14:13:39 -08003395 if (mCaches.blend) {
3396 glDisable(GL_BLEND);
3397 mCaches.blend = false;
3398 }
3399
3400 return;
3401 } else {
3402 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003403 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003404 }
3405
3406 if (!mCaches.blend) {
3407 glEnable(GL_BLEND);
3408 }
3409
3410 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3411 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3412
3413 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3414 glBlendFunc(sourceMode, destMode);
3415 mCaches.lastSrcMode = sourceMode;
3416 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003417 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003418 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003419 glDisable(GL_BLEND);
3420 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003421 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003422}
3423
Romain Guy889f8d12010-07-29 14:37:42 -07003424bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003425 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003426 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003427 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003428 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003429 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003430 }
Romain Guy6926c722010-07-12 20:20:03 -07003431 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003432}
3433
Romain Guy026c5e162010-06-28 17:12:22 -07003434void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003435 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003436 TextureVertex::setUV(v++, u1, v1);
3437 TextureVertex::setUV(v++, u2, v1);
3438 TextureVertex::setUV(v++, u1, v2);
3439 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003440}
3441
Chris Craikd218a922014-01-02 17:13:34 -08003442void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003443 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003444 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3445 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003446 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003447 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003448 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003449}
3450
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003451float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003452 float alpha;
3453 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3454 alpha = mDrawModifiers.mOverrideLayerAlpha;
3455 } else {
3456 alpha = layer->getAlpha() / 255.0f;
3457 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003458 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003459}
3460
Romain Guy9d5316e2010-06-24 19:30:36 -07003461}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003462}; // namespace android