blob: 9b8201351dcc6de26690561c539d327e65e758cc [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"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700183 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guyd643bb52011-03-01 14:55:21 -0800454
Romain Guy80911b82011-03-16 15:30:12 -0700455 Rect clip(*mSnapshot->clipRect);
456 clip.snapToPixelBoundaries();
457
Romain Guyd643bb52011-03-01 14:55:21 -0800458 // Since we don't know what the functor will draw, let's dirty
459 // tne entire clip region
460 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800461 dirtyLayerUnchecked(clip, getRegion());
462 }
Romain Guyd643bb52011-03-01 14:55:21 -0800463
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 DrawGlInfo info;
465 info.clipLeft = clip.left;
466 info.clipTop = clip.top;
467 info.clipRight = clip.right;
468 info.clipBottom = clip.bottom;
469 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700470 info.width = getSnapshot()->viewport.getWidth();
471 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700473
John Reck25d2f7b2013-09-10 13:12:09 -0700474 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700475 // setup GL state for functor
476 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700477 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
478 }
John Reck25d2f7b2013-09-10 13:12:09 -0700479 if (mCaches.enableScissor() || dirtyClip) {
480 setScissorFromClip();
481 }
Chris Craik54f574a2013-08-26 11:23:46 -0700482 interrupt();
483
484 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700485 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800486
Romain Guy8f3b8e32012-03-27 16:33:45 -0700487 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700488 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800489 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700490
Chris Craik65924a32012-04-05 17:52:11 -0700491 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700492 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700493 }
Romain Guycabfcc12011-03-07 18:06:46 -0800494 }
495
Chet Haasedaf98e92011-01-10 14:10:36 -0800496 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700497 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800498}
499
Romain Guyf6a11b82010-06-23 17:47:49 -0700500///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700501// Debug
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guy0f6675332013-03-01 14:31:04 -0800504void OpenGLRenderer::eventMark(const char* name) const {
505 mCaches.eventMark(0, name);
506}
507
Romain Guy87e2f7572012-09-24 11:37:12 -0700508void OpenGLRenderer::startMark(const char* name) const {
509 mCaches.startMark(0, name);
510}
511
512void OpenGLRenderer::endMark() const {
513 mCaches.endMark();
514}
515
516void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
517 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
518 if (clear) {
519 mCaches.disableScissor();
520 mCaches.stencil.clear();
521 }
522 if (enable) {
523 mCaches.stencil.enableDebugWrite();
524 } else {
525 mCaches.stencil.disable();
526 }
527 }
528}
529
530void OpenGLRenderer::renderOverdraw() {
531 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700532 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700533
534 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700535 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700536 clip->right - clip->left, clip->bottom - clip->top);
537
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700539 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700540 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
541
542 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700543 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700544 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
545
546 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700547 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700548 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
549
550 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700551 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700552 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
553
Romain Guy87e2f7572012-09-24 11:37:12 -0700554 mCaches.stencil.disable();
555 }
556}
557
Romain Guy78dd96d2013-05-03 14:24:16 -0700558void OpenGLRenderer::countOverdraw() {
559 size_t count = mWidth * mHeight;
560 uint32_t* buffer = new uint32_t[count];
561 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
562
563 size_t total = 0;
564 for (size_t i = 0; i < count; i++) {
565 total += buffer[i] & 0xff;
566 }
567
568 mOverdraw = total / float(count);
569
570 delete[] buffer;
571}
572
Romain Guy87e2f7572012-09-24 11:37:12 -0700573///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700574// Layers
575///////////////////////////////////////////////////////////////////////////////
576
577bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (layer->deferredUpdateScheduled && layer->renderer &&
579 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700580 ATRACE_CALL();
581
Romain Guy11cb6422012-09-21 00:39:43 -0700582 Rect& dirty = layer->dirtyRect;
583
Romain Guy7c450aa2012-09-21 19:15:00 -0700584 if (inFrame) {
585 endTiling();
586 debugOverdraw(false, false);
587 }
Romain Guy11cb6422012-09-21 00:39:43 -0700588
Romain Guy96885eb2013-03-26 15:05:58 -0700589 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700590 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700591 } else {
592 layer->defer();
593 }
Romain Guy11cb6422012-09-21 00:39:43 -0700594
595 if (inFrame) {
596 resumeAfterLayer();
597 startTiling(mSnapshot);
598 }
599
Romain Guy5bb3c732012-11-29 17:52:58 -0800600 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700601 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700602
603 return true;
604 }
605
606 return false;
607}
608
609void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700610 // If draw deferring is enabled this method will simply defer
611 // the display list of each individual layer. The layers remain
612 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700613 int count = mLayerUpdates.size();
614 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700615 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
616 startMark("Layer Updates");
617 } else {
618 startMark("Defer Layer Updates");
619 }
Romain Guy11cb6422012-09-21 00:39:43 -0700620
Chris Craik1206b9b2013-04-04 14:46:24 -0700621 // Note: it is very important to update the layers in order
622 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700623 Layer* layer = mLayerUpdates.itemAt(i);
624 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700625 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
626 mCaches.resourceCache.decrementRefcount(layer);
627 }
Romain Guy11cb6422012-09-21 00:39:43 -0700628 }
Romain Guy11cb6422012-09-21 00:39:43 -0700629
Romain Guy96885eb2013-03-26 15:05:58 -0700630 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
631 mLayerUpdates.clear();
632 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
633 }
634 endMark();
635 }
636}
637
638void OpenGLRenderer::flushLayers() {
639 int count = mLayerUpdates.size();
640 if (count > 0) {
641 startMark("Apply Layer Updates");
642 char layerName[12];
643
Chris Craik1206b9b2013-04-04 14:46:24 -0700644 // Note: it is very important to update the layers in order
645 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700646 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700647 startMark(layerName);
648
Romain Guy40543602013-06-12 15:31:28 -0700649 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700650 Layer* layer = mLayerUpdates.itemAt(i);
651 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700652 ATRACE_END();
653
Romain Guy02b49b72013-03-29 12:37:16 -0700654 mCaches.resourceCache.decrementRefcount(layer);
655
Romain Guy96885eb2013-03-26 15:05:58 -0700656 endMark();
657 }
658
659 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700660 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700661
Romain Guy11cb6422012-09-21 00:39:43 -0700662 endMark();
663 }
664}
665
666void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
667 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700668 // Make sure we don't introduce duplicates.
669 // SortedVector would do this automatically but we need to respect
670 // the insertion order. The linear search is not an issue since
671 // this list is usually very short (typically one item, at most a few)
672 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
673 if (mLayerUpdates.itemAt(i) == layer) {
674 return;
675 }
676 }
Romain Guy11cb6422012-09-21 00:39:43 -0700677 mLayerUpdates.push_back(layer);
678 mCaches.resourceCache.incrementRefcount(layer);
679 }
680}
681
Romain Guye93482f2013-06-17 13:14:51 -0700682void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
683 if (layer) {
684 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
685 if (mLayerUpdates.itemAt(i) == layer) {
686 mLayerUpdates.removeAt(i);
687 mCaches.resourceCache.decrementRefcount(layer);
688 break;
689 }
690 }
691 }
692}
693
Romain Guy11cb6422012-09-21 00:39:43 -0700694void OpenGLRenderer::clearLayerUpdates() {
695 size_t count = mLayerUpdates.size();
696 if (count > 0) {
697 mCaches.resourceCache.lock();
698 for (size_t i = 0; i < count; i++) {
699 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
700 }
701 mCaches.resourceCache.unlock();
702 mLayerUpdates.clear();
703 }
704}
705
Romain Guy40543602013-06-12 15:31:28 -0700706void OpenGLRenderer::flushLayerUpdates() {
707 syncState();
708 updateLayers();
709 flushLayers();
710 // Wait for all the layer updates to be executed
711 AutoFence fence;
712}
713
Romain Guy11cb6422012-09-21 00:39:43 -0700714///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700715// State management
716///////////////////////////////////////////////////////////////////////////////
717
Romain Guybb9524b2010-06-22 18:56:38 -0700718int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700719 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700720}
721
722int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700723 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
726void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700727 if (mSaveCount > 1) {
728 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700729 }
Romain Guybb9524b2010-06-22 18:56:38 -0700730}
731
732void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700733 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700734
Romain Guy8fb95422010-08-17 18:38:51 -0700735 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700736 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700737 }
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
Romain Guy8aef54f2010-09-01 15:13:49 -0700740int OpenGLRenderer::saveSnapshot(int flags) {
741 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700742 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700743}
744
745bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700746 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700747 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700748 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700749
Romain Guybd6b79b2010-06-26 00:13:53 -0700750 sp<Snapshot> current = mSnapshot;
751 sp<Snapshot> previous = mSnapshot->previous;
752
Romain Guyeb993562010-10-05 18:14:38 -0700753 if (restoreOrtho) {
754 Rect& r = previous->viewport;
755 glViewport(r.left, r.top, r.right, r.bottom);
756 mOrthoMatrix.load(current->orthoMatrix);
757 }
758
Romain Guy8b55f372010-08-18 17:10:07 -0700759 mSaveCount--;
760 mSnapshot = previous;
761
Romain Guy2542d192010-08-18 11:47:12 -0700762 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700763 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700764 }
Romain Guy2542d192010-08-18 11:47:12 -0700765
Romain Guy5ec99242010-11-03 16:19:08 -0700766 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700767 endMark(); // Savelayer
768 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700769 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700770 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700771 }
772
Romain Guy2542d192010-08-18 11:47:12 -0700773 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700774}
775
Romain Guyf6a11b82010-06-23 17:47:49 -0700776///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700777// Layers
778///////////////////////////////////////////////////////////////////////////////
779
780int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800781 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700782 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700783
Romain Guyaf636eb2010-12-09 17:47:21 -0800784 if (!mSnapshot->isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700785 createLayer(left, top, right, bottom, alpha, mode, flags);
Romain Guydbc26d22010-10-11 17:58:29 -0700786 }
Romain Guyd55a8612010-06-28 17:42:46 -0700787
788 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700789}
790
Chris Craikd90144d2013-03-19 15:03:48 -0700791void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
792 const Rect untransformedBounds(bounds);
793
794 currentTransform().mapRect(bounds);
795
796 // Layers only make sense if they are in the framebuffer's bounds
797 if (bounds.intersect(*mSnapshot->clipRect)) {
798 // We cannot work with sub-pixels in this case
799 bounds.snapToPixelBoundaries();
800
801 // When the layer is not an FBO, we may use glCopyTexImage so we
802 // need to make sure the layer does not extend outside the bounds
803 // of the framebuffer
804 if (!bounds.intersect(mSnapshot->previous->viewport)) {
805 bounds.setEmpty();
806 } else if (fboLayer) {
807 clip.set(bounds);
808 mat4 inverse;
809 inverse.loadInverse(currentTransform());
810 inverse.mapRect(clip);
811 clip.snapToPixelBoundaries();
812 if (clip.intersect(untransformedBounds)) {
813 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
814 bounds.set(untransformedBounds);
815 } else {
816 clip.setEmpty();
817 }
818 }
819 } else {
820 bounds.setEmpty();
821 }
822}
823
Chris Craik408eb122013-03-26 18:55:15 -0700824void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
825 bool fboLayer, int alpha) {
826 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
827 bounds.getHeight() > mCaches.maxTextureSize ||
828 (fboLayer && clip.isEmpty())) {
829 mSnapshot->empty = fboLayer;
830 } else {
831 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
832 }
833}
834
Chris Craikd90144d2013-03-19 15:03:48 -0700835int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
836 int alpha, SkXfermode::Mode mode, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700837 const int count = saveSnapshot(flags);
838
839 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
840 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
841 // operations will be able to store and restore the current clip and transform info, and
842 // quick rejection will be correct (for display lists)
843
844 Rect bounds(left, top, right, bottom);
845 Rect clip;
846 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700847 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700848
Chris Craik408eb122013-03-26 18:55:15 -0700849 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700850 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
851 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700852 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700853 }
854 }
855
856 return count;
857}
858
859
Romain Guy1c740bc2010-09-13 18:00:09 -0700860/**
861 * Layers are viewed by Skia are slightly different than layers in image editing
862 * programs (for instance.) When a layer is created, previously created layers
863 * and the frame buffer still receive every drawing command. For instance, if a
864 * layer is created and a shape intersecting the bounds of the layers and the
865 * framebuffer is draw, the shape will be drawn on both (unless the layer was
866 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
867 *
868 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
869 * texture. Unfortunately, this is inefficient as it requires every primitive to
870 * be drawn n + 1 times, where n is the number of active layers. In practice this
871 * means, for every primitive:
872 * - Switch active frame buffer
873 * - Change viewport, clip and projection matrix
874 * - Issue the drawing
875 *
876 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700877 * To avoid this, layers are implemented in a different way here, at least in the
878 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
879 * is set. When this flag is set we can redirect all drawing operations into a
880 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700881 *
882 * This implementation relies on the frame buffer being at least RGBA 8888. When
883 * a layer is created, only a texture is created, not an FBO. The content of the
884 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700885 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700886 * buffer and drawing continues as normal. This technique therefore treats the
887 * frame buffer as a scratch buffer for the layers.
888 *
889 * To compose the layers back onto the frame buffer, each layer texture
890 * (containing the original frame buffer data) is drawn as a simple quad over
891 * the frame buffer. The trick is that the quad is set as the composition
892 * destination in the blending equation, and the frame buffer becomes the source
893 * of the composition.
894 *
895 * Drawing layers with an alpha value requires an extra step before composition.
896 * An empty quad is drawn over the layer's region in the frame buffer. This quad
897 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
898 * quad is used to multiply the colors in the frame buffer. This is achieved by
899 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
900 * GL_ZERO, GL_SRC_ALPHA.
901 *
902 * Because glCopyTexImage2D() can be slow, an alternative implementation might
903 * be use to draw a single clipped layer. The implementation described above
904 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700905 *
906 * (1) The frame buffer is actually not cleared right away. To allow the GPU
907 * to potentially optimize series of calls to glCopyTexImage2D, the frame
908 * buffer is left untouched until the first drawing operation. Only when
909 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700910 */
Chet Haased48885a2012-08-28 17:43:28 -0700911bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700912 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700913 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700915
Romain Guyeb993562010-10-05 18:14:38 -0700916 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
917
Romain Guyf607bdc2010-09-10 19:20:06 -0700918 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700919 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700920 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700921 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700922 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700923
924 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700925 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700926 return false;
927 }
Romain Guyf18fd992010-07-08 11:45:51 -0700928
Romain Guya1d3c912011-12-13 14:55:06 -0800929 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700930 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700931 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700932 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700933 }
934
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
938 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800939 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700940 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700941 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700942
Romain Guy8fb95422010-08-17 18:38:51 -0700943 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700944 mSnapshot->flags |= Snapshot::kFlagIsLayer;
945 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700946
Chris Craik7273daa2013-03-28 11:25:24 -0700947 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700948 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700949 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700950 } else {
951 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800953 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700955 // Workaround for some GL drivers. When reading pixels lying outside
956 // of the window we should get undefined values for those pixels.
957 // Unfortunately some drivers will turn the entire target texture black
958 // when reading outside of the window.
959 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
960 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800962 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700963
Romain Guyb254c242013-06-27 17:15:24 -0700964 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
965 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
966
Romain Guy54be1cd2011-06-13 19:04:27 -0700967 // Enqueue the buffer coordinates to clear the corresponding region later
968 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700969 }
Romain Guyeb993562010-10-05 18:14:38 -0700970 }
Romain Guyf86ef572010-07-01 11:05:42 -0700971
Romain Guyd55a8612010-06-28 17:42:46 -0700972 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700973}
974
Chris Craike63f7c622013-10-17 10:30:55 -0700975bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800976 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
Chet Haased48885a2012-08-28 17:43:28 -0700979 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800980 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
981 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700982 mSnapshot->fbo = layer->getFbo();
983 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
984 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
985 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
986 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700987 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988
Romain Guy2b7028e2012-09-19 17:25:38 -0700989 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700990 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700991 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
993 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
995 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700997 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 }
1000
1001 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
Romain Guyf735c8e2013-01-31 17:45:55 -08001004 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
1006 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001007 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001008 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001009 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 glClear(GL_COLOR_BUFFER_BIT);
1011
1012 dirtyClip();
1013
1014 // Change the ortho projection
1015 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1016 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1017
1018 return true;
1019}
1020
Romain Guy1c740bc2010-09-13 18:00:09 -07001021/**
1022 * Read the documentation of createLayer() before doing anything in this method.
1023 */
Romain Guy1d83e192010-08-17 11:37:00 -07001024void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1025 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001026 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001027 return;
1028 }
1029
Romain Guy8ce00302013-01-15 18:51:42 -08001030 Layer* layer = current->layer;
1031 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001032 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001033
Chris Craik39a908c2013-06-13 14:39:01 -07001034 bool clipRequired = false;
1035 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1036 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1037
Romain Guyeb993562010-10-05 18:14:38 -07001038 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001039 endTiling();
1040
Romain Guye0aa84b2012-04-03 19:30:26 -07001041 // Detach the texture from the FBO
1042 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001043
1044 layer->removeFbo(false);
1045
Romain Guyeb993562010-10-05 18:14:38 -07001046 // Unbind current FBO and restore previous one
1047 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001048 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001049
1050 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001051 }
1052
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001054 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001056 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001058 }
1059
Romain Guy03750a02010-10-18 14:06:08 -07001060 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001061
Romain Guya1d3c912011-12-13 14:55:06 -08001062 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001063
Romain Guy5b3b3522010-10-27 18:57:51 -07001064 // When the layer is stored in an FBO, we can save a bit of fillrate by
1065 // drawing only the dirty region
1066 if (fboLayer) {
1067 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001068 if (layer->getColorFilter()) {
1069 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001070 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001071 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001073 resetColorFilter();
1074 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 } else if (!rect.isEmpty()) {
1076 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301077
1078 save(0);
1079 // the layer contains screen buffer content that shouldn't be alpha modulated
1080 // (and any necessary alpha modulation was handled drawing into the layer)
1081 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301083 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001084 }
Romain Guy8b55f372010-08-18 17:10:07 -07001085
Romain Guy746b7402010-10-26 16:27:31 -07001086 dirtyClip();
1087
Romain Guyeb993562010-10-05 18:14:38 -07001088 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001089 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001090 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001091 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001092 }
1093}
1094
Romain Guyaa6c24c2011-04-28 18:40:04 -07001095void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001096 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001097
1098 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001099 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001100 setupDrawWithTexture();
1101 } else {
1102 setupDrawWithExternalTexture();
1103 }
1104 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001105 setupDrawColor(alpha, alpha, alpha, alpha);
1106 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001107 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001108 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001109 setupDrawPureColorUniforms();
1110 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001111 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1112 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001113 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001115 }
Romain Guy3b753822013-03-05 10:27:35 -08001116 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001117 layer->getWidth() == (uint32_t) rect.getWidth() &&
1118 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001119 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1120 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001121
Romain Guyd21b6e12011-11-30 20:21:23 -08001122 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001123 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1124 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001125 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001126 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001127 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1128 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001129 }
1130 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001131 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001132
1133 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001134}
1135
Romain Guy5b3b3522010-10-27 18:57:51 -07001136void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001137 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001138 const Rect& texCoords = layer->texCoords;
1139 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1140 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001141
Romain Guy9ace8f52011-07-07 20:50:11 -07001142 float x = rect.left;
1143 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001144 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001145 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001146 layer->getHeight() == (uint32_t) rect.getHeight();
1147
1148 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001149 // When we're swapping, the layer is already in screen coordinates
1150 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001151 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1152 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001153 }
1154
Romain Guyd21b6e12011-11-30 20:21:23 -08001155 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001157 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001158 }
1159
Chris Craik16ecda52013-03-29 10:59:59 -07001160 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001161 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001162 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001163 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001164 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001165 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001166
Romain Guyaa6c24c2011-04-28 18:40:04 -07001167 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1168 } else {
1169 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1170 drawTextureLayer(layer, rect);
1171 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1172 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001173}
1174
Chris Craik34416ea2013-04-15 16:08:28 -07001175/**
1176 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1177 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1178 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1179 * by saveLayer's restore
1180 */
1181#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1182 DRAW_COMMAND; \
1183 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1184 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1185 DRAW_COMMAND; \
1186 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1187 } \
1188 }
1189
1190#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1191
Romain Guy5b3b3522010-10-27 18:57:51 -07001192void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001193 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001194 layer->setRegionAsRect();
1195
Chris Craik34416ea2013-04-15 16:08:28 -07001196 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001197
Romain Guy5b3b3522010-10-27 18:57:51 -07001198 layer->region.clear();
1199 return;
1200 }
1201
Romain Guy211370f2012-02-01 16:10:55 -08001202 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001203 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001204 const android::Rect* rects;
1205 Region safeRegion;
1206 if (CC_LIKELY(hasRectToRectTransform())) {
1207 rects = layer->region.getArray(&count);
1208 } else {
1209 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1210 rects = safeRegion.getArray(&count);
1211 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001212
Chris Craik16ecda52013-03-29 10:59:59 -07001213 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001214 const float texX = 1.0f / float(layer->getWidth());
1215 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001216 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001217
Romain Guy8ce00302013-01-15 18:51:42 -08001218 setupDraw();
1219
1220 // We must get (and therefore bind) the region mesh buffer
1221 // after we setup drawing in case we need to mess with the
1222 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001223 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001224 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001225
Romain Guy7230a742011-01-10 22:26:16 -08001226 setupDrawWithTexture();
1227 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001228 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001229 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001230 setupDrawProgram();
1231 setupDrawDirtyRegionsDisabled();
1232 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001233 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001234 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001235 if (currentTransform().isPureTranslate()) {
1236 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1237 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001238
Romain Guyd21b6e12011-11-30 20:21:23 -08001239 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001240 setupDrawModelView(kModelViewMode_Translate, false,
1241 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001242 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001243 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001244 setupDrawModelView(kModelViewMode_Translate, false,
1245 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001246 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001247 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001248
1249 for (size_t i = 0; i < count; i++) {
1250 const android::Rect* r = &rects[i];
1251
1252 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001253 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001254 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001255 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001256
1257 // TODO: Reject quads outside of the clip
1258 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1259 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1260 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1261 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1262
1263 numQuads++;
1264
Romain Guy31e08e92013-06-18 15:53:53 -07001265 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001266 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1267 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001268 numQuads = 0;
1269 mesh = mCaches.getRegionMesh();
1270 }
1271 }
1272
1273 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001274 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1275 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001276 }
1277
Romain Guy5b3b3522010-10-27 18:57:51 -07001278#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001279 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001280#endif
1281
1282 layer->region.clear();
1283 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001284}
1285
Romain Guy3a3133d2011-02-01 22:59:58 -08001286#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001287void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001288 size_t count;
1289 const android::Rect* rects = region.getArray(&count);
1290
1291 uint32_t colors[] = {
1292 0x7fff0000, 0x7f00ff00,
1293 0x7f0000ff, 0x7fff00ff,
1294 };
1295
1296 int offset = 0;
1297 int32_t top = rects[0].top;
1298
1299 for (size_t i = 0; i < count; i++) {
1300 if (top != rects[i].top) {
1301 offset ^= 0x2;
1302 top = rects[i].top;
1303 }
1304
1305 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1306 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1307 SkXfermode::kSrcOver_Mode);
1308 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001309}
Chris Craike63f7c622013-10-17 10:30:55 -07001310#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001311
Romain Guy8ce00302013-01-15 18:51:42 -08001312void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1313 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001314 Vector<float> rects;
1315
1316 SkRegion::Iterator it(region);
1317 while (!it.done()) {
1318 const SkIRect& r = it.rect();
1319 rects.push(r.fLeft);
1320 rects.push(r.fTop);
1321 rects.push(r.fRight);
1322 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001323 it.next();
1324 }
1325
Romain Guy448455f2013-07-22 13:57:50 -07001326 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001327}
1328
Romain Guy5b3b3522010-10-27 18:57:51 -07001329void OpenGLRenderer::dirtyLayer(const float left, const float top,
1330 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001331 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001332 Rect bounds(left, top, right, bottom);
1333 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001334 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001335 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001336}
1337
1338void OpenGLRenderer::dirtyLayer(const float left, const float top,
1339 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001340 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001341 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001342 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001343 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001344}
1345
1346void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001347 if (bounds.intersect(*mSnapshot->clipRect)) {
1348 bounds.snapToPixelBoundaries();
1349 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1350 if (!dirty.isEmpty()) {
1351 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001352 }
1353 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001354}
1355
Chris Craik4063a0e2013-11-15 16:06:56 -08001356void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001357 GLsizei elementsCount = quadsCount * 6;
1358 while (elementsCount > 0) {
1359 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1360
Romain Guy3380cfd2013-08-15 16:57:57 -07001361 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001362 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1363
1364 elementsCount -= drawCount;
1365 // Though there are 4 vertices in a quad, we use 6 indices per
1366 // quad to draw with GL_TRIANGLES
1367 mesh += (drawCount / 6) * 4;
1368 }
1369}
1370
Romain Guy54be1cd2011-06-13 19:04:27 -07001371void OpenGLRenderer::clearLayerRegions() {
1372 const size_t count = mLayers.size();
1373 if (count == 0) return;
1374
1375 if (!mSnapshot->isIgnored()) {
1376 // Doing several glScissor/glClear here can negatively impact
1377 // GPUs with a tiler architecture, instead we draw quads with
1378 // the Clear blending mode
1379
1380 // The list contains bounds that have already been clipped
1381 // against their initial clip rect, and the current clip
1382 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001383 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001384
Romain Guy448455f2013-07-22 13:57:50 -07001385 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001386 Vertex* vertex = mesh;
1387
1388 for (uint32_t i = 0; i < count; i++) {
1389 Rect* bounds = mLayers.itemAt(i);
1390
Romain Guy54be1cd2011-06-13 19:04:27 -07001391 Vertex::set(vertex++, bounds->left, bounds->top);
1392 Vertex::set(vertex++, bounds->right, bounds->top);
1393 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001394 Vertex::set(vertex++, bounds->right, bounds->bottom);
1395
1396 delete bounds;
1397 }
Romain Guye67307c2013-02-11 18:01:20 -08001398 // We must clear the list of dirty rects before we
1399 // call setupDraw() to prevent stencil setup to do
1400 // the same thing again
1401 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001402
1403 setupDraw(false);
1404 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1405 setupDrawBlending(true, SkXfermode::kClear_Mode);
1406 setupDrawProgram();
1407 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001408 setupDrawModelView(kModelViewMode_Translate, false,
1409 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001410
Chris Craik4063a0e2013-11-15 16:06:56 -08001411 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001412
1413 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001414 } else {
1415 for (uint32_t i = 0; i < count; i++) {
1416 delete mLayers.itemAt(i);
1417 }
Romain Guye67307c2013-02-11 18:01:20 -08001418 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001419 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001420}
1421
Romain Guybd6b79b2010-06-26 00:13:53 -07001422///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001423// State Deferral
1424///////////////////////////////////////////////////////////////////////////////
1425
Chris Craikff785832013-03-08 13:12:16 -08001426bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001427 const Rect& currentClip = *(mSnapshot->clipRect);
1428 const mat4& currentMatrix = *(mSnapshot->transform);
1429
Chris Craikff785832013-03-08 13:12:16 -08001430 if (stateDeferFlags & kStateDeferFlag_Draw) {
1431 // state has bounds initialized in local coordinates
1432 if (!state.mBounds.isEmpty()) {
1433 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001434 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001435 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1436 // is used, it should more closely duplicate the quickReject logic (in how it uses
1437 // snapToPixelBoundaries)
1438
Chris Craik28ce94a2013-05-31 11:38:03 -07001439 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001440 // quick rejected
1441 return true;
1442 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001443
Chris Craika02c4ed2013-06-14 13:43:58 -07001444 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001445 if (!currentClip.contains(state.mBounds)) {
1446 int& flags = state.mClipSideFlags;
1447 // op partially clipped, so record which sides are clipped for clip-aware merging
1448 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1449 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1450 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1451 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1452 }
1453 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001454 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001455 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1456 // overdraw avoidance (since we don't know what it overlaps)
1457 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001458 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001459 }
1460 }
1461
Chris Craik527a3aa2013-03-04 10:19:31 -08001462 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1463 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001464 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001465 }
1466
Chris Craik7273daa2013-03-28 11:25:24 -07001467 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1468 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001469 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001470 state.mDrawModifiers = mDrawModifiers;
1471 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001472 return false;
1473}
1474
Chris Craik527a3aa2013-03-04 10:19:31 -08001475void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001476 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001477 mDrawModifiers = state.mDrawModifiers;
1478 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001479
Chris Craik527a3aa2013-03-04 10:19:31 -08001480 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001481 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1482 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001483 dirtyClip();
1484 }
Chris Craikc3566d02013-02-04 16:16:33 -08001485}
1486
Chris Craik28ce94a2013-05-31 11:38:03 -07001487/**
1488 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1489 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1490 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1491 *
1492 * This method should be called when restoreDisplayState() won't be restoring the clip
1493 */
1494void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1495 if (clipRect != NULL) {
1496 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1497 } else {
1498 mSnapshot->setClip(0, 0, mWidth, mHeight);
1499 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001500 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001501 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001502}
1503
Chris Craikc3566d02013-02-04 16:16:33 -08001504///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001505// Transforms
1506///////////////////////////////////////////////////////////////////////////////
1507
1508void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001509 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001510}
1511
1512void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001513 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001514}
1515
1516void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001517 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001518}
1519
Romain Guy807daf72011-01-18 11:19:19 -08001520void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001521 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001522}
1523
Romain Guyf6a11b82010-06-23 17:47:49 -07001524void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001525 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001526 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001527 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001528 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001529 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001530}
1531
Chris Craikb98a0162013-02-21 11:30:22 -08001532bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001533 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001534}
1535
Romain Guyf6a11b82010-06-23 17:47:49 -07001536void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001537 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001538}
1539
1540void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001541 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001542 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001543 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001544 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001545}
1546
1547///////////////////////////////////////////////////////////////////////////////
1548// Clipping
1549///////////////////////////////////////////////////////////////////////////////
1550
Romain Guybb9524b2010-06-22 18:56:38 -07001551void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001552 Rect clip(*mSnapshot->clipRect);
1553 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001554
Romain Guy8a4ac612012-07-17 17:32:48 -07001555 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1556 clip.getWidth(), clip.getHeight())) {
1557 mDirtyClip = false;
1558 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001559}
1560
Romain Guy8ce00302013-01-15 18:51:42 -08001561void OpenGLRenderer::ensureStencilBuffer() {
1562 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1563 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1564 // just hope we have one when hasLayer() returns false.
1565 if (hasLayer()) {
1566 attachStencilBufferToLayer(mSnapshot->layer);
1567 }
1568}
1569
1570void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1571 // The layer's FBO is already bound when we reach this stage
1572 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001573 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1574 // is attached after we initiated tiling. We must turn it off,
1575 // attach the new render buffer then turn tiling back on
1576 endTiling();
1577
Romain Guy8d4aeb72013-02-12 16:08:55 -08001578 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001579 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001580 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001581
Romain Guyf735c8e2013-01-31 17:45:55 -08001582 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001583 }
1584}
1585
1586void OpenGLRenderer::setStencilFromClip() {
1587 if (!mCaches.debugOverdraw) {
1588 if (!mSnapshot->clipRegion->isEmpty()) {
1589 // NOTE: The order here is important, we must set dirtyClip to false
1590 // before any draw call to avoid calling back into this method
1591 mDirtyClip = false;
1592
1593 ensureStencilBuffer();
1594
1595 mCaches.stencil.enableWrite();
1596
1597 // Clear the stencil but first make sure we restrict drawing
1598 // to the region's bounds
1599 bool resetScissor = mCaches.enableScissor();
1600 if (resetScissor) {
1601 // The scissor was not set so we now need to update it
1602 setScissorFromClip();
1603 }
1604 mCaches.stencil.clear();
1605 if (resetScissor) mCaches.disableScissor();
1606
1607 // NOTE: We could use the region contour path to generate a smaller mesh
1608 // Since we are using the stencil we could use the red book path
1609 // drawing technique. It might increase bandwidth usage though.
1610
1611 // The last parameter is important: we are not drawing in the color buffer
1612 // so we don't want to dirty the current layer, if any
1613 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1614
1615 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001616
1617 // Draw the region used to generate the stencil if the appropriate debug
1618 // mode is enabled
1619 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1620 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1621 }
Romain Guy8ce00302013-01-15 18:51:42 -08001622 } else {
1623 mCaches.stencil.disable();
1624 }
1625 }
1626}
1627
Romain Guy9d5316e2010-06-24 19:30:36 -07001628const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001629 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001630}
1631
Chris Craik39a908c2013-06-13 14:39:01 -07001632bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001633 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001634 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001635 return true;
1636 }
1637
1638 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001639 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001640 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001641
1642 Rect clipRect(*mSnapshot->clipRect);
1643 clipRect.snapToPixelBoundaries();
1644
Chris Craik39a908c2013-06-13 14:39:01 -07001645 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001646
Chris Craik39a908c2013-06-13 14:39:01 -07001647 if (clipRequired) *clipRequired = !clipRect.contains(r);
1648 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001649}
1650
Romain Guy672433d2013-01-04 19:05:13 -08001651bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1652 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001653 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1654 // the final mapped rect to ensure correct clipping behavior for the ramp.
1655 bool snapOut = paint->isAntiAlias();
1656
Chris Craikcb4d6002012-09-25 12:00:29 -07001657 if (paint->getStyle() != SkPaint::kFill_Style) {
1658 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001659 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001660 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001661 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001662 }
1663}
1664
Chris Craik5e49b302013-07-30 19:05:20 -07001665bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001666 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001667 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001668 return true;
1669 }
1670
Chris Craik39a908c2013-06-13 14:39:01 -07001671 if (!isDeferred()) {
1672 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001673 }
Chris Craik39a908c2013-06-13 14:39:01 -07001674 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001675}
1676
Romain Guy8ce00302013-01-15 18:51:42 -08001677void OpenGLRenderer::debugClip() {
1678#if DEBUG_CLIP_REGIONS
1679 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1680 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1681 }
1682#endif
1683}
1684
Romain Guy079ba2c2010-07-16 14:12:24 -07001685bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001686 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001687 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1688 if (clipped) {
1689 dirtyClip();
1690 }
1691 return !mSnapshot->clipRect->isEmpty();
1692 }
1693
1694 SkPath path;
1695 path.addRect(left, top, right, bottom);
1696
Romain Guyb7b93e02013-08-01 15:29:25 -07001697 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001698}
1699
1700bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1701 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001702 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001703
1704 SkPath transformed;
1705 path->transform(transform, &transformed);
1706
1707 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001708 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1709 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001710 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001711 if (mSnapshot->previous == mFirstSnapshot) {
1712 clip.setRect(0, 0, mWidth, mHeight);
1713 } else {
1714 Rect* bounds = mSnapshot->previous->clipRect;
1715 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1716 }
Romain Guy8ce00302013-01-15 18:51:42 -08001717 }
1718
1719 SkRegion region;
1720 region.setPath(transformed, clip);
1721
1722 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001723 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001724 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001725 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001726 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001727}
1728
Romain Guy735738c2012-12-03 12:34:51 -08001729bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001730 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1731 if (clipped) {
1732 dirtyClip();
1733 }
1734 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001735}
1736
Chet Haasea23eed82012-04-12 15:19:04 -07001737Rect* OpenGLRenderer::getClipRect() {
1738 return mSnapshot->clipRect;
1739}
1740
Romain Guyf6a11b82010-06-23 17:47:49 -07001741///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001742// Drawing commands
1743///////////////////////////////////////////////////////////////////////////////
1744
Romain Guy54be1cd2011-06-13 19:04:27 -07001745void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001746 // TODO: It would be best if we could do this before quickReject()
1747 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001748 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001749 // Make sure setScissor & setStencil happen at the beginning of
1750 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001751 if (mDirtyClip) {
1752 if (mCaches.scissorEnabled) {
1753 setScissorFromClip();
1754 }
Romain Guy8ce00302013-01-15 18:51:42 -08001755 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001757
Romain Guy70ca14e2010-12-13 18:24:33 -08001758 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001759
Romain Guy70ca14e2010-12-13 18:24:33 -08001760 mSetShaderColor = false;
1761 mColorSet = false;
1762 mColorA = mColorR = mColorG = mColorB = 0.0f;
1763 mTextureUnit = 0;
1764 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001765
1766 // Enable debug highlight when what we're about to draw is tested against
1767 // the stencil buffer and if stencil highlight debugging is on
1768 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1769 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1770 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001771
1772 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001773}
1774
1775void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1776 mDescription.hasTexture = true;
1777 mDescription.hasAlpha8Texture = isAlpha8;
1778}
1779
Romain Guyff316ec2013-02-13 18:39:43 -08001780void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1781 mDescription.hasTexture = true;
1782 mDescription.hasColors = true;
1783 mDescription.hasAlpha8Texture = isAlpha8;
1784}
1785
Romain Guyaa6c24c2011-04-28 18:40:04 -07001786void OpenGLRenderer::setupDrawWithExternalTexture() {
1787 mDescription.hasExternalTexture = true;
1788}
1789
Romain Guy15bc6432011-12-13 13:11:32 -08001790void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001791 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001792}
1793
Chris Craik710f46d2012-09-17 17:25:49 -07001794void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001795 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001796}
1797
Romain Guy8d0d4782010-12-14 20:13:35 -08001798void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1799 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001800 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1801 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1802 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001803 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001804 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805}
1806
Romain Guy86568192010-12-14 15:55:39 -08001807void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1808 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001809 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1810 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1811 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001812 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001813 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001814}
1815
Romain Guy41210632012-07-16 17:04:24 -07001816void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1817 mCaches.fontRenderer->describe(mDescription, paint);
1818}
1819
Romain Guy70ca14e2010-12-13 18:24:33 -08001820void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1821 mColorA = a;
1822 mColorR = r;
1823 mColorG = g;
1824 mColorB = b;
1825 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001826 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001827}
1828
1829void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001830 if (mDrawModifiers.mShader) {
1831 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001832 }
1833}
1834
1835void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001836 if (mDrawModifiers.mColorFilter) {
1837 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001838 }
1839}
1840
Romain Guyf09ef512011-05-27 11:43:46 -07001841void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1842 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1843 mColorA = 1.0f;
1844 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001845 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001846 }
1847}
1848
Romain Guy70ca14e2010-12-13 18:24:33 -08001849void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001850 // When the blending mode is kClear_Mode, we need to use a modulate color
1851 // argb=1,0,0,0
1852 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001853 bool blend = (mColorSet && mColorA < 1.0f) ||
1854 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1855 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001856}
1857
1858void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001859 // When the blending mode is kClear_Mode, we need to use a modulate color
1860 // argb=1,0,0,0
1861 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001862 blend |= (mColorSet && mColorA < 1.0f) ||
1863 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1864 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1865 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001866}
1867
1868void OpenGLRenderer::setupDrawProgram() {
1869 useProgram(mCaches.programCache.get(mDescription));
1870}
1871
1872void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1873 mTrackDirtyRegions = false;
1874}
1875
Chris Craik4063a0e2013-11-15 16:06:56 -08001876void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1877 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001878 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001879 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001880 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001881 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001882
Romain Guy86568192010-12-14 15:55:39 -08001883 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1884 if (!ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001885 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform(), offset);
1886 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001887 } else {
Chris Craik4063a0e2013-11-15 16:06:56 -08001888 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity(), offset);
1889 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001890 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001891}
1892
1893void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001894 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001895 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1896 }
1897}
1898
Romain Guy86568192010-12-14 15:55:39 -08001899void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001900 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001901 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001902 }
1903}
1904
Romain Guy70ca14e2010-12-13 18:24:33 -08001905void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001906 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001907 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001908 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1909 // because it was built into modelView / the geometry, and the SkiaShader needs to
1910 // compensate.
1911 mat4 modelViewWithoutTransform;
1912 modelViewWithoutTransform.loadInverse(currentTransform());
1913 modelViewWithoutTransform.multiply(mModelView);
1914 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001915 }
Chris Craikc3566d02013-02-04 16:16:33 -08001916 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1917 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001918 }
1919}
1920
1921void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001922 if (mDrawModifiers.mColorFilter) {
1923 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001924 }
1925}
1926
Romain Guy41210632012-07-16 17:04:24 -07001927void OpenGLRenderer::setupDrawTextGammaUniforms() {
1928 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1929}
1930
Romain Guy70ca14e2010-12-13 18:24:33 -08001931void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001932 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001933 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001934 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001935}
1936
1937void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001938 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001939 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001940 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001941}
1942
Romain Guyaa6c24c2011-04-28 18:40:04 -07001943void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1944 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001945 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001946 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001947}
1948
Romain Guy8f0095c2011-05-02 17:24:22 -07001949void OpenGLRenderer::setupDrawTextureTransform() {
1950 mDescription.hasTextureTransform = true;
1951}
1952
1953void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001954 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1955 GL_FALSE, &transform.data[0]);
1956}
1957
Romain Guy70ca14e2010-12-13 18:24:33 -08001958void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001959 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001960 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001961 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001962 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001963 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001964 }
Romain Guyd71dd362011-12-12 19:03:35 -08001965
Chris Craikcb4d6002012-09-25 12:00:29 -07001966 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001967 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001968 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001969 }
1970
1971 mCaches.unbindIndicesBuffer();
1972}
1973
Romain Guyff316ec2013-02-13 18:39:43 -08001974void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1975 bool force = mCaches.unbindMeshBuffer();
1976 GLsizei stride = sizeof(ColorTextureVertex);
1977
1978 mCaches.bindPositionVertexPointer(force, vertices, stride);
1979 if (mCaches.currentProgram->texCoords >= 0) {
1980 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1981 }
1982 int slot = mCaches.currentProgram->getAttrib("colors");
1983 if (slot >= 0) {
1984 glEnableVertexAttribArray(slot);
1985 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1986 }
1987
1988 mCaches.unbindIndicesBuffer();
1989}
1990
Romain Guy3b748a42013-04-17 18:54:38 -07001991void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1992 bool force = false;
1993 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1994 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1995 // use the default VBO found in Caches
1996 if (!vertices || vbo) {
1997 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1998 } else {
1999 force = mCaches.unbindMeshBuffer();
2000 }
2001 mCaches.bindIndicesBuffer();
2002
Chris Craikcb4d6002012-09-25 12:00:29 -07002003 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002004 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002005 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002006 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002007}
2008
Romain Guy448455f2013-07-22 13:57:50 -07002009void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002010 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002011 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002012 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002013}
2014
Romain Guy70ca14e2010-12-13 18:24:33 -08002015///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002016// Drawing
2017///////////////////////////////////////////////////////////////////////////////
2018
Chris Craikff785832013-03-08 13:12:16 -08002019status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2020 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002021 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002022 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2023 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002024 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002025 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002026 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002027 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2028 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002029 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002030 }
2031
Chris Craik28ce94a2013-05-31 11:38:03 -07002032 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2033 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002034 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2035 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002036
2037 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002038 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002039
Chet Haase58d110a2013-04-10 07:43:29 -07002040 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002041 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002042
Romain Guy65549432012-03-26 16:45:05 -07002043 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002044}
2045
Chris Craikc3566d02013-02-04 16:16:33 -08002046void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002047 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002048 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002049 }
2050}
2051
Romain Guya168d732011-03-18 16:50:13 -07002052void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2053 int alpha;
2054 SkXfermode::Mode mode;
2055 getAlphaAndMode(paint, &alpha, &mode);
2056
Romain Guy886b2752013-01-04 12:26:18 -08002057 int color = paint != NULL ? paint->getColor() : 0;
2058
Romain Guya168d732011-03-18 16:50:13 -07002059 float x = left;
2060 float y = top;
2061
Romain Guy886b2752013-01-04 12:26:18 -08002062 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2063
Romain Guya168d732011-03-18 16:50:13 -07002064 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002065 if (currentTransform().isPureTranslate()) {
2066 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2067 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002068 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002069
2070 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002071 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002072 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002073 }
2074
Romain Guy3b748a42013-04-17 18:54:38 -07002075 // No need to check for a UV mapper on the texture object, only ARGB_8888
2076 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002077 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002078 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2079 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002080}
2081
Romain Guy03c00b52013-06-20 18:30:28 -07002082/**
2083 * Important note: this method is intended to draw batches of bitmaps and
2084 * will not set the scissor enable or dirty the current layer, if any.
2085 * The caller is responsible for properly dirtying the current layer.
2086 */
Romain Guy55b6f952013-06-27 15:27:09 -07002087status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002088 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002089 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002090 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002091 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002092
Chris Craik527a3aa2013-03-04 10:19:31 -08002093 const AutoTexture autoCleanup(texture);
2094
2095 int alpha;
2096 SkXfermode::Mode mode;
2097 getAlphaAndMode(paint, &alpha, &mode);
2098
2099 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002100 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002101
2102 const float x = (int) floorf(bounds.left + 0.5f);
2103 const float y = (int) floorf(bounds.top + 0.5f);
2104 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2105 int color = paint != NULL ? paint->getColor() : 0;
2106 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2107 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002108 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002109 GL_TRIANGLES, bitmapCount * 6, true,
2110 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002111 } else {
2112 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2113 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002114 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002115 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2116 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002117 }
2118
2119 return DrawGlInfo::kStatusDrew;
2120}
2121
Chet Haase48659092012-05-31 15:21:51 -07002122status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002123 const float right = left + bitmap->width();
2124 const float bottom = top + bitmap->height();
2125
2126 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002127 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002128 }
2129
Romain Guya1d3c912011-12-13 14:55:06 -08002130 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002131 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002132 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002133 const AutoTexture autoCleanup(texture);
2134
Romain Guy211370f2012-02-01 16:10:55 -08002135 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002136 drawAlphaBitmap(texture, left, top, paint);
2137 } else {
2138 drawTextureRect(left, top, right, bottom, texture, paint);
2139 }
Chet Haase48659092012-05-31 15:21:51 -07002140
2141 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002142}
2143
Chet Haase48659092012-05-31 15:21:51 -07002144status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002145 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2146 const mat4 transform(*matrix);
2147 transform.mapRect(r);
2148
Romain Guy6926c722010-07-12 20:20:03 -07002149 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002150 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002151 }
2152
Romain Guya1d3c912011-12-13 14:55:06 -08002153 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002154 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002155 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002156 const AutoTexture autoCleanup(texture);
2157
Romain Guy5b3b3522010-10-27 18:57:51 -07002158 // This could be done in a cheaper way, all we need is pass the matrix
2159 // to the vertex shader. The save/restore is a bit overkill.
2160 save(SkCanvas::kMatrix_SaveFlag);
2161 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002162 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2163 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2164 } else {
2165 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2166 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002167 restore();
Chet Haase48659092012-05-31 15:21:51 -07002168
2169 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002170}
2171
Chet Haase48659092012-05-31 15:21:51 -07002172status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002173 const float right = left + bitmap->width();
2174 const float bottom = top + bitmap->height();
2175
2176 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002177 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002178 }
2179
2180 mCaches.activeTexture(0);
2181 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2182 const AutoTexture autoCleanup(texture);
2183
Romain Guy886b2752013-01-04 12:26:18 -08002184 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2185 drawAlphaBitmap(texture, left, top, paint);
2186 } else {
2187 drawTextureRect(left, top, right, bottom, texture, paint);
2188 }
Chet Haase48659092012-05-31 15:21:51 -07002189
2190 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002191}
2192
Chet Haase48659092012-05-31 15:21:51 -07002193status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002194 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002195 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002196 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002197 }
2198
Chris Craik39a908c2013-06-13 14:39:01 -07002199 // TODO: use quickReject on bounds from vertices
2200 mCaches.enableScissor();
2201
Romain Guyb18d2d02011-02-10 15:52:54 -08002202 float left = FLT_MAX;
2203 float top = FLT_MAX;
2204 float right = FLT_MIN;
2205 float bottom = FLT_MIN;
2206
Romain Guya92bb4d2012-10-16 11:08:44 -07002207 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002208
Romain Guyff316ec2013-02-13 18:39:43 -08002209 ColorTextureVertex mesh[count];
2210 ColorTextureVertex* vertex = mesh;
2211
2212 bool cleanupColors = false;
2213 if (!colors) {
2214 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2215 colors = new int[colorsCount];
2216 memset(colors, 0xff, colorsCount * sizeof(int));
2217 cleanupColors = true;
2218 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002219
Romain Guy3b748a42013-04-17 18:54:38 -07002220 mCaches.activeTexture(0);
2221 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2222 const UvMapper& mapper(getMapper(texture));
2223
Romain Guy5a7b4662011-01-20 19:09:30 -08002224 for (int32_t y = 0; y < meshHeight; y++) {
2225 for (int32_t x = 0; x < meshWidth; x++) {
2226 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2227
2228 float u1 = float(x) / meshWidth;
2229 float u2 = float(x + 1) / meshWidth;
2230 float v1 = float(y) / meshHeight;
2231 float v2 = float(y + 1) / meshHeight;
2232
Romain Guy3b748a42013-04-17 18:54:38 -07002233 mapper.map(u1, v1, u2, v2);
2234
Romain Guy5a7b4662011-01-20 19:09:30 -08002235 int ax = i + (meshWidth + 1) * 2;
2236 int ay = ax + 1;
2237 int bx = i;
2238 int by = bx + 1;
2239 int cx = i + 2;
2240 int cy = cx + 1;
2241 int dx = i + (meshWidth + 1) * 2 + 2;
2242 int dy = dx + 1;
2243
Romain Guyff316ec2013-02-13 18:39:43 -08002244 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2245 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2246 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002247
Romain Guyff316ec2013-02-13 18:39:43 -08002248 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2249 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2250 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002251
Romain Guya92bb4d2012-10-16 11:08:44 -07002252 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2253 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2254 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2255 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002256 }
2257 }
2258
Romain Guya92bb4d2012-10-16 11:08:44 -07002259 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002260 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002261 return DrawGlInfo::kStatusDone;
2262 }
2263
Romain Guyff316ec2013-02-13 18:39:43 -08002264 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002265 texture = mCaches.textureCache.get(bitmap);
2266 if (!texture) {
2267 if (cleanupColors) delete[] colors;
2268 return DrawGlInfo::kStatusDone;
2269 }
Romain Guyff316ec2013-02-13 18:39:43 -08002270 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002271 const AutoTexture autoCleanup(texture);
2272
2273 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2274 texture->setFilter(FILTER(paint), true);
2275
2276 int alpha;
2277 SkXfermode::Mode mode;
2278 getAlphaAndMode(paint, &alpha, &mode);
2279
Romain Guyff316ec2013-02-13 18:39:43 -08002280 float a = alpha / 255.0f;
2281
Romain Guya92bb4d2012-10-16 11:08:44 -07002282 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002283 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002284 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002285
Romain Guyff316ec2013-02-13 18:39:43 -08002286 setupDraw();
2287 setupDrawWithTextureAndColor();
2288 setupDrawColor(a, a, a, a);
2289 setupDrawColorFilter();
2290 setupDrawBlending(true, mode, false);
2291 setupDrawProgram();
2292 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002293 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002294 setupDrawTexture(texture->id);
2295 setupDrawPureColorUniforms();
2296 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002297 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002298
2299 glDrawArrays(GL_TRIANGLES, 0, count);
2300
Romain Guyff316ec2013-02-13 18:39:43 -08002301 int slot = mCaches.currentProgram->getAttrib("colors");
2302 if (slot >= 0) {
2303 glDisableVertexAttribArray(slot);
2304 }
2305
2306 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002307
2308 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002309}
2310
Chet Haase48659092012-05-31 15:21:51 -07002311status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002312 float srcLeft, float srcTop, float srcRight, float srcBottom,
2313 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002314 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002315 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002316 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002317 }
2318
Romain Guya1d3c912011-12-13 14:55:06 -08002319 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002320 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002321 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002322 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002323
Romain Guy8ba548f2010-06-30 19:21:21 -07002324 const float width = texture->width;
2325 const float height = texture->height;
2326
Romain Guy3b748a42013-04-17 18:54:38 -07002327 float u1 = fmax(0.0f, srcLeft / width);
2328 float v1 = fmax(0.0f, srcTop / height);
2329 float u2 = fmin(1.0f, srcRight / width);
2330 float v2 = fmin(1.0f, srcBottom / height);
2331
2332 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002333
Romain Guy03750a02010-10-18 14:06:08 -07002334 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002335 resetDrawTextureTexCoords(u1, v1, u2, v2);
2336
Romain Guy03750a02010-10-18 14:06:08 -07002337 int alpha;
2338 SkXfermode::Mode mode;
2339 getAlphaAndMode(paint, &alpha, &mode);
2340
Romain Guyd21b6e12011-11-30 20:21:23 -08002341 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2342
Romain Guy886b2752013-01-04 12:26:18 -08002343 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2344 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002345
Romain Guy886b2752013-01-04 12:26:18 -08002346 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2347 // Apply a scale transform on the canvas only when a shader is in use
2348 // Skia handles the ratio between the dst and src rects as a scale factor
2349 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002350 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002351 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002352
Romain Guy3b753822013-03-05 10:27:35 -08002353 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2354 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2355 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002356
2357 dstRight = x + (dstRight - dstLeft);
2358 dstBottom = y + (dstBottom - dstTop);
2359
2360 dstLeft = x;
2361 dstTop = y;
2362
2363 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2364 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002365 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002366 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002367 }
2368
2369 if (CC_UNLIKELY(useScaleTransform)) {
2370 save(SkCanvas::kMatrix_SaveFlag);
2371 translate(dstLeft, dstTop);
2372 scale(scaleX, scaleY);
2373
2374 dstLeft = 0.0f;
2375 dstTop = 0.0f;
2376
2377 dstRight = srcRight - srcLeft;
2378 dstBottom = srcBottom - srcTop;
2379 }
2380
2381 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2382 int color = paint ? paint->getColor() : 0;
2383 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2384 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002385 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002386 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2387 } else {
2388 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2389 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002390 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002391 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2392 }
2393
2394 if (CC_UNLIKELY(useScaleTransform)) {
2395 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002396 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002397
2398 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002399
2400 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002401}
2402
Romain Guy3b748a42013-04-17 18:54:38 -07002403status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002404 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002405 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002406 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002407 }
2408
Romain Guy4c2547f2013-06-11 16:19:24 -07002409 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002410 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2411 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002412
Romain Guy03c00b52013-06-20 18:30:28 -07002413 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002414}
2415
Romain Guy03c00b52013-06-20 18:30:28 -07002416status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2417 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002418 if (quickReject(left, top, right, bottom)) {
2419 return DrawGlInfo::kStatusDone;
2420 }
2421
Romain Guy211370f2012-02-01 16:10:55 -08002422 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002423 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002424 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002425 if (!texture) return DrawGlInfo::kStatusDone;
2426 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002427
Romain Guya4adcf02013-02-28 12:15:35 -08002428 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2429 texture->setFilter(GL_LINEAR, true);
2430
Romain Guy03c00b52013-06-20 18:30:28 -07002431 int alpha;
2432 SkXfermode::Mode mode;
2433 getAlphaAndMode(paint, &alpha, &mode);
2434
Romain Guy3b753822013-03-05 10:27:35 -08002435 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002436 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002437 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002438 const float offsetX = left + currentTransform().getTranslateX();
2439 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002440 const size_t count = mesh->quads.size();
2441 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002442 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002443 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002444 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2445 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2446 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002447 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002448 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002449 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002450 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002451 }
2452 }
2453
Chris Craik4063a0e2013-11-15 16:06:56 -08002454 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002455 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002456 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2457 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002458
Romain Guy3b748a42013-04-17 18:54:38 -07002459 right = x + right - left;
2460 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002461 left = x;
2462 top = y;
2463 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002464 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002465 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2466 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2467 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2468 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002469 }
Chet Haase48659092012-05-31 15:21:51 -07002470
2471 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002472}
2473
Romain Guy03c00b52013-06-20 18:30:28 -07002474/**
2475 * Important note: this method is intended to draw batches of 9-patch objects and
2476 * will not set the scissor enable or dirty the current layer, if any.
2477 * The caller is responsible for properly dirtying the current layer.
2478 */
2479status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2480 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2481 mCaches.activeTexture(0);
2482 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2483 if (!texture) return DrawGlInfo::kStatusDone;
2484 const AutoTexture autoCleanup(texture);
2485
2486 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2487 texture->setFilter(GL_LINEAR, true);
2488
2489 int alpha;
2490 SkXfermode::Mode mode;
2491 getAlphaAndMode(paint, &alpha, &mode);
2492
2493 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002494 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002495 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002496
2497 return DrawGlInfo::kStatusDrew;
2498}
2499
Chris Craik65cd6122012-12-10 17:56:27 -08002500status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2501 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002502 // not missing call to quickReject/dirtyLayer, always done at a higher level
2503
Chris Craik6d29c8d2013-05-08 18:35:44 -07002504 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002505 // no vertices to draw
2506 return DrawGlInfo::kStatusDone;
2507 }
2508
Chris Craikcb4d6002012-09-25 12:00:29 -07002509 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002510 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2511 bool isAA = paint->isAntiAlias();
2512
Chris Craik710f46d2012-09-17 17:25:49 -07002513 setupDraw();
2514 setupDrawNoTexture();
2515 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002516 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2517 setupDrawColorFilter();
2518 setupDrawShader();
2519 setupDrawBlending(isAA, mode);
2520 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002521 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002522 setupDrawColorUniforms();
2523 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002524 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002525
Chris Craik710f46d2012-09-17 17:25:49 -07002526 void* vertices = vertexBuffer.getBuffer();
2527 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002528 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002529 mCaches.resetTexCoordsVertexPointer();
2530 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002531
Chris Craik710f46d2012-09-17 17:25:49 -07002532 int alphaSlot = -1;
2533 if (isAA) {
2534 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2535 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002536
Chris Craik710f46d2012-09-17 17:25:49 -07002537 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002538 glEnableVertexAttribArray(alphaSlot);
2539 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002540 }
Romain Guy04299382012-07-18 17:15:41 -07002541
Chris Craik6d29c8d2013-05-08 18:35:44 -07002542 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002543
Chris Craik710f46d2012-09-17 17:25:49 -07002544 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002545 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002546 }
Chris Craik65cd6122012-12-10 17:56:27 -08002547
2548 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002549}
2550
2551/**
Chris Craik65cd6122012-12-10 17:56:27 -08002552 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2553 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2554 * screen space in all directions. However, instead of using a fragment shader to compute the
2555 * translucency of the color from its position, we simply use a varying parameter to define how far
2556 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2557 *
2558 * Doesn't yet support joins, caps, or path effects.
2559 */
2560status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2561 VertexBuffer vertexBuffer;
2562 // TODO: try clipping large paths to viewport
2563 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2564
Romain Guyc46d07a2013-03-15 19:06:39 -07002565 if (hasLayer()) {
2566 SkRect bounds = path.getBounds();
2567 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2568 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2569 }
Chris Craik65cd6122012-12-10 17:56:27 -08002570
2571 return drawVertexBuffer(vertexBuffer, paint);
2572}
2573
2574/**
2575 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2576 * and additional geometry for defining an alpha slope perimeter.
2577 *
2578 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2579 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2580 * in-shader alpha region, but found it to be taxing on some GPUs.
2581 *
2582 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2583 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002584 */
Chet Haase48659092012-05-31 15:21:51 -07002585status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002586 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002587
Chris Craik65cd6122012-12-10 17:56:27 -08002588 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002589
Chris Craik65cd6122012-12-10 17:56:27 -08002590 VertexBuffer buffer;
2591 SkRect bounds;
2592 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002593
2594 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2595 return DrawGlInfo::kStatusDone;
2596 }
2597
Romain Guy3b753822013-03-05 10:27:35 -08002598 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002599
Chris Craik65cd6122012-12-10 17:56:27 -08002600 bool useOffset = !paint->isAntiAlias();
2601 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002602}
2603
Chet Haase48659092012-05-31 15:21:51 -07002604status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002605 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002606
Chris Craik6d29c8d2013-05-08 18:35:44 -07002607 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002608
Chris Craik6d29c8d2013-05-08 18:35:44 -07002609 VertexBuffer buffer;
2610 SkRect bounds;
2611 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002612
Chris Craik6d29c8d2013-05-08 18:35:44 -07002613 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2614 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002615 }
2616
Chris Craik6d29c8d2013-05-08 18:35:44 -07002617 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002618
Chris Craik6d29c8d2013-05-08 18:35:44 -07002619 bool useOffset = !paint->isAntiAlias();
2620 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002621}
2622
Chet Haase48659092012-05-31 15:21:51 -07002623status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002624 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002625 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002626
Romain Guyae88e5e2010-10-22 17:49:18 -07002627 Rect& clip(*mSnapshot->clipRect);
2628 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002629
Romain Guy3d58c032010-07-14 16:34:53 -07002630 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002631
2632 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002633}
Romain Guy9d5316e2010-06-24 19:30:36 -07002634
Chet Haase48659092012-05-31 15:21:51 -07002635status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2636 SkPaint* paint) {
2637 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002638 const AutoTexture autoCleanup(texture);
2639
2640 const float x = left + texture->left - texture->offset;
2641 const float y = top + texture->top - texture->offset;
2642
2643 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002644
2645 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002646}
2647
Chet Haase48659092012-05-31 15:21:51 -07002648status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002649 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002650 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2651 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002652 return DrawGlInfo::kStatusDone;
2653 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002654
Chris Craikcb4d6002012-09-25 12:00:29 -07002655 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002656 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002657 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002658 right - left, bottom - top, rx, ry, p);
2659 return drawShape(left, top, texture, p);
2660 }
2661
2662 SkPath path;
2663 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002664 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2665 float outset = p->getStrokeWidth() / 2;
2666 rect.outset(outset, outset);
2667 rx += outset;
2668 ry += outset;
2669 }
Chris Craik710f46d2012-09-17 17:25:49 -07002670 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002671 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002672}
2673
Chris Craik710f46d2012-09-17 17:25:49 -07002674status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002675 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002676 x + radius, y + radius, p) ||
2677 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002678 return DrawGlInfo::kStatusDone;
2679 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002680 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002681 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002682 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002683 return drawShape(x - radius, y - radius, texture, p);
2684 }
2685
2686 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002687 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2688 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2689 } else {
2690 path.addCircle(x, y, radius);
2691 }
Chris Craik65cd6122012-12-10 17:56:27 -08002692 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002693}
Romain Guy01d58e42011-01-19 21:54:02 -08002694
Chet Haase48659092012-05-31 15:21:51 -07002695status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002696 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002697 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2698 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002699 return DrawGlInfo::kStatusDone;
2700 }
Romain Guy01d58e42011-01-19 21:54:02 -08002701
Chris Craikcb4d6002012-09-25 12:00:29 -07002702 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002703 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002704 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002705 return drawShape(left, top, texture, p);
2706 }
2707
2708 SkPath path;
2709 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002710 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2711 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2712 }
Chris Craik710f46d2012-09-17 17:25:49 -07002713 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002714 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002715}
2716
Chet Haase48659092012-05-31 15:21:51 -07002717status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002718 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002719 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2720 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002721 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002722 }
2723
Chris Craik780c1282012-10-04 14:10:49 -07002724 if (fabs(sweepAngle) >= 360.0f) {
2725 return drawOval(left, top, right, bottom, p);
2726 }
2727
2728 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002729 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002730 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002731 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002732 startAngle, sweepAngle, useCenter, p);
2733 return drawShape(left, top, texture, p);
2734 }
2735
2736 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2737 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2738 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2739 }
2740
2741 SkPath path;
2742 if (useCenter) {
2743 path.moveTo(rect.centerX(), rect.centerY());
2744 }
2745 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2746 if (useCenter) {
2747 path.close();
2748 }
Chris Craik65cd6122012-12-10 17:56:27 -08002749 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002750}
2751
Romain Guycf8675e2012-10-02 12:32:25 -07002752// See SkPaintDefaults.h
2753#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2754
Chet Haase48659092012-05-31 15:21:51 -07002755status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002756 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2757 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002758 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002759 }
2760
Chris Craik710f46d2012-09-17 17:25:49 -07002761 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002762 // only fill style is supported by drawConvexPath, since others have to handle joins
2763 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2764 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2765 mCaches.activeTexture(0);
2766 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002767 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002768 return drawShape(left, top, texture, p);
2769 }
2770
2771 SkPath path;
2772 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2773 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2774 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2775 }
2776 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002777 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002778 }
2779
Romain Guy3b753822013-03-05 10:27:35 -08002780 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002781 SkPath path;
2782 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002783 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002784 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002785 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002786 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002787 }
Romain Guyc7d53492010-06-25 13:41:57 -07002788}
Romain Guy9d5316e2010-06-24 19:30:36 -07002789
Raph Levien416a8472012-07-19 22:48:17 -07002790void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2791 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2792 float x, float y) {
2793 mCaches.activeTexture(0);
2794
2795 // NOTE: The drop shadow will not perform gamma correction
2796 // if shader-based correction is enabled
2797 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2798 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002799 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002800 // If the drop shadow exceeds the max texture size or couldn't be
2801 // allocated, skip drawing
2802 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002803 const AutoTexture autoCleanup(shadow);
2804
Chris Craikc3566d02013-02-04 16:16:33 -08002805 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2806 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002807
Chris Craikc3566d02013-02-04 16:16:33 -08002808 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2809 int shadowColor = mDrawModifiers.mShadowColor;
2810 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002811 shadowColor = 0xffffffff;
2812 }
2813
2814 setupDraw();
2815 setupDrawWithTexture(true);
2816 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2817 setupDrawColorFilter();
2818 setupDrawShader();
2819 setupDrawBlending(true, mode);
2820 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002821 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2822 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002823 setupDrawTexture(shadow->id);
2824 setupDrawPureColorUniforms();
2825 setupDrawColorFilterUniforms();
2826 setupDrawShaderUniforms();
2827 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2828
2829 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2830}
2831
Romain Guy768bffc2013-02-27 13:50:45 -08002832bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2833 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2834 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2835}
2836
Chet Haase48659092012-05-31 15:21:51 -07002837status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002838 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002839 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002840 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002841 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002842
Romain Guy671d6cf2012-01-18 12:39:17 -08002843 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002844 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002845 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002846 }
2847
Chris Craik39a908c2013-06-13 14:39:01 -07002848 mCaches.enableScissor();
2849
Romain Guy671d6cf2012-01-18 12:39:17 -08002850 float x = 0.0f;
2851 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002852 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002853 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002854 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2855 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002856 }
2857
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002858 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002859 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002860
2861 int alpha;
2862 SkXfermode::Mode mode;
2863 getAlphaAndMode(paint, &alpha, &mode);
2864
Chris Craikc3566d02013-02-04 16:16:33 -08002865 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002866 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2867 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002868 }
2869
Romain Guy671d6cf2012-01-18 12:39:17 -08002870 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002871 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002872 if (pureTranslate && !linearFilter) {
2873 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2874 }
Romain Guy257ae352013-03-20 16:31:12 -07002875 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002876
2877 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2878 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2879
Romain Guy211370f2012-02-01 16:10:55 -08002880 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002881
Victoria Lease1e546812013-06-25 14:25:17 -07002882 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002883 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002884 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002885 if (hasActiveLayer) {
2886 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002887 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002888 }
2889 dirtyLayerUnchecked(bounds, getRegion());
2890 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002891 }
Chet Haase48659092012-05-31 15:21:51 -07002892
2893 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002894}
2895
Romain Guy624234f2013-03-05 16:43:31 -08002896mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2897 mat4 fontTransform;
2898 if (CC_LIKELY(transform.isPureTranslate())) {
2899 fontTransform = mat4::identity();
2900 } else {
2901 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002902 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002903 } else {
2904 float sx, sy;
2905 currentTransform().decomposeScale(sx, sy);
2906 fontTransform.loadScale(sx, sy, 1.0f);
2907 }
2908 }
2909 return fontTransform;
2910}
2911
Chris Craik41541822013-05-03 16:35:54 -07002912status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2913 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002914 DrawOpMode drawOpMode) {
2915
Chris Craik527a3aa2013-03-04 10:19:31 -08002916 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002917 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2918 // drawing as ops from DeferredDisplayList are already filtered for these
2919 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2920 quickReject(bounds)) {
2921 return DrawGlInfo::kStatusDone;
2922 }
Romain Guycac5fd32011-12-01 20:08:50 -08002923 }
2924
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002925 const float oldX = x;
2926 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002927
2928 const mat4& transform = currentTransform();
2929 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002930
Romain Guy211370f2012-02-01 16:10:55 -08002931 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002932 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2933 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002934 }
2935
Romain Guy86568192010-12-14 15:55:39 -08002936 int alpha;
2937 SkXfermode::Mode mode;
2938 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002939
Romain Guyc74f45a2013-02-26 19:10:14 -08002940 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2941
Chris Craikc3566d02013-02-04 16:16:33 -08002942 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002943 fontRenderer.setFont(paint, mat4::identity());
2944 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2945 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002946 }
2947
Romain Guy19d4dd82013-03-04 11:14:26 -08002948 const bool hasActiveLayer = hasLayer();
2949
Romain Guy624234f2013-03-05 16:43:31 -08002950 // We only pass a partial transform to the font renderer. That partial
2951 // matrix defines how glyphs are rasterized. Typically we want glyphs
2952 // to be rasterized at their final size on screen, which means the partial
2953 // matrix needs to take the scale factor into account.
2954 // When a partial matrix is used to transform glyphs during rasterization,
2955 // the mesh is generated with the inverse transform (in the case of scale,
2956 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2957 // apply the full transform matrix at draw time in the vertex shader.
2958 // Applying the full matrix in the shader is the easiest way to handle
2959 // rotation and perspective and allows us to always generated quads in the
2960 // font renderer which greatly simplifies the code, clipping in particular.
2961 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002962 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002963
Romain Guy6620c6d2010-12-06 18:07:02 -08002964 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002965 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002966 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002967
Romain Guy624234f2013-03-05 16:43:31 -08002968 // TODO: Implement better clipping for scaled/rotated text
2969 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002970 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 -07002971
Raph Levien996e57c2012-07-23 15:22:52 -07002972 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002973 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002974
2975 // don't call issuedrawcommand, do it at end of batch
2976 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002977 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002978 SkPaint paintCopy(*paint);
2979 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2980 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002981 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002982 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002983 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002984 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002985 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002986
Chris Craik527a3aa2013-03-04 10:19:31 -08002987 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002988 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002989 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002990 }
Chris Craik41541822013-05-03 16:35:54 -07002991 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002992 }
Romain Guy694b5192010-07-21 21:33:20 -07002993
Chris Craike63f7c622013-10-17 10:30:55 -07002994 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002995
2996 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002997}
2998
Chet Haase48659092012-05-31 15:21:51 -07002999status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003000 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003001 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003002 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003003 }
3004
Chris Craik39a908c2013-06-13 14:39:01 -07003005 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3006 mCaches.enableScissor();
3007
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003008 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003009 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003010 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003011
3012 int alpha;
3013 SkXfermode::Mode mode;
3014 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003015 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003016
Romain Guy97771732012-02-28 18:17:02 -08003017 const Rect* clip = &mSnapshot->getLocalClip();
3018 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 -08003019
Romain Guy97771732012-02-28 18:17:02 -08003020 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003021
3022 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003023 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003024 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003025 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003026 dirtyLayerUnchecked(bounds, getRegion());
3027 }
Romain Guy97771732012-02-28 18:17:02 -08003028 }
Chet Haase48659092012-05-31 15:21:51 -07003029
3030 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003031}
3032
Chet Haase48659092012-05-31 15:21:51 -07003033status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3034 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003035
Romain Guya1d3c912011-12-13 14:55:06 -08003036 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003037
Romain Guyfb8b7632010-08-23 21:05:08 -07003038 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003039 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003040 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003041
Romain Guy8b55f372010-08-18 17:10:07 -07003042 const float x = texture->left - texture->offset;
3043 const float y = texture->top - texture->offset;
3044
Romain Guy01d58e42011-01-19 21:54:02 -08003045 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003046
3047 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003048}
3049
Chris Craika08f95c2013-03-15 17:24:33 -07003050status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003051 if (!layer) {
3052 return DrawGlInfo::kStatusDone;
3053 }
3054
Romain Guyb2e2f242012-10-17 18:18:35 -07003055 mat4* transform = NULL;
3056 if (layer->isTextureLayer()) {
3057 transform = &layer->getTransform();
3058 if (!transform->isIdentity()) {
3059 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003060 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003061 }
3062 }
3063
Chris Craik39a908c2013-06-13 14:39:01 -07003064 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003065 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003066 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003067
3068 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003069 if (transform && !transform->isIdentity()) {
3070 restore();
3071 }
Chet Haase48659092012-05-31 15:21:51 -07003072 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003073 }
3074
Romain Guy5bb3c732012-11-29 17:52:58 -08003075 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003076
Chris Craik39a908c2013-06-13 14:39:01 -07003077 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003078 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003079
Romain Guy211370f2012-02-01 16:10:55 -08003080 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003081 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3082 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003083
Romain Guyc88e3572011-01-22 00:32:12 -08003084 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003085 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3086 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003087 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003088 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003089 setupDraw();
3090 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003091 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003092 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003093 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003094 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003095 setupDrawPureColorUniforms();
3096 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003097 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003098 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3099 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3100 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003101
Romain Guyd21b6e12011-11-30 20:21:23 -08003102 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003103 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003104 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003105 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003106 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003107 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003108 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3109 }
Romain Guyf219da52011-01-16 12:54:25 -08003110
Romain Guy448455f2013-07-22 13:57:50 -07003111 TextureVertex* mesh = &layer->mesh[0];
3112 GLsizei elementsCount = layer->meshElementCount;
3113
3114 while (elementsCount > 0) {
3115 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3116
Romain Guy3380cfd2013-08-15 16:57:57 -07003117 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003118 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3119 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3120
3121 elementsCount -= drawCount;
3122 // Though there are 4 vertices in a quad, we use 6 indices per
3123 // quad to draw with GL_TRIANGLES
3124 mesh += (drawCount / 6) * 4;
3125 }
Romain Guyf219da52011-01-16 12:54:25 -08003126
Romain Guy3a3133d2011-02-01 22:59:58 -08003127#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003128 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003129#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003130 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003131
Chris Craikc3566d02013-02-04 16:16:33 -08003132 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003133
Romain Guy5bb3c732012-11-29 17:52:58 -08003134 if (layer->debugDrawUpdate) {
3135 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003136 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3137 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3138 }
Romain Guyf219da52011-01-16 12:54:25 -08003139 }
Chris Craik34416ea2013-04-15 16:08:28 -07003140 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003141
Romain Guyb2e2f242012-10-17 18:18:35 -07003142 if (transform && !transform->isIdentity()) {
3143 restore();
3144 }
3145
Chet Haase48659092012-05-31 15:21:51 -07003146 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003147}
3148
Romain Guy6926c722010-07-12 20:20:03 -07003149///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003150// Shaders
3151///////////////////////////////////////////////////////////////////////////////
3152
3153void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003154 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003155}
3156
Romain Guy06f96e22010-07-30 19:18:16 -07003157void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003158 mDrawModifiers.mShader = shader;
3159 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003160 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003161 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003162}
3163
Romain Guyd27977d2010-07-14 19:18:51 -07003164///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003165// Color filters
3166///////////////////////////////////////////////////////////////////////////////
3167
3168void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003169 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003170}
3171
3172void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003173 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003174}
3175
3176///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003177// Drop shadow
3178///////////////////////////////////////////////////////////////////////////////
3179
3180void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003181 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003182}
3183
3184void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003185 mDrawModifiers.mHasShadow = true;
3186 mDrawModifiers.mShadowRadius = radius;
3187 mDrawModifiers.mShadowDx = dx;
3188 mDrawModifiers.mShadowDy = dy;
3189 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003190}
3191
3192///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003193// Draw filters
3194///////////////////////////////////////////////////////////////////////////////
3195
3196void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003197 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3198 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003199 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003200 mDrawModifiers.mPaintFilterClearBits = 0;
3201 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003202}
3203
3204void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003205 mDrawModifiers.mHasDrawFilter = true;
3206 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3207 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003208}
3209
Chris Craika08f95c2013-03-15 17:24:33 -07003210SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003211 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003212 return paint;
3213 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003214
3215 uint32_t flags = paint->getFlags();
3216
3217 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003218 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3219 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003220
3221 return &mFilteredPaint;
3222}
3223
3224///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003225// Drawing implementation
3226///////////////////////////////////////////////////////////////////////////////
3227
Romain Guy3b748a42013-04-17 18:54:38 -07003228Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3229 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3230 if (!texture) {
3231 return mCaches.textureCache.get(bitmap);
3232 }
3233 return texture;
3234}
3235
Romain Guy01d58e42011-01-19 21:54:02 -08003236void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3237 float x, float y, SkPaint* paint) {
3238 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3239 return;
3240 }
3241
3242 int alpha;
3243 SkXfermode::Mode mode;
3244 getAlphaAndMode(paint, &alpha, &mode);
3245
3246 setupDraw();
3247 setupDrawWithTexture(true);
3248 setupDrawAlpha8Color(paint->getColor(), alpha);
3249 setupDrawColorFilter();
3250 setupDrawShader();
3251 setupDrawBlending(true, mode);
3252 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003253 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3254 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003255 setupDrawTexture(texture->id);
3256 setupDrawPureColorUniforms();
3257 setupDrawColorFilterUniforms();
3258 setupDrawShaderUniforms();
3259 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3260
3261 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003262}
3263
Romain Guyf607bdc2010-09-10 19:20:06 -07003264// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003265#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3266#define kStdUnderline_Offset (1.0f / 9.0f)
3267#define kStdUnderline_Thickness (1.0f / 18.0f)
3268
Chris Craike63f7c622013-10-17 10:30:55 -07003269void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003270 // Handle underline and strike-through
3271 uint32_t flags = paint->getFlags();
3272 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003273 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003274
Romain Guy211370f2012-02-01 16:10:55 -08003275 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003276 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003277 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003278
Raph Levien8b4072d2012-07-30 15:50:00 -07003279 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003280 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003281
Romain Guyf6834472011-01-23 13:32:12 -08003282 int linesCount = 0;
3283 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3284 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3285
3286 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003287 float points[pointsCount];
3288 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003289
3290 if (flags & SkPaint::kUnderlineText_Flag) {
3291 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003292 points[currentPoint++] = left;
3293 points[currentPoint++] = top;
3294 points[currentPoint++] = left + underlineWidth;
3295 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003296 }
3297
3298 if (flags & SkPaint::kStrikeThruText_Flag) {
3299 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003300 points[currentPoint++] = left;
3301 points[currentPoint++] = top;
3302 points[currentPoint++] = left + underlineWidth;
3303 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003304 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003305
Romain Guy726aeba2011-06-01 14:52:00 -07003306 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003307
Romain Guy726aeba2011-06-01 14:52:00 -07003308 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003309 }
3310 }
3311}
3312
Romain Guy672433d2013-01-04 19:05:13 -08003313status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3314 if (mSnapshot->isIgnored()) {
3315 return DrawGlInfo::kStatusDone;
3316 }
3317
Romain Guy735738c2012-12-03 12:34:51 -08003318 int color = paint->getColor();
3319 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003320 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003321 color |= 0x00ffffff;
3322 }
3323 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3324
3325 return drawColorRects(rects, count, color, mode);
3326}
3327
3328status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003329 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003330 if (count == 0) {
3331 return DrawGlInfo::kStatusDone;
3332 }
Romain Guy735738c2012-12-03 12:34:51 -08003333
Romain Guy672433d2013-01-04 19:05:13 -08003334 float left = FLT_MAX;
3335 float top = FLT_MAX;
3336 float right = FLT_MIN;
3337 float bottom = FLT_MIN;
3338
Romain Guy448455f2013-07-22 13:57:50 -07003339 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003340 Vertex* vertex = mesh;
3341
Chris Craik2af46352012-11-26 18:30:17 -08003342 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003343 float l = rects[index + 0];
3344 float t = rects[index + 1];
3345 float r = rects[index + 2];
3346 float b = rects[index + 3];
3347
Romain Guy3b753822013-03-05 10:27:35 -08003348 Vertex::set(vertex++, l, t);
3349 Vertex::set(vertex++, r, t);
3350 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003351 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003352
Romain Guy3b753822013-03-05 10:27:35 -08003353 left = fminf(left, l);
3354 top = fminf(top, t);
3355 right = fmaxf(right, r);
3356 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003357 }
3358
Romain Guy3b753822013-03-05 10:27:35 -08003359 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003360 return DrawGlInfo::kStatusDone;
3361 }
Romain Guy672433d2013-01-04 19:05:13 -08003362
Romain Guy672433d2013-01-04 19:05:13 -08003363 setupDraw();
3364 setupDrawNoTexture();
3365 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3366 setupDrawShader();
3367 setupDrawColorFilter();
3368 setupDrawBlending(mode);
3369 setupDrawProgram();
3370 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003371 setupDrawModelView(kModelViewMode_Translate, false,
3372 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003373 setupDrawColorUniforms();
3374 setupDrawShaderUniforms();
3375 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003376
Romain Guy8ce00302013-01-15 18:51:42 -08003377 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003378 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003379 }
3380
Chris Craik4063a0e2013-11-15 16:06:56 -08003381 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003382
3383 return DrawGlInfo::kStatusDrew;
3384}
3385
Romain Guy026c5e162010-06-28 17:12:22 -07003386void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003387 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003388 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003389 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003390 color |= 0x00ffffff;
3391 }
3392
Romain Guy70ca14e2010-12-13 18:24:33 -08003393 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003394 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003395 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003396 setupDrawShader();
3397 setupDrawColorFilter();
3398 setupDrawBlending(mode);
3399 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003400 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3401 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003402 setupDrawColorUniforms();
3403 setupDrawShaderUniforms(ignoreTransform);
3404 setupDrawColorFilterUniforms();
3405 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003406
Romain Guyc95c8d62010-09-17 15:31:32 -07003407 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3408}
3409
Romain Guy82ba8142010-07-09 13:25:56 -07003410void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003411 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003412 int alpha;
3413 SkXfermode::Mode mode;
3414 getAlphaAndMode(paint, &alpha, &mode);
3415
Romain Guyd21b6e12011-11-30 20:21:23 -08003416 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003417
Romain Guy3b748a42013-04-17 18:54:38 -07003418 GLvoid* vertices = (GLvoid*) NULL;
3419 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3420
3421 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003422 vertices = &mMeshVertices[0].x;
3423 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003424
3425 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3426 texture->uvMapper->map(uvs);
3427
3428 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3429 }
3430
Romain Guy3b753822013-03-05 10:27:35 -08003431 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3432 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3433 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003434
Romain Guyd21b6e12011-11-30 20:21:23 -08003435 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003436 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003437 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3438 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003439 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003440 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003441 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003442 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3443 }
3444
3445 if (texture->uvMapper) {
3446 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003447 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003448}
3449
Romain Guybd6b79b2010-06-26 00:13:53 -07003450void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003451 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3452 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003453 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003454}
3455
3456void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003457 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003458 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003459 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3460 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003461
Romain Guy746b7402010-10-26 16:27:31 -07003462 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003463 setupDrawWithTexture();
3464 setupDrawColor(alpha, alpha, alpha, alpha);
3465 setupDrawColorFilter();
3466 setupDrawBlending(blend, mode, swapSrcDst);
3467 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003468 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003469 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003470 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003471 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003472 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003473 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003474
Romain Guy6820ac82010-09-15 18:11:50 -07003475 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003476}
3477
Romain Guy3b748a42013-04-17 18:54:38 -07003478void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3479 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3480 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003481 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3482 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003483
3484 setupDraw();
3485 setupDrawWithTexture();
3486 setupDrawColor(alpha, alpha, alpha, alpha);
3487 setupDrawColorFilter();
3488 setupDrawBlending(blend, mode, swapSrcDst);
3489 setupDrawProgram();
3490 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003491 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003492 setupDrawTexture(texture);
3493 setupDrawPureColorUniforms();
3494 setupDrawColorFilterUniforms();
3495 setupDrawMeshIndices(vertices, texCoords, vbo);
3496
3497 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003498}
3499
Romain Guy886b2752013-01-04 12:26:18 -08003500void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3501 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3502 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003503 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003504
3505 setupDraw();
3506 setupDrawWithTexture(true);
3507 if (hasColor) {
3508 setupDrawAlpha8Color(color, alpha);
3509 }
3510 setupDrawColorFilter();
3511 setupDrawShader();
3512 setupDrawBlending(true, mode);
3513 setupDrawProgram();
3514 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003515 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003516 setupDrawTexture(texture);
3517 setupDrawPureColorUniforms();
3518 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003519 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003520 setupDrawMesh(vertices, texCoords);
3521
3522 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003523}
3524
Romain Guya5aed0d2010-09-09 14:42:43 -07003525void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003526 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003527 if (mCountOverdraw) {
3528 if (!mCaches.blend) glEnable(GL_BLEND);
3529 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3530 glBlendFunc(GL_ONE, GL_ONE);
3531 }
3532
3533 mCaches.blend = true;
3534 mCaches.lastSrcMode = GL_ONE;
3535 mCaches.lastDstMode = GL_ONE;
3536
3537 return;
3538 }
3539
Romain Guy82ba8142010-07-09 13:25:56 -07003540 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003541
Romain Guy82ba8142010-07-09 13:25:56 -07003542 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003543 // These blend modes are not supported by OpenGL directly and have
3544 // to be implemented using shaders. Since the shader will perform
3545 // the blending, turn blending off here
3546 // If the blend mode cannot be implemented using shaders, fall
3547 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003548 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003549 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003550 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003551 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003552
Romain Guy82bc7a72012-01-03 14:13:39 -08003553 if (mCaches.blend) {
3554 glDisable(GL_BLEND);
3555 mCaches.blend = false;
3556 }
3557
3558 return;
3559 } else {
3560 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003561 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003562 }
3563
3564 if (!mCaches.blend) {
3565 glEnable(GL_BLEND);
3566 }
3567
3568 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3569 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3570
3571 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3572 glBlendFunc(sourceMode, destMode);
3573 mCaches.lastSrcMode = sourceMode;
3574 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003575 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003576 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003577 glDisable(GL_BLEND);
3578 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003579 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003580}
3581
Romain Guy889f8d12010-07-29 14:37:42 -07003582bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003583 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003584 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003585 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003586 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003587 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003588 }
Romain Guy6926c722010-07-12 20:20:03 -07003589 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003590}
3591
Romain Guy026c5e162010-06-28 17:12:22 -07003592void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003593 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003594 TextureVertex::setUV(v++, u1, v1);
3595 TextureVertex::setUV(v++, u2, v1);
3596 TextureVertex::setUV(v++, u1, v2);
3597 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003598}
3599
Chris Craik16ecda52013-03-29 10:59:59 -07003600void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003601 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003602 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3603 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003604 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003605 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003606 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003607}
3608
Chris Craik16ecda52013-03-29 10:59:59 -07003609float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3610 float alpha;
3611 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3612 alpha = mDrawModifiers.mOverrideLayerAlpha;
3613 } else {
3614 alpha = layer->getAlpha() / 255.0f;
3615 }
3616 return alpha * mSnapshot->alpha;
3617}
3618
Romain Guy9d5316e2010-06-24 19:30:36 -07003619}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003620}; // namespace android