blob: 87b07b30b79135fe567dec66b2ff7ddda3a674af [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>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080039#include "ShadowTessellator.h"
Romain Guya957eea2010-12-08 18:34:42 -080040#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080041#include "VertexBuffer.h"
Romain Guye4d01122010-06-16 18:44:05 -070042
43namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070044namespace uirenderer {
45
46///////////////////////////////////////////////////////////////////////////////
47// Defines
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy759ea802010-09-16 20:49:46 -070050#define RAD_TO_DEG (180.0f / 3.14159265f)
51#define MIN_ANGLE 0.001f
52
Romain Guyf8773082012-07-12 18:01:00 -070053#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070054
Chris Craik678625242014-02-28 12:26:34 -080055static GLenum getFilter(const SkPaint* paint) {
56 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
57 return GL_LINEAR;
58 }
59 return GL_NEAREST;
60}
Romain Guyd21b6e12011-11-30 20:21:23 -080061
Romain Guy9d5316e2010-06-24 19:30:36 -070062///////////////////////////////////////////////////////////////////////////////
63// Globals
64///////////////////////////////////////////////////////////////////////////////
65
Romain Guy889f8d12010-07-29 14:37:42 -070066/**
67 * Structure mapping Skia xfermodes to OpenGL blending factors.
68 */
69struct Blender {
70 SkXfermode::Mode mode;
71 GLenum src;
72 GLenum dst;
73}; // struct Blender
74
Romain Guy026c5e162010-06-28 17:12:22 -070075// In this array, the index of each Blender equals the value of the first
76// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
77static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070078 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
80 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
81 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
83 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
84 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
85 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
86 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
87 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
89 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
90 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050091 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070093};
Romain Guye4d01122010-06-16 18:44:05 -070094
Romain Guy87a76572010-09-13 18:11:21 -070095// This array contains the swapped version of each SkXfermode. For instance
96// this array's SrcOver blending mode is actually DstOver. You can refer to
97// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070098static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070099 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
101 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
102 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
103 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
105 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
106 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
107 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
108 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
109 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
110 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
111 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500112 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700113 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700114};
115
Romain Guyf6a11b82010-06-23 17:47:49 -0700116///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700117// Functions
118///////////////////////////////////////////////////////////////////////////////
119
120template<typename T>
121static inline T min(T a, T b) {
122 return a < b ? a : b;
123}
124
125///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700126// Constructors/destructor
127///////////////////////////////////////////////////////////////////////////////
128
Romain Guy3bbacf22013-02-06 16:51:04 -0800129OpenGLRenderer::OpenGLRenderer():
130 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800131 // *set* draw modifiers to be 0
132 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700133 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700134
Romain Guyac670c02010-07-27 17:39:27 -0700135 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
136
Romain Guy96885eb2013-03-26 15:05:58 -0700137 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700138 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700139
140 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700141}
142
Romain Guy85bf02f2010-06-22 13:11:24 -0700143OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700144 // The context has already been destroyed at this point, do not call
145 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700146}
147
Romain Guy87e2f7572012-09-24 11:37:12 -0700148void OpenGLRenderer::initProperties() {
149 char property[PROPERTY_VALUE_MAX];
150 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
151 mScissorOptimizationDisabled = !strcasecmp(property, "true");
152 INIT_LOGD(" Scissor optimization %s",
153 mScissorOptimizationDisabled ? "disabled" : "enabled");
154 } else {
155 INIT_LOGD(" Scissor optimization enabled");
156 }
Romain Guy13631f32012-01-30 17:41:55 -0800157}
158
159///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700160// Setup
161///////////////////////////////////////////////////////////////////////////////
162
Romain Guy85bf02f2010-06-22 13:11:24 -0700163void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700164 initViewport(width, height);
165
166 glDisable(GL_DITHER);
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168
169 glEnableVertexAttribArray(Program::kBindingPosition);
170}
171
172void OpenGLRenderer::initViewport(int width, int height) {
Chris Craike361ad72014-03-11 10:48:43 -0700173 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700174
Chris Craik14e51302013-12-30 15:32:54 -0800175 initializeViewport(width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700176}
177
Romain Guy96885eb2013-03-26 15:05:58 -0700178void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800179 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800180 mCaches.clearGarbage();
181
Chris Craik14e51302013-12-30 15:32:54 -0800182 initializeSaveStack(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700183 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700184 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700185}
186
187status_t OpenGLRenderer::startFrame() {
188 if (mFrameStarted) return DrawGlInfo::kStatusDone;
189 mFrameStarted = true;
190
Romain Guy41308e22012-10-22 20:02:43 -0700191 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700192
Romain Guy96885eb2013-03-26 15:05:58 -0700193 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700194
Chris Craik14e51302013-12-30 15:32:54 -0800195 glViewport(0, 0, getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800196
Romain Guy54c1a642012-09-27 17:55:46 -0700197 // Functors break the tiling extension in pretty spectacular ways
198 // This ensures we don't use tiling when a functor is going to be
199 // invoked during the frame
200 mSuppressTiling = mCaches.hasRegisteredFunctors();
201
Chris Craikd6b65f62014-01-01 14:45:21 -0800202 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700203
Romain Guy7c450aa2012-09-21 19:15:00 -0700204 debugOverdraw(true, true);
205
Romain Guy96885eb2013-03-26 15:05:58 -0700206 return clear(mTilingClip.left, mTilingClip.top,
207 mTilingClip.right, mTilingClip.bottom, mOpaque);
208}
209
Romain Guy96885eb2013-03-26 15:05:58 -0700210status_t OpenGLRenderer::prepareDirty(float left, float top,
211 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700212
Romain Guy96885eb2013-03-26 15:05:58 -0700213 setupFrameState(left, top, right, bottom, opaque);
214
215 // Layer renderers will start the frame immediately
216 // The framebuffer renderer will first defer the display list
217 // for each layer and wait until the first drawing command
218 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800219 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700220 syncState();
221 updateLayers();
222 } else {
223 return startFrame();
224 }
225
226 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700227}
228
Romain Guydcfc8362013-01-03 13:08:57 -0800229void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
230 // If we know that we are going to redraw the entire framebuffer,
231 // perform a discard to let the driver know we don't need to preserve
232 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800233 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800234 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800235 const bool isFbo = getTargetFbo() == 0;
236 const GLenum attachments[] = {
237 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
238 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800239 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
240 }
241}
242
Romain Guy7c25aab2012-10-18 15:05:02 -0700243status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700244 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700245 mCaches.enableScissor();
Chris Craikd6b65f62014-01-01 14:45:21 -0800246 mCaches.setScissor(left, currentSnapshot()->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700247 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700248 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700249 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700250
Romain Guy7c25aab2012-10-18 15:05:02 -0700251 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700252 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700253}
254
255void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700256 if (mCaches.blend) {
257 glEnable(GL_BLEND);
258 } else {
259 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700260 }
Romain Guybb9524b2010-06-22 18:56:38 -0700261}
262
Chris Craikd6b65f62014-01-01 14:45:21 -0800263void OpenGLRenderer::startTilingCurrentClip(bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700264 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800265 const Snapshot* snapshot = currentSnapshot();
266
Chris Craik14e51302013-12-30 15:32:54 -0800267 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800268 if (snapshot->flags & Snapshot::kFlagFboTarget) {
269 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700270 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700271
Chris Craikd6b65f62014-01-01 14:45:21 -0800272 startTiling(*clip, snapshot->height, opaque);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800273 }
274}
275
276void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
277 if (!mSuppressTiling) {
278 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800279 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700280 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700281}
282
283void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700284 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700285}
286
Romain Guyb025b9c2010-09-16 14:16:48 -0700287void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700288 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700289 endTiling();
290
Romain Guyca89e2a2013-03-08 17:44:20 -0800291 // When finish() is invoked on FBO 0 we've reached the end
292 // of the current frame
293 if (getTargetFbo() == 0) {
294 mCaches.pathCache.trim();
295 }
296
Romain Guy11cb6422012-09-21 00:39:43 -0700297 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700298#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700299 GLenum status = GL_NO_ERROR;
300 while ((status = glGetError()) != GL_NO_ERROR) {
301 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
302 switch (status) {
303 case GL_INVALID_ENUM:
304 ALOGE(" GL_INVALID_ENUM");
305 break;
306 case GL_INVALID_VALUE:
307 ALOGE(" GL_INVALID_VALUE");
308 break;
309 case GL_INVALID_OPERATION:
310 ALOGE(" GL_INVALID_OPERATION");
311 break;
312 case GL_OUT_OF_MEMORY:
313 ALOGE(" Out of memory!");
314 break;
315 }
Romain Guya07105b2011-01-10 21:14:18 -0800316 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700317#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700318
Romain Guyc15008e2010-11-10 11:59:15 -0800319#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800320 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700321#else
322 if (mCaches.getDebugLevel() & kDebugMemory) {
323 mCaches.dumpMemoryUsage();
324 }
Romain Guyc15008e2010-11-10 11:59:15 -0800325#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700326 }
Romain Guy96885eb2013-03-26 15:05:58 -0700327
Romain Guy78dd96d2013-05-03 14:24:16 -0700328 if (mCountOverdraw) {
329 countOverdraw();
330 }
331
Romain Guy96885eb2013-03-26 15:05:58 -0700332 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700333}
334
Romain Guy6c319ca2011-01-11 14:29:25 -0800335void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700336 if (mCaches.currentProgram) {
337 if (mCaches.currentProgram->isInUse()) {
338 mCaches.currentProgram->remove();
339 mCaches.currentProgram = NULL;
340 }
341 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700342 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700343 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800344 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800345 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800346 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700347 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700348}
349
Romain Guy6c319ca2011-01-11 14:29:25 -0800350void OpenGLRenderer::resume() {
Chris Craikd6b65f62014-01-01 14:45:21 -0800351 const Snapshot* snapshot = currentSnapshot();
352 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
353 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700354 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700355
Romain Guy3e263fa2011-12-12 16:47:48 -0800356 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
357
Chet Haase80250612012-08-15 13:46:54 -0700358 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700359 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800360 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700361 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700362
Romain Guya1d3c912011-12-13 14:55:06 -0800363 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700364 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700365
Romain Guy50c0f092010-10-19 11:42:22 -0700366 mCaches.blend = true;
367 glEnable(GL_BLEND);
368 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
369 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700370}
371
Romain Guy35643dd2012-09-18 15:40:58 -0700372void OpenGLRenderer::resumeAfterLayer() {
Chris Craikd6b65f62014-01-01 14:45:21 -0800373 const Snapshot* snapshot = currentSnapshot();
374 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
375 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700376 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700377
378 mCaches.resetScissor();
379 dirtyClip();
380}
381
Romain Guy8f3b8e32012-03-27 16:33:45 -0700382status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800383 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craik408eb122013-03-26 18:55:15 -0700384
Chris Craikd6b65f62014-01-01 14:45:21 -0800385 Rect clip(*currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700386 clip.snapToPixelBoundaries();
387
Romain Guyd643bb52011-03-01 14:55:21 -0800388 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800389 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800390 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800391 dirtyLayerUnchecked(clip, getRegion());
392 }
Romain Guyd643bb52011-03-01 14:55:21 -0800393
Romain Guy08aa2cb2011-03-17 11:06:57 -0700394 DrawGlInfo info;
395 info.clipLeft = clip.left;
396 info.clipTop = clip.top;
397 info.clipRight = clip.right;
398 info.clipBottom = clip.bottom;
399 info.isLayer = hasLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800400 info.width = currentSnapshot()->viewport.getWidth();
401 info.height = currentSnapshot()->height;
402 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700403
John Reck25d2f7b2013-09-10 13:12:09 -0700404 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700405 // setup GL state for functor
406 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700407 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
408 }
John Reck25d2f7b2013-09-10 13:12:09 -0700409 if (mCaches.enableScissor() || dirtyClip) {
410 setScissorFromClip();
411 }
Chris Craik54f574a2013-08-26 11:23:46 -0700412 interrupt();
413
414 // call functor immediately after GL state setup
John Reck750ca6d2014-03-28 16:33:18 -0700415 (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800416
Chet Haasedaf98e92011-01-10 14:10:36 -0800417 resume();
John Reck750ca6d2014-03-28 16:33:18 -0700418 return DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800419}
420
Romain Guyf6a11b82010-06-23 17:47:49 -0700421///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700422// Debug
423///////////////////////////////////////////////////////////////////////////////
424
Romain Guy0f6675332013-03-01 14:31:04 -0800425void OpenGLRenderer::eventMark(const char* name) const {
426 mCaches.eventMark(0, name);
427}
428
Romain Guy87e2f7572012-09-24 11:37:12 -0700429void OpenGLRenderer::startMark(const char* name) const {
430 mCaches.startMark(0, name);
431}
432
433void OpenGLRenderer::endMark() const {
434 mCaches.endMark();
435}
436
437void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
438 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
439 if (clear) {
440 mCaches.disableScissor();
441 mCaches.stencil.clear();
442 }
443 if (enable) {
444 mCaches.stencil.enableDebugWrite();
445 } else {
446 mCaches.stencil.disable();
447 }
448 }
449}
450
451void OpenGLRenderer::renderOverdraw() {
452 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700453 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700454
455 mCaches.enableScissor();
Chris Craikd6b65f62014-01-01 14:45:21 -0800456 mCaches.setScissor(clip->left, firstSnapshot()->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700457 clip->right - clip->left, clip->bottom - clip->top);
458
Romain Guy627c6fd2013-08-21 11:53:18 -0700459 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700460 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700461 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
462
463 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700464 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700465 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
466
467 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700468 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700469 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
470
471 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700472 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700473 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
474
Romain Guy87e2f7572012-09-24 11:37:12 -0700475 mCaches.stencil.disable();
476 }
477}
478
Romain Guy78dd96d2013-05-03 14:24:16 -0700479void OpenGLRenderer::countOverdraw() {
Chris Craik14e51302013-12-30 15:32:54 -0800480 size_t count = getWidth() * getHeight();
Romain Guy78dd96d2013-05-03 14:24:16 -0700481 uint32_t* buffer = new uint32_t[count];
Chris Craik14e51302013-12-30 15:32:54 -0800482 glReadPixels(0, 0, getWidth(), getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
Romain Guy78dd96d2013-05-03 14:24:16 -0700483
484 size_t total = 0;
485 for (size_t i = 0; i < count; i++) {
486 total += buffer[i] & 0xff;
487 }
488
489 mOverdraw = total / float(count);
490
491 delete[] buffer;
492}
493
Romain Guy87e2f7572012-09-24 11:37:12 -0700494///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700495// Layers
496///////////////////////////////////////////////////////////////////////////////
497
498bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700499 if (layer->deferredUpdateScheduled && layer->renderer &&
John Reck087bc0c2014-04-04 16:20:08 -0700500 layer->displayList.get() && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700501 ATRACE_CALL();
502
Romain Guy11cb6422012-09-21 00:39:43 -0700503 Rect& dirty = layer->dirtyRect;
504
Romain Guy7c450aa2012-09-21 19:15:00 -0700505 if (inFrame) {
506 endTiling();
507 debugOverdraw(false, false);
508 }
Romain Guy11cb6422012-09-21 00:39:43 -0700509
Romain Guy96885eb2013-03-26 15:05:58 -0700510 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700511 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700512 } else {
513 layer->defer();
514 }
Romain Guy11cb6422012-09-21 00:39:43 -0700515
516 if (inFrame) {
517 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800518 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700519 }
520
Romain Guy5bb3c732012-11-29 17:52:58 -0800521 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700522 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700523
524 return true;
525 }
526
527 return false;
528}
529
530void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700531 // If draw deferring is enabled this method will simply defer
532 // the display list of each individual layer. The layers remain
533 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700534 int count = mLayerUpdates.size();
535 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700536 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
537 startMark("Layer Updates");
538 } else {
539 startMark("Defer Layer Updates");
540 }
Romain Guy11cb6422012-09-21 00:39:43 -0700541
Chris Craik1206b9b2013-04-04 14:46:24 -0700542 // Note: it is very important to update the layers in order
543 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700544 Layer* layer = mLayerUpdates.itemAt(i);
545 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700546 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
547 mCaches.resourceCache.decrementRefcount(layer);
548 }
Romain Guy11cb6422012-09-21 00:39:43 -0700549 }
Romain Guy11cb6422012-09-21 00:39:43 -0700550
Romain Guy96885eb2013-03-26 15:05:58 -0700551 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
552 mLayerUpdates.clear();
553 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
554 }
555 endMark();
556 }
557}
558
559void OpenGLRenderer::flushLayers() {
560 int count = mLayerUpdates.size();
561 if (count > 0) {
562 startMark("Apply Layer Updates");
563 char layerName[12];
564
Chris Craik1206b9b2013-04-04 14:46:24 -0700565 // Note: it is very important to update the layers in order
566 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700567 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700568 startMark(layerName);
569
Romain Guy40543602013-06-12 15:31:28 -0700570 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700571 Layer* layer = mLayerUpdates.itemAt(i);
572 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700573 ATRACE_END();
574
Romain Guy02b49b72013-03-29 12:37:16 -0700575 mCaches.resourceCache.decrementRefcount(layer);
576
Romain Guy96885eb2013-03-26 15:05:58 -0700577 endMark();
578 }
579
580 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700581 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700582
Romain Guy11cb6422012-09-21 00:39:43 -0700583 endMark();
584 }
585}
586
587void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
588 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700589 // Make sure we don't introduce duplicates.
590 // SortedVector would do this automatically but we need to respect
591 // the insertion order. The linear search is not an issue since
592 // this list is usually very short (typically one item, at most a few)
593 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
594 if (mLayerUpdates.itemAt(i) == layer) {
595 return;
596 }
597 }
Romain Guy11cb6422012-09-21 00:39:43 -0700598 mLayerUpdates.push_back(layer);
599 mCaches.resourceCache.incrementRefcount(layer);
600 }
601}
602
Romain Guye93482f2013-06-17 13:14:51 -0700603void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
604 if (layer) {
605 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
606 if (mLayerUpdates.itemAt(i) == layer) {
607 mLayerUpdates.removeAt(i);
608 mCaches.resourceCache.decrementRefcount(layer);
609 break;
610 }
611 }
612 }
613}
614
Romain Guy11cb6422012-09-21 00:39:43 -0700615void OpenGLRenderer::clearLayerUpdates() {
616 size_t count = mLayerUpdates.size();
617 if (count > 0) {
618 mCaches.resourceCache.lock();
619 for (size_t i = 0; i < count; i++) {
620 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
621 }
622 mCaches.resourceCache.unlock();
623 mLayerUpdates.clear();
624 }
625}
626
Romain Guy40543602013-06-12 15:31:28 -0700627void OpenGLRenderer::flushLayerUpdates() {
628 syncState();
629 updateLayers();
630 flushLayers();
631 // Wait for all the layer updates to be executed
632 AutoFence fence;
633}
634
Romain Guy11cb6422012-09-21 00:39:43 -0700635///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700636// State management
637///////////////////////////////////////////////////////////////////////////////
638
Chris Craik14e51302013-12-30 15:32:54 -0800639void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
640 bool restoreOrtho = removed.flags & Snapshot::kFlagDirtyOrtho;
641 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
642 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700643
Romain Guyeb993562010-10-05 18:14:38 -0700644 if (restoreOrtho) {
Chris Craik14e51302013-12-30 15:32:54 -0800645 const Rect& r = restored.viewport;
Romain Guyeb993562010-10-05 18:14:38 -0700646 glViewport(r.left, r.top, r.right, r.bottom);
Chris Craikd6b65f62014-01-01 14:45:21 -0800647 mViewProjMatrix.load(removed.orthoMatrix); // TODO: should ortho be stored in 'restored'?
Romain Guyeb993562010-10-05 18:14:38 -0700648 }
649
Romain Guy2542d192010-08-18 11:47:12 -0700650 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700651 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700652 }
Romain Guy2542d192010-08-18 11:47:12 -0700653
Romain Guy5ec99242010-11-03 16:19:08 -0700654 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700655 endMark(); // Savelayer
656 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800657 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700658 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700659 }
Romain Guybb9524b2010-06-22 18:56:38 -0700660}
661
Romain Guyf6a11b82010-06-23 17:47:49 -0700662///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700663// Layers
664///////////////////////////////////////////////////////////////////////////////
665
666int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700667 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700668 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700669
Chris Craikd6b65f62014-01-01 14:45:21 -0800670 if (!currentSnapshot()->isIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700671 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700672 }
Romain Guyd55a8612010-06-28 17:42:46 -0700673
674 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700675}
676
Chris Craikd90144d2013-03-19 15:03:48 -0700677void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
678 const Rect untransformedBounds(bounds);
679
Chris Craikd6b65f62014-01-01 14:45:21 -0800680 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700681
682 // Layers only make sense if they are in the framebuffer's bounds
Chris Craikd6b65f62014-01-01 14:45:21 -0800683 if (bounds.intersect(*currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700684 // We cannot work with sub-pixels in this case
685 bounds.snapToPixelBoundaries();
686
687 // When the layer is not an FBO, we may use glCopyTexImage so we
688 // need to make sure the layer does not extend outside the bounds
689 // of the framebuffer
Chris Craikd6b65f62014-01-01 14:45:21 -0800690 if (!bounds.intersect(currentSnapshot()->previous->viewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700691 bounds.setEmpty();
692 } else if (fboLayer) {
693 clip.set(bounds);
694 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800695 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700696 inverse.mapRect(clip);
697 clip.snapToPixelBoundaries();
698 if (clip.intersect(untransformedBounds)) {
699 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
700 bounds.set(untransformedBounds);
701 } else {
702 clip.setEmpty();
703 }
704 }
705 } else {
706 bounds.setEmpty();
707 }
708}
709
Chris Craik408eb122013-03-26 18:55:15 -0700710void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
711 bool fboLayer, int alpha) {
712 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
713 bounds.getHeight() > mCaches.maxTextureSize ||
714 (fboLayer && clip.isEmpty())) {
715 mSnapshot->empty = fboLayer;
716 } else {
717 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
718 }
719}
720
Chris Craikd90144d2013-03-19 15:03:48 -0700721int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500722 const SkPaint* paint, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700723 const int count = saveSnapshot(flags);
724
Chris Craikd6b65f62014-01-01 14:45:21 -0800725 if (!currentSnapshot()->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700726 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
727 // operations will be able to store and restore the current clip and transform info, and
728 // quick rejection will be correct (for display lists)
729
730 Rect bounds(left, top, right, bottom);
731 Rect clip;
732 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500733 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700734
Chris Craikd6b65f62014-01-01 14:45:21 -0800735 if (!currentSnapshot()->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700736 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
737 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700738 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700739 }
740 }
741
742 return count;
743}
744
Romain Guy1c740bc2010-09-13 18:00:09 -0700745/**
746 * Layers are viewed by Skia are slightly different than layers in image editing
747 * programs (for instance.) When a layer is created, previously created layers
748 * and the frame buffer still receive every drawing command. For instance, if a
749 * layer is created and a shape intersecting the bounds of the layers and the
750 * framebuffer is draw, the shape will be drawn on both (unless the layer was
751 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
752 *
753 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
754 * texture. Unfortunately, this is inefficient as it requires every primitive to
755 * be drawn n + 1 times, where n is the number of active layers. In practice this
756 * means, for every primitive:
757 * - Switch active frame buffer
758 * - Change viewport, clip and projection matrix
759 * - Issue the drawing
760 *
761 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700762 * To avoid this, layers are implemented in a different way here, at least in the
763 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
764 * is set. When this flag is set we can redirect all drawing operations into a
765 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700766 *
767 * This implementation relies on the frame buffer being at least RGBA 8888. When
768 * a layer is created, only a texture is created, not an FBO. The content of the
769 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700770 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700771 * buffer and drawing continues as normal. This technique therefore treats the
772 * frame buffer as a scratch buffer for the layers.
773 *
774 * To compose the layers back onto the frame buffer, each layer texture
775 * (containing the original frame buffer data) is drawn as a simple quad over
776 * the frame buffer. The trick is that the quad is set as the composition
777 * destination in the blending equation, and the frame buffer becomes the source
778 * of the composition.
779 *
780 * Drawing layers with an alpha value requires an extra step before composition.
781 * An empty quad is drawn over the layer's region in the frame buffer. This quad
782 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
783 * quad is used to multiply the colors in the frame buffer. This is achieved by
784 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
785 * GL_ZERO, GL_SRC_ALPHA.
786 *
787 * Because glCopyTexImage2D() can be slow, an alternative implementation might
788 * be use to draw a single clipped layer. The implementation described above
789 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700790 *
791 * (1) The frame buffer is actually not cleared right away. To allow the GPU
792 * to potentially optimize series of calls to glCopyTexImage2D, the frame
793 * buffer is left untouched until the first drawing operation. Only when
794 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700795 */
Chet Haased48885a2012-08-28 17:43:28 -0700796bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700797 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700798 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700799 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700800
Romain Guyeb993562010-10-05 18:14:38 -0700801 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
802
Romain Guyf607bdc2010-09-10 19:20:06 -0700803 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700804 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700805 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700806 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000807 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700808
809 // Bail out if we won't draw in this snapshot
Chris Craikd6b65f62014-01-01 14:45:21 -0800810 if (currentSnapshot()->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700811 return false;
812 }
Romain Guyf18fd992010-07-08 11:45:51 -0700813
Romain Guya1d3c912011-12-13 14:55:06 -0800814 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700815 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700816 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700817 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700818 }
819
Derek Sollenberger674554f2014-02-19 16:47:32 +0000820 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700821 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700822 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
823 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500824
Chet Haasea23eed82012-04-12 15:19:04 -0700825 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700826 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700827 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700828
Romain Guy8fb95422010-08-17 18:38:51 -0700829 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700830 mSnapshot->flags |= Snapshot::kFlagIsLayer;
831 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700832
Chris Craik7273daa2013-03-28 11:25:24 -0700833 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700834 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700835 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700836 } else {
837 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700838 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800839 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700840 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700841 // Workaround for some GL drivers. When reading pixels lying outside
842 // of the window we should get undefined values for those pixels.
843 // Unfortunately some drivers will turn the entire target texture black
844 // when reading outside of the window.
845 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
846 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700847 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800848 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700849
Romain Guyb254c242013-06-27 17:15:24 -0700850 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
851 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
852
Romain Guy54be1cd2011-06-13 19:04:27 -0700853 // Enqueue the buffer coordinates to clear the corresponding region later
854 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700855 }
Romain Guyeb993562010-10-05 18:14:38 -0700856 }
Romain Guyf86ef572010-07-01 11:05:42 -0700857
Romain Guyd55a8612010-06-28 17:42:46 -0700858 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700859}
860
Chris Craike63f7c622013-10-17 10:30:55 -0700861bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800862 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700864
Chet Haased48885a2012-08-28 17:43:28 -0700865 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800866 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
867 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700868 mSnapshot->fbo = layer->getFbo();
869 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
870 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
871 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
872 mSnapshot->height = bounds.getHeight();
Chris Craikf57776b2013-10-25 18:30:17 -0700873 mSnapshot->orthoMatrix.load(mViewProjMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700874
Romain Guy2b7028e2012-09-19 17:25:38 -0700875 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700876 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700878 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
879 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700880
881 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700882 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700883 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700885 }
886
887 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700889
Chris Craikd6b65f62014-01-01 14:45:21 -0800890 startTilingCurrentClip(true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700891
892 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700893 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800894 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700895 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700896 glClear(GL_COLOR_BUFFER_BIT);
897
898 dirtyClip();
899
900 // Change the ortho projection
901 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craikf57776b2013-10-25 18:30:17 -0700902
903 // TODO: determine best way to support 3d drawing within HW layers
904 mViewProjMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700905
906 return true;
907}
908
Romain Guy1c740bc2010-09-13 18:00:09 -0700909/**
910 * Read the documentation of createLayer() before doing anything in this method.
911 */
Chris Craik14e51302013-12-30 15:32:54 -0800912void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
913 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000914 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700915 return;
916 }
917
Chris Craik14e51302013-12-30 15:32:54 -0800918 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800919 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800920 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700921
Chris Craik39a908c2013-06-13 14:39:01 -0700922 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800923 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
924 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700925 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
926
Romain Guyeb993562010-10-05 18:14:38 -0700927 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700928 endTiling();
929
Romain Guye0aa84b2012-04-03 19:30:26 -0700930 // Detach the texture from the FBO
931 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800932
933 layer->removeFbo(false);
934
Romain Guyeb993562010-10-05 18:14:38 -0700935 // Unbind current FBO and restore previous one
Chris Craik14e51302013-12-30 15:32:54 -0800936 glBindFramebuffer(GL_FRAMEBUFFER, restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700937 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700938
Chris Craikd6b65f62014-01-01 14:45:21 -0800939 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700940 }
941
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500943 SkPaint layerPaint;
944 layerPaint.setAlpha(layer->getAlpha());
945 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
946 layerPaint.setColorFilter(layer->getColorFilter());
947
948 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700949 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700950 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700951 }
952
Romain Guy03750a02010-10-18 14:06:08 -0700953 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700954
Romain Guya1d3c912011-12-13 14:55:06 -0800955 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700956
Romain Guy5b3b3522010-10-27 18:57:51 -0700957 // When the layer is stored in an FBO, we can save a bit of fillrate by
958 // drawing only the dirty region
959 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800960 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700962 } else if (!rect.isEmpty()) {
963 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530964
965 save(0);
966 // the layer contains screen buffer content that shouldn't be alpha modulated
967 // (and any necessary alpha modulation was handled drawing into the layer)
968 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530970 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700971 }
Romain Guy8b55f372010-08-18 17:10:07 -0700972
Romain Guy746b7402010-10-26 16:27:31 -0700973 dirtyClip();
974
Romain Guyeb993562010-10-05 18:14:38 -0700975 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700976 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700977 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700978 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700979 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700980 }
981}
982
Romain Guyaa6c24c2011-04-28 18:40:04 -0700983void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700984 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700985
986 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700988 setupDrawWithTexture();
989 } else {
990 setupDrawWithExternalTexture();
991 }
992 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700993 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500994 setupDrawColorFilter(layer->getColorFilter());
995 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700996 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700997 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500998 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1000 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001001 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001003 }
Chris Craikd6b65f62014-01-01 14:45:21 -08001004 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -08001005 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001006 layer->getWidth() == (uint32_t) rect.getWidth() &&
1007 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001008 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1009 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001010
Romain Guyd21b6e12011-11-30 20:21:23 -08001011 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001012 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1013 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001014 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001015 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001016 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1017 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 }
1019 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001020 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001021
1022 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001023}
1024
Romain Guy5b3b3522010-10-27 18:57:51 -07001025void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001027 const Rect& texCoords = layer->texCoords;
1028 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1029 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001030
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 float x = rect.left;
1032 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001033 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001034 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001035 layer->getHeight() == (uint32_t) rect.getHeight();
1036
1037 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001038 // When we're swapping, the layer is already in screen coordinates
1039 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001040 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1041 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001042 }
1043
Romain Guyd21b6e12011-11-30 20:21:23 -08001044 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001045 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001046 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001047 }
1048
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001049 SkPaint layerPaint;
1050 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1051 layerPaint.setXfermodeMode(layer->getMode());
1052 layerPaint.setColorFilter(layer->getColorFilter());
1053
1054 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001056 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001057 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001059
Romain Guyaa6c24c2011-04-28 18:40:04 -07001060 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1061 } else {
1062 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1063 drawTextureLayer(layer, rect);
1064 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1065 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001066}
1067
Chris Craik34416ea2013-04-15 16:08:28 -07001068/**
1069 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1070 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1071 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1072 * by saveLayer's restore
1073 */
1074#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1075 DRAW_COMMAND; \
1076 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1077 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1078 DRAW_COMMAND; \
1079 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1080 } \
1081 }
1082
1083#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1084
Romain Guy5b3b3522010-10-27 18:57:51 -07001085void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001086 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1087
1088 if (layer->getConvexMask()) {
1089 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1090
1091 // clip to the area of the layer the mask can be larger
1092 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1093
1094 SkPaint paint;
1095 paint.setAntiAlias(true);
1096 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1097
1098 SkiaShader* oldShader = mDrawModifiers.mShader;
1099
1100 // create LayerShader to map SaveLayer content into subsequent draw
1101 SkMatrix shaderMatrix;
1102 shaderMatrix.setTranslate(rect.left, rect.bottom);
1103 shaderMatrix.preScale(1, -1);
1104 SkiaLayerShader layerShader(layer, &shaderMatrix);
1105 mDrawModifiers.mShader = &layerShader;
1106
1107 // Since the drawing primitive is defined in local drawing space,
1108 // we don't need to modify the draw matrix
1109 const SkPath* maskPath = layer->getConvexMask();
1110 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1111
1112 mDrawModifiers.mShader = oldShader;
1113 restore();
1114
1115 return;
1116 }
1117
Romain Guy5b3b3522010-10-27 18:57:51 -07001118 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001119 layer->setRegionAsRect();
1120
Chris Craik34416ea2013-04-15 16:08:28 -07001121 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001122
Romain Guy5b3b3522010-10-27 18:57:51 -07001123 layer->region.clear();
1124 return;
1125 }
1126
Chris Craik3f0854292014-04-15 16:18:08 -07001127 // standard Region based draw
1128 size_t count;
1129 const android::Rect* rects;
1130 Region safeRegion;
1131 if (CC_LIKELY(hasRectToRectTransform())) {
1132 rects = layer->region.getArray(&count);
1133 } else {
1134 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1135 rects = safeRegion.getArray(&count);
1136 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001137
Chris Craik3f0854292014-04-15 16:18:08 -07001138 const float alpha = getLayerAlpha(layer);
1139 const float texX = 1.0f / float(layer->getWidth());
1140 const float texY = 1.0f / float(layer->getHeight());
1141 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001142
Chris Craik3f0854292014-04-15 16:18:08 -07001143 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001144
Chris Craik3f0854292014-04-15 16:18:08 -07001145 // We must get (and therefore bind) the region mesh buffer
1146 // after we setup drawing in case we need to mess with the
1147 // stencil buffer in setupDraw()
1148 TextureVertex* mesh = mCaches.getRegionMesh();
1149 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001150
Chris Craik3f0854292014-04-15 16:18:08 -07001151 setupDrawWithTexture();
1152 setupDrawColor(alpha, alpha, alpha, alpha);
1153 setupDrawColorFilter(layer->getColorFilter());
1154 setupDrawBlending(layer);
1155 setupDrawProgram();
1156 setupDrawDirtyRegionsDisabled();
1157 setupDrawPureColorUniforms();
1158 setupDrawColorFilterUniforms(layer->getColorFilter());
1159 setupDrawTexture(layer->getTexture());
1160 if (currentTransform()->isPureTranslate()) {
1161 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1162 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001163
Chris Craik3f0854292014-04-15 16:18:08 -07001164 layer->setFilter(GL_NEAREST);
1165 setupDrawModelView(kModelViewMode_Translate, false,
1166 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1167 } else {
1168 layer->setFilter(GL_LINEAR);
1169 setupDrawModelView(kModelViewMode_Translate, false,
1170 rect.left, rect.top, rect.right, rect.bottom);
1171 }
1172 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001173
Chris Craik3f0854292014-04-15 16:18:08 -07001174 for (size_t i = 0; i < count; i++) {
1175 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001176
Chris Craik3f0854292014-04-15 16:18:08 -07001177 const float u1 = r->left * texX;
1178 const float v1 = (height - r->top) * texY;
1179 const float u2 = r->right * texX;
1180 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001181
Chris Craik3f0854292014-04-15 16:18:08 -07001182 // TODO: Reject quads outside of the clip
1183 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1184 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1185 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1186 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001187
Chris Craik3f0854292014-04-15 16:18:08 -07001188 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001189
Chris Craik3f0854292014-04-15 16:18:08 -07001190 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001191 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1192 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001193 numQuads = 0;
1194 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001195 }
Chris Craik3f0854292014-04-15 16:18:08 -07001196 }
1197
1198 if (numQuads > 0) {
1199 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1200 GL_UNSIGNED_SHORT, NULL));
1201 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001202
Romain Guy5b3b3522010-10-27 18:57:51 -07001203#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001204 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001205#endif
1206
Chris Craik3f0854292014-04-15 16:18:08 -07001207 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001208}
1209
Romain Guy3a3133d2011-02-01 22:59:58 -08001210#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001211void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001212 size_t count;
1213 const android::Rect* rects = region.getArray(&count);
1214
1215 uint32_t colors[] = {
1216 0x7fff0000, 0x7f00ff00,
1217 0x7f0000ff, 0x7fff00ff,
1218 };
1219
1220 int offset = 0;
1221 int32_t top = rects[0].top;
1222
1223 for (size_t i = 0; i < count; i++) {
1224 if (top != rects[i].top) {
1225 offset ^= 0x2;
1226 top = rects[i].top;
1227 }
1228
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001229 SkPaint paint;
1230 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001231 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001232 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001233 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001234}
Chris Craike63f7c622013-10-17 10:30:55 -07001235#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001236
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001237void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001238 Vector<float> rects;
1239
1240 SkRegion::Iterator it(region);
1241 while (!it.done()) {
1242 const SkIRect& r = it.rect();
1243 rects.push(r.fLeft);
1244 rects.push(r.fTop);
1245 rects.push(r.fRight);
1246 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001247 it.next();
1248 }
1249
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001250 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001251}
1252
Romain Guy5b3b3522010-10-27 18:57:51 -07001253void OpenGLRenderer::dirtyLayer(const float left, const float top,
1254 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001255 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 Rect bounds(left, top, right, bottom);
1257 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001258 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001259 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001260}
1261
1262void OpenGLRenderer::dirtyLayer(const float left, const float top,
1263 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001264 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001265 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001266 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001267 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001268}
1269
1270void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001271 if (bounds.intersect(*currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001272 bounds.snapToPixelBoundaries();
1273 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1274 if (!dirty.isEmpty()) {
1275 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001276 }
1277 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001278}
1279
Chris Craik4063a0e2013-11-15 16:06:56 -08001280void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001281 GLsizei elementsCount = quadsCount * 6;
1282 while (elementsCount > 0) {
1283 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1284
Romain Guy3380cfd2013-08-15 16:57:57 -07001285 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001286 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1287
1288 elementsCount -= drawCount;
1289 // Though there are 4 vertices in a quad, we use 6 indices per
1290 // quad to draw with GL_TRIANGLES
1291 mesh += (drawCount / 6) * 4;
1292 }
1293}
1294
Romain Guy54be1cd2011-06-13 19:04:27 -07001295void OpenGLRenderer::clearLayerRegions() {
1296 const size_t count = mLayers.size();
1297 if (count == 0) return;
1298
Chris Craikd6b65f62014-01-01 14:45:21 -08001299 if (!currentSnapshot()->isIgnored()) {
Romain Guy54be1cd2011-06-13 19:04:27 -07001300 // Doing several glScissor/glClear here can negatively impact
1301 // GPUs with a tiler architecture, instead we draw quads with
1302 // the Clear blending mode
1303
1304 // The list contains bounds that have already been clipped
1305 // against their initial clip rect, and the current clip
1306 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001307 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001308
Romain Guy448455f2013-07-22 13:57:50 -07001309 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001310 Vertex* vertex = mesh;
1311
1312 for (uint32_t i = 0; i < count; i++) {
1313 Rect* bounds = mLayers.itemAt(i);
1314
Romain Guy54be1cd2011-06-13 19:04:27 -07001315 Vertex::set(vertex++, bounds->left, bounds->top);
1316 Vertex::set(vertex++, bounds->right, bounds->top);
1317 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001318 Vertex::set(vertex++, bounds->right, bounds->bottom);
1319
1320 delete bounds;
1321 }
Romain Guye67307c2013-02-11 18:01:20 -08001322 // We must clear the list of dirty rects before we
1323 // call setupDraw() to prevent stencil setup to do
1324 // the same thing again
1325 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001326
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001327 SkPaint clearPaint;
1328 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1329
Romain Guy54be1cd2011-06-13 19:04:27 -07001330 setupDraw(false);
1331 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001332 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001333 setupDrawProgram();
1334 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001335 setupDrawModelView(kModelViewMode_Translate, false,
1336 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001337
Chris Craik4063a0e2013-11-15 16:06:56 -08001338 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001339
1340 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001341 } else {
1342 for (uint32_t i = 0; i < count; i++) {
1343 delete mLayers.itemAt(i);
1344 }
Romain Guye67307c2013-02-11 18:01:20 -08001345 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001346 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001347}
1348
Romain Guybd6b79b2010-06-26 00:13:53 -07001349///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001350// State Deferral
1351///////////////////////////////////////////////////////////////////////////////
1352
Chris Craikff785832013-03-08 13:12:16 -08001353bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001354 const Rect* currentClip = currentClipRect();
1355 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001356
Chris Craikff785832013-03-08 13:12:16 -08001357 if (stateDeferFlags & kStateDeferFlag_Draw) {
1358 // state has bounds initialized in local coordinates
1359 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001360 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001361 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001362 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1363 // is used, it should more closely duplicate the quickReject logic (in how it uses
1364 // snapToPixelBoundaries)
1365
Chris Craikd6b65f62014-01-01 14:45:21 -08001366 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001367 // quick rejected
1368 return true;
1369 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001370
Chris Craika02c4ed2013-06-14 13:43:58 -07001371 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001372 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001373 int& flags = state.mClipSideFlags;
1374 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1376 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1377 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1378 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001379 }
1380 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001381 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001382 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1383 // overdraw avoidance (since we don't know what it overlaps)
1384 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001385 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001386 }
1387 }
1388
Chris Craik527a3aa2013-03-04 10:19:31 -08001389 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1390 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001391 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001392 }
1393
Chris Craik7273daa2013-03-28 11:25:24 -07001394 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1395 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001396 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001397 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001398 state.mAlpha = currentSnapshot()->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001399 return false;
1400}
1401
Chris Craik527a3aa2013-03-04 10:19:31 -08001402void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001403 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001404 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001405 mDrawModifiers = state.mDrawModifiers;
Chris Craikff785832013-03-08 13:12:16 -08001406
Chris Craik527a3aa2013-03-04 10:19:31 -08001407 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001408 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1409 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001410 dirtyClip();
1411 }
Chris Craikc3566d02013-02-04 16:16:33 -08001412}
1413
Chris Craik28ce94a2013-05-31 11:38:03 -07001414/**
1415 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1416 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1417 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1418 *
1419 * This method should be called when restoreDisplayState() won't be restoring the clip
1420 */
1421void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1422 if (clipRect != NULL) {
1423 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1424 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001425 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001426 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001427 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001428 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001429}
1430
Chris Craikc3566d02013-02-04 16:16:33 -08001431///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001432// Clipping
1433///////////////////////////////////////////////////////////////////////////////
1434
Romain Guybb9524b2010-06-22 18:56:38 -07001435void OpenGLRenderer::setScissorFromClip() {
Chris Craikd6b65f62014-01-01 14:45:21 -08001436 Rect clip(*currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001437 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001438
Chris Craikd6b65f62014-01-01 14:45:21 -08001439 if (mCaches.setScissor(clip.left, currentSnapshot()->height - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001440 clip.getWidth(), clip.getHeight())) {
1441 mDirtyClip = false;
1442 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001443}
1444
Romain Guy8ce00302013-01-15 18:51:42 -08001445void OpenGLRenderer::ensureStencilBuffer() {
1446 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1447 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1448 // just hope we have one when hasLayer() returns false.
1449 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001450 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001451 }
1452}
1453
1454void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1455 // The layer's FBO is already bound when we reach this stage
1456 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001457 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1458 // is attached after we initiated tiling. We must turn it off,
1459 // attach the new render buffer then turn tiling back on
1460 endTiling();
1461
Romain Guy8d4aeb72013-02-12 16:08:55 -08001462 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001463 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001464 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001465
Romain Guyf735c8e2013-01-31 17:45:55 -08001466 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001467 }
1468}
1469
1470void OpenGLRenderer::setStencilFromClip() {
1471 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001472 if (!currentSnapshot()->clipRegion->isEmpty()) {
Romain Guy8ce00302013-01-15 18:51:42 -08001473 // NOTE: The order here is important, we must set dirtyClip to false
1474 // before any draw call to avoid calling back into this method
1475 mDirtyClip = false;
1476
1477 ensureStencilBuffer();
1478
1479 mCaches.stencil.enableWrite();
1480
1481 // Clear the stencil but first make sure we restrict drawing
1482 // to the region's bounds
1483 bool resetScissor = mCaches.enableScissor();
1484 if (resetScissor) {
1485 // The scissor was not set so we now need to update it
1486 setScissorFromClip();
1487 }
1488 mCaches.stencil.clear();
1489 if (resetScissor) mCaches.disableScissor();
1490
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001491 SkPaint paint;
1492 paint.setColor(0xff000000);
1493 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1494
Romain Guy8ce00302013-01-15 18:51:42 -08001495 // NOTE: We could use the region contour path to generate a smaller mesh
1496 // Since we are using the stencil we could use the red book path
1497 // drawing technique. It might increase bandwidth usage though.
1498
1499 // The last parameter is important: we are not drawing in the color buffer
1500 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001501 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001502
1503 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001504
1505 // Draw the region used to generate the stencil if the appropriate debug
1506 // mode is enabled
1507 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001508 paint.setColor(0x7f0000ff);
1509 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1510 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001511 }
Romain Guy8ce00302013-01-15 18:51:42 -08001512 } else {
1513 mCaches.stencil.disable();
1514 }
1515 }
1516}
1517
Chris Craikf0a59072013-11-19 18:00:46 -08001518/**
1519 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1520 *
1521 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1522 * style, and tessellated AA ramp
1523 */
1524bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001525 const SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001526 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001527 bool snapOut = paint && paint->isAntiAlias();
1528
1529 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1530 float outset = paint->getStrokeWidth() * 0.5f;
1531 left -= outset;
1532 top -= outset;
1533 right += outset;
1534 bottom += outset;
1535 }
1536
1537 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001538 return true;
1539 }
1540
Chris Craikb4589422013-12-26 15:13:13 -08001541 if (!isRecording()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001542 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001543 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001544 }
Chris Craik39a908c2013-06-13 14:39:01 -07001545 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001546}
1547
Romain Guy8ce00302013-01-15 18:51:42 -08001548void OpenGLRenderer::debugClip() {
1549#if DEBUG_CLIP_REGIONS
Chris Craikd6b65f62014-01-01 14:45:21 -08001550 if (!isRecording() && !currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001551 SkPaint paint;
1552 paint.setColor(0x7f00ff00);
1553 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1554
Romain Guy8ce00302013-01-15 18:51:42 -08001555 }
1556#endif
1557}
1558
Romain Guyf6a11b82010-06-23 17:47:49 -07001559///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001560// Drawing commands
1561///////////////////////////////////////////////////////////////////////////////
1562
Romain Guy54be1cd2011-06-13 19:04:27 -07001563void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001564 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001565 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001566 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001567 // Make sure setScissor & setStencil happen at the beginning of
1568 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001569 if (mDirtyClip) {
1570 if (mCaches.scissorEnabled) {
1571 setScissorFromClip();
1572 }
Romain Guy8ce00302013-01-15 18:51:42 -08001573 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001574 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001575
Romain Guy70ca14e2010-12-13 18:24:33 -08001576 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001577
Romain Guy70ca14e2010-12-13 18:24:33 -08001578 mSetShaderColor = false;
1579 mColorSet = false;
1580 mColorA = mColorR = mColorG = mColorB = 0.0f;
1581 mTextureUnit = 0;
1582 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001583
1584 // Enable debug highlight when what we're about to draw is tested against
1585 // the stencil buffer and if stencil highlight debugging is on
1586 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1587 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1588 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001589
1590 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001591}
1592
1593void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1594 mDescription.hasTexture = true;
1595 mDescription.hasAlpha8Texture = isAlpha8;
1596}
1597
Romain Guyff316ec2013-02-13 18:39:43 -08001598void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1599 mDescription.hasTexture = true;
1600 mDescription.hasColors = true;
1601 mDescription.hasAlpha8Texture = isAlpha8;
1602}
1603
Romain Guyaa6c24c2011-04-28 18:40:04 -07001604void OpenGLRenderer::setupDrawWithExternalTexture() {
1605 mDescription.hasExternalTexture = true;
1606}
1607
Romain Guy15bc6432011-12-13 13:11:32 -08001608void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001609 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001610}
1611
Chris Craik710f46d2012-09-17 17:25:49 -07001612void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001613 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001614}
1615
Romain Guy8d0d4782010-12-14 20:13:35 -08001616void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1617 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001618 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1619 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1620 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001621 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001622 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001623}
1624
Romain Guy86568192010-12-14 15:55:39 -08001625void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1626 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001627 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1628 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1629 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001630 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001631 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001632}
1633
Romain Guy41210632012-07-16 17:04:24 -07001634void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1635 mCaches.fontRenderer->describe(mDescription, paint);
1636}
1637
Romain Guy70ca14e2010-12-13 18:24:33 -08001638void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1639 mColorA = a;
1640 mColorR = r;
1641 mColorG = g;
1642 mColorB = b;
1643 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001644 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001645}
1646
1647void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001648 if (mDrawModifiers.mShader) {
1649 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 }
1651}
1652
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001653void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1654 if (filter == NULL) {
1655 return;
1656 }
1657
1658 SkXfermode::Mode mode;
1659 if (filter->asColorMode(NULL, &mode)) {
1660 mDescription.colorOp = ProgramDescription::kColorBlend;
1661 mDescription.colorMode = mode;
1662 } else if (filter->asColorMatrix(NULL)) {
1663 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001664 }
1665}
1666
Romain Guyf09ef512011-05-27 11:43:46 -07001667void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1668 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1669 mColorA = 1.0f;
1670 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001671 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001672 }
1673}
1674
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001675void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1676 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001677 // When the blending mode is kClear_Mode, we need to use a modulate color
1678 // argb=1,0,0,0
1679 accountForClear(mode);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001680 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
1681 (mColorSet && mColorA < 1.0f) ||
1682 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1683 layer->getColorFilter();
Chris Craikc3566d02013-02-04 16:16:33 -08001684 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001685}
1686
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001687void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1688 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001689 // When the blending mode is kClear_Mode, we need to use a modulate color
1690 // argb=1,0,0,0
1691 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001692 blend |= (mColorSet && mColorA < 1.0f) ||
1693 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001694 (paint && paint->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001695 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001696}
1697
1698void OpenGLRenderer::setupDrawProgram() {
1699 useProgram(mCaches.programCache.get(mDescription));
1700}
1701
1702void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1703 mTrackDirtyRegions = false;
1704}
1705
Chris Craik4063a0e2013-11-15 16:06:56 -08001706void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1707 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001708 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001709 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001710 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001711 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001712
Romain Guy86568192010-12-14 15:55:39 -08001713 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1714 if (!ignoreTransform) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001715 mCaches.currentProgram->set(mViewProjMatrix, mModelView, *currentTransform(), offset);
1716 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001717 } else {
Chris Craikf57776b2013-10-25 18:30:17 -07001718 mCaches.currentProgram->set(mViewProjMatrix, mModelView, mat4::identity(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001719 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001720 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001721}
1722
1723void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001724 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001725 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1726 }
1727}
1728
Romain Guy86568192010-12-14 15:55:39 -08001729void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001730 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001731 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001732 }
1733}
1734
Romain Guy70ca14e2010-12-13 18:24:33 -08001735void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001736 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001737 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001738 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1739 // because it was built into modelView / the geometry, and the SkiaShader needs to
1740 // compensate.
1741 mat4 modelViewWithoutTransform;
Chris Craikd6b65f62014-01-01 14:45:21 -08001742 modelViewWithoutTransform.loadInverse(*currentTransform());
Chris Craik4063a0e2013-11-15 16:06:56 -08001743 modelViewWithoutTransform.multiply(mModelView);
1744 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001745 }
Chris Craikc3566d02013-02-04 16:16:33 -08001746 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1747 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001748 }
1749}
1750
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001751void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1752 if (NULL == filter) {
1753 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001754 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001755
1756 SkColor color;
1757 SkXfermode::Mode mode;
1758 if (filter->asColorMode(&color, &mode)) {
1759 const int alpha = SkColorGetA(color);
1760 const GLfloat a = alpha / 255.0f;
1761 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1762 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1763 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1764 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1765 return;
1766 }
1767
1768 SkScalar srcColorMatrix[20];
1769 if (filter->asColorMatrix(srcColorMatrix)) {
1770
1771 float colorMatrix[16];
1772 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1773 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1774 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1775 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1776
1777 // Skia uses the range [0..255] for the addition vector, but we need
1778 // the [0..1] range to apply the vector in GLSL
1779 float colorVector[4];
1780 colorVector[0] = srcColorMatrix[4] / 255.0f;
1781 colorVector[1] = srcColorMatrix[9] / 255.0f;
1782 colorVector[2] = srcColorMatrix[14] / 255.0f;
1783 colorVector[3] = srcColorMatrix[19] / 255.0f;
1784
1785 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1786 GL_FALSE, colorMatrix);
1787 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1788 return;
1789 }
1790
1791 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001792}
1793
Romain Guy41210632012-07-16 17:04:24 -07001794void OpenGLRenderer::setupDrawTextGammaUniforms() {
1795 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1796}
1797
Romain Guy70ca14e2010-12-13 18:24:33 -08001798void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001799 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001800 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001801 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001802}
1803
1804void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001805 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001806 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001807 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001808}
1809
Romain Guyaa6c24c2011-04-28 18:40:04 -07001810void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1811 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001812 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001813 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001814}
1815
Romain Guy8f0095c2011-05-02 17:24:22 -07001816void OpenGLRenderer::setupDrawTextureTransform() {
1817 mDescription.hasTextureTransform = true;
1818}
1819
1820void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001821 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1822 GL_FALSE, &transform.data[0]);
1823}
1824
Chris Craik564acf72014-01-02 16:46:18 -08001825void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1826 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001827 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001828 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001829 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001830 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001831 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001832 }
Romain Guyd71dd362011-12-12 19:03:35 -08001833
Chris Craikcb4d6002012-09-25 12:00:29 -07001834 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001835 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001836 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001837 }
1838
1839 mCaches.unbindIndicesBuffer();
1840}
1841
Chris Craik564acf72014-01-02 16:46:18 -08001842void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1843 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001844 bool force = mCaches.unbindMeshBuffer();
1845 GLsizei stride = sizeof(ColorTextureVertex);
1846
1847 mCaches.bindPositionVertexPointer(force, vertices, stride);
1848 if (mCaches.currentProgram->texCoords >= 0) {
1849 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1850 }
1851 int slot = mCaches.currentProgram->getAttrib("colors");
1852 if (slot >= 0) {
1853 glEnableVertexAttribArray(slot);
1854 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1855 }
1856
1857 mCaches.unbindIndicesBuffer();
1858}
1859
Chris Craik564acf72014-01-02 16:46:18 -08001860void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1861 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001862 bool force = false;
1863 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1864 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1865 // use the default VBO found in Caches
1866 if (!vertices || vbo) {
1867 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1868 } else {
1869 force = mCaches.unbindMeshBuffer();
1870 }
ztenghui63d41ab2014-02-14 13:13:41 -08001871 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001872
Chris Craikcb4d6002012-09-25 12:00:29 -07001873 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001874 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001875 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001876 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001877}
1878
Romain Guy448455f2013-07-22 13:57:50 -07001879void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001880 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001881 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001882 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001883}
1884
Romain Guy70ca14e2010-12-13 18:24:33 -08001885///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001886// Drawing
1887///////////////////////////////////////////////////////////////////////////////
1888
John Recke18264b2014-03-12 13:56:30 -07001889status_t OpenGLRenderer::drawDisplayList(RenderNode* displayList, Rect& dirty,
Chris Craikff785832013-03-08 13:12:16 -08001890 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001891 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001892 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1893 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001894 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001895 // compute 3d ordering
1896 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001897 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001898 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001899 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craikb265e2c2014-03-27 15:50:09 -07001900 displayList->replayNodeTree(replayStruct);
Chet Haase58d110a2013-04-10 07:43:29 -07001901 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001902 }
1903
Chris Craik28ce94a2013-05-31 11:38:03 -07001904 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
Chris Craikd6b65f62014-01-01 14:45:21 -08001905 DeferredDisplayList deferredList(*currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001906 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craikb265e2c2014-03-27 15:50:09 -07001907 displayList->deferNodeTree(deferStruct);
Romain Guy96885eb2013-03-26 15:05:58 -07001908
1909 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001910 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001911
Chris Craikf57776b2013-10-25 18:30:17 -07001912 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001913 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001914
Romain Guy65549432012-03-26 16:45:05 -07001915 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001916}
1917
Chris Craikd218a922014-01-02 17:13:34 -08001918void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001919 int color = paint != NULL ? paint->getColor() : 0;
1920
Romain Guya168d732011-03-18 16:50:13 -07001921 float x = left;
1922 float y = top;
1923
Romain Guy886b2752013-01-04 12:26:18 -08001924 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1925
Romain Guya168d732011-03-18 16:50:13 -07001926 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001927 if (currentTransform()->isPureTranslate()) {
1928 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1929 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001930 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001931
1932 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001933 } else {
Chris Craik678625242014-02-28 12:26:34 -08001934 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001935 }
1936
Romain Guy3b748a42013-04-17 18:54:38 -07001937 // No need to check for a UV mapper on the texture object, only ARGB_8888
1938 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001939 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001940 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001941 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001942}
1943
Romain Guy03c00b52013-06-20 18:30:28 -07001944/**
1945 * Important note: this method is intended to draw batches of bitmaps and
1946 * will not set the scissor enable or dirty the current layer, if any.
1947 * The caller is responsible for properly dirtying the current layer.
1948 */
Chris Craikd218a922014-01-02 17:13:34 -08001949status_t OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
1950 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1951 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001952 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001953 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08001954 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07001955
Chris Craik527a3aa2013-03-04 10:19:31 -08001956 const AutoTexture autoCleanup(texture);
1957
Chris Craik527a3aa2013-03-04 10:19:31 -08001958 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001959 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001960
1961 const float x = (int) floorf(bounds.left + 0.5f);
1962 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05001963 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001964 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001965 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001966 GL_TRIANGLES, bitmapCount * 6, true,
1967 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001968 } else {
1969 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001970 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001971 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1972 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001973 }
1974
1975 return DrawGlInfo::kStatusDrew;
1976}
1977
Chris Craikd218a922014-01-02 17:13:34 -08001978status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top,
1979 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001980 const float right = left + bitmap->width();
1981 const float bottom = top + bitmap->height();
1982
Chris Craikf0a59072013-11-19 18:00:46 -08001983 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001984 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001985 }
1986
Romain Guya1d3c912011-12-13 14:55:06 -08001987 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001988 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001989 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001990 const AutoTexture autoCleanup(texture);
1991
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05001992 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001993 drawAlphaBitmap(texture, left, top, paint);
1994 } else {
1995 drawTextureRect(left, top, right, bottom, texture, paint);
1996 }
Chet Haase48659092012-05-31 15:21:51 -07001997
1998 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001999}
2000
Chris Craikd218a922014-01-02 17:13:34 -08002001status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix,
2002 const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002003 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2004 const mat4 transform(*matrix);
2005 transform.mapRect(r);
2006
Chris Craikf0a59072013-11-19 18:00:46 -08002007 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002008 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002009 }
2010
Romain Guya1d3c912011-12-13 14:55:06 -08002011 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002012 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002013 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002014 const AutoTexture autoCleanup(texture);
2015
Romain Guy5b3b3522010-10-27 18:57:51 -07002016 // This could be done in a cheaper way, all we need is pass the matrix
2017 // to the vertex shader. The save/restore is a bit overkill.
2018 save(SkCanvas::kMatrix_SaveFlag);
2019 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002020 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002021 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2022 } else {
2023 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2024 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002025 restore();
Chet Haase48659092012-05-31 15:21:51 -07002026
2027 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002028}
2029
Chris Craikd218a922014-01-02 17:13:34 -08002030status_t OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top,
2031 const SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002032 const float right = left + bitmap->width();
2033 const float bottom = top + bitmap->height();
2034
Chris Craikf0a59072013-11-19 18:00:46 -08002035 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002036 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002037 }
2038
2039 mCaches.activeTexture(0);
2040 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2041 const AutoTexture autoCleanup(texture);
2042
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002043 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002044 drawAlphaBitmap(texture, left, top, paint);
2045 } else {
2046 drawTextureRect(left, top, right, bottom, texture, paint);
2047 }
Chet Haase48659092012-05-31 15:21:51 -07002048
2049 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002050}
2051
Chris Craikd218a922014-01-02 17:13:34 -08002052status_t OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
2053 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002054 if (!vertices || currentSnapshot()->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002055 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002056 }
2057
Chris Craik39a908c2013-06-13 14:39:01 -07002058 // TODO: use quickReject on bounds from vertices
2059 mCaches.enableScissor();
2060
Romain Guyb18d2d02011-02-10 15:52:54 -08002061 float left = FLT_MAX;
2062 float top = FLT_MAX;
2063 float right = FLT_MIN;
2064 float bottom = FLT_MIN;
2065
Romain Guya92bb4d2012-10-16 11:08:44 -07002066 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002067
Chris Craik564acf72014-01-02 16:46:18 -08002068 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2069 mesh.setCapacity(count);
2070 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002071
2072 bool cleanupColors = false;
2073 if (!colors) {
2074 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002075 int* newColors = new int[colorsCount];
2076 memset(newColors, 0xff, colorsCount * sizeof(int));
2077 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002078 cleanupColors = true;
2079 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002080
Romain Guy3b748a42013-04-17 18:54:38 -07002081 mCaches.activeTexture(0);
2082 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2083 const UvMapper& mapper(getMapper(texture));
2084
Romain Guy5a7b4662011-01-20 19:09:30 -08002085 for (int32_t y = 0; y < meshHeight; y++) {
2086 for (int32_t x = 0; x < meshWidth; x++) {
2087 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2088
2089 float u1 = float(x) / meshWidth;
2090 float u2 = float(x + 1) / meshWidth;
2091 float v1 = float(y) / meshHeight;
2092 float v2 = float(y + 1) / meshHeight;
2093
Romain Guy3b748a42013-04-17 18:54:38 -07002094 mapper.map(u1, v1, u2, v2);
2095
Romain Guy5a7b4662011-01-20 19:09:30 -08002096 int ax = i + (meshWidth + 1) * 2;
2097 int ay = ax + 1;
2098 int bx = i;
2099 int by = bx + 1;
2100 int cx = i + 2;
2101 int cy = cx + 1;
2102 int dx = i + (meshWidth + 1) * 2 + 2;
2103 int dy = dx + 1;
2104
Romain Guyff316ec2013-02-13 18:39:43 -08002105 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2106 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2107 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002108
Romain Guyff316ec2013-02-13 18:39:43 -08002109 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2110 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2111 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002112
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2114 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2115 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2116 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002117 }
2118 }
2119
Chris Craikf0a59072013-11-19 18:00:46 -08002120 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002121 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002122 return DrawGlInfo::kStatusDone;
2123 }
2124
Romain Guyff316ec2013-02-13 18:39:43 -08002125 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002126 texture = mCaches.textureCache.get(bitmap);
2127 if (!texture) {
2128 if (cleanupColors) delete[] colors;
2129 return DrawGlInfo::kStatusDone;
2130 }
Romain Guyff316ec2013-02-13 18:39:43 -08002131 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002132 const AutoTexture autoCleanup(texture);
2133
2134 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002135 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002136
2137 int alpha;
2138 SkXfermode::Mode mode;
2139 getAlphaAndMode(paint, &alpha, &mode);
2140
Romain Guyff316ec2013-02-13 18:39:43 -08002141 float a = alpha / 255.0f;
2142
Romain Guya92bb4d2012-10-16 11:08:44 -07002143 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002144 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002145 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002146
Romain Guyff316ec2013-02-13 18:39:43 -08002147 setupDraw();
2148 setupDrawWithTextureAndColor();
2149 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002150 setupDrawColorFilter(getColorFilter(paint));
2151 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002152 setupDrawProgram();
2153 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002154 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002155 setupDrawTexture(texture->id);
2156 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002157 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002158 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002159
2160 glDrawArrays(GL_TRIANGLES, 0, count);
2161
Romain Guyff316ec2013-02-13 18:39:43 -08002162 int slot = mCaches.currentProgram->getAttrib("colors");
2163 if (slot >= 0) {
2164 glDisableVertexAttribArray(slot);
2165 }
2166
2167 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002168
2169 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002170}
2171
Chris Craikd218a922014-01-02 17:13:34 -08002172status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002173 float srcLeft, float srcTop, float srcRight, float srcBottom,
2174 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002175 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002176 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002177 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002178 }
2179
Romain Guya1d3c912011-12-13 14:55:06 -08002180 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002181 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002182 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002183 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002184
Romain Guy8ba548f2010-06-30 19:21:21 -07002185 const float width = texture->width;
2186 const float height = texture->height;
2187
Romain Guy3b748a42013-04-17 18:54:38 -07002188 float u1 = fmax(0.0f, srcLeft / width);
2189 float v1 = fmax(0.0f, srcTop / height);
2190 float u2 = fmin(1.0f, srcRight / width);
2191 float v2 = fmin(1.0f, srcBottom / height);
2192
2193 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002194
Romain Guy03750a02010-10-18 14:06:08 -07002195 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002196 resetDrawTextureTexCoords(u1, v1, u2, v2);
2197
Romain Guyd21b6e12011-11-30 20:21:23 -08002198 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2199
Romain Guy886b2752013-01-04 12:26:18 -08002200 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2201 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002202
Romain Guy886b2752013-01-04 12:26:18 -08002203 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2204 // Apply a scale transform on the canvas only when a shader is in use
2205 // Skia handles the ratio between the dst and src rects as a scale factor
2206 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002207 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002208 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002209
Chris Craikd6b65f62014-01-01 14:45:21 -08002210 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2211 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2212 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002213
2214 dstRight = x + (dstRight - dstLeft);
2215 dstBottom = y + (dstBottom - dstTop);
2216
2217 dstLeft = x;
2218 dstTop = y;
2219
Chris Craik678625242014-02-28 12:26:34 -08002220 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002221 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002222 } else {
Chris Craik678625242014-02-28 12:26:34 -08002223 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002224 }
2225
2226 if (CC_UNLIKELY(useScaleTransform)) {
2227 save(SkCanvas::kMatrix_SaveFlag);
2228 translate(dstLeft, dstTop);
2229 scale(scaleX, scaleY);
2230
2231 dstLeft = 0.0f;
2232 dstTop = 0.0f;
2233
2234 dstRight = srcRight - srcLeft;
2235 dstBottom = srcBottom - srcTop;
2236 }
2237
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002238 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002239 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002240 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002241 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002242 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2243 } else {
2244 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002245 texture->id, paint, texture->blend,
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, false, ignoreTransform);
2248 }
2249
2250 if (CC_UNLIKELY(useScaleTransform)) {
2251 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002252 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002253
2254 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002255
2256 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002257}
2258
Chris Craikd218a922014-01-02 17:13:34 -08002259status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
2260 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002261 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002262 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002263 }
2264
Romain Guy4c2547f2013-06-11 16:19:24 -07002265 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002266 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2267 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002268
Romain Guy03c00b52013-06-20 18:30:28 -07002269 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002270}
2271
Chris Craikd218a922014-01-02 17:13:34 -08002272status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
2273 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2274 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002275 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002276 return DrawGlInfo::kStatusDone;
2277 }
2278
Romain Guy211370f2012-02-01 16:10:55 -08002279 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002280 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002281 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002282 if (!texture) return DrawGlInfo::kStatusDone;
2283 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002284
Romain Guya4adcf02013-02-28 12:15:35 -08002285 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2286 texture->setFilter(GL_LINEAR, true);
2287
Chris Craikd6b65f62014-01-01 14:45:21 -08002288 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002289 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002290 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002291 const float offsetX = left + currentTransform()->getTranslateX();
2292 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002293 const size_t count = mesh->quads.size();
2294 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002295 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002296 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002297 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2298 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2299 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002300 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002301 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002302 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002303 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002304 }
2305 }
2306
Chris Craik4063a0e2013-11-15 16:06:56 -08002307 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002308 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002309 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2310 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002311
Romain Guy3b748a42013-04-17 18:54:38 -07002312 right = x + right - left;
2313 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002314 left = x;
2315 top = y;
2316 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002317 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002318 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2319 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002320 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2321 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002322 }
Chet Haase48659092012-05-31 15:21:51 -07002323
2324 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002325}
2326
Romain Guy03c00b52013-06-20 18:30:28 -07002327/**
2328 * Important note: this method is intended to draw batches of 9-patch objects and
2329 * will not set the scissor enable or dirty the current layer, if any.
2330 * The caller is responsible for properly dirtying the current layer.
2331 */
Chris Craikd218a922014-01-02 17:13:34 -08002332status_t OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
2333 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002334 mCaches.activeTexture(0);
2335 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2336 if (!texture) return DrawGlInfo::kStatusDone;
2337 const AutoTexture autoCleanup(texture);
2338
2339 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2340 texture->setFilter(GL_LINEAR, true);
2341
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002342 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2343 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002344 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002345
2346 return DrawGlInfo::kStatusDrew;
2347}
2348
ztenghui63d41ab2014-02-14 13:13:41 -08002349status_t OpenGLRenderer::drawVertexBuffer(VertexBufferMode mode,
2350 const VertexBuffer& vertexBuffer, const SkPaint* paint, bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002351 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002352 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002353 // no vertices to draw
2354 return DrawGlInfo::kStatusDone;
2355 }
2356
Chris Craikcb4d6002012-09-25 12:00:29 -07002357 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002358 bool isAA = paint->isAntiAlias();
2359
Chris Craik710f46d2012-09-17 17:25:49 -07002360 setupDraw();
2361 setupDrawNoTexture();
2362 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002363 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002364 setupDrawColorFilter(getColorFilter(paint));
Chris Craik710f46d2012-09-17 17:25:49 -07002365 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002366 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002367 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002368 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002369 setupDrawColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002370 setupDrawColorFilterUniforms(getColorFilter(paint));
Chris Craik4063a0e2013-11-15 16:06:56 -08002371 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002372
ztenghui55bfb4e2013-12-03 10:38:55 -08002373 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002374 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002375 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002376 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002377
Chet Haase858aa932011-05-12 09:06:00 -07002378
Chris Craik710f46d2012-09-17 17:25:49 -07002379 int alphaSlot = -1;
2380 if (isAA) {
2381 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2382 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002383 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002384 glEnableVertexAttribArray(alphaSlot);
2385 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002386 }
Romain Guy04299382012-07-18 17:15:41 -07002387
ztenghui63d41ab2014-02-14 13:13:41 -08002388 if (mode == kVertexBufferMode_Standard) {
2389 mCaches.unbindIndicesBuffer();
2390 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui50ecf842014-03-11 16:52:30 -07002391 } else if (mode == kVertexBufferMode_OnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002392 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002393 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
2394 } else if (mode == kVertexBufferMode_TwoPolyRingShadow) {
2395 mCaches.bindShadowIndicesBuffer();
2396 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghui63d41ab2014-02-14 13:13:41 -08002397 }
Romain Guy04299382012-07-18 17:15:41 -07002398
Chris Craik710f46d2012-09-17 17:25:49 -07002399 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002400 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002401 }
Chris Craik65cd6122012-12-10 17:56:27 -08002402
2403 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002404}
2405
2406/**
Chris Craik65cd6122012-12-10 17:56:27 -08002407 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2408 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2409 * screen space in all directions. However, instead of using a fragment shader to compute the
2410 * translucency of the color from its position, we simply use a varying parameter to define how far
2411 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2412 *
2413 * Doesn't yet support joins, caps, or path effects.
2414 */
Chris Craikd218a922014-01-02 17:13:34 -08002415status_t OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002416 VertexBuffer vertexBuffer;
2417 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002418 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -08002419
Romain Guyc46d07a2013-03-15 19:06:39 -07002420 if (hasLayer()) {
2421 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002422 PathTessellator::expandBoundsForStroke(bounds, paint);
Chris Craikd6b65f62014-01-01 14:45:21 -08002423 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Romain Guyc46d07a2013-03-15 19:06:39 -07002424 }
Chris Craik65cd6122012-12-10 17:56:27 -08002425
ztenghui63d41ab2014-02-14 13:13:41 -08002426 return drawVertexBuffer(kVertexBufferMode_Standard, vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002427}
2428
2429/**
2430 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2431 * and additional geometry for defining an alpha slope perimeter.
2432 *
2433 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2434 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2435 * in-shader alpha region, but found it to be taxing on some GPUs.
2436 *
2437 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2438 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002439 */
Chris Craikd218a922014-01-02 17:13:34 -08002440status_t OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002441 if (currentSnapshot()->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002442
Chris Craik65cd6122012-12-10 17:56:27 -08002443 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002444
Chris Craik65cd6122012-12-10 17:56:27 -08002445 VertexBuffer buffer;
2446 SkRect bounds;
Chris Craikd6b65f62014-01-01 14:45:21 -08002447 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002448
Chris Craikf0a59072013-11-19 18:00:46 -08002449 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2450 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002451 return DrawGlInfo::kStatusDone;
2452 }
2453
Chris Craikd6b65f62014-01-01 14:45:21 -08002454 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002455
Chris Craik65cd6122012-12-10 17:56:27 -08002456 bool useOffset = !paint->isAntiAlias();
ztenghui63d41ab2014-02-14 13:13:41 -08002457 return drawVertexBuffer(kVertexBufferMode_Standard, buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002458}
2459
Chris Craikd218a922014-01-02 17:13:34 -08002460status_t OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002461 if (currentSnapshot()->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002462
Chris Craik6d29c8d2013-05-08 18:35:44 -07002463 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002464
Chris Craik6d29c8d2013-05-08 18:35:44 -07002465 VertexBuffer buffer;
2466 SkRect bounds;
Chris Craikd6b65f62014-01-01 14:45:21 -08002467 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002468
Chris Craikf0a59072013-11-19 18:00:46 -08002469 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2470 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002471 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002472 }
2473
Chris Craikd6b65f62014-01-01 14:45:21 -08002474 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002475
Chris Craik6d29c8d2013-05-08 18:35:44 -07002476 bool useOffset = !paint->isAntiAlias();
ztenghui63d41ab2014-02-14 13:13:41 -08002477 return drawVertexBuffer(kVertexBufferMode_Standard, buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002478}
2479
Chet Haase48659092012-05-31 15:21:51 -07002480status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002481 // No need to check against the clip, we fill the clip region
Chris Craikd6b65f62014-01-01 14:45:21 -08002482 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002483
Chris Craikd6b65f62014-01-01 14:45:21 -08002484 Rect clip(*currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002485 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002486
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002487 SkPaint paint;
2488 paint.setColor(color);
2489 paint.setXfermodeMode(mode);
2490
2491 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002492
2493 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002494}
Romain Guy9d5316e2010-06-24 19:30:36 -07002495
Chet Haase48659092012-05-31 15:21:51 -07002496status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002497 const SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002498 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002499 const AutoTexture autoCleanup(texture);
2500
2501 const float x = left + texture->left - texture->offset;
2502 const float y = top + texture->top - texture->offset;
2503
2504 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002505
2506 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002507}
2508
Chet Haase48659092012-05-31 15:21:51 -07002509status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002510 float rx, float ry, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002511 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002512 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002513 return DrawGlInfo::kStatusDone;
2514 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002515
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002517 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002518 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002519 right - left, bottom - top, rx, ry, p);
2520 return drawShape(left, top, texture, p);
2521 }
2522
2523 SkPath path;
2524 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002525 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2526 float outset = p->getStrokeWidth() / 2;
2527 rect.outset(outset, outset);
2528 rx += outset;
2529 ry += outset;
2530 }
Chris Craik710f46d2012-09-17 17:25:49 -07002531 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002532 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002533}
2534
Chris Craikd218a922014-01-02 17:13:34 -08002535status_t OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002536 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002537 x + radius, y + radius, p) ||
2538 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002539 return DrawGlInfo::kStatusDone;
2540 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002542 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002543 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002544 return drawShape(x - radius, y - radius, texture, p);
2545 }
2546
2547 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002548 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2549 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2550 } else {
2551 path.addCircle(x, y, radius);
2552 }
Chris Craik65cd6122012-12-10 17:56:27 -08002553 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002554}
Romain Guy01d58e42011-01-19 21:54:02 -08002555
Chet Haase48659092012-05-31 15:21:51 -07002556status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002557 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002558 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002559 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002560 return DrawGlInfo::kStatusDone;
2561 }
Romain Guy01d58e42011-01-19 21:54:02 -08002562
Chris Craikcb4d6002012-09-25 12:00:29 -07002563 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002564 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002565 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002566 return drawShape(left, top, texture, p);
2567 }
2568
2569 SkPath path;
2570 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002571 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2572 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2573 }
Chris Craik710f46d2012-09-17 17:25:49 -07002574 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002575 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002576}
2577
Chet Haase48659092012-05-31 15:21:51 -07002578status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002579 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002580 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002581 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002582 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002583 }
2584
Chris Craik780c1282012-10-04 14:10:49 -07002585 if (fabs(sweepAngle) >= 360.0f) {
2586 return drawOval(left, top, right, bottom, p);
2587 }
2588
2589 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002590 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002591 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002592 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002593 startAngle, sweepAngle, useCenter, p);
2594 return drawShape(left, top, texture, p);
2595 }
2596
2597 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2598 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2599 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2600 }
2601
2602 SkPath path;
2603 if (useCenter) {
2604 path.moveTo(rect.centerX(), rect.centerY());
2605 }
2606 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2607 if (useCenter) {
2608 path.close();
2609 }
Chris Craik65cd6122012-12-10 17:56:27 -08002610 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002611}
2612
Romain Guycf8675e2012-10-02 12:32:25 -07002613// See SkPaintDefaults.h
2614#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2615
Chris Craikd218a922014-01-02 17:13:34 -08002616status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
2617 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002618 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002619 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002620 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002621 }
2622
Chris Craik710f46d2012-09-17 17:25:49 -07002623 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002624 // only fill style is supported by drawConvexPath, since others have to handle joins
2625 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2626 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2627 mCaches.activeTexture(0);
2628 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002629 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002630 return drawShape(left, top, texture, p);
2631 }
2632
2633 SkPath path;
2634 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2635 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2636 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2637 }
2638 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002639 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002640 }
2641
Chris Craikd6b65f62014-01-01 14:45:21 -08002642 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002643 SkPath path;
2644 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002645 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002646 } else {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002647 drawColorRect(left, top, right, bottom, p);
Chris Craik65cd6122012-12-10 17:56:27 -08002648 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002649 }
Romain Guyc7d53492010-06-25 13:41:57 -07002650}
Romain Guy9d5316e2010-06-24 19:30:36 -07002651
Chris Craikd218a922014-01-02 17:13:34 -08002652void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2653 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002654 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002655 mCaches.activeTexture(0);
2656
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002657 TextShadow textShadow;
2658 if (!getTextShadow(paint, &textShadow)) {
2659 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2660 }
2661
Raph Levien416a8472012-07-19 22:48:17 -07002662 // NOTE: The drop shadow will not perform gamma correction
2663 // if shader-based correction is enabled
2664 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2665 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002666 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002667 // If the drop shadow exceeds the max texture size or couldn't be
2668 // allocated, skip drawing
2669 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002670 const AutoTexture autoCleanup(shadow);
2671
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002672 const float sx = x - shadow->left + textShadow.dx;
2673 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002674
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002675 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08002676 if (mDrawModifiers.mShader) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002677 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002678 }
2679
2680 setupDraw();
2681 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002682 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002683 setupDrawColorFilter(getColorFilter(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002684 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002685 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002686 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002687 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2688 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002689 setupDrawTexture(shadow->id);
2690 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002691 setupDrawColorFilterUniforms(getColorFilter(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002692 setupDrawShaderUniforms();
2693 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2694
2695 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2696}
2697
Romain Guy768bffc2013-02-27 13:50:45 -08002698bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002699 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002700 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2701}
2702
Chet Haase48659092012-05-31 15:21:51 -07002703status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002704 const float* positions, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002705 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002706 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002707 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002708
Romain Guy671d6cf2012-01-18 12:39:17 -08002709 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002710 if (!currentTransform()->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002711 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002712 }
2713
Chris Craik39a908c2013-06-13 14:39:01 -07002714 mCaches.enableScissor();
2715
Romain Guy671d6cf2012-01-18 12:39:17 -08002716 float x = 0.0f;
2717 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002718 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002720 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2721 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002722 }
2723
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002724 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002725 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002726
2727 int alpha;
2728 SkXfermode::Mode mode;
2729 getAlphaAndMode(paint, &alpha, &mode);
2730
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002731 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002732 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002733 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002734 }
2735
Romain Guy671d6cf2012-01-18 12:39:17 -08002736 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002737 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002738 if (pureTranslate && !linearFilter) {
2739 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2740 }
Romain Guy257ae352013-03-20 16:31:12 -07002741 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002742
2743 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2744 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2745
Romain Guy211370f2012-02-01 16:10:55 -08002746 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002747
Victoria Lease1e546812013-06-25 14:25:17 -07002748 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002749 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002750 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 if (hasActiveLayer) {
2752 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002753 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 }
2755 dirtyLayerUnchecked(bounds, getRegion());
2756 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002757 }
Chet Haase48659092012-05-31 15:21:51 -07002758
2759 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002760}
2761
Romain Guy624234f2013-03-05 16:43:31 -08002762mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2763 mat4 fontTransform;
2764 if (CC_LIKELY(transform.isPureTranslate())) {
2765 fontTransform = mat4::identity();
2766 } else {
2767 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002768 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002769 } else {
2770 float sx, sy;
Chris Craikd6b65f62014-01-01 14:45:21 -08002771 currentTransform()->decomposeScale(sx, sy);
Romain Guy624234f2013-03-05 16:43:31 -08002772 fontTransform.loadScale(sx, sy, 1.0f);
2773 }
2774 }
2775 return fontTransform;
2776}
2777
Chris Craik41541822013-05-03 16:35:54 -07002778status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002779 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002780 DrawOpMode drawOpMode) {
2781
Chris Craik527a3aa2013-03-04 10:19:31 -08002782 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002783 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2784 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craikd6b65f62014-01-01 14:45:21 -08002785 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002786 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002787 return DrawGlInfo::kStatusDone;
2788 }
Romain Guycac5fd32011-12-01 20:08:50 -08002789 }
2790
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002791 const float oldX = x;
2792 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002793
Chris Craikd6b65f62014-01-01 14:45:21 -08002794 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002795 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002796
Romain Guy211370f2012-02-01 16:10:55 -08002797 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002798 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2799 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002800 }
2801
Romain Guy86568192010-12-14 15:55:39 -08002802 int alpha;
2803 SkXfermode::Mode mode;
2804 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002805
Romain Guyc74f45a2013-02-26 19:10:14 -08002806 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2807
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002808 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002809 fontRenderer.setFont(paint, mat4::identity());
2810 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002811 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002812 }
2813
Romain Guy19d4dd82013-03-04 11:14:26 -08002814 const bool hasActiveLayer = hasLayer();
2815
Romain Guy624234f2013-03-05 16:43:31 -08002816 // We only pass a partial transform to the font renderer. That partial
2817 // matrix defines how glyphs are rasterized. Typically we want glyphs
2818 // to be rasterized at their final size on screen, which means the partial
2819 // matrix needs to take the scale factor into account.
2820 // When a partial matrix is used to transform glyphs during rasterization,
2821 // the mesh is generated with the inverse transform (in the case of scale,
2822 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2823 // apply the full transform matrix at draw time in the vertex shader.
2824 // Applying the full matrix in the shader is the easiest way to handle
2825 // rotation and perspective and allows us to always generated quads in the
2826 // font renderer which greatly simplifies the code, clipping in particular.
2827 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002828 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002829
Romain Guy6620c6d2010-12-06 18:07:02 -08002830 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002831 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002832 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002833
Romain Guy624234f2013-03-05 16:43:31 -08002834 // TODO: Implement better clipping for scaled/rotated text
Chris Craikd6b65f62014-01-01 14:45:21 -08002835 const Rect* clip = !pureTranslate ? NULL : currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002836 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 -07002837
Raph Levien996e57c2012-07-23 15:22:52 -07002838 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002839 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002840
2841 // don't call issuedrawcommand, do it at end of batch
2842 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002843 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002844 SkPaint paintCopy(*paint);
2845 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2846 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002847 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002848 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002849 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002850 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002851 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002852
Chris Craik527a3aa2013-03-04 10:19:31 -08002853 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002854 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002855 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002856 }
Chris Craik41541822013-05-03 16:35:54 -07002857 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002858 }
Romain Guy694b5192010-07-21 21:33:20 -07002859
Chris Craike63f7c622013-10-17 10:30:55 -07002860 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002861
2862 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002863}
2864
Chris Craikd218a922014-01-02 17:13:34 -08002865status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
2866 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002867 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002868 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002869 }
2870
Chris Craik39a908c2013-06-13 14:39:01 -07002871 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2872 mCaches.enableScissor();
2873
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002874 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002875 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002876 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002877
2878 int alpha;
2879 SkXfermode::Mode mode;
2880 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002881 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002882
Romain Guy97771732012-02-28 18:17:02 -08002883 const Rect* clip = &mSnapshot->getLocalClip();
2884 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 -08002885
Romain Guy97771732012-02-28 18:17:02 -08002886 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002887
2888 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002889 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002890 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002891 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002892 dirtyLayerUnchecked(bounds, getRegion());
2893 }
Romain Guy97771732012-02-28 18:17:02 -08002894 }
Chet Haase48659092012-05-31 15:21:51 -07002895
2896 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002897}
2898
Chris Craikd218a922014-01-02 17:13:34 -08002899status_t OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002900 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002901
Romain Guya1d3c912011-12-13 14:55:06 -08002902 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002903
Romain Guyfb8b7632010-08-23 21:05:08 -07002904 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002905 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002906 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002907
Romain Guy8b55f372010-08-18 17:10:07 -07002908 const float x = texture->left - texture->offset;
2909 const float y = texture->top - texture->offset;
2910
Romain Guy01d58e42011-01-19 21:54:02 -08002911 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002912
2913 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002914}
2915
Chris Craika08f95c2013-03-15 17:24:33 -07002916status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002917 if (!layer) {
2918 return DrawGlInfo::kStatusDone;
2919 }
2920
Romain Guyb2e2f242012-10-17 18:18:35 -07002921 mat4* transform = NULL;
2922 if (layer->isTextureLayer()) {
2923 transform = &layer->getTransform();
2924 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002925 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002926 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002927 }
2928 }
2929
Chris Craik39a908c2013-06-13 14:39:01 -07002930 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002931 const bool rejected = calculateQuickRejectForScissor(x, y,
2932 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002933
2934 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002935 if (transform && !transform->isIdentity()) {
2936 restore();
2937 }
Chet Haase48659092012-05-31 15:21:51 -07002938 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002939 }
2940
Romain Guy5bb3c732012-11-29 17:52:58 -08002941 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002942
Chris Craik39a908c2013-06-13 14:39:01 -07002943 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002944 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002945
Romain Guy211370f2012-02-01 16:10:55 -08002946 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002947 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002948 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2949 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002950 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002951
Chris Craik16ecda52013-03-29 10:59:59 -07002952 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002953 setupDraw();
2954 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002955 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002956 setupDrawColorFilter(layer->getColorFilter());
2957 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002958 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002959 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002960 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002961 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002962 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2963 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2964 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002965
Romain Guyd21b6e12011-11-30 20:21:23 -08002966 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002967 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002968 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002969 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002970 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08002971 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07002972 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2973 }
Romain Guyf219da52011-01-16 12:54:25 -08002974
Romain Guy448455f2013-07-22 13:57:50 -07002975 TextureVertex* mesh = &layer->mesh[0];
2976 GLsizei elementsCount = layer->meshElementCount;
2977
2978 while (elementsCount > 0) {
2979 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
2980
Romain Guy3380cfd2013-08-15 16:57:57 -07002981 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07002982 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2983 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
2984
2985 elementsCount -= drawCount;
2986 // Though there are 4 vertices in a quad, we use 6 indices per
2987 // quad to draw with GL_TRIANGLES
2988 mesh += (drawCount / 6) * 4;
2989 }
Romain Guyf219da52011-01-16 12:54:25 -08002990
Romain Guy3a3133d2011-02-01 22:59:58 -08002991#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002992 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002993#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002994 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002995
Romain Guy5bb3c732012-11-29 17:52:58 -08002996 if (layer->debugDrawUpdate) {
2997 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002998
2999 SkPaint paint;
3000 paint.setColor(0x7f00ff00);
3001 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003002 }
Romain Guyf219da52011-01-16 12:54:25 -08003003 }
Chris Craik34416ea2013-04-15 16:08:28 -07003004 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003005
Romain Guyb2e2f242012-10-17 18:18:35 -07003006 if (transform && !transform->isIdentity()) {
3007 restore();
3008 }
3009
Chet Haase48659092012-05-31 15:21:51 -07003010 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003011}
3012
Romain Guy6926c722010-07-12 20:20:03 -07003013///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003014// Shaders
3015///////////////////////////////////////////////////////////////////////////////
3016
3017void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003018 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003019}
3020
Romain Guy06f96e22010-07-30 19:18:16 -07003021void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003022 mDrawModifiers.mShader = shader;
3023 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003024 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003025 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003026}
3027
Romain Guyd27977d2010-07-14 19:18:51 -07003028///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003029// Draw filters
3030///////////////////////////////////////////////////////////////////////////////
3031
3032void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003033 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3034 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003035 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003036 mDrawModifiers.mPaintFilterClearBits = 0;
3037 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003038}
3039
3040void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003041 mDrawModifiers.mHasDrawFilter = true;
3042 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3043 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003044}
3045
Chris Craikd218a922014-01-02 17:13:34 -08003046const SkPaint* OpenGLRenderer::filterPaint(const SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003047 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003048 return paint;
3049 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003050
3051 uint32_t flags = paint->getFlags();
3052
3053 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003054 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3055 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003056
3057 return &mFilteredPaint;
3058}
3059
3060///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003061// Drawing implementation
3062///////////////////////////////////////////////////////////////////////////////
3063
Chris Craikd218a922014-01-02 17:13:34 -08003064Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003065 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3066 if (!texture) {
3067 return mCaches.textureCache.get(bitmap);
3068 }
3069 return texture;
3070}
3071
Romain Guy01d58e42011-01-19 21:54:02 -08003072void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003073 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003074 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003075 return;
3076 }
3077
3078 int alpha;
3079 SkXfermode::Mode mode;
3080 getAlphaAndMode(paint, &alpha, &mode);
3081
3082 setupDraw();
3083 setupDrawWithTexture(true);
3084 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003085 setupDrawColorFilter(getColorFilter(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003086 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003087 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003088 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003089 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3090 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003091 setupDrawTexture(texture->id);
3092 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003093 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003094 setupDrawShaderUniforms();
3095 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3096
3097 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003098}
3099
Romain Guyf607bdc2010-09-10 19:20:06 -07003100// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003101#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3102#define kStdUnderline_Offset (1.0f / 9.0f)
3103#define kStdUnderline_Thickness (1.0f / 18.0f)
3104
Chris Craikd218a922014-01-02 17:13:34 -08003105void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3106 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003107 // Handle underline and strike-through
3108 uint32_t flags = paint->getFlags();
3109 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003110 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003111
Romain Guy211370f2012-02-01 16:10:55 -08003112 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003113 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003114 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003115
Raph Levien8b4072d2012-07-30 15:50:00 -07003116 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003117 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003118
Romain Guyf6834472011-01-23 13:32:12 -08003119 int linesCount = 0;
3120 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3121 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3122
3123 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003124 float points[pointsCount];
3125 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003126
3127 if (flags & SkPaint::kUnderlineText_Flag) {
3128 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003129 points[currentPoint++] = left;
3130 points[currentPoint++] = top;
3131 points[currentPoint++] = left + underlineWidth;
3132 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003133 }
3134
3135 if (flags & SkPaint::kStrikeThruText_Flag) {
3136 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003137 points[currentPoint++] = left;
3138 points[currentPoint++] = top;
3139 points[currentPoint++] = left + underlineWidth;
3140 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003141 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003142
Romain Guy726aeba2011-06-01 14:52:00 -07003143 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003144
Romain Guy726aeba2011-06-01 14:52:00 -07003145 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003146 }
3147 }
3148}
3149
Chris Craikd218a922014-01-02 17:13:34 -08003150status_t OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003151 if (currentSnapshot()->isIgnored()) {
Romain Guy672433d2013-01-04 19:05:13 -08003152 return DrawGlInfo::kStatusDone;
3153 }
3154
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003155 return drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003156}
3157
Chris Craikb79a3e32014-03-11 12:20:17 -07003158static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3159 // map z coordinate with true 3d matrix
3160 point.z = transformZ.mapZ(point);
3161
3162 // map x,y coordinates with draw/Skia matrix
3163 transformXY.mapPoint(point.x, point.y);
3164}
3165
3166status_t OpenGLRenderer::drawShadow(const mat4& casterTransformXY, const mat4& casterTransformZ,
Chris Craik024433f2014-03-26 13:19:14 -07003167 float casterAlpha, bool casterUnclipped, const SkPath* casterPerimeter) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003168 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craikf57776b2013-10-25 18:30:17 -07003169
Chris Craik15a07a22014-01-26 13:43:53 -08003170 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003171 mCaches.enableScissor();
3172
3173 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003174 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003175
Chris Craik15a07a22014-01-26 13:43:53 -08003176 // tessellate caster outline into a 2d polygon
3177 Vector<Vertex> casterVertices2d;
3178 const float casterRefinementThresholdSquared = 20.0f; // TODO: experiment with this value
Chris Craik8c271ca2014-03-25 10:33:01 -07003179 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
Chris Craik15a07a22014-01-26 13:43:53 -08003180 casterRefinementThresholdSquared, casterVertices2d);
ztenghui2e023f32014-04-28 16:43:13 -07003181 if (!ShadowTessellator::isClockwisePath(*casterPerimeter)) {
3182 ShadowTessellator::reverseVertexArray(casterVertices2d.editArray(),
3183 casterVertices2d.size());
3184 }
Chris Craik15a07a22014-01-26 13:43:53 -08003185
Chris Craika2fe7af2014-01-28 17:25:06 -08003186 if (casterVertices2d.size() == 0) {
3187 // empty caster polygon computed from path
3188 return DrawGlInfo::kStatusDone;
3189 }
3190
Chris Craik15a07a22014-01-26 13:43:53 -08003191 // map 2d caster poly into 3d
3192 const int casterVertexCount = casterVertices2d.size();
3193 Vector3 casterPolygon[casterVertexCount];
Chris Craikb98f2112014-03-11 17:42:29 -07003194 float minZ = FLT_MAX;
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003195 float maxZ = -FLT_MAX;
Chris Craik15a07a22014-01-26 13:43:53 -08003196 for (int i = 0; i < casterVertexCount; i++) {
3197 const Vertex& point2d = casterVertices2d[i];
3198 casterPolygon[i] = Vector3(point2d.x, point2d.y, 0);
Chris Craikb79a3e32014-03-11 12:20:17 -07003199 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
Chris Craikb98f2112014-03-11 17:42:29 -07003200 minZ = fmin(minZ, casterPolygon[i].z);
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003201 maxZ = fmax(maxZ, casterPolygon[i].z);
Chris Craik15a07a22014-01-26 13:43:53 -08003202 }
3203
ztenghui63d41ab2014-02-14 13:13:41 -08003204 // map the centroid of the caster into 3d
3205 Vector2 centroid = ShadowTessellator::centroid2d(
3206 reinterpret_cast<const Vector2*>(casterVertices2d.array()),
3207 casterVertexCount);
3208 Vector3 centroid3d(centroid.x, centroid.y, 0);
Chris Craikb79a3e32014-03-11 12:20:17 -07003209 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
ztenghui63d41ab2014-02-14 13:13:41 -08003210
Chris Craikb98f2112014-03-11 17:42:29 -07003211 // if the caster intersects the z=0 plane, lift it in Z so it doesn't
3212 if (minZ < SHADOW_MIN_CASTER_Z) {
3213 float casterLift = SHADOW_MIN_CASTER_Z - minZ;
3214 for (int i = 0; i < casterVertexCount; i++) {
3215 casterPolygon[i].z += casterLift;
3216 }
3217 centroid3d.z += casterLift;
3218 }
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003219
3220 // Check whether we want to draw the shadow at all by checking the caster's
3221 // bounds against clip.
3222 // We only have ortho projection, so we can just ignore the Z in caster for
3223 // simple rejection calculation.
3224 Rect localClip = mSnapshot->getLocalClip();
Chris Craik8c271ca2014-03-25 10:33:01 -07003225 Rect casterBounds(casterPerimeter->getBounds());
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003226 casterTransformXY.mapRect(casterBounds);
3227
Chris Craik024433f2014-03-26 13:19:14 -07003228 bool isCasterOpaque = (casterAlpha == 1.0f) && casterUnclipped;
Chris Craik15a07a22014-01-26 13:43:53 -08003229 // draw caster's shadows
ztenghuief94c6f2014-02-13 17:09:45 -08003230 if (mCaches.propertyAmbientShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003231 paint.setARGB(casterAlpha * mCaches.propertyAmbientShadowStrength, 0, 0, 0);
ztenghuief94c6f2014-02-13 17:09:45 -08003232 VertexBuffer ambientShadowVertexBuffer;
ztenghui50ecf842014-03-11 16:52:30 -07003233 VertexBufferMode vertexBufferMode = ShadowTessellator::tessellateAmbientShadow(
3234 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003235 casterBounds, localClip, maxZ, ambientShadowVertexBuffer);
ztenghui50ecf842014-03-11 16:52:30 -07003236 drawVertexBuffer(vertexBufferMode, ambientShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003237 }
ztenghui7b4516e2014-01-07 10:42:55 -08003238
ztenghuief94c6f2014-02-13 17:09:45 -08003239 if (mCaches.propertySpotShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003240 paint.setARGB(casterAlpha * mCaches.propertySpotShadowStrength, 0, 0, 0);
ztenghuief94c6f2014-02-13 17:09:45 -08003241 VertexBuffer spotShadowVertexBuffer;
ztenghui50ecf842014-03-11 16:52:30 -07003242 VertexBufferMode vertexBufferMode = ShadowTessellator::tessellateSpotShadow(
Chris Craikf5be3ca2014-04-30 18:20:03 -07003243 isCasterOpaque, casterPolygon, casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003244 *currentTransform(), getWidth(), getHeight(), casterBounds, localClip,
3245 spotShadowVertexBuffer);
ztenghui50ecf842014-03-11 16:52:30 -07003246 drawVertexBuffer(vertexBufferMode, spotShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003247 }
ztenghui7b4516e2014-01-07 10:42:55 -08003248
3249 return DrawGlInfo::kStatusDrew;
Chris Craikf57776b2013-10-25 18:30:17 -07003250}
3251
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003252status_t OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
3253 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003254 if (count == 0) {
3255 return DrawGlInfo::kStatusDone;
3256 }
Romain Guy735738c2012-12-03 12:34:51 -08003257
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003258 int color = paint->getColor();
3259 // If a shader is set, preserve only the alpha
3260 if (mDrawModifiers.mShader) {
3261 color |= 0x00ffffff;
3262 }
3263
Romain Guy672433d2013-01-04 19:05:13 -08003264 float left = FLT_MAX;
3265 float top = FLT_MAX;
3266 float right = FLT_MIN;
3267 float bottom = FLT_MIN;
3268
Romain Guy448455f2013-07-22 13:57:50 -07003269 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003270 Vertex* vertex = mesh;
3271
Chris Craik2af46352012-11-26 18:30:17 -08003272 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003273 float l = rects[index + 0];
3274 float t = rects[index + 1];
3275 float r = rects[index + 2];
3276 float b = rects[index + 3];
3277
Romain Guy3b753822013-03-05 10:27:35 -08003278 Vertex::set(vertex++, l, t);
3279 Vertex::set(vertex++, r, t);
3280 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003281 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003282
Romain Guy3b753822013-03-05 10:27:35 -08003283 left = fminf(left, l);
3284 top = fminf(top, t);
3285 right = fmaxf(right, r);
3286 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003287 }
3288
Chris Craikf0a59072013-11-19 18:00:46 -08003289 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003290 return DrawGlInfo::kStatusDone;
3291 }
Romain Guy672433d2013-01-04 19:05:13 -08003292
Romain Guy672433d2013-01-04 19:05:13 -08003293 setupDraw();
3294 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003295 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Romain Guy672433d2013-01-04 19:05:13 -08003296 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003297 setupDrawColorFilter(getColorFilter(paint));
3298 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003299 setupDrawProgram();
3300 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003301 setupDrawModelView(kModelViewMode_Translate, false,
3302 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003303 setupDrawColorUniforms();
3304 setupDrawShaderUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003305 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003306
Romain Guy8ce00302013-01-15 18:51:42 -08003307 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003308 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003309 }
3310
Chris Craik4063a0e2013-11-15 16:06:56 -08003311 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003312
3313 return DrawGlInfo::kStatusDrew;
3314}
3315
Romain Guy026c5e162010-06-28 17:12:22 -07003316void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003317 const SkPaint* paint, bool ignoreTransform) {
3318 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003319 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003320 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003321 color |= 0x00ffffff;
3322 }
3323
Romain Guy70ca14e2010-12-13 18:24:33 -08003324 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003325 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003326 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003327 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003328 setupDrawColorFilter(getColorFilter(paint));
3329 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003330 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003331 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3332 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003333 setupDrawColorUniforms();
3334 setupDrawShaderUniforms(ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003335 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003336 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003337
Romain Guyc95c8d62010-09-17 15:31:32 -07003338 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3339}
3340
Romain Guy82ba8142010-07-09 13:25:56 -07003341void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003342 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003343 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003344
Romain Guy3b748a42013-04-17 18:54:38 -07003345 GLvoid* vertices = (GLvoid*) NULL;
3346 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3347
3348 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003349 vertices = &mMeshVertices[0].x;
3350 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003351
3352 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3353 texture->uvMapper->map(uvs);
3354
3355 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3356 }
3357
Chris Craikd6b65f62014-01-01 14:45:21 -08003358 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3359 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3360 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003361
Romain Guyd21b6e12011-11-30 20:21:23 -08003362 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003363 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003364 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003365 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003366 } else {
Chris Craik678625242014-02-28 12:26:34 -08003367 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003368 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003369 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3370 }
3371
3372 if (texture->uvMapper) {
3373 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003374 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003375}
3376
Romain Guyf7f93552010-07-08 19:17:03 -07003377void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003378 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003379 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003380 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3381 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003382
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003383 int a;
3384 SkXfermode::Mode mode;
3385 getAlphaAndMode(paint, &a, &mode);
3386 const float alpha = a / 255.0f;
3387
Romain Guy746b7402010-10-26 16:27:31 -07003388 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003389 setupDrawWithTexture();
3390 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 setupDrawColorFilter(getColorFilter(paint));
3392 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003393 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003394 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003395 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003396 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003397 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003398 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003399 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003400
Romain Guy6820ac82010-09-15 18:11:50 -07003401 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003402}
3403
Romain Guy3b748a42013-04-17 18:54:38 -07003404void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003405 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003406 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003407 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3408 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003409
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003410 int a;
3411 SkXfermode::Mode mode;
3412 getAlphaAndMode(paint, &a, &mode);
3413 const float alpha = a / 255.0f;
3414
Romain Guy3b748a42013-04-17 18:54:38 -07003415 setupDraw();
3416 setupDrawWithTexture();
3417 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003418 setupDrawColorFilter(getColorFilter(paint));
3419 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003420 setupDrawProgram();
3421 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003422 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003423 setupDrawTexture(texture);
3424 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003425 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003426 setupDrawMeshIndices(vertices, texCoords, vbo);
3427
3428 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003429}
3430
Romain Guy886b2752013-01-04 12:26:18 -08003431void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003432 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003433 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003434 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003435
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003436 int color = paint != NULL ? paint->getColor() : 0;
3437 int alpha;
3438 SkXfermode::Mode mode;
3439 getAlphaAndMode(paint, &alpha, &mode);
3440
Romain Guy886b2752013-01-04 12:26:18 -08003441 setupDraw();
3442 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003443 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003444 setupDrawAlpha8Color(color, alpha);
3445 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003446 setupDrawColorFilter(getColorFilter(paint));
Romain Guy886b2752013-01-04 12:26:18 -08003447 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003448 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003449 setupDrawProgram();
3450 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003451 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003452 setupDrawTexture(texture);
3453 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003454 setupDrawColorFilterUniforms(getColorFilter(paint));
Chris Craik4063a0e2013-11-15 16:06:56 -08003455 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003456 setupDrawMesh(vertices, texCoords);
3457
3458 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003459}
3460
Romain Guya5aed0d2010-09-09 14:42:43 -07003461void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003462 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003463 if (mCountOverdraw) {
3464 if (!mCaches.blend) glEnable(GL_BLEND);
3465 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3466 glBlendFunc(GL_ONE, GL_ONE);
3467 }
3468
3469 mCaches.blend = true;
3470 mCaches.lastSrcMode = GL_ONE;
3471 mCaches.lastDstMode = GL_ONE;
3472
3473 return;
3474 }
3475
Romain Guy82ba8142010-07-09 13:25:56 -07003476 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003477
Romain Guy82ba8142010-07-09 13:25:56 -07003478 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003479 // These blend modes are not supported by OpenGL directly and have
3480 // to be implemented using shaders. Since the shader will perform
3481 // the blending, turn blending off here
3482 // If the blend mode cannot be implemented using shaders, fall
3483 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003484 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003485 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003486 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003487 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003488
Romain Guy82bc7a72012-01-03 14:13:39 -08003489 if (mCaches.blend) {
3490 glDisable(GL_BLEND);
3491 mCaches.blend = false;
3492 }
3493
3494 return;
3495 } else {
3496 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003497 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003498 }
3499
3500 if (!mCaches.blend) {
3501 glEnable(GL_BLEND);
3502 }
3503
3504 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3505 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3506
3507 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3508 glBlendFunc(sourceMode, destMode);
3509 mCaches.lastSrcMode = sourceMode;
3510 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003511 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003512 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003513 glDisable(GL_BLEND);
3514 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003515 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003516}
3517
Romain Guy889f8d12010-07-29 14:37:42 -07003518bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003519 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003520 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003521 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003522 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003523 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003524 }
Romain Guy6926c722010-07-12 20:20:03 -07003525 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003526}
3527
Romain Guy026c5e162010-06-28 17:12:22 -07003528void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003529 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003530 TextureVertex::setUV(v++, u1, v1);
3531 TextureVertex::setUV(v++, u2, v1);
3532 TextureVertex::setUV(v++, u1, v2);
3533 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003534}
3535
Chris Craikd218a922014-01-02 17:13:34 -08003536void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003537 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003538 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3539 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003540 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003541 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003542 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003543}
3544
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003545float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003546 float alpha;
3547 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3548 alpha = mDrawModifiers.mOverrideLayerAlpha;
3549 } else {
3550 alpha = layer->getAlpha() / 255.0f;
3551 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003552 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003553}
3554
Romain Guy9d5316e2010-06-24 19:30:36 -07003555}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003556}; // namespace android