blob: f81b4ffe35aba84919650782ab5de9bdc78fbdcf [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 Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.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///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800115 // *set* draw modifiers to be 0
116 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700117 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700118
Romain Guyac670c02010-07-27 17:39:27 -0700119 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
120
Romain Guyae5575b2010-07-29 18:48:04 -0700121 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700122 mFrameStarted = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700123
124 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700125}
126
Romain Guy85bf02f2010-06-22 13:11:24 -0700127OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700128 // The context has already been destroyed at this point, do not call
129 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700130}
131
Romain Guy87e2f7572012-09-24 11:37:12 -0700132void OpenGLRenderer::initProperties() {
133 char property[PROPERTY_VALUE_MAX];
134 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
135 mScissorOptimizationDisabled = !strcasecmp(property, "true");
136 INIT_LOGD(" Scissor optimization %s",
137 mScissorOptimizationDisabled ? "disabled" : "enabled");
138 } else {
139 INIT_LOGD(" Scissor optimization enabled");
140 }
Romain Guy13631f32012-01-30 17:41:55 -0800141}
142
143///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700144// Setup
145///////////////////////////////////////////////////////////////////////////////
146
Romain Guyef359272013-01-31 19:07:29 -0800147void OpenGLRenderer::setName(const char* name) {
148 if (name) {
149 mName.setTo(name);
150 } else {
151 mName.clear();
152 }
153}
154
155const char* OpenGLRenderer::getName() const {
156 return mName.string();
157}
158
Romain Guy49c5fc02012-05-15 11:10:01 -0700159bool OpenGLRenderer::isDeferred() {
160 return false;
161}
162
Romain Guy85bf02f2010-06-22 13:11:24 -0700163void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700164 initViewport(width, height);
165
166 glDisable(GL_DITHER);
167 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
168
169 glEnableVertexAttribArray(Program::kBindingPosition);
170}
171
172void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700173 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700174
175 mWidth = width;
176 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700177
178 mFirstSnapshot->height = height;
179 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700180}
181
Romain Guy96885eb2013-03-26 15:05:58 -0700182void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800183 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800184 mCaches.clearGarbage();
185
Romain Guy96885eb2013-03-26 15:05:58 -0700186 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700187 mSnapshot = new Snapshot(mFirstSnapshot,
188 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800189 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700190 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700191
Romain Guy7d7b5492011-01-24 16:33:45 -0800192 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700193 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700194}
195
196status_t OpenGLRenderer::startFrame() {
197 if (mFrameStarted) return DrawGlInfo::kStatusDone;
198 mFrameStarted = true;
199
Romain Guy41308e22012-10-22 20:02:43 -0700200 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700201
Romain Guy96885eb2013-03-26 15:05:58 -0700202 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700203
Romain Guy96885eb2013-03-26 15:05:58 -0700204 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800205
Romain Guy54c1a642012-09-27 17:55:46 -0700206 // Functors break the tiling extension in pretty spectacular ways
207 // This ensures we don't use tiling when a functor is going to be
208 // invoked during the frame
209 mSuppressTiling = mCaches.hasRegisteredFunctors();
210
Chris Craik5f803622013-03-21 14:39:04 -0700211 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700212
Romain Guy7c450aa2012-09-21 19:15:00 -0700213 debugOverdraw(true, true);
214
Romain Guy96885eb2013-03-26 15:05:58 -0700215 return clear(mTilingClip.left, mTilingClip.top,
216 mTilingClip.right, mTilingClip.bottom, mOpaque);
217}
218
219status_t OpenGLRenderer::prepare(bool opaque) {
220 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
221}
222
223status_t OpenGLRenderer::prepareDirty(float left, float top,
224 float right, float bottom, bool opaque) {
225 setupFrameState(left, top, right, bottom, opaque);
226
227 // Layer renderers will start the frame immediately
228 // The framebuffer renderer will first defer the display list
229 // for each layer and wait until the first drawing command
230 // to start the frame
231 if (mSnapshot->fbo == 0) {
232 syncState();
233 updateLayers();
234 } else {
235 return startFrame();
236 }
237
238 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700239}
240
Romain Guydcfc8362013-01-03 13:08:57 -0800241void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
242 // If we know that we are going to redraw the entire framebuffer,
243 // perform a discard to let the driver know we don't need to preserve
244 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800245 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800246 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800247 const bool isFbo = getTargetFbo() == 0;
248 const GLenum attachments[] = {
249 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
250 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800251 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
252 }
253}
254
Romain Guy7c25aab2012-10-18 15:05:02 -0700255status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700256 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700257 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700258 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700259 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700260 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700261 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700262
Romain Guy7c25aab2012-10-18 15:05:02 -0700263 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700264 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700265}
266
267void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700268 if (mCaches.blend) {
269 glEnable(GL_BLEND);
270 } else {
271 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700272 }
Romain Guybb9524b2010-06-22 18:56:38 -0700273}
274
Romain Guy57b52682012-09-20 17:38:46 -0700275void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700276 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700277 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800278 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700279 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700280 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700281
Romain Guyc3fedaf2013-01-29 17:26:25 -0800282 startTiling(*clip, s->height, opaque);
283 }
284}
285
286void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
287 if (!mSuppressTiling) {
288 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800289 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700290 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700291}
292
293void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700294 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700295}
296
Romain Guyb025b9c2010-09-16 14:16:48 -0700297void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700298 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700299 endTiling();
300
Romain Guyca89e2a2013-03-08 17:44:20 -0800301 // When finish() is invoked on FBO 0 we've reached the end
302 // of the current frame
303 if (getTargetFbo() == 0) {
304 mCaches.pathCache.trim();
305 }
306
Romain Guy11cb6422012-09-21 00:39:43 -0700307 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700308#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700309 GLenum status = GL_NO_ERROR;
310 while ((status = glGetError()) != GL_NO_ERROR) {
311 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
312 switch (status) {
313 case GL_INVALID_ENUM:
314 ALOGE(" GL_INVALID_ENUM");
315 break;
316 case GL_INVALID_VALUE:
317 ALOGE(" GL_INVALID_VALUE");
318 break;
319 case GL_INVALID_OPERATION:
320 ALOGE(" GL_INVALID_OPERATION");
321 break;
322 case GL_OUT_OF_MEMORY:
323 ALOGE(" Out of memory!");
324 break;
325 }
Romain Guya07105b2011-01-10 21:14:18 -0800326 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700327#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700328
Romain Guyc15008e2010-11-10 11:59:15 -0800329#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800330 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700331#else
332 if (mCaches.getDebugLevel() & kDebugMemory) {
333 mCaches.dumpMemoryUsage();
334 }
Romain Guyc15008e2010-11-10 11:59:15 -0800335#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700336 }
Romain Guy96885eb2013-03-26 15:05:58 -0700337
338 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700339}
340
Romain Guy6c319ca2011-01-11 14:29:25 -0800341void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700342 if (mCaches.currentProgram) {
343 if (mCaches.currentProgram->isInUse()) {
344 mCaches.currentProgram->remove();
345 mCaches.currentProgram = NULL;
346 }
347 }
Romain Guy50c0f092010-10-19 11:42:22 -0700348 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800349 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800350 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800351 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700352 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700353}
354
Romain Guy6c319ca2011-01-11 14:29:25 -0800355void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800356 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800357 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700358 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700359 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700360
Romain Guy3e263fa2011-12-12 16:47:48 -0800361 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
362
Chet Haase80250612012-08-15 13:46:54 -0700363 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700364 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800365 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700366 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700367
Romain Guya1d3c912011-12-13 14:55:06 -0800368 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700369
Romain Guy50c0f092010-10-19 11:42:22 -0700370 mCaches.blend = true;
371 glEnable(GL_BLEND);
372 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
373 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700374}
375
Romain Guy35643dd2012-09-18 15:40:58 -0700376void OpenGLRenderer::resumeAfterLayer() {
377 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
378 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
379 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700380 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700381
382 mCaches.resetScissor();
383 dirtyClip();
384}
385
Romain Guyba6be8a2012-04-23 18:22:09 -0700386void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700387 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700388}
389
390void OpenGLRenderer::attachFunctor(Functor* functor) {
391 mFunctors.add(functor);
392}
393
Romain Guy8f3b8e32012-03-27 16:33:45 -0700394status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
395 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700396 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700397
Romain Guyba6be8a2012-04-23 18:22:09 -0700398 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800399 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700400 SortedVector<Functor*> functors(mFunctors);
401 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403 DrawGlInfo info;
404 info.clipLeft = 0;
405 info.clipTop = 0;
406 info.clipRight = 0;
407 info.clipBottom = 0;
408 info.isLayer = false;
409 info.width = 0;
410 info.height = 0;
411 memset(info.transform, 0, sizeof(float) * 16);
412
413 for (size_t i = 0; i < count; i++) {
414 Functor* f = functors.itemAt(i);
415 result |= (*f)(DrawGlInfo::kModeProcess, &info);
416
Chris Craikc2c95432012-04-25 15:13:52 -0700417 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700418 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
419 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700420 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700421
Chris Craikc2c95432012-04-25 15:13:52 -0700422 if (result & DrawGlInfo::kStatusInvoke) {
423 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700424 }
425 }
Chris Craikd15321b2012-11-28 14:45:04 -0800426 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700427 }
428
429 return result;
430}
431
432status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700433 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
434
Chet Haasedaf98e92011-01-10 14:10:36 -0800435 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700436 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700437
Romain Guy8a4ac612012-07-17 17:32:48 -0700438 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800439 if (mDirtyClip) {
440 setScissorFromClip();
441 }
Romain Guyd643bb52011-03-01 14:55:21 -0800442
Romain Guy80911b82011-03-16 15:30:12 -0700443 Rect clip(*mSnapshot->clipRect);
444 clip.snapToPixelBoundaries();
445
Romain Guyd643bb52011-03-01 14:55:21 -0800446 // Since we don't know what the functor will draw, let's dirty
447 // tne entire clip region
448 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800449 dirtyLayerUnchecked(clip, getRegion());
450 }
Romain Guyd643bb52011-03-01 14:55:21 -0800451
Romain Guy08aa2cb2011-03-17 11:06:57 -0700452 DrawGlInfo info;
453 info.clipLeft = clip.left;
454 info.clipTop = clip.top;
455 info.clipRight = clip.right;
456 info.clipBottom = clip.bottom;
457 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700458 info.width = getSnapshot()->viewport.getWidth();
459 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700460 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700461
Chet Haase48659092012-05-31 15:21:51 -0700462 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800463
Romain Guy8f3b8e32012-03-27 16:33:45 -0700464 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700465 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800466 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700467
Chris Craik65924a32012-04-05 17:52:11 -0700468 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700469 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700470 }
Romain Guycabfcc12011-03-07 18:06:46 -0800471 }
472
Chet Haasedaf98e92011-01-10 14:10:36 -0800473 resume();
Romain Guy65549432012-03-26 16:45:05 -0700474 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800475}
476
Romain Guyf6a11b82010-06-23 17:47:49 -0700477///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700478// Debug
479///////////////////////////////////////////////////////////////////////////////
480
Romain Guy0f667532013-03-01 14:31:04 -0800481void OpenGLRenderer::eventMark(const char* name) const {
482 mCaches.eventMark(0, name);
483}
484
Romain Guy87e2f7572012-09-24 11:37:12 -0700485void OpenGLRenderer::startMark(const char* name) const {
486 mCaches.startMark(0, name);
487}
488
489void OpenGLRenderer::endMark() const {
490 mCaches.endMark();
491}
492
493void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
494 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
495 if (clear) {
496 mCaches.disableScissor();
497 mCaches.stencil.clear();
498 }
499 if (enable) {
500 mCaches.stencil.enableDebugWrite();
501 } else {
502 mCaches.stencil.disable();
503 }
504 }
505}
506
507void OpenGLRenderer::renderOverdraw() {
508 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700509 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700510
511 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700512 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700513 clip->right - clip->left, clip->bottom - clip->top);
514
515 mCaches.stencil.enableDebugTest(2);
516 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
517 mCaches.stencil.enableDebugTest(3);
518 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
519 mCaches.stencil.enableDebugTest(4);
520 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
521 mCaches.stencil.enableDebugTest(4, true);
522 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
523 mCaches.stencil.disable();
524 }
525}
526
527///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700528// Layers
529///////////////////////////////////////////////////////////////////////////////
530
531bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700532 if (layer->deferredUpdateScheduled && layer->renderer &&
533 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700534 Rect& dirty = layer->dirtyRect;
535
Romain Guy7c450aa2012-09-21 19:15:00 -0700536 if (inFrame) {
537 endTiling();
538 debugOverdraw(false, false);
539 }
Romain Guy11cb6422012-09-21 00:39:43 -0700540
Romain Guy96885eb2013-03-26 15:05:58 -0700541 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700542 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700543 } else {
544 layer->defer();
545 }
Romain Guy11cb6422012-09-21 00:39:43 -0700546
547 if (inFrame) {
548 resumeAfterLayer();
549 startTiling(mSnapshot);
550 }
551
Romain Guy5bb3c732012-11-29 17:52:58 -0800552 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700553
554 return true;
555 }
556
557 return false;
558}
559
560void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700561 // If draw deferring is enabled this method will simply defer
562 // the display list of each individual layer. The layers remain
563 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700564 int count = mLayerUpdates.size();
565 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700566 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
567 startMark("Layer Updates");
568 } else {
569 startMark("Defer Layer Updates");
570 }
Romain Guy11cb6422012-09-21 00:39:43 -0700571
Chris Craik1206b9b2013-04-04 14:46:24 -0700572 // Note: it is very important to update the layers in order
573 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700574 Layer* layer = mLayerUpdates.itemAt(i);
575 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700576 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
577 mCaches.resourceCache.decrementRefcount(layer);
578 }
Romain Guy11cb6422012-09-21 00:39:43 -0700579 }
Romain Guy11cb6422012-09-21 00:39:43 -0700580
Romain Guy96885eb2013-03-26 15:05:58 -0700581 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
582 mLayerUpdates.clear();
583 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
584 }
585 endMark();
586 }
587}
588
589void OpenGLRenderer::flushLayers() {
590 int count = mLayerUpdates.size();
591 if (count > 0) {
592 startMark("Apply Layer Updates");
593 char layerName[12];
594
Chris Craik1206b9b2013-04-04 14:46:24 -0700595 // Note: it is very important to update the layers in order
596 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700597 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700598 startMark(layerName);
599
600 Layer* layer = mLayerUpdates.itemAt(i);
601 layer->flush();
602 mCaches.resourceCache.decrementRefcount(layer);
603
Romain Guy96885eb2013-03-26 15:05:58 -0700604 endMark();
605 }
606
607 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700608 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700609
Romain Guy11cb6422012-09-21 00:39:43 -0700610 endMark();
611 }
612}
613
614void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
615 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700616 // Make sure we don't introduce duplicates.
617 // SortedVector would do this automatically but we need to respect
618 // the insertion order. The linear search is not an issue since
619 // this list is usually very short (typically one item, at most a few)
620 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
621 if (mLayerUpdates.itemAt(i) == layer) {
622 return;
623 }
624 }
Romain Guy11cb6422012-09-21 00:39:43 -0700625 mLayerUpdates.push_back(layer);
626 mCaches.resourceCache.incrementRefcount(layer);
627 }
628}
629
630void OpenGLRenderer::clearLayerUpdates() {
631 size_t count = mLayerUpdates.size();
632 if (count > 0) {
633 mCaches.resourceCache.lock();
634 for (size_t i = 0; i < count; i++) {
635 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
636 }
637 mCaches.resourceCache.unlock();
638 mLayerUpdates.clear();
639 }
640}
641
642///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700643// State management
644///////////////////////////////////////////////////////////////////////////////
645
Romain Guybb9524b2010-06-22 18:56:38 -0700646int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700647 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700648}
649
650int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700651 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700652}
653
654void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700655 if (mSaveCount > 1) {
656 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700657 }
Romain Guybb9524b2010-06-22 18:56:38 -0700658}
659
660void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700661 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700662
Romain Guy8fb95422010-08-17 18:38:51 -0700663 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700664 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700665 }
Romain Guybb9524b2010-06-22 18:56:38 -0700666}
667
Romain Guy8aef54f2010-09-01 15:13:49 -0700668int OpenGLRenderer::saveSnapshot(int flags) {
669 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700670 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700671}
672
673bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700674 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700675 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700676 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700677
Romain Guybd6b79b2010-06-26 00:13:53 -0700678 sp<Snapshot> current = mSnapshot;
679 sp<Snapshot> previous = mSnapshot->previous;
680
Romain Guyeb993562010-10-05 18:14:38 -0700681 if (restoreOrtho) {
682 Rect& r = previous->viewport;
683 glViewport(r.left, r.top, r.right, r.bottom);
684 mOrthoMatrix.load(current->orthoMatrix);
685 }
686
Romain Guy8b55f372010-08-18 17:10:07 -0700687 mSaveCount--;
688 mSnapshot = previous;
689
Romain Guy2542d192010-08-18 11:47:12 -0700690 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700691 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700692 }
Romain Guy2542d192010-08-18 11:47:12 -0700693
Romain Guy5ec99242010-11-03 16:19:08 -0700694 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700695 endMark(); // Savelayer
696 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700697 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700698 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700699 }
700
Romain Guy2542d192010-08-18 11:47:12 -0700701 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700702}
703
Romain Guyf6a11b82010-06-23 17:47:49 -0700704///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700705// Layers
706///////////////////////////////////////////////////////////////////////////////
707
708int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800709 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700710 const GLuint previousFbo = mSnapshot->fbo;
711 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700712
Romain Guyaf636eb2010-12-09 17:47:21 -0800713 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700714 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700715 }
Romain Guyd55a8612010-06-28 17:42:46 -0700716
717 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700718}
719
Chris Craikd90144d2013-03-19 15:03:48 -0700720void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
721 const Rect untransformedBounds(bounds);
722
723 currentTransform().mapRect(bounds);
724
725 // Layers only make sense if they are in the framebuffer's bounds
726 if (bounds.intersect(*mSnapshot->clipRect)) {
727 // We cannot work with sub-pixels in this case
728 bounds.snapToPixelBoundaries();
729
730 // When the layer is not an FBO, we may use glCopyTexImage so we
731 // need to make sure the layer does not extend outside the bounds
732 // of the framebuffer
733 if (!bounds.intersect(mSnapshot->previous->viewport)) {
734 bounds.setEmpty();
735 } else if (fboLayer) {
736 clip.set(bounds);
737 mat4 inverse;
738 inverse.loadInverse(currentTransform());
739 inverse.mapRect(clip);
740 clip.snapToPixelBoundaries();
741 if (clip.intersect(untransformedBounds)) {
742 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
743 bounds.set(untransformedBounds);
744 } else {
745 clip.setEmpty();
746 }
747 }
748 } else {
749 bounds.setEmpty();
750 }
751}
752
Chris Craik408eb122013-03-26 18:55:15 -0700753void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
754 bool fboLayer, int alpha) {
755 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
756 bounds.getHeight() > mCaches.maxTextureSize ||
757 (fboLayer && clip.isEmpty())) {
758 mSnapshot->empty = fboLayer;
759 } else {
760 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
761 }
762}
763
Chris Craikd90144d2013-03-19 15:03:48 -0700764int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
765 int alpha, SkXfermode::Mode mode, int flags) {
766 const GLuint previousFbo = mSnapshot->fbo;
767 const int count = saveSnapshot(flags);
768
769 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
770 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
771 // operations will be able to store and restore the current clip and transform info, and
772 // quick rejection will be correct (for display lists)
773
774 Rect bounds(left, top, right, bottom);
775 Rect clip;
776 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700777 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700778
Chris Craik408eb122013-03-26 18:55:15 -0700779 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700780 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
781 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
782 }
783 }
784
785 return count;
786}
787
788
Romain Guy1c740bc2010-09-13 18:00:09 -0700789/**
790 * Layers are viewed by Skia are slightly different than layers in image editing
791 * programs (for instance.) When a layer is created, previously created layers
792 * and the frame buffer still receive every drawing command. For instance, if a
793 * layer is created and a shape intersecting the bounds of the layers and the
794 * framebuffer is draw, the shape will be drawn on both (unless the layer was
795 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
796 *
797 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
798 * texture. Unfortunately, this is inefficient as it requires every primitive to
799 * be drawn n + 1 times, where n is the number of active layers. In practice this
800 * means, for every primitive:
801 * - Switch active frame buffer
802 * - Change viewport, clip and projection matrix
803 * - Issue the drawing
804 *
805 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700806 * To avoid this, layers are implemented in a different way here, at least in the
807 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
808 * is set. When this flag is set we can redirect all drawing operations into a
809 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700810 *
811 * This implementation relies on the frame buffer being at least RGBA 8888. When
812 * a layer is created, only a texture is created, not an FBO. The content of the
813 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700814 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700815 * buffer and drawing continues as normal. This technique therefore treats the
816 * frame buffer as a scratch buffer for the layers.
817 *
818 * To compose the layers back onto the frame buffer, each layer texture
819 * (containing the original frame buffer data) is drawn as a simple quad over
820 * the frame buffer. The trick is that the quad is set as the composition
821 * destination in the blending equation, and the frame buffer becomes the source
822 * of the composition.
823 *
824 * Drawing layers with an alpha value requires an extra step before composition.
825 * An empty quad is drawn over the layer's region in the frame buffer. This quad
826 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
827 * quad is used to multiply the colors in the frame buffer. This is achieved by
828 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
829 * GL_ZERO, GL_SRC_ALPHA.
830 *
831 * Because glCopyTexImage2D() can be slow, an alternative implementation might
832 * be use to draw a single clipped layer. The implementation described above
833 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700834 *
835 * (1) The frame buffer is actually not cleared right away. To allow the GPU
836 * to potentially optimize series of calls to glCopyTexImage2D, the frame
837 * buffer is left untouched until the first drawing operation. Only when
838 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700839 */
Chet Haased48885a2012-08-28 17:43:28 -0700840bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
841 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700842 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700843 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700844
Romain Guyeb993562010-10-05 18:14:38 -0700845 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
846
Romain Guyf607bdc2010-09-10 19:20:06 -0700847 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700848 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700849 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700850 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700851 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700852
853 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700854 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700855 return false;
856 }
Romain Guyf18fd992010-07-08 11:45:51 -0700857
Romain Guya1d3c912011-12-13 14:55:06 -0800858 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700859 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700860 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700861 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700862 }
863
Romain Guy9ace8f52011-07-07 20:50:11 -0700864 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700865 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
867 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800868 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700869 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700870 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700871
Romain Guy8fb95422010-08-17 18:38:51 -0700872 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700873 mSnapshot->flags |= Snapshot::kFlagIsLayer;
874 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700875
Chris Craik7273daa2013-03-28 11:25:24 -0700876 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700877 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700878 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700879 } else {
880 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700881 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800882 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 if (layer->isEmpty()) {
884 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700885 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 layer->getWidth(), layer->getHeight(), 0);
887 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800888 } else {
889 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700890 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800891 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700892
Romain Guy54be1cd2011-06-13 19:04:27 -0700893 // Enqueue the buffer coordinates to clear the corresponding region later
894 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700895 }
Romain Guyeb993562010-10-05 18:14:38 -0700896 }
Romain Guyf86ef572010-07-01 11:05:42 -0700897
Romain Guyd55a8612010-06-28 17:42:46 -0700898 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700899}
900
Chet Haased48885a2012-08-28 17:43:28 -0700901bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800902 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700904
Chet Haased48885a2012-08-28 17:43:28 -0700905 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800906 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
907 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700908 mSnapshot->fbo = layer->getFbo();
909 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
910 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
911 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
912 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700913 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700914
Romain Guy2b7028e2012-09-19 17:25:38 -0700915 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700916 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700917 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700918 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
919 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700920
921 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700922 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700923 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700925 }
926
927 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700929
Romain Guyf735c8e2013-01-31 17:45:55 -0800930 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700931
932 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700933 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800934 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700935 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700936 glClear(GL_COLOR_BUFFER_BIT);
937
938 dirtyClip();
939
940 // Change the ortho projection
941 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
942 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
943
944 return true;
945}
946
Romain Guy1c740bc2010-09-13 18:00:09 -0700947/**
948 * Read the documentation of createLayer() before doing anything in this method.
949 */
Romain Guy1d83e192010-08-17 11:37:00 -0700950void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
951 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000952 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700953 return;
954 }
955
Romain Guy8ce00302013-01-15 18:51:42 -0800956 Layer* layer = current->layer;
957 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700958 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700959
960 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700961 endTiling();
962
Romain Guye0aa84b2012-04-03 19:30:26 -0700963 // Detach the texture from the FBO
964 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800965
966 layer->removeFbo(false);
967
Romain Guyeb993562010-10-05 18:14:38 -0700968 // Unbind current FBO and restore previous one
969 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700970 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700971
972 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700973 }
974
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700976 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700978 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700980 }
981
Romain Guy03750a02010-10-18 14:06:08 -0700982 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700983
Romain Guya1d3c912011-12-13 14:55:06 -0800984 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700985
Romain Guy5b3b3522010-10-27 18:57:51 -0700986 // When the layer is stored in an FBO, we can save a bit of fillrate by
987 // drawing only the dirty region
988 if (fboLayer) {
989 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700990 if (layer->getColorFilter()) {
991 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800992 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700993 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800995 resetColorFilter();
996 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 } else if (!rect.isEmpty()) {
998 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
999 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 }
Romain Guy8b55f372010-08-18 17:10:07 -07001001
Romain Guy746b7402010-10-26 16:27:31 -07001002 dirtyClip();
1003
Romain Guyeb993562010-10-05 18:14:38 -07001004 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001005 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001006 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001007 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001008 }
1009}
1010
Romain Guyaa6c24c2011-04-28 18:40:04 -07001011void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001012 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001013
1014 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001015 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001016 setupDrawWithTexture();
1017 } else {
1018 setupDrawWithExternalTexture();
1019 }
1020 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001021 setupDrawColor(alpha, alpha, alpha, alpha);
1022 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001023 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001024 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001025 setupDrawPureColorUniforms();
1026 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001027 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1028 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001029 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001030 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001031 }
Romain Guy3b753822013-03-05 10:27:35 -08001032 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001033 layer->getWidth() == (uint32_t) rect.getWidth() &&
1034 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001035 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1036 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001037
Romain Guyd21b6e12011-11-30 20:21:23 -08001038 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001039 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1040 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001041 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001042 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1043 }
1044 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001045 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1046
1047 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1048
1049 finishDrawTexture();
1050}
1051
Romain Guy5b3b3522010-10-27 18:57:51 -07001052void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001054 const Rect& texCoords = layer->texCoords;
1055 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1056 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001057
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 float x = rect.left;
1059 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001060 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001061 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001062 layer->getHeight() == (uint32_t) rect.getHeight();
1063
1064 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001065 // When we're swapping, the layer is already in screen coordinates
1066 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001067 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1068 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001069 }
1070
Romain Guyd21b6e12011-11-30 20:21:23 -08001071 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001073 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 }
1075
Chris Craik16ecda52013-03-29 10:59:59 -07001076 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001077 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001078 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001079 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001080 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1081 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001082
Romain Guyaa6c24c2011-04-28 18:40:04 -07001083 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1084 } else {
1085 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1086 drawTextureLayer(layer, rect);
1087 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1088 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001089}
1090
1091void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001092 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001093 layer->setRegionAsRect();
1094
Romain Guy40667672011-03-18 14:34:03 -07001095 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001096
Romain Guy5b3b3522010-10-27 18:57:51 -07001097 layer->region.clear();
1098 return;
1099 }
1100
Romain Guy8a3957d2011-09-07 17:55:15 -07001101 // TODO: See LayerRenderer.cpp::generateMesh() for important
1102 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001103 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001104 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001105 const android::Rect* rects;
1106 Region safeRegion;
1107 if (CC_LIKELY(hasRectToRectTransform())) {
1108 rects = layer->region.getArray(&count);
1109 } else {
1110 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1111 rects = safeRegion.getArray(&count);
1112 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001113
Chris Craik16ecda52013-03-29 10:59:59 -07001114 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001115 const float texX = 1.0f / float(layer->getWidth());
1116 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001117 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001118
Romain Guy8ce00302013-01-15 18:51:42 -08001119 setupDraw();
1120
1121 // We must get (and therefore bind) the region mesh buffer
1122 // after we setup drawing in case we need to mess with the
1123 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001124 TextureVertex* mesh = mCaches.getRegionMesh();
1125 GLsizei numQuads = 0;
1126
Romain Guy7230a742011-01-10 22:26:16 -08001127 setupDrawWithTexture();
1128 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001129 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001130 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001131 setupDrawProgram();
1132 setupDrawDirtyRegionsDisabled();
1133 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001134 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001135 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001136 if (currentTransform().isPureTranslate()) {
1137 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1138 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001139
Romain Guyd21b6e12011-11-30 20:21:23 -08001140 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001141 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1142 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001143 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1145 }
Romain Guy15bc6432011-12-13 13:11:32 -08001146 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001147
1148 for (size_t i = 0; i < count; i++) {
1149 const android::Rect* r = &rects[i];
1150
1151 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001152 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001153 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001154 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001155
1156 // TODO: Reject quads outside of the clip
1157 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1158 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1159 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1160 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1161
1162 numQuads++;
1163
1164 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1165 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1166 numQuads = 0;
1167 mesh = mCaches.getRegionMesh();
1168 }
1169 }
1170
1171 if (numQuads > 0) {
1172 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1173 }
1174
Romain Guy7230a742011-01-10 22:26:16 -08001175 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001176
1177#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001178 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001179#endif
1180
1181 layer->region.clear();
1182 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001183}
1184
Romain Guy3a3133d2011-02-01 22:59:58 -08001185void OpenGLRenderer::drawRegionRects(const Region& region) {
1186#if DEBUG_LAYERS_AS_REGIONS
1187 size_t count;
1188 const android::Rect* rects = region.getArray(&count);
1189
1190 uint32_t colors[] = {
1191 0x7fff0000, 0x7f00ff00,
1192 0x7f0000ff, 0x7fff00ff,
1193 };
1194
1195 int offset = 0;
1196 int32_t top = rects[0].top;
1197
1198 for (size_t i = 0; i < count; i++) {
1199 if (top != rects[i].top) {
1200 offset ^= 0x2;
1201 top = rects[i].top;
1202 }
1203
1204 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1205 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1206 SkXfermode::kSrcOver_Mode);
1207 }
1208#endif
1209}
1210
Romain Guy8ce00302013-01-15 18:51:42 -08001211void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1212 SkXfermode::Mode mode, bool dirty) {
1213 int count = 0;
1214 Vector<float> rects;
1215
1216 SkRegion::Iterator it(region);
1217 while (!it.done()) {
1218 const SkIRect& r = it.rect();
1219 rects.push(r.fLeft);
1220 rects.push(r.fTop);
1221 rects.push(r.fRight);
1222 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001223 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001224 it.next();
1225 }
1226
Romain Guy3bbacf22013-02-06 16:51:04 -08001227 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001228}
1229
Romain Guy5b3b3522010-10-27 18:57:51 -07001230void OpenGLRenderer::dirtyLayer(const float left, const float top,
1231 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001232 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001233 Rect bounds(left, top, right, bottom);
1234 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001235 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001236 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001237}
1238
1239void OpenGLRenderer::dirtyLayer(const float left, const float top,
1240 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001241 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001242 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001243 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001244 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001245}
1246
1247void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001248 if (bounds.intersect(*mSnapshot->clipRect)) {
1249 bounds.snapToPixelBoundaries();
1250 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1251 if (!dirty.isEmpty()) {
1252 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001253 }
1254 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001255}
1256
Romain Guy54be1cd2011-06-13 19:04:27 -07001257void OpenGLRenderer::clearLayerRegions() {
1258 const size_t count = mLayers.size();
1259 if (count == 0) return;
1260
1261 if (!mSnapshot->isIgnored()) {
1262 // Doing several glScissor/glClear here can negatively impact
1263 // GPUs with a tiler architecture, instead we draw quads with
1264 // the Clear blending mode
1265
1266 // The list contains bounds that have already been clipped
1267 // against their initial clip rect, and the current clip
1268 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001269 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001270
1271 Vertex mesh[count * 6];
1272 Vertex* vertex = mesh;
1273
1274 for (uint32_t i = 0; i < count; i++) {
1275 Rect* bounds = mLayers.itemAt(i);
1276
1277 Vertex::set(vertex++, bounds->left, bounds->bottom);
1278 Vertex::set(vertex++, bounds->left, bounds->top);
1279 Vertex::set(vertex++, bounds->right, bounds->top);
1280 Vertex::set(vertex++, bounds->left, bounds->bottom);
1281 Vertex::set(vertex++, bounds->right, bounds->top);
1282 Vertex::set(vertex++, bounds->right, bounds->bottom);
1283
1284 delete bounds;
1285 }
Romain Guye67307c2013-02-11 18:01:20 -08001286 // We must clear the list of dirty rects before we
1287 // call setupDraw() to prevent stencil setup to do
1288 // the same thing again
1289 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001290
1291 setupDraw(false);
1292 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1293 setupDrawBlending(true, SkXfermode::kClear_Mode);
1294 setupDrawProgram();
1295 setupDrawPureColorUniforms();
1296 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001297 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001298
Romain Guy54be1cd2011-06-13 19:04:27 -07001299 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001300
1301 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001302 } else {
1303 for (uint32_t i = 0; i < count; i++) {
1304 delete mLayers.itemAt(i);
1305 }
Romain Guye67307c2013-02-11 18:01:20 -08001306 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001307 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001308}
1309
Romain Guybd6b79b2010-06-26 00:13:53 -07001310///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001311// State Deferral
1312///////////////////////////////////////////////////////////////////////////////
1313
Chris Craikff785832013-03-08 13:12:16 -08001314bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001315 const Rect& currentClip = *(mSnapshot->clipRect);
1316 const mat4& currentMatrix = *(mSnapshot->transform);
1317
Chris Craikff785832013-03-08 13:12:16 -08001318 if (stateDeferFlags & kStateDeferFlag_Draw) {
1319 // state has bounds initialized in local coordinates
1320 if (!state.mBounds.isEmpty()) {
1321 currentMatrix.mapRect(state.mBounds);
1322 if (!state.mBounds.intersect(currentClip)) {
1323 // quick rejected
1324 return true;
1325 }
1326 } else {
1327 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001328 }
1329 }
1330
Chris Craik527a3aa2013-03-04 10:19:31 -08001331 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1332 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001333 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001334 }
1335
Chris Craik7273daa2013-03-28 11:25:24 -07001336 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1337 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001338 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001339 state.mDrawModifiers = mDrawModifiers;
1340 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001341 return false;
1342}
1343
Chris Craik527a3aa2013-03-04 10:19:31 -08001344void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001345 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001346 mDrawModifiers = state.mDrawModifiers;
1347 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001348
Chris Craik527a3aa2013-03-04 10:19:31 -08001349 if (state.mClipValid && !skipClipRestore) {
Chris Craikff785832013-03-08 13:12:16 -08001350 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1351 dirtyClip();
1352 }
Chris Craikc3566d02013-02-04 16:16:33 -08001353}
1354
Chris Craik527a3aa2013-03-04 10:19:31 -08001355void OpenGLRenderer::setFullScreenClip() {
1356 mSnapshot->setClip(0, 0, mWidth, mHeight);
1357 dirtyClip();
1358}
1359
Chris Craikc3566d02013-02-04 16:16:33 -08001360///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001361// Transforms
1362///////////////////////////////////////////////////////////////////////////////
1363
1364void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001365 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001366}
1367
1368void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001369 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001370}
1371
1372void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001373 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001374}
1375
Romain Guy807daf72011-01-18 11:19:19 -08001376void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001377 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001378}
1379
Romain Guyf6a11b82010-06-23 17:47:49 -07001380void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001381 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001382 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001383 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001384 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001385 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001386}
1387
Chris Craikb98a0162013-02-21 11:30:22 -08001388bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001389 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001390}
1391
Romain Guyf6a11b82010-06-23 17:47:49 -07001392void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001393 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001394}
1395
1396void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001397 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001398 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001399 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001400 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001401}
1402
1403///////////////////////////////////////////////////////////////////////////////
1404// Clipping
1405///////////////////////////////////////////////////////////////////////////////
1406
Romain Guybb9524b2010-06-22 18:56:38 -07001407void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001408 Rect clip(*mSnapshot->clipRect);
1409 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001410
Romain Guy8a4ac612012-07-17 17:32:48 -07001411 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1412 clip.getWidth(), clip.getHeight())) {
1413 mDirtyClip = false;
1414 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001415}
1416
Romain Guy8ce00302013-01-15 18:51:42 -08001417void OpenGLRenderer::ensureStencilBuffer() {
1418 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1419 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1420 // just hope we have one when hasLayer() returns false.
1421 if (hasLayer()) {
1422 attachStencilBufferToLayer(mSnapshot->layer);
1423 }
1424}
1425
1426void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1427 // The layer's FBO is already bound when we reach this stage
1428 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001429 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1430 // is attached after we initiated tiling. We must turn it off,
1431 // attach the new render buffer then turn tiling back on
1432 endTiling();
1433
Romain Guy8d4aeb72013-02-12 16:08:55 -08001434 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001435 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001436 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001437
Romain Guyf735c8e2013-01-31 17:45:55 -08001438 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001439 }
1440}
1441
1442void OpenGLRenderer::setStencilFromClip() {
1443 if (!mCaches.debugOverdraw) {
1444 if (!mSnapshot->clipRegion->isEmpty()) {
1445 // NOTE: The order here is important, we must set dirtyClip to false
1446 // before any draw call to avoid calling back into this method
1447 mDirtyClip = false;
1448
1449 ensureStencilBuffer();
1450
1451 mCaches.stencil.enableWrite();
1452
1453 // Clear the stencil but first make sure we restrict drawing
1454 // to the region's bounds
1455 bool resetScissor = mCaches.enableScissor();
1456 if (resetScissor) {
1457 // The scissor was not set so we now need to update it
1458 setScissorFromClip();
1459 }
1460 mCaches.stencil.clear();
1461 if (resetScissor) mCaches.disableScissor();
1462
1463 // NOTE: We could use the region contour path to generate a smaller mesh
1464 // Since we are using the stencil we could use the red book path
1465 // drawing technique. It might increase bandwidth usage though.
1466
1467 // The last parameter is important: we are not drawing in the color buffer
1468 // so we don't want to dirty the current layer, if any
1469 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1470
1471 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001472
1473 // Draw the region used to generate the stencil if the appropriate debug
1474 // mode is enabled
1475 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1476 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1477 }
Romain Guy8ce00302013-01-15 18:51:42 -08001478 } else {
1479 mCaches.stencil.disable();
1480 }
1481 }
1482}
1483
Romain Guy9d5316e2010-06-24 19:30:36 -07001484const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001485 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001486}
1487
Romain Guy8a4ac612012-07-17 17:32:48 -07001488bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1489 if (mSnapshot->isIgnored()) {
1490 return true;
1491 }
1492
1493 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001494 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001495 r.snapToPixelBoundaries();
1496
1497 Rect clipRect(*mSnapshot->clipRect);
1498 clipRect.snapToPixelBoundaries();
1499
1500 return !clipRect.intersects(r);
1501}
1502
Romain Guy35643dd2012-09-18 15:40:58 -07001503bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1504 Rect& transformed, Rect& clip) {
1505 if (mSnapshot->isIgnored()) {
1506 return true;
1507 }
1508
1509 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001510 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001511 transformed.snapToPixelBoundaries();
1512
1513 clip.set(*mSnapshot->clipRect);
1514 clip.snapToPixelBoundaries();
1515
1516 return !clip.intersects(transformed);
1517}
1518
Romain Guy672433d2013-01-04 19:05:13 -08001519bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1520 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001521 if (paint->getStyle() != SkPaint::kFill_Style) {
1522 float outset = paint->getStrokeWidth() * 0.5f;
1523 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1524 } else {
1525 return quickReject(left, top, right, bottom);
1526 }
1527}
1528
Romain Guyc7d53492010-06-25 13:41:57 -07001529bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001530 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001531 return true;
1532 }
1533
Romain Guy1d83e192010-08-17 11:37:00 -07001534 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001535 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001536 r.snapToPixelBoundaries();
1537
1538 Rect clipRect(*mSnapshot->clipRect);
1539 clipRect.snapToPixelBoundaries();
1540
Romain Guy586cae32012-07-13 15:28:31 -07001541 bool rejected = !clipRect.intersects(r);
1542 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001543 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001544 }
1545
1546 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001547}
1548
Romain Guy8ce00302013-01-15 18:51:42 -08001549void OpenGLRenderer::debugClip() {
1550#if DEBUG_CLIP_REGIONS
1551 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1552 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1553 }
1554#endif
1555}
1556
Romain Guy079ba2c2010-07-16 14:12:24 -07001557bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001558 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001559 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1560 if (clipped) {
1561 dirtyClip();
1562 }
1563 return !mSnapshot->clipRect->isEmpty();
1564 }
1565
1566 SkPath path;
1567 path.addRect(left, top, right, bottom);
1568
1569 return clipPath(&path, op);
1570}
1571
1572bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1573 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001574 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001575
1576 SkPath transformed;
1577 path->transform(transform, &transformed);
1578
1579 SkRegion clip;
1580 if (!mSnapshot->clipRegion->isEmpty()) {
1581 clip.setRegion(*mSnapshot->clipRegion);
1582 } else {
1583 Rect* bounds = mSnapshot->clipRect;
1584 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1585 }
1586
1587 SkRegion region;
1588 region.setPath(transformed, clip);
1589
1590 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001591 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001592 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001593 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001594 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001595}
1596
Romain Guy735738c2012-12-03 12:34:51 -08001597bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001598 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1599 if (clipped) {
1600 dirtyClip();
1601 }
1602 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001603}
1604
Chet Haasea23eed82012-04-12 15:19:04 -07001605Rect* OpenGLRenderer::getClipRect() {
1606 return mSnapshot->clipRect;
1607}
1608
Romain Guyf6a11b82010-06-23 17:47:49 -07001609///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001610// Drawing commands
1611///////////////////////////////////////////////////////////////////////////////
1612
Romain Guy54be1cd2011-06-13 19:04:27 -07001613void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001614 // TODO: It would be best if we could do this before quickReject()
1615 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001616 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001617 // Make sure setScissor & setStencil happen at the beginning of
1618 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001619 if (mDirtyClip) {
1620 if (mCaches.scissorEnabled) {
1621 setScissorFromClip();
1622 }
Romain Guy8ce00302013-01-15 18:51:42 -08001623 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001624 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001625
Romain Guy70ca14e2010-12-13 18:24:33 -08001626 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001627
Romain Guy70ca14e2010-12-13 18:24:33 -08001628 mSetShaderColor = false;
1629 mColorSet = false;
1630 mColorA = mColorR = mColorG = mColorB = 0.0f;
1631 mTextureUnit = 0;
1632 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001633
1634 // Enable debug highlight when what we're about to draw is tested against
1635 // the stencil buffer and if stencil highlight debugging is on
1636 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1637 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1638 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001639}
1640
1641void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1642 mDescription.hasTexture = true;
1643 mDescription.hasAlpha8Texture = isAlpha8;
1644}
1645
Romain Guyff316ec2013-02-13 18:39:43 -08001646void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1647 mDescription.hasTexture = true;
1648 mDescription.hasColors = true;
1649 mDescription.hasAlpha8Texture = isAlpha8;
1650}
1651
Romain Guyaa6c24c2011-04-28 18:40:04 -07001652void OpenGLRenderer::setupDrawWithExternalTexture() {
1653 mDescription.hasExternalTexture = true;
1654}
1655
Romain Guy15bc6432011-12-13 13:11:32 -08001656void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001657 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001658}
1659
Chris Craik710f46d2012-09-17 17:25:49 -07001660void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001661 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001662}
1663
Romain Guyed6fcb02011-03-21 13:11:28 -07001664void OpenGLRenderer::setupDrawPoint(float pointSize) {
1665 mDescription.isPoint = true;
1666 mDescription.pointSize = pointSize;
1667}
1668
Romain Guy8d0d4782010-12-14 20:13:35 -08001669void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1670 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001671 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1672 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1673 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001674 mColorSet = true;
1675 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1676}
1677
Romain Guy86568192010-12-14 15:55:39 -08001678void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1679 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001680 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1681 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1682 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001683 mColorSet = true;
1684 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1685}
1686
Romain Guy41210632012-07-16 17:04:24 -07001687void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1688 mCaches.fontRenderer->describe(mDescription, paint);
1689}
1690
Romain Guy70ca14e2010-12-13 18:24:33 -08001691void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1692 mColorA = a;
1693 mColorR = r;
1694 mColorG = g;
1695 mColorB = b;
1696 mColorSet = true;
1697 mSetShaderColor = mDescription.setColor(r, g, b, a);
1698}
1699
1700void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001701 if (mDrawModifiers.mShader) {
1702 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001703 }
1704}
1705
1706void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001707 if (mDrawModifiers.mColorFilter) {
1708 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001709 }
1710}
1711
Romain Guyf09ef512011-05-27 11:43:46 -07001712void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1713 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1714 mColorA = 1.0f;
1715 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001716 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001717 }
1718}
1719
Romain Guy70ca14e2010-12-13 18:24:33 -08001720void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001721 // When the blending mode is kClear_Mode, we need to use a modulate color
1722 // argb=1,0,0,0
1723 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001724 bool blend = (mColorSet && mColorA < 1.0f) ||
1725 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1726 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001727}
1728
1729void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001730 // When the blending mode is kClear_Mode, we need to use a modulate color
1731 // argb=1,0,0,0
1732 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001733 blend |= (mColorSet && mColorA < 1.0f) ||
1734 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1735 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1736 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001737}
1738
1739void OpenGLRenderer::setupDrawProgram() {
1740 useProgram(mCaches.programCache.get(mDescription));
1741}
1742
1743void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1744 mTrackDirtyRegions = false;
1745}
1746
1747void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1748 bool ignoreTransform) {
1749 mModelView.loadTranslate(left, top, 0.0f);
1750 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001751 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1752 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001754 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001755 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1756 }
1757}
1758
Chet Haase8a5cc922011-04-26 07:28:09 -07001759void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001760 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001761}
1762
Romain Guy70ca14e2010-12-13 18:24:33 -08001763void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1764 bool ignoreTransform, bool ignoreModelView) {
1765 if (!ignoreModelView) {
1766 mModelView.loadTranslate(left, top, 0.0f);
1767 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001768 } else {
1769 mModelView.loadIdentity();
1770 }
Romain Guy86568192010-12-14 15:55:39 -08001771 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1772 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001773 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001774 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001775 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001776 }
1777 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001778 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001779 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1780 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001781}
1782
Romain Guyed6fcb02011-03-21 13:11:28 -07001783void OpenGLRenderer::setupDrawPointUniforms() {
1784 int slot = mCaches.currentProgram->getUniform("pointSize");
1785 glUniform1f(slot, mDescription.pointSize);
1786}
1787
Romain Guy70ca14e2010-12-13 18:24:33 -08001788void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001789 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001790 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1791 }
1792}
1793
Romain Guy86568192010-12-14 15:55:39 -08001794void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001795 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001796 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001797 }
1798}
1799
Romain Guy70ca14e2010-12-13 18:24:33 -08001800void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001801 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001802 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001803 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001804 }
Chris Craikc3566d02013-02-04 16:16:33 -08001805 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1806 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001807 }
1808}
1809
Romain Guy8d0d4782010-12-14 20:13:35 -08001810void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001811 if (mDrawModifiers.mShader) {
1812 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001813 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001814 }
1815}
1816
Romain Guy70ca14e2010-12-13 18:24:33 -08001817void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001818 if (mDrawModifiers.mColorFilter) {
1819 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001820 }
1821}
1822
Romain Guy41210632012-07-16 17:04:24 -07001823void OpenGLRenderer::setupDrawTextGammaUniforms() {
1824 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1825}
1826
Romain Guy70ca14e2010-12-13 18:24:33 -08001827void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001828 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001829 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001830 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
1833void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001834 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001835 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001836 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001837}
1838
Romain Guyaa6c24c2011-04-28 18:40:04 -07001839void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1840 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001841 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001842 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001843}
1844
Romain Guy8f0095c2011-05-02 17:24:22 -07001845void OpenGLRenderer::setupDrawTextureTransform() {
1846 mDescription.hasTextureTransform = true;
1847}
1848
1849void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001850 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1851 GL_FALSE, &transform.data[0]);
1852}
1853
Romain Guy70ca14e2010-12-13 18:24:33 -08001854void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001855 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001856 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001857 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001858 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001859 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001860 }
Romain Guyd71dd362011-12-12 19:03:35 -08001861
Chris Craikcb4d6002012-09-25 12:00:29 -07001862 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001863 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001864 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001865 }
1866
1867 mCaches.unbindIndicesBuffer();
1868}
1869
Romain Guyff316ec2013-02-13 18:39:43 -08001870void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1871 bool force = mCaches.unbindMeshBuffer();
1872 GLsizei stride = sizeof(ColorTextureVertex);
1873
1874 mCaches.bindPositionVertexPointer(force, vertices, stride);
1875 if (mCaches.currentProgram->texCoords >= 0) {
1876 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1877 }
1878 int slot = mCaches.currentProgram->getAttrib("colors");
1879 if (slot >= 0) {
1880 glEnableVertexAttribArray(slot);
1881 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1882 }
1883
1884 mCaches.unbindIndicesBuffer();
1885}
1886
Romain Guy15bc6432011-12-13 13:11:32 -08001887void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1888 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001889 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001890 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001891 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001892 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001893}
1894
Chet Haase5b0200b2011-04-13 17:58:08 -07001895void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001896 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001897 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001898 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001899}
1900
Romain Guy70ca14e2010-12-13 18:24:33 -08001901void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001902}
1903
1904///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001905// Drawing
1906///////////////////////////////////////////////////////////////////////////////
1907
Chris Craikff785832013-03-08 13:12:16 -08001908status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1909 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001910 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001911 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1912 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001913 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001914 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001915 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001916 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1917 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001918 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001919 }
1920
1921 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001922 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1923 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001924
1925 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001926 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001927
Chet Haase58d110a2013-04-10 07:43:29 -07001928 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001929 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001930
Romain Guy65549432012-03-26 16:45:05 -07001931 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001932}
1933
Chris Craikc3566d02013-02-04 16:16:33 -08001934void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001935 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001936 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001937 }
1938}
1939
Romain Guya168d732011-03-18 16:50:13 -07001940void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1941 int alpha;
1942 SkXfermode::Mode mode;
1943 getAlphaAndMode(paint, &alpha, &mode);
1944
Romain Guy886b2752013-01-04 12:26:18 -08001945 int color = paint != NULL ? paint->getColor() : 0;
1946
Romain Guya168d732011-03-18 16:50:13 -07001947 float x = left;
1948 float y = top;
1949
Romain Guy886b2752013-01-04 12:26:18 -08001950 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1951
Romain Guya168d732011-03-18 16:50:13 -07001952 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001953 if (currentTransform().isPureTranslate()) {
1954 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1955 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001956 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001957
1958 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001959 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001960 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001961 }
1962
Romain Guy886b2752013-01-04 12:26:18 -08001963 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1964 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1965 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001966}
1967
Chris Craik527a3aa2013-03-04 10:19:31 -08001968status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
1969 const Rect& bounds, SkPaint* paint) {
1970
1971 // merged draw operations don't need scissor, but clip should still be valid
1972 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
1973
1974 mCaches.activeTexture(0);
1975 Texture* texture = mCaches.textureCache.get(bitmap);
1976 if (!texture) return DrawGlInfo::kStatusDone;
1977 const AutoTexture autoCleanup(texture);
1978
1979 int alpha;
1980 SkXfermode::Mode mode;
1981 getAlphaAndMode(paint, &alpha, &mode);
1982
1983 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1984 texture->setFilter(GL_NEAREST, true); // merged ops are always pure-translation for now
1985
1986 const float x = (int) floorf(bounds.left + 0.5f);
1987 const float y = (int) floorf(bounds.top + 0.5f);
1988 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
1989 int color = paint != NULL ? paint->getColor() : 0;
1990 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
1991 texture->id, paint != NULL, color, alpha, mode,
1992 &vertices[0].position[0], &vertices[0].texture[0],
1993 GL_TRIANGLES, bitmapCount * 6, true, true);
1994 } else {
1995 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
1996 texture->id, alpha / 255.0f, mode, texture->blend,
1997 &vertices[0].position[0], &vertices[0].texture[0],
1998 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true);
1999 }
2000
2001 return DrawGlInfo::kStatusDrew;
2002}
2003
Chet Haase48659092012-05-31 15:21:51 -07002004status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002005 const float right = left + bitmap->width();
2006 const float bottom = top + bitmap->height();
2007
2008 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002009 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002010 }
2011
Romain Guya1d3c912011-12-13 14:55:06 -08002012 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002013 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002014 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002015 const AutoTexture autoCleanup(texture);
2016
Romain Guy211370f2012-02-01 16:10:55 -08002017 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002018 drawAlphaBitmap(texture, left, top, paint);
2019 } else {
2020 drawTextureRect(left, top, right, bottom, texture, paint);
2021 }
Chet Haase48659092012-05-31 15:21:51 -07002022
2023 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002024}
2025
Chet Haase48659092012-05-31 15:21:51 -07002026status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002027 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2028 const mat4 transform(*matrix);
2029 transform.mapRect(r);
2030
Romain Guy6926c722010-07-12 20:20:03 -07002031 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002032 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002033 }
2034
Romain Guya1d3c912011-12-13 14:55:06 -08002035 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002036 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002037 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002038 const AutoTexture autoCleanup(texture);
2039
Romain Guy5b3b3522010-10-27 18:57:51 -07002040 // This could be done in a cheaper way, all we need is pass the matrix
2041 // to the vertex shader. The save/restore is a bit overkill.
2042 save(SkCanvas::kMatrix_SaveFlag);
2043 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002044 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2045 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2046 } else {
2047 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2048 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002049 restore();
Chet Haase48659092012-05-31 15:21:51 -07002050
2051 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002052}
2053
Chet Haase48659092012-05-31 15:21:51 -07002054status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002055 const float right = left + bitmap->width();
2056 const float bottom = top + bitmap->height();
2057
2058 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002059 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002060 }
2061
2062 mCaches.activeTexture(0);
2063 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2064 const AutoTexture autoCleanup(texture);
2065
Romain Guy886b2752013-01-04 12:26:18 -08002066 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2067 drawAlphaBitmap(texture, left, top, paint);
2068 } else {
2069 drawTextureRect(left, top, right, bottom, texture, paint);
2070 }
Chet Haase48659092012-05-31 15:21:51 -07002071
2072 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002073}
2074
Chet Haase48659092012-05-31 15:21:51 -07002075status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002076 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002077 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002078 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002079 }
2080
Romain Guyb18d2d02011-02-10 15:52:54 -08002081 float left = FLT_MAX;
2082 float top = FLT_MAX;
2083 float right = FLT_MIN;
2084 float bottom = FLT_MIN;
2085
Romain Guya92bb4d2012-10-16 11:08:44 -07002086 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002087
Romain Guyff316ec2013-02-13 18:39:43 -08002088 ColorTextureVertex mesh[count];
2089 ColorTextureVertex* vertex = mesh;
2090
2091 bool cleanupColors = false;
2092 if (!colors) {
2093 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2094 colors = new int[colorsCount];
2095 memset(colors, 0xff, colorsCount * sizeof(int));
2096 cleanupColors = true;
2097 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002098
Romain Guy5a7b4662011-01-20 19:09:30 -08002099 for (int32_t y = 0; y < meshHeight; y++) {
2100 for (int32_t x = 0; x < meshWidth; x++) {
2101 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2102
2103 float u1 = float(x) / meshWidth;
2104 float u2 = float(x + 1) / meshWidth;
2105 float v1 = float(y) / meshHeight;
2106 float v2 = float(y + 1) / meshHeight;
2107
2108 int ax = i + (meshWidth + 1) * 2;
2109 int ay = ax + 1;
2110 int bx = i;
2111 int by = bx + 1;
2112 int cx = i + 2;
2113 int cy = cx + 1;
2114 int dx = i + (meshWidth + 1) * 2 + 2;
2115 int dy = dx + 1;
2116
Romain Guyff316ec2013-02-13 18:39:43 -08002117 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2118 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2119 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002120
Romain Guyff316ec2013-02-13 18:39:43 -08002121 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2122 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2123 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002124
Romain Guya92bb4d2012-10-16 11:08:44 -07002125 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2126 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2127 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2128 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002129 }
2130 }
2131
Romain Guya92bb4d2012-10-16 11:08:44 -07002132 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002133 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002134 return DrawGlInfo::kStatusDone;
2135 }
2136
2137 mCaches.activeTexture(0);
2138 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002139 if (!texture) {
2140 if (cleanupColors) delete[] colors;
2141 return DrawGlInfo::kStatusDone;
2142 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002143 const AutoTexture autoCleanup(texture);
2144
2145 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2146 texture->setFilter(FILTER(paint), true);
2147
2148 int alpha;
2149 SkXfermode::Mode mode;
2150 getAlphaAndMode(paint, &alpha, &mode);
2151
Romain Guyff316ec2013-02-13 18:39:43 -08002152 float a = alpha / 255.0f;
2153
Romain Guya92bb4d2012-10-16 11:08:44 -07002154 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002155 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002156 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002157
Romain Guyff316ec2013-02-13 18:39:43 -08002158 setupDraw();
2159 setupDrawWithTextureAndColor();
2160 setupDrawColor(a, a, a, a);
2161 setupDrawColorFilter();
2162 setupDrawBlending(true, mode, false);
2163 setupDrawProgram();
2164 setupDrawDirtyRegionsDisabled();
2165 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2166 setupDrawTexture(texture->id);
2167 setupDrawPureColorUniforms();
2168 setupDrawColorFilterUniforms();
2169 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2170
2171 glDrawArrays(GL_TRIANGLES, 0, count);
2172
2173 finishDrawTexture();
2174
2175 int slot = mCaches.currentProgram->getAttrib("colors");
2176 if (slot >= 0) {
2177 glDisableVertexAttribArray(slot);
2178 }
2179
2180 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002181
2182 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002183}
2184
Chet Haase48659092012-05-31 15:21:51 -07002185status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002186 float srcLeft, float srcTop, float srcRight, float srcBottom,
2187 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002188 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002189 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002190 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002191 }
2192
Romain Guya1d3c912011-12-13 14:55:06 -08002193 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002194 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002195 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002196 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002197
Romain Guy8ba548f2010-06-30 19:21:21 -07002198 const float width = texture->width;
2199 const float height = texture->height;
2200
Romain Guy68169722011-08-22 17:33:33 -07002201 const float u1 = fmax(0.0f, srcLeft / width);
2202 const float v1 = fmax(0.0f, srcTop / height);
2203 const float u2 = fmin(1.0f, srcRight / width);
2204 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002205
Romain Guy03750a02010-10-18 14:06:08 -07002206 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002207 resetDrawTextureTexCoords(u1, v1, u2, v2);
2208
Romain Guy03750a02010-10-18 14:06:08 -07002209 int alpha;
2210 SkXfermode::Mode mode;
2211 getAlphaAndMode(paint, &alpha, &mode);
2212
Romain Guyd21b6e12011-11-30 20:21:23 -08002213 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2214
Romain Guy886b2752013-01-04 12:26:18 -08002215 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2216 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002217
Romain Guy886b2752013-01-04 12:26:18 -08002218 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2219 // Apply a scale transform on the canvas only when a shader is in use
2220 // Skia handles the ratio between the dst and src rects as a scale factor
2221 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002222 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002223 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002224
Romain Guy3b753822013-03-05 10:27:35 -08002225 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2226 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2227 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002228
2229 dstRight = x + (dstRight - dstLeft);
2230 dstBottom = y + (dstBottom - dstTop);
2231
2232 dstLeft = x;
2233 dstTop = y;
2234
2235 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2236 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002237 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002238 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002239 }
2240
2241 if (CC_UNLIKELY(useScaleTransform)) {
2242 save(SkCanvas::kMatrix_SaveFlag);
2243 translate(dstLeft, dstTop);
2244 scale(scaleX, scaleY);
2245
2246 dstLeft = 0.0f;
2247 dstTop = 0.0f;
2248
2249 dstRight = srcRight - srcLeft;
2250 dstBottom = srcBottom - srcTop;
2251 }
2252
2253 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2254 int color = paint ? paint->getColor() : 0;
2255 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2256 texture->id, paint != NULL, color, alpha, mode,
2257 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2258 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2259 } else {
2260 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2261 texture->id, alpha / 255.0f, mode, texture->blend,
2262 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2263 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2264 }
2265
2266 if (CC_UNLIKELY(useScaleTransform)) {
2267 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002268 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002269
2270 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002271
2272 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002273}
2274
Chet Haase48659092012-05-31 15:21:51 -07002275status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002276 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002277 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002278 int alpha;
2279 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002280 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002281
2282 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2283 left, top, right, bottom, alpha, mode);
2284}
2285
2286status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2287 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2288 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002289 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002290 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002291 }
2292
Romain Guybe6f9dc2012-07-16 12:41:17 -07002293 alpha *= mSnapshot->alpha;
2294
Romain Guy2728f962010-10-08 18:36:15 -07002295 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002296 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002297
Romain Guy211370f2012-02-01 16:10:55 -08002298 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002299 mCaches.activeTexture(0);
2300 Texture* texture = mCaches.textureCache.get(bitmap);
2301 if (!texture) return DrawGlInfo::kStatusDone;
2302 const AutoTexture autoCleanup(texture);
2303 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2304 texture->setFilter(GL_LINEAR, true);
2305
Romain Guy3b753822013-03-05 10:27:35 -08002306 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002307 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002308 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002309 const float offsetX = left + currentTransform().getTranslateX();
2310 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002311 const size_t count = mesh->quads.size();
2312 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002313 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002314 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002315 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2316 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2317 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002318 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002319 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002320 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002321 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002322 }
2323 }
2324
Romain Guy211370f2012-02-01 16:10:55 -08002325 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002326 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2327 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002328
2329 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2330 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2331 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2332 true, !mesh->hasEmptyQuads);
2333 } else {
2334 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2335 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2336 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2337 true, !mesh->hasEmptyQuads);
2338 }
Romain Guy054dc182010-10-15 17:55:25 -07002339 }
Chet Haase48659092012-05-31 15:21:51 -07002340
2341 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002342}
2343
Chris Craik65cd6122012-12-10 17:56:27 -08002344status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2345 bool useOffset) {
2346 if (!vertexBuffer.getSize()) {
2347 // no vertices to draw
2348 return DrawGlInfo::kStatusDone;
2349 }
2350
Chris Craikcb4d6002012-09-25 12:00:29 -07002351 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002352 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2353 bool isAA = paint->isAntiAlias();
2354
Chris Craik710f46d2012-09-17 17:25:49 -07002355 setupDraw();
2356 setupDrawNoTexture();
2357 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002358 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2359 setupDrawColorFilter();
2360 setupDrawShader();
2361 setupDrawBlending(isAA, mode);
2362 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002363 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002364 setupDrawColorUniforms();
2365 setupDrawColorFilterUniforms();
2366 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002367
Chris Craik710f46d2012-09-17 17:25:49 -07002368 void* vertices = vertexBuffer.getBuffer();
2369 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002370 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002371 mCaches.resetTexCoordsVertexPointer();
2372 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002373
Chris Craik710f46d2012-09-17 17:25:49 -07002374 int alphaSlot = -1;
2375 if (isAA) {
2376 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2377 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002378
Chris Craik710f46d2012-09-17 17:25:49 -07002379 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002380 glEnableVertexAttribArray(alphaSlot);
2381 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002382 }
Romain Guy04299382012-07-18 17:15:41 -07002383
Chris Craik710f46d2012-09-17 17:25:49 -07002384 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002385
Chris Craik710f46d2012-09-17 17:25:49 -07002386 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002387 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002388 }
Chris Craik65cd6122012-12-10 17:56:27 -08002389
2390 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002391}
2392
2393/**
Chris Craik65cd6122012-12-10 17:56:27 -08002394 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2395 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2396 * screen space in all directions. However, instead of using a fragment shader to compute the
2397 * translucency of the color from its position, we simply use a varying parameter to define how far
2398 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2399 *
2400 * Doesn't yet support joins, caps, or path effects.
2401 */
2402status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2403 VertexBuffer vertexBuffer;
2404 // TODO: try clipping large paths to viewport
2405 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2406
Romain Guyc46d07a2013-03-15 19:06:39 -07002407 if (hasLayer()) {
2408 SkRect bounds = path.getBounds();
2409 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2410 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2411 }
Chris Craik65cd6122012-12-10 17:56:27 -08002412
2413 return drawVertexBuffer(vertexBuffer, paint);
2414}
2415
2416/**
2417 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2418 * and additional geometry for defining an alpha slope perimeter.
2419 *
2420 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2421 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2422 * in-shader alpha region, but found it to be taxing on some GPUs.
2423 *
2424 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2425 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002426 */
Chet Haase48659092012-05-31 15:21:51 -07002427status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002428 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002429
Chris Craik65cd6122012-12-10 17:56:27 -08002430 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002431
Chris Craik65cd6122012-12-10 17:56:27 -08002432 VertexBuffer buffer;
2433 SkRect bounds;
2434 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002435
2436 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2437 return DrawGlInfo::kStatusDone;
2438 }
2439
Romain Guy3b753822013-03-05 10:27:35 -08002440 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002441
Chris Craik65cd6122012-12-10 17:56:27 -08002442 bool useOffset = !paint->isAntiAlias();
2443 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002444}
2445
Chet Haase48659092012-05-31 15:21:51 -07002446status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2447 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002448
2449 // TODO: The paint's cap style defines whether the points are square or circular
2450 // TODO: Handle AA for round points
2451
Chet Haase5b0200b2011-04-13 17:58:08 -07002452 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002453 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002454 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002455 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002456 if (isHairLine) {
2457 // Now that we know it's hairline, we can set the effective width, to be used later
2458 strokeWidth = 1.0f;
2459 }
2460 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002461
Romain Guyed6fcb02011-03-21 13:11:28 -07002462 int alpha;
2463 SkXfermode::Mode mode;
2464 getAlphaAndMode(paint, &alpha, &mode);
2465
2466 int verticesCount = count >> 1;
2467 int generatedVerticesCount = 0;
2468
2469 TextureVertex pointsData[verticesCount];
2470 TextureVertex* vertex = &pointsData[0];
2471
Romain Guy04299382012-07-18 17:15:41 -07002472 // TODO: We should optimize this method to not generate vertices for points
2473 // that lie outside of the clip.
2474 mCaches.enableScissor();
2475
Romain Guyed6fcb02011-03-21 13:11:28 -07002476 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002477 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002478 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002479 setupDrawColor(paint->getColor(), alpha);
2480 setupDrawColorFilter();
2481 setupDrawShader();
2482 setupDrawBlending(mode);
2483 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002484 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002485 setupDrawColorUniforms();
2486 setupDrawColorFilterUniforms();
2487 setupDrawPointUniforms();
2488 setupDrawShaderIdentityUniforms();
2489 setupDrawMesh(vertex);
2490
2491 for (int i = 0; i < count; i += 2) {
2492 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2493 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002494
Chet Haase8a5cc922011-04-26 07:28:09 -07002495 float left = points[i] - halfWidth;
2496 float right = points[i] + halfWidth;
2497 float top = points[i + 1] - halfWidth;
2498 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002499
Romain Guy3b753822013-03-05 10:27:35 -08002500 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002501 }
2502
2503 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002504
2505 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002506}
2507
Chet Haase48659092012-05-31 15:21:51 -07002508status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002509 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002510 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002511
Romain Guyae88e5e2010-10-22 17:49:18 -07002512 Rect& clip(*mSnapshot->clipRect);
2513 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002514
Romain Guy3d58c032010-07-14 16:34:53 -07002515 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002516
2517 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002518}
Romain Guy9d5316e2010-06-24 19:30:36 -07002519
Chet Haase48659092012-05-31 15:21:51 -07002520status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2521 SkPaint* paint) {
2522 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002523 const AutoTexture autoCleanup(texture);
2524
2525 const float x = left + texture->left - texture->offset;
2526 const float y = top + texture->top - texture->offset;
2527
2528 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002529
2530 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002531}
2532
Chet Haase48659092012-05-31 15:21:51 -07002533status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002534 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002535 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2536 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002537 return DrawGlInfo::kStatusDone;
2538 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002539
Chris Craikcb4d6002012-09-25 12:00:29 -07002540 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002541 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002542 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002543 right - left, bottom - top, rx, ry, p);
2544 return drawShape(left, top, texture, p);
2545 }
2546
2547 SkPath path;
2548 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002549 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2550 float outset = p->getStrokeWidth() / 2;
2551 rect.outset(outset, outset);
2552 rx += outset;
2553 ry += outset;
2554 }
Chris Craik710f46d2012-09-17 17:25:49 -07002555 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002556 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002557}
2558
Chris Craik710f46d2012-09-17 17:25:49 -07002559status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002560 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002561 x + radius, y + radius, p) ||
2562 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002563 return DrawGlInfo::kStatusDone;
2564 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002565 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002566 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002567 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002568 return drawShape(x - radius, y - radius, texture, p);
2569 }
2570
2571 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002572 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2573 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2574 } else {
2575 path.addCircle(x, y, radius);
2576 }
Chris Craik65cd6122012-12-10 17:56:27 -08002577 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002578}
Romain Guy01d58e42011-01-19 21:54:02 -08002579
Chet Haase48659092012-05-31 15:21:51 -07002580status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002581 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002582 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2583 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002584 return DrawGlInfo::kStatusDone;
2585 }
Romain Guy01d58e42011-01-19 21:54:02 -08002586
Chris Craikcb4d6002012-09-25 12:00:29 -07002587 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002588 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002589 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002590 return drawShape(left, top, texture, p);
2591 }
2592
2593 SkPath path;
2594 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002595 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2596 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2597 }
Chris Craik710f46d2012-09-17 17:25:49 -07002598 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002599 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002600}
2601
Chet Haase48659092012-05-31 15:21:51 -07002602status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002603 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002604 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2605 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002606 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002607 }
2608
Chris Craik780c1282012-10-04 14:10:49 -07002609 if (fabs(sweepAngle) >= 360.0f) {
2610 return drawOval(left, top, right, bottom, p);
2611 }
2612
2613 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002614 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002615 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002616 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002617 startAngle, sweepAngle, useCenter, p);
2618 return drawShape(left, top, texture, p);
2619 }
2620
2621 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2622 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2623 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2624 }
2625
2626 SkPath path;
2627 if (useCenter) {
2628 path.moveTo(rect.centerX(), rect.centerY());
2629 }
2630 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2631 if (useCenter) {
2632 path.close();
2633 }
Chris Craik65cd6122012-12-10 17:56:27 -08002634 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002635}
2636
Romain Guycf8675e2012-10-02 12:32:25 -07002637// See SkPaintDefaults.h
2638#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2639
Chet Haase48659092012-05-31 15:21:51 -07002640status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002641 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2642 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002643 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002644 }
2645
Chris Craik710f46d2012-09-17 17:25:49 -07002646 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002647 // only fill style is supported by drawConvexPath, since others have to handle joins
2648 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2649 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2650 mCaches.activeTexture(0);
2651 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002652 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002653 return drawShape(left, top, texture, p);
2654 }
2655
2656 SkPath path;
2657 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2658 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2659 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2660 }
2661 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002662 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002663 }
2664
Romain Guy3b753822013-03-05 10:27:35 -08002665 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002666 SkPath path;
2667 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002668 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002669 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002670 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002671 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002672 }
Romain Guyc7d53492010-06-25 13:41:57 -07002673}
Romain Guy9d5316e2010-06-24 19:30:36 -07002674
Raph Levien416a8472012-07-19 22:48:17 -07002675void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2676 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2677 float x, float y) {
2678 mCaches.activeTexture(0);
2679
2680 // NOTE: The drop shadow will not perform gamma correction
2681 // if shader-based correction is enabled
2682 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2683 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002684 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002685 // If the drop shadow exceeds the max texture size or couldn't be
2686 // allocated, skip drawing
2687 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002688 const AutoTexture autoCleanup(shadow);
2689
Chris Craikc3566d02013-02-04 16:16:33 -08002690 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2691 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002692
Chris Craikc3566d02013-02-04 16:16:33 -08002693 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2694 int shadowColor = mDrawModifiers.mShadowColor;
2695 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002696 shadowColor = 0xffffffff;
2697 }
2698
2699 setupDraw();
2700 setupDrawWithTexture(true);
2701 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2702 setupDrawColorFilter();
2703 setupDrawShader();
2704 setupDrawBlending(true, mode);
2705 setupDrawProgram();
2706 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2707 setupDrawTexture(shadow->id);
2708 setupDrawPureColorUniforms();
2709 setupDrawColorFilterUniforms();
2710 setupDrawShaderUniforms();
2711 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2712
2713 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2714}
2715
Romain Guy768bffc2013-02-27 13:50:45 -08002716bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2717 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2718 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2719}
2720
Romain Guy257ae352013-03-20 16:31:12 -07002721class TextSetupFunctor: public Functor {
2722public:
2723 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2724 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2725 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2726 alpha(alpha), mode(mode), paint(paint) {
2727 }
2728 ~TextSetupFunctor() { }
2729
2730 status_t operator ()(int what, void* data) {
2731 renderer.setupDraw();
2732 renderer.setupDrawTextGamma(paint);
2733 renderer.setupDrawDirtyRegionsDisabled();
2734 renderer.setupDrawWithTexture(true);
2735 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2736 renderer.setupDrawColorFilter();
2737 renderer.setupDrawShader();
2738 renderer.setupDrawBlending(true, mode);
2739 renderer.setupDrawProgram();
2740 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2741 // Calling setupDrawTexture with the name 0 will enable the
2742 // uv attributes and increase the texture unit count
2743 // texture binding will be performed by the font renderer as
2744 // needed
2745 renderer.setupDrawTexture(0);
2746 renderer.setupDrawPureColorUniforms();
2747 renderer.setupDrawColorFilterUniforms();
2748 renderer.setupDrawShaderUniforms(pureTranslate);
2749 renderer.setupDrawTextGammaUniforms();
2750
2751 return NO_ERROR;
2752 }
2753
2754 OpenGLRenderer& renderer;
2755 float x;
2756 float y;
2757 bool pureTranslate;
2758 int alpha;
2759 SkXfermode::Mode mode;
2760 SkPaint* paint;
2761};
2762
Chet Haase48659092012-05-31 15:21:51 -07002763status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002764 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002765 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002766 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002767 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002768
Romain Guy671d6cf2012-01-18 12:39:17 -08002769 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002770 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002771 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002772 }
2773
2774 float x = 0.0f;
2775 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002776 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002777 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002778 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2779 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002780 }
2781
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002782 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002783 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002784
2785 int alpha;
2786 SkXfermode::Mode mode;
2787 getAlphaAndMode(paint, &alpha, &mode);
2788
Chris Craikc3566d02013-02-04 16:16:33 -08002789 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002790 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2791 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002792 }
2793
Romain Guy671d6cf2012-01-18 12:39:17 -08002794 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002795 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002796 if (pureTranslate && !linearFilter) {
2797 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2798 }
Romain Guy257ae352013-03-20 16:31:12 -07002799 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002800
2801 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2802 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2803
Romain Guy211370f2012-02-01 16:10:55 -08002804 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002805
Romain Guy257ae352013-03-20 16:31:12 -07002806 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002807 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002808 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002809 if (hasActiveLayer) {
2810 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002811 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002812 }
2813 dirtyLayerUnchecked(bounds, getRegion());
2814 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002815 }
Chet Haase48659092012-05-31 15:21:51 -07002816
2817 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002818}
2819
Romain Guy624234f2013-03-05 16:43:31 -08002820mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2821 mat4 fontTransform;
2822 if (CC_LIKELY(transform.isPureTranslate())) {
2823 fontTransform = mat4::identity();
2824 } else {
2825 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002826 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002827 } else {
2828 float sx, sy;
2829 currentTransform().decomposeScale(sx, sy);
2830 fontTransform.loadScale(sx, sy, 1.0f);
2831 }
2832 }
2833 return fontTransform;
2834}
2835
Romain Guyc2525952012-07-27 16:41:22 -07002836status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Chris Craik527a3aa2013-03-04 10:19:31 -08002837 float x, float y, const float* positions, SkPaint* paint, float length,
2838 DrawOpMode drawOpMode) {
2839
2840 if (drawOpMode == kDrawOpMode_Immediate &&
2841 (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint))) {
Chet Haase48659092012-05-31 15:21:51 -07002842 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002843 }
2844
Chet Haasea1cff502012-02-21 13:43:44 -08002845 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002846 switch (paint->getTextAlign()) {
2847 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002848 x -= length / 2.0f;
2849 break;
2850 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002851 x -= length;
2852 break;
2853 default:
2854 break;
2855 }
2856
Romain Guycac5fd32011-12-01 20:08:50 -08002857 SkPaint::FontMetrics metrics;
2858 paint->getFontMetrics(&metrics, 0.0f);
Chris Craik527a3aa2013-03-04 10:19:31 -08002859 if (drawOpMode == kDrawOpMode_Immediate) {
2860 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
2861 return DrawGlInfo::kStatusDone;
2862 }
2863 } else {
2864 // merged draw operations don't need scissor, but clip should still be valid
2865 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
Romain Guycac5fd32011-12-01 20:08:50 -08002866 }
2867
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002868 const float oldX = x;
2869 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002870
2871 const mat4& transform = currentTransform();
2872 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002873
Romain Guy211370f2012-02-01 16:10:55 -08002874 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002875 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2876 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002877 }
2878
Romain Guy86568192010-12-14 15:55:39 -08002879 int alpha;
2880 SkXfermode::Mode mode;
2881 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002882
Romain Guyc74f45a2013-02-26 19:10:14 -08002883 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2884
Chris Craikc3566d02013-02-04 16:16:33 -08002885 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002886 fontRenderer.setFont(paint, mat4::identity());
2887 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2888 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002889 }
2890
Romain Guy19d4dd82013-03-04 11:14:26 -08002891 const bool hasActiveLayer = hasLayer();
2892
Romain Guy624234f2013-03-05 16:43:31 -08002893 // We only pass a partial transform to the font renderer. That partial
2894 // matrix defines how glyphs are rasterized. Typically we want glyphs
2895 // to be rasterized at their final size on screen, which means the partial
2896 // matrix needs to take the scale factor into account.
2897 // When a partial matrix is used to transform glyphs during rasterization,
2898 // the mesh is generated with the inverse transform (in the case of scale,
2899 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2900 // apply the full transform matrix at draw time in the vertex shader.
2901 // Applying the full matrix in the shader is the easiest way to handle
2902 // rotation and perspective and allows us to always generated quads in the
2903 // font renderer which greatly simplifies the code, clipping in particular.
2904 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002905 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002906
Romain Guy6620c6d2010-12-06 18:07:02 -08002907 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002908 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002909 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002910
Romain Guy624234f2013-03-05 16:43:31 -08002911 // TODO: Implement better clipping for scaled/rotated text
2912 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002913 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2914
Raph Levien996e57c2012-07-23 15:22:52 -07002915 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002916 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002917
2918 // don't call issuedrawcommand, do it at end of batch
2919 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002920 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002921 SkPaint paintCopy(*paint);
2922 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2923 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002924 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002925 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002926 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002927 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002928 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002929
Chris Craik527a3aa2013-03-04 10:19:31 -08002930 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002931 if (!pureTranslate) {
2932 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002933 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002934 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002935 }
Romain Guy694b5192010-07-21 21:33:20 -07002936
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002937 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002938
2939 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002940}
2941
Chet Haase48659092012-05-31 15:21:51 -07002942status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002943 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002944 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002945 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002946 }
2947
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002948 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002949 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002950 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002951
2952 int alpha;
2953 SkXfermode::Mode mode;
2954 getAlphaAndMode(paint, &alpha, &mode);
2955
Romain Guy03d58522012-02-24 17:54:07 -08002956 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002957 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002958 setupDrawDirtyRegionsDisabled();
2959 setupDrawWithTexture(true);
2960 setupDrawAlpha8Color(paint->getColor(), alpha);
2961 setupDrawColorFilter();
2962 setupDrawShader();
2963 setupDrawBlending(true, mode);
2964 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002965 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002966 // Calling setupDrawTexture with the name 0 will enable the
2967 // uv attributes and increase the texture unit count
2968 // texture binding will be performed by the font renderer as
2969 // needed
2970 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002971 setupDrawPureColorUniforms();
2972 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002973 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002974 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002975
Romain Guy97771732012-02-28 18:17:02 -08002976 const Rect* clip = &mSnapshot->getLocalClip();
2977 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 -08002978
Romain Guy97771732012-02-28 18:17:02 -08002979 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002980
2981 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2982 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002983 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002984 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002985 dirtyLayerUnchecked(bounds, getRegion());
2986 }
Romain Guy97771732012-02-28 18:17:02 -08002987 }
Chet Haase48659092012-05-31 15:21:51 -07002988
2989 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002990}
2991
Chet Haase48659092012-05-31 15:21:51 -07002992status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2993 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002994
Romain Guya1d3c912011-12-13 14:55:06 -08002995 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002996
Romain Guyfb8b7632010-08-23 21:05:08 -07002997 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002998 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002999 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003000
Romain Guy8b55f372010-08-18 17:10:07 -07003001 const float x = texture->left - texture->offset;
3002 const float y = texture->top - texture->offset;
3003
Romain Guy01d58e42011-01-19 21:54:02 -08003004 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003005
3006 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003007}
3008
Chris Craika08f95c2013-03-15 17:24:33 -07003009status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003010 if (!layer) {
3011 return DrawGlInfo::kStatusDone;
3012 }
3013
Romain Guyb2e2f242012-10-17 18:18:35 -07003014 mat4* transform = NULL;
3015 if (layer->isTextureLayer()) {
3016 transform = &layer->getTransform();
3017 if (!transform->isIdentity()) {
3018 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003019 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003020 }
3021 }
3022
Romain Guy35643dd2012-09-18 15:40:58 -07003023 Rect transformed;
3024 Rect clip;
3025 const bool rejected = quickRejectNoScissor(x, y,
3026 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
3027
3028 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003029 if (transform && !transform->isIdentity()) {
3030 restore();
3031 }
Chet Haase48659092012-05-31 15:21:51 -07003032 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003033 }
3034
Romain Guy5bb3c732012-11-29 17:52:58 -08003035 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003036
Romain Guy87e2f7572012-09-24 11:37:12 -07003037 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08003038 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003039
Romain Guy211370f2012-02-01 16:10:55 -08003040 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003041 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3042 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003043
Romain Guyc88e3572011-01-22 00:32:12 -08003044 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07003045 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08003046 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003047 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003048 setupDraw();
3049 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003050 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003051 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003052 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003053 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003054 setupDrawPureColorUniforms();
3055 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003056 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003057 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3058 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3059 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003060
Romain Guyd21b6e12011-11-30 20:21:23 -08003061 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003062 setupDrawModelViewTranslate(tx, ty,
3063 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003064 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003065 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003066 setupDrawModelViewTranslate(x, y,
3067 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3068 }
Romain Guyc88e3572011-01-22 00:32:12 -08003069 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003070
Romain Guyc88e3572011-01-22 00:32:12 -08003071 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3072 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003073
Romain Guyc88e3572011-01-22 00:32:12 -08003074 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003075
3076#if DEBUG_LAYERS_AS_REGIONS
3077 drawRegionRects(layer->region);
3078#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003079 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003080
Chris Craikc3566d02013-02-04 16:16:33 -08003081 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003082
Romain Guy5bb3c732012-11-29 17:52:58 -08003083 if (layer->debugDrawUpdate) {
3084 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003085 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3086 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3087 }
Romain Guyf219da52011-01-16 12:54:25 -08003088 }
Chet Haase48659092012-05-31 15:21:51 -07003089
Romain Guyb2e2f242012-10-17 18:18:35 -07003090 if (transform && !transform->isIdentity()) {
3091 restore();
3092 }
3093
Chet Haase48659092012-05-31 15:21:51 -07003094 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003095}
3096
Romain Guy6926c722010-07-12 20:20:03 -07003097///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003098// Shaders
3099///////////////////////////////////////////////////////////////////////////////
3100
3101void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003102 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003103}
3104
Romain Guy06f96e22010-07-30 19:18:16 -07003105void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003106 mDrawModifiers.mShader = shader;
3107 if (mDrawModifiers.mShader) {
3108 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003109 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003110}
3111
Romain Guyd27977d2010-07-14 19:18:51 -07003112///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003113// Color filters
3114///////////////////////////////////////////////////////////////////////////////
3115
3116void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003117 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003118}
3119
3120void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003121 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003122}
3123
3124///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003125// Drop shadow
3126///////////////////////////////////////////////////////////////////////////////
3127
3128void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003129 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003130}
3131
3132void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003133 mDrawModifiers.mHasShadow = true;
3134 mDrawModifiers.mShadowRadius = radius;
3135 mDrawModifiers.mShadowDx = dx;
3136 mDrawModifiers.mShadowDy = dy;
3137 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003138}
3139
3140///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003141// Draw filters
3142///////////////////////////////////////////////////////////////////////////////
3143
3144void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003145 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3146 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003147 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003148 mDrawModifiers.mPaintFilterClearBits = 0;
3149 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003150}
3151
3152void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003153 mDrawModifiers.mHasDrawFilter = true;
3154 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3155 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003156}
3157
Chris Craika08f95c2013-03-15 17:24:33 -07003158SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003159 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003160 return paint;
3161 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003162
3163 uint32_t flags = paint->getFlags();
3164
3165 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003166 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3167 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003168
3169 return &mFilteredPaint;
3170}
3171
3172///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003173// Drawing implementation
3174///////////////////////////////////////////////////////////////////////////////
3175
Romain Guy01d58e42011-01-19 21:54:02 -08003176void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3177 float x, float y, SkPaint* paint) {
3178 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3179 return;
3180 }
3181
3182 int alpha;
3183 SkXfermode::Mode mode;
3184 getAlphaAndMode(paint, &alpha, &mode);
3185
3186 setupDraw();
3187 setupDrawWithTexture(true);
3188 setupDrawAlpha8Color(paint->getColor(), alpha);
3189 setupDrawColorFilter();
3190 setupDrawShader();
3191 setupDrawBlending(true, mode);
3192 setupDrawProgram();
3193 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3194 setupDrawTexture(texture->id);
3195 setupDrawPureColorUniforms();
3196 setupDrawColorFilterUniforms();
3197 setupDrawShaderUniforms();
3198 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3199
3200 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3201
3202 finishDrawTexture();
3203}
3204
Romain Guyf607bdc2010-09-10 19:20:06 -07003205// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003206#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3207#define kStdUnderline_Offset (1.0f / 9.0f)
3208#define kStdUnderline_Thickness (1.0f / 18.0f)
3209
3210void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3211 float x, float y, SkPaint* paint) {
3212 // Handle underline and strike-through
3213 uint32_t flags = paint->getFlags();
3214 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003215 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003216 float underlineWidth = length;
3217 // If length is > 0.0f, we already measured the text for the text alignment
3218 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003219 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003220 }
3221
Romain Guy211370f2012-02-01 16:10:55 -08003222 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003223 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003224 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003225
Raph Levien8b4072d2012-07-30 15:50:00 -07003226 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003227 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003228
Romain Guyf6834472011-01-23 13:32:12 -08003229 int linesCount = 0;
3230 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3231 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3232
3233 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003234 float points[pointsCount];
3235 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003236
3237 if (flags & SkPaint::kUnderlineText_Flag) {
3238 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003239 points[currentPoint++] = left;
3240 points[currentPoint++] = top;
3241 points[currentPoint++] = left + underlineWidth;
3242 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003243 }
3244
3245 if (flags & SkPaint::kStrikeThruText_Flag) {
3246 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003247 points[currentPoint++] = left;
3248 points[currentPoint++] = top;
3249 points[currentPoint++] = left + underlineWidth;
3250 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003251 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003252
Romain Guy726aeba2011-06-01 14:52:00 -07003253 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003254
Romain Guy726aeba2011-06-01 14:52:00 -07003255 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003256 }
3257 }
3258}
3259
Romain Guy672433d2013-01-04 19:05:13 -08003260status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3261 if (mSnapshot->isIgnored()) {
3262 return DrawGlInfo::kStatusDone;
3263 }
3264
Romain Guy735738c2012-12-03 12:34:51 -08003265 int color = paint->getColor();
3266 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003267 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003268 color |= 0x00ffffff;
3269 }
3270 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3271
3272 return drawColorRects(rects, count, color, mode);
3273}
3274
3275status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003276 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003277 if (count == 0) {
3278 return DrawGlInfo::kStatusDone;
3279 }
Romain Guy735738c2012-12-03 12:34:51 -08003280
Romain Guy672433d2013-01-04 19:05:13 -08003281 float left = FLT_MAX;
3282 float top = FLT_MAX;
3283 float right = FLT_MIN;
3284 float bottom = FLT_MIN;
3285
3286 int vertexCount = 0;
3287 Vertex mesh[count * 6];
3288 Vertex* vertex = mesh;
3289
Chris Craik2af46352012-11-26 18:30:17 -08003290 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003291 float l = rects[index + 0];
3292 float t = rects[index + 1];
3293 float r = rects[index + 2];
3294 float b = rects[index + 3];
3295
Romain Guy3b753822013-03-05 10:27:35 -08003296 Vertex::set(vertex++, l, b);
3297 Vertex::set(vertex++, l, t);
3298 Vertex::set(vertex++, r, t);
3299 Vertex::set(vertex++, l, b);
3300 Vertex::set(vertex++, r, t);
3301 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003302
Romain Guy3b753822013-03-05 10:27:35 -08003303 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003304
Romain Guy3b753822013-03-05 10:27:35 -08003305 left = fminf(left, l);
3306 top = fminf(top, t);
3307 right = fmaxf(right, r);
3308 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003309 }
3310
Romain Guy3b753822013-03-05 10:27:35 -08003311 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003312 return DrawGlInfo::kStatusDone;
3313 }
Romain Guy672433d2013-01-04 19:05:13 -08003314
Romain Guy672433d2013-01-04 19:05:13 -08003315 setupDraw();
3316 setupDrawNoTexture();
3317 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3318 setupDrawShader();
3319 setupDrawColorFilter();
3320 setupDrawBlending(mode);
3321 setupDrawProgram();
3322 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003323 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003324 setupDrawColorUniforms();
3325 setupDrawShaderUniforms();
3326 setupDrawColorFilterUniforms();
3327 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3328
Romain Guy8ce00302013-01-15 18:51:42 -08003329 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003330 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003331 }
3332
3333 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3334
3335 return DrawGlInfo::kStatusDrew;
3336}
3337
Romain Guy026c5e162010-06-28 17:12:22 -07003338void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003339 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003340 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003341 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003342 color |= 0x00ffffff;
3343 }
3344
Romain Guy70ca14e2010-12-13 18:24:33 -08003345 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003346 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003347 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003348 setupDrawShader();
3349 setupDrawColorFilter();
3350 setupDrawBlending(mode);
3351 setupDrawProgram();
3352 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3353 setupDrawColorUniforms();
3354 setupDrawShaderUniforms(ignoreTransform);
3355 setupDrawColorFilterUniforms();
3356 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003357
Romain Guyc95c8d62010-09-17 15:31:32 -07003358 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3359}
3360
Romain Guy82ba8142010-07-09 13:25:56 -07003361void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003362 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003363 int alpha;
3364 SkXfermode::Mode mode;
3365 getAlphaAndMode(paint, &alpha, &mode);
3366
Romain Guyd21b6e12011-11-30 20:21:23 -08003367 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003368
Romain Guy3b753822013-03-05 10:27:35 -08003369 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3370 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3371 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003372
Romain Guyd21b6e12011-11-30 20:21:23 -08003373 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003374 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3375 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3376 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3377 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003378 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003379 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3380 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3381 GL_TRIANGLE_STRIP, gMeshCount);
3382 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003383}
3384
Romain Guybd6b79b2010-06-26 00:13:53 -07003385void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003386 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3387 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003388 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003389}
3390
3391void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003392 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003393 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003394 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003395
Romain Guy746b7402010-10-26 16:27:31 -07003396 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003397 setupDrawWithTexture();
3398 setupDrawColor(alpha, alpha, alpha, alpha);
3399 setupDrawColorFilter();
3400 setupDrawBlending(blend, mode, swapSrcDst);
3401 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003402 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003403 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003404 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003405 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003406 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003407 }
Romain Guy886b2752013-01-04 12:26:18 -08003408 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003409 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003410 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003411 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003412
Romain Guy6820ac82010-09-15 18:11:50 -07003413 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003414
3415 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003416}
3417
Romain Guy886b2752013-01-04 12:26:18 -08003418void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3419 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3420 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003421 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003422
3423 setupDraw();
3424 setupDrawWithTexture(true);
3425 if (hasColor) {
3426 setupDrawAlpha8Color(color, alpha);
3427 }
3428 setupDrawColorFilter();
3429 setupDrawShader();
3430 setupDrawBlending(true, mode);
3431 setupDrawProgram();
3432 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003433 if (!ignoreScale) {
3434 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3435 } else {
3436 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3437 }
Romain Guy886b2752013-01-04 12:26:18 -08003438 setupDrawTexture(texture);
3439 setupDrawPureColorUniforms();
3440 setupDrawColorFilterUniforms();
3441 setupDrawShaderUniforms();
3442 setupDrawMesh(vertices, texCoords);
3443
3444 glDrawArrays(drawMode, 0, elementsCount);
3445
3446 finishDrawTexture();
3447}
3448
Romain Guya5aed0d2010-09-09 14:42:43 -07003449void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003450 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003451 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003452
Romain Guy82ba8142010-07-09 13:25:56 -07003453 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003454 // These blend modes are not supported by OpenGL directly and have
3455 // to be implemented using shaders. Since the shader will perform
3456 // the blending, turn blending off here
3457 // If the blend mode cannot be implemented using shaders, fall
3458 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003459 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003460 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003461 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003462 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003463
Romain Guy82bc7a72012-01-03 14:13:39 -08003464 if (mCaches.blend) {
3465 glDisable(GL_BLEND);
3466 mCaches.blend = false;
3467 }
3468
3469 return;
3470 } else {
3471 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003472 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003473 }
3474
3475 if (!mCaches.blend) {
3476 glEnable(GL_BLEND);
3477 }
3478
3479 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3480 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3481
3482 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3483 glBlendFunc(sourceMode, destMode);
3484 mCaches.lastSrcMode = sourceMode;
3485 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003486 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003487 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003488 glDisable(GL_BLEND);
3489 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003490 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003491}
3492
Romain Guy889f8d12010-07-29 14:37:42 -07003493bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003494 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003495 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003496 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003497 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003498 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003499 }
Romain Guy6926c722010-07-12 20:20:03 -07003500 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003501}
3502
Romain Guy026c5e162010-06-28 17:12:22 -07003503void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003504 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003505 TextureVertex::setUV(v++, u1, v1);
3506 TextureVertex::setUV(v++, u2, v1);
3507 TextureVertex::setUV(v++, u1, v2);
3508 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003509}
3510
Chris Craik16ecda52013-03-29 10:59:59 -07003511void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003512 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003513 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3514 // if drawing a layer, ignore the paint's alpha
3515 *alpha = mDrawModifiers.mOverrideLayerAlpha;
3516 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003517 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003518}
3519
Chris Craik16ecda52013-03-29 10:59:59 -07003520float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3521 float alpha;
3522 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3523 alpha = mDrawModifiers.mOverrideLayerAlpha;
3524 } else {
3525 alpha = layer->getAlpha() / 255.0f;
3526 }
3527 return alpha * mSnapshot->alpha;
3528}
3529
Romain Guy9d5316e2010-06-24 19:30:36 -07003530}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003531}; // namespace android