blob: 025e9f8e6cafd7c6efe688bc0aaccaa8a1a46156 [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
Chris Craik4a2bff72013-04-16 13:50:16 -0700462 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
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();
Chris Craik4a2bff72013-04-16 13:50:16 -0700474 return result | DrawGlInfo::kStatusDrew;
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;
Chris Craik34416ea2013-04-15 16:08:28 -0700553 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700554
555 return true;
556 }
557
558 return false;
559}
560
561void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700562 // If draw deferring is enabled this method will simply defer
563 // the display list of each individual layer. The layers remain
564 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700565 int count = mLayerUpdates.size();
566 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700567 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
568 startMark("Layer Updates");
569 } else {
570 startMark("Defer Layer Updates");
571 }
Romain Guy11cb6422012-09-21 00:39:43 -0700572
Chris Craik1206b9b2013-04-04 14:46:24 -0700573 // Note: it is very important to update the layers in order
574 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700575 Layer* layer = mLayerUpdates.itemAt(i);
576 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700577 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
578 mCaches.resourceCache.decrementRefcount(layer);
579 }
Romain Guy11cb6422012-09-21 00:39:43 -0700580 }
Romain Guy11cb6422012-09-21 00:39:43 -0700581
Romain Guy96885eb2013-03-26 15:05:58 -0700582 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
583 mLayerUpdates.clear();
584 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
585 }
586 endMark();
587 }
588}
589
590void OpenGLRenderer::flushLayers() {
591 int count = mLayerUpdates.size();
592 if (count > 0) {
593 startMark("Apply Layer Updates");
594 char layerName[12];
595
Chris Craik1206b9b2013-04-04 14:46:24 -0700596 // Note: it is very important to update the layers in order
597 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700598 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700599 startMark(layerName);
600
601 Layer* layer = mLayerUpdates.itemAt(i);
602 layer->flush();
603 mCaches.resourceCache.decrementRefcount(layer);
604
Romain Guy96885eb2013-03-26 15:05:58 -0700605 endMark();
606 }
607
608 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700609 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700610
Romain Guy11cb6422012-09-21 00:39:43 -0700611 endMark();
612 }
613}
614
615void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
616 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700617 // Make sure we don't introduce duplicates.
618 // SortedVector would do this automatically but we need to respect
619 // the insertion order. The linear search is not an issue since
620 // this list is usually very short (typically one item, at most a few)
621 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
622 if (mLayerUpdates.itemAt(i) == layer) {
623 return;
624 }
625 }
Romain Guy11cb6422012-09-21 00:39:43 -0700626 mLayerUpdates.push_back(layer);
627 mCaches.resourceCache.incrementRefcount(layer);
628 }
629}
630
631void OpenGLRenderer::clearLayerUpdates() {
632 size_t count = mLayerUpdates.size();
633 if (count > 0) {
634 mCaches.resourceCache.lock();
635 for (size_t i = 0; i < count; i++) {
636 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
637 }
638 mCaches.resourceCache.unlock();
639 mLayerUpdates.clear();
640 }
641}
642
643///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700644// State management
645///////////////////////////////////////////////////////////////////////////////
646
Romain Guybb9524b2010-06-22 18:56:38 -0700647int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700648 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700649}
650
651int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700652 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700653}
654
655void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700656 if (mSaveCount > 1) {
657 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700658 }
Romain Guybb9524b2010-06-22 18:56:38 -0700659}
660
661void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700662 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700663
Romain Guy8fb95422010-08-17 18:38:51 -0700664 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700665 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700666 }
Romain Guybb9524b2010-06-22 18:56:38 -0700667}
668
Romain Guy8aef54f2010-09-01 15:13:49 -0700669int OpenGLRenderer::saveSnapshot(int flags) {
670 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700671 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700672}
673
674bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700675 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700676 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700677 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700678
Romain Guybd6b79b2010-06-26 00:13:53 -0700679 sp<Snapshot> current = mSnapshot;
680 sp<Snapshot> previous = mSnapshot->previous;
681
Romain Guyeb993562010-10-05 18:14:38 -0700682 if (restoreOrtho) {
683 Rect& r = previous->viewport;
684 glViewport(r.left, r.top, r.right, r.bottom);
685 mOrthoMatrix.load(current->orthoMatrix);
686 }
687
Romain Guy8b55f372010-08-18 17:10:07 -0700688 mSaveCount--;
689 mSnapshot = previous;
690
Romain Guy2542d192010-08-18 11:47:12 -0700691 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700692 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700693 }
Romain Guy2542d192010-08-18 11:47:12 -0700694
Romain Guy5ec99242010-11-03 16:19:08 -0700695 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700696 endMark(); // Savelayer
697 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700698 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700699 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700700 }
701
Romain Guy2542d192010-08-18 11:47:12 -0700702 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700703}
704
Romain Guyf6a11b82010-06-23 17:47:49 -0700705///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700706// Layers
707///////////////////////////////////////////////////////////////////////////////
708
709int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800710 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700711 const GLuint previousFbo = mSnapshot->fbo;
712 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700713
Romain Guyaf636eb2010-12-09 17:47:21 -0800714 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700715 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700716 }
Romain Guyd55a8612010-06-28 17:42:46 -0700717
718 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700719}
720
Chris Craikd90144d2013-03-19 15:03:48 -0700721void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
722 const Rect untransformedBounds(bounds);
723
724 currentTransform().mapRect(bounds);
725
726 // Layers only make sense if they are in the framebuffer's bounds
727 if (bounds.intersect(*mSnapshot->clipRect)) {
728 // We cannot work with sub-pixels in this case
729 bounds.snapToPixelBoundaries();
730
731 // When the layer is not an FBO, we may use glCopyTexImage so we
732 // need to make sure the layer does not extend outside the bounds
733 // of the framebuffer
734 if (!bounds.intersect(mSnapshot->previous->viewport)) {
735 bounds.setEmpty();
736 } else if (fboLayer) {
737 clip.set(bounds);
738 mat4 inverse;
739 inverse.loadInverse(currentTransform());
740 inverse.mapRect(clip);
741 clip.snapToPixelBoundaries();
742 if (clip.intersect(untransformedBounds)) {
743 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
744 bounds.set(untransformedBounds);
745 } else {
746 clip.setEmpty();
747 }
748 }
749 } else {
750 bounds.setEmpty();
751 }
752}
753
Chris Craik408eb122013-03-26 18:55:15 -0700754void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
755 bool fboLayer, int alpha) {
756 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
757 bounds.getHeight() > mCaches.maxTextureSize ||
758 (fboLayer && clip.isEmpty())) {
759 mSnapshot->empty = fboLayer;
760 } else {
761 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
762 }
763}
764
Chris Craikd90144d2013-03-19 15:03:48 -0700765int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
766 int alpha, SkXfermode::Mode mode, int flags) {
767 const GLuint previousFbo = mSnapshot->fbo;
768 const int count = saveSnapshot(flags);
769
770 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
771 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
772 // operations will be able to store and restore the current clip and transform info, and
773 // quick rejection will be correct (for display lists)
774
775 Rect bounds(left, top, right, bottom);
776 Rect clip;
777 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700778 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700779
Chris Craik408eb122013-03-26 18:55:15 -0700780 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700781 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
782 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
783 }
784 }
785
786 return count;
787}
788
789
Romain Guy1c740bc2010-09-13 18:00:09 -0700790/**
791 * Layers are viewed by Skia are slightly different than layers in image editing
792 * programs (for instance.) When a layer is created, previously created layers
793 * and the frame buffer still receive every drawing command. For instance, if a
794 * layer is created and a shape intersecting the bounds of the layers and the
795 * framebuffer is draw, the shape will be drawn on both (unless the layer was
796 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
797 *
798 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
799 * texture. Unfortunately, this is inefficient as it requires every primitive to
800 * be drawn n + 1 times, where n is the number of active layers. In practice this
801 * means, for every primitive:
802 * - Switch active frame buffer
803 * - Change viewport, clip and projection matrix
804 * - Issue the drawing
805 *
806 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700807 * To avoid this, layers are implemented in a different way here, at least in the
808 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
809 * is set. When this flag is set we can redirect all drawing operations into a
810 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700811 *
812 * This implementation relies on the frame buffer being at least RGBA 8888. When
813 * a layer is created, only a texture is created, not an FBO. The content of the
814 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700815 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700816 * buffer and drawing continues as normal. This technique therefore treats the
817 * frame buffer as a scratch buffer for the layers.
818 *
819 * To compose the layers back onto the frame buffer, each layer texture
820 * (containing the original frame buffer data) is drawn as a simple quad over
821 * the frame buffer. The trick is that the quad is set as the composition
822 * destination in the blending equation, and the frame buffer becomes the source
823 * of the composition.
824 *
825 * Drawing layers with an alpha value requires an extra step before composition.
826 * An empty quad is drawn over the layer's region in the frame buffer. This quad
827 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
828 * quad is used to multiply the colors in the frame buffer. This is achieved by
829 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
830 * GL_ZERO, GL_SRC_ALPHA.
831 *
832 * Because glCopyTexImage2D() can be slow, an alternative implementation might
833 * be use to draw a single clipped layer. The implementation described above
834 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700835 *
836 * (1) The frame buffer is actually not cleared right away. To allow the GPU
837 * to potentially optimize series of calls to glCopyTexImage2D, the frame
838 * buffer is left untouched until the first drawing operation. Only when
839 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700840 */
Chet Haased48885a2012-08-28 17:43:28 -0700841bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
842 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700843 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700844 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700845
Romain Guyeb993562010-10-05 18:14:38 -0700846 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
847
Romain Guyf607bdc2010-09-10 19:20:06 -0700848 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700849 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700850 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700851 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700852 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700853
854 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700855 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700856 return false;
857 }
Romain Guyf18fd992010-07-08 11:45:51 -0700858
Romain Guya1d3c912011-12-13 14:55:06 -0800859 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700860 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700861 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700862 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700863 }
864
Romain Guy9ace8f52011-07-07 20:50:11 -0700865 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700866 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700867 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
868 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800869 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700870 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700871 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700872
Romain Guy8fb95422010-08-17 18:38:51 -0700873 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700874 mSnapshot->flags |= Snapshot::kFlagIsLayer;
875 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700876
Chris Craik7273daa2013-03-28 11:25:24 -0700877 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700878 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700879 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700880 } else {
881 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700882 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800883 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700884 if (layer->isEmpty()) {
885 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700886 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700887 layer->getWidth(), layer->getHeight(), 0);
888 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800889 } else {
890 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700891 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800892 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700893
Romain Guy54be1cd2011-06-13 19:04:27 -0700894 // Enqueue the buffer coordinates to clear the corresponding region later
895 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700896 }
Romain Guyeb993562010-10-05 18:14:38 -0700897 }
Romain Guyf86ef572010-07-01 11:05:42 -0700898
Romain Guyd55a8612010-06-28 17:42:46 -0700899 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700900}
901
Chet Haased48885a2012-08-28 17:43:28 -0700902bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800903 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700905
Chet Haased48885a2012-08-28 17:43:28 -0700906 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800907 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
908 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700909 mSnapshot->fbo = layer->getFbo();
910 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
911 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
912 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
913 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700914 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700915
Romain Guy2b7028e2012-09-19 17:25:38 -0700916 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700917 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700918 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700919 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
920 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700921
922 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700924 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700925 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700926 }
927
928 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700929 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700930
Romain Guyf735c8e2013-01-31 17:45:55 -0800931 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700932
933 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700934 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800935 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700936 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 glClear(GL_COLOR_BUFFER_BIT);
938
939 dirtyClip();
940
941 // Change the ortho projection
942 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
943 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
944
945 return true;
946}
947
Romain Guy1c740bc2010-09-13 18:00:09 -0700948/**
949 * Read the documentation of createLayer() before doing anything in this method.
950 */
Romain Guy1d83e192010-08-17 11:37:00 -0700951void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
952 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000953 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700954 return;
955 }
956
Romain Guy8ce00302013-01-15 18:51:42 -0800957 Layer* layer = current->layer;
958 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700959 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700960
961 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700962 endTiling();
963
Romain Guye0aa84b2012-04-03 19:30:26 -0700964 // Detach the texture from the FBO
965 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800966
967 layer->removeFbo(false);
968
Romain Guyeb993562010-10-05 18:14:38 -0700969 // Unbind current FBO and restore previous one
970 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700971 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700972
973 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700974 }
975
Romain Guy9ace8f52011-07-07 20:50:11 -0700976 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700977 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700981 }
982
Romain Guy03750a02010-10-18 14:06:08 -0700983 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700984
Romain Guya1d3c912011-12-13 14:55:06 -0800985 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700986
Romain Guy5b3b3522010-10-27 18:57:51 -0700987 // When the layer is stored in an FBO, we can save a bit of fillrate by
988 // drawing only the dirty region
989 if (fboLayer) {
990 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 if (layer->getColorFilter()) {
992 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800993 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700994 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800996 resetColorFilter();
997 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 } else if (!rect.isEmpty()) {
999 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1000 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001001 }
Romain Guy8b55f372010-08-18 17:10:07 -07001002
Romain Guy746b7402010-10-26 16:27:31 -07001003 dirtyClip();
1004
Romain Guyeb993562010-10-05 18:14:38 -07001005 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001006 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001007 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001008 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001009 }
1010}
1011
Romain Guyaa6c24c2011-04-28 18:40:04 -07001012void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001013 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001014
1015 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001017 setupDrawWithTexture();
1018 } else {
1019 setupDrawWithExternalTexture();
1020 }
1021 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001022 setupDrawColor(alpha, alpha, alpha, alpha);
1023 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001024 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001025 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001026 setupDrawPureColorUniforms();
1027 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001028 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1029 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001030 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001031 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001032 }
Romain Guy3b753822013-03-05 10:27:35 -08001033 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001034 layer->getWidth() == (uint32_t) rect.getWidth() &&
1035 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001036 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1037 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001038
Romain Guyd21b6e12011-11-30 20:21:23 -08001039 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001040 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1041 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001042 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001043 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1044 }
1045 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001046 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1047
1048 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1049
1050 finishDrawTexture();
1051}
1052
Romain Guy5b3b3522010-10-27 18:57:51 -07001053void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001054 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001055 const Rect& texCoords = layer->texCoords;
1056 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1057 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001058
Romain Guy9ace8f52011-07-07 20:50:11 -07001059 float x = rect.left;
1060 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001061 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001062 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001063 layer->getHeight() == (uint32_t) rect.getHeight();
1064
1065 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001066 // When we're swapping, the layer is already in screen coordinates
1067 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001068 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1069 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001070 }
1071
Romain Guyd21b6e12011-11-30 20:21:23 -08001072 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001073 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001074 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 }
1076
Chris Craik16ecda52013-03-29 10:59:59 -07001077 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001078 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001079 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001080 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001081 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1082 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001083
Romain Guyaa6c24c2011-04-28 18:40:04 -07001084 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1085 } else {
1086 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1087 drawTextureLayer(layer, rect);
1088 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1089 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001090}
1091
Chris Craik34416ea2013-04-15 16:08:28 -07001092/**
1093 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1094 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1095 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1096 * by saveLayer's restore
1097 */
1098#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1099 DRAW_COMMAND; \
1100 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1101 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1102 DRAW_COMMAND; \
1103 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1104 } \
1105 }
1106
1107#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1108
Romain Guy5b3b3522010-10-27 18:57:51 -07001109void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001110 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001111 layer->setRegionAsRect();
1112
Chris Craik34416ea2013-04-15 16:08:28 -07001113 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001114
Romain Guy5b3b3522010-10-27 18:57:51 -07001115 layer->region.clear();
1116 return;
1117 }
1118
Romain Guy8a3957d2011-09-07 17:55:15 -07001119 // TODO: See LayerRenderer.cpp::generateMesh() for important
1120 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001121 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001122 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001123 const android::Rect* rects;
1124 Region safeRegion;
1125 if (CC_LIKELY(hasRectToRectTransform())) {
1126 rects = layer->region.getArray(&count);
1127 } else {
1128 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1129 rects = safeRegion.getArray(&count);
1130 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001131
Chris Craik16ecda52013-03-29 10:59:59 -07001132 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001133 const float texX = 1.0f / float(layer->getWidth());
1134 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001135 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001136
Romain Guy8ce00302013-01-15 18:51:42 -08001137 setupDraw();
1138
1139 // We must get (and therefore bind) the region mesh buffer
1140 // after we setup drawing in case we need to mess with the
1141 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001142 TextureVertex* mesh = mCaches.getRegionMesh();
1143 GLsizei numQuads = 0;
1144
Romain Guy7230a742011-01-10 22:26:16 -08001145 setupDrawWithTexture();
1146 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001147 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001148 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001149 setupDrawProgram();
1150 setupDrawDirtyRegionsDisabled();
1151 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001152 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001153 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001154 if (currentTransform().isPureTranslate()) {
1155 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1156 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001157
Romain Guyd21b6e12011-11-30 20:21:23 -08001158 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001159 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1160 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001161 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001162 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1163 }
Romain Guy15bc6432011-12-13 13:11:32 -08001164 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001165
1166 for (size_t i = 0; i < count; i++) {
1167 const android::Rect* r = &rects[i];
1168
1169 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001170 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001171 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001172 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001173
1174 // TODO: Reject quads outside of the clip
1175 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1176 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1177 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1178 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1179
1180 numQuads++;
1181
1182 if (numQuads >= REGION_MESH_QUAD_COUNT) {
Chris Craik34416ea2013-04-15 16:08:28 -07001183 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1184 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001185 numQuads = 0;
1186 mesh = mCaches.getRegionMesh();
1187 }
1188 }
1189
1190 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001191 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1192 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001193 }
1194
Romain Guy7230a742011-01-10 22:26:16 -08001195 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001196
1197#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001198 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001199#endif
1200
1201 layer->region.clear();
1202 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001203}
1204
Romain Guy3a3133d2011-02-01 22:59:58 -08001205void OpenGLRenderer::drawRegionRects(const Region& region) {
1206#if DEBUG_LAYERS_AS_REGIONS
1207 size_t count;
1208 const android::Rect* rects = region.getArray(&count);
1209
1210 uint32_t colors[] = {
1211 0x7fff0000, 0x7f00ff00,
1212 0x7f0000ff, 0x7fff00ff,
1213 };
1214
1215 int offset = 0;
1216 int32_t top = rects[0].top;
1217
1218 for (size_t i = 0; i < count; i++) {
1219 if (top != rects[i].top) {
1220 offset ^= 0x2;
1221 top = rects[i].top;
1222 }
1223
1224 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1225 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1226 SkXfermode::kSrcOver_Mode);
1227 }
1228#endif
1229}
1230
Romain Guy8ce00302013-01-15 18:51:42 -08001231void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1232 SkXfermode::Mode mode, bool dirty) {
1233 int count = 0;
1234 Vector<float> rects;
1235
1236 SkRegion::Iterator it(region);
1237 while (!it.done()) {
1238 const SkIRect& r = it.rect();
1239 rects.push(r.fLeft);
1240 rects.push(r.fTop);
1241 rects.push(r.fRight);
1242 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001243 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001244 it.next();
1245 }
1246
Romain Guy3bbacf22013-02-06 16:51:04 -08001247 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001248}
1249
Romain Guy5b3b3522010-10-27 18:57:51 -07001250void OpenGLRenderer::dirtyLayer(const float left, const float top,
1251 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001252 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001253 Rect bounds(left, top, right, bottom);
1254 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001255 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001257}
1258
1259void OpenGLRenderer::dirtyLayer(const float left, const float top,
1260 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001261 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001262 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001263 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001264 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001265}
1266
1267void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001268 if (bounds.intersect(*mSnapshot->clipRect)) {
1269 bounds.snapToPixelBoundaries();
1270 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1271 if (!dirty.isEmpty()) {
1272 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001273 }
1274 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001275}
1276
Romain Guy54be1cd2011-06-13 19:04:27 -07001277void OpenGLRenderer::clearLayerRegions() {
1278 const size_t count = mLayers.size();
1279 if (count == 0) return;
1280
1281 if (!mSnapshot->isIgnored()) {
1282 // Doing several glScissor/glClear here can negatively impact
1283 // GPUs with a tiler architecture, instead we draw quads with
1284 // the Clear blending mode
1285
1286 // The list contains bounds that have already been clipped
1287 // against their initial clip rect, and the current clip
1288 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001289 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001290
1291 Vertex mesh[count * 6];
1292 Vertex* vertex = mesh;
1293
1294 for (uint32_t i = 0; i < count; i++) {
1295 Rect* bounds = mLayers.itemAt(i);
1296
1297 Vertex::set(vertex++, bounds->left, bounds->bottom);
1298 Vertex::set(vertex++, bounds->left, bounds->top);
1299 Vertex::set(vertex++, bounds->right, bounds->top);
1300 Vertex::set(vertex++, bounds->left, bounds->bottom);
1301 Vertex::set(vertex++, bounds->right, bounds->top);
1302 Vertex::set(vertex++, bounds->right, bounds->bottom);
1303
1304 delete bounds;
1305 }
Romain Guye67307c2013-02-11 18:01:20 -08001306 // We must clear the list of dirty rects before we
1307 // call setupDraw() to prevent stencil setup to do
1308 // the same thing again
1309 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001310
1311 setupDraw(false);
1312 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1313 setupDrawBlending(true, SkXfermode::kClear_Mode);
1314 setupDrawProgram();
1315 setupDrawPureColorUniforms();
1316 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001317 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001318
Romain Guy54be1cd2011-06-13 19:04:27 -07001319 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001320
1321 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001322 } else {
1323 for (uint32_t i = 0; i < count; i++) {
1324 delete mLayers.itemAt(i);
1325 }
Romain Guye67307c2013-02-11 18:01:20 -08001326 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001327 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001328}
1329
Romain Guybd6b79b2010-06-26 00:13:53 -07001330///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001331// State Deferral
1332///////////////////////////////////////////////////////////////////////////////
1333
Chris Craikff785832013-03-08 13:12:16 -08001334bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001335 const Rect& currentClip = *(mSnapshot->clipRect);
1336 const mat4& currentMatrix = *(mSnapshot->transform);
1337
Chris Craikff785832013-03-08 13:12:16 -08001338 if (stateDeferFlags & kStateDeferFlag_Draw) {
1339 // state has bounds initialized in local coordinates
1340 if (!state.mBounds.isEmpty()) {
1341 currentMatrix.mapRect(state.mBounds);
1342 if (!state.mBounds.intersect(currentClip)) {
1343 // quick rejected
1344 return true;
1345 }
1346 } else {
1347 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001348 }
1349 }
1350
Chris Craik527a3aa2013-03-04 10:19:31 -08001351 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1352 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001353 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001354 }
1355
Chris Craik7273daa2013-03-28 11:25:24 -07001356 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1357 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001358 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001359 state.mDrawModifiers = mDrawModifiers;
1360 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001361 return false;
1362}
1363
Chris Craik527a3aa2013-03-04 10:19:31 -08001364void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001365 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001366 mDrawModifiers = state.mDrawModifiers;
1367 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001368
Chris Craik527a3aa2013-03-04 10:19:31 -08001369 if (state.mClipValid && !skipClipRestore) {
Chris Craikff785832013-03-08 13:12:16 -08001370 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1371 dirtyClip();
1372 }
Chris Craikc3566d02013-02-04 16:16:33 -08001373}
1374
Chris Craik527a3aa2013-03-04 10:19:31 -08001375void OpenGLRenderer::setFullScreenClip() {
1376 mSnapshot->setClip(0, 0, mWidth, mHeight);
1377 dirtyClip();
1378}
1379
Chris Craikc3566d02013-02-04 16:16:33 -08001380///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001381// Transforms
1382///////////////////////////////////////////////////////////////////////////////
1383
1384void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001385 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001386}
1387
1388void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001389 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001390}
1391
1392void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001393 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001394}
1395
Romain Guy807daf72011-01-18 11:19:19 -08001396void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001397 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001398}
1399
Romain Guyf6a11b82010-06-23 17:47:49 -07001400void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001401 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001402 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001403 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001404 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001405 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001406}
1407
Chris Craikb98a0162013-02-21 11:30:22 -08001408bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001409 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001410}
1411
Romain Guyf6a11b82010-06-23 17:47:49 -07001412void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001413 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001414}
1415
1416void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001417 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001418 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001419 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001420 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001421}
1422
1423///////////////////////////////////////////////////////////////////////////////
1424// Clipping
1425///////////////////////////////////////////////////////////////////////////////
1426
Romain Guybb9524b2010-06-22 18:56:38 -07001427void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001428 Rect clip(*mSnapshot->clipRect);
1429 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001430
Romain Guy8a4ac612012-07-17 17:32:48 -07001431 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1432 clip.getWidth(), clip.getHeight())) {
1433 mDirtyClip = false;
1434 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001435}
1436
Romain Guy8ce00302013-01-15 18:51:42 -08001437void OpenGLRenderer::ensureStencilBuffer() {
1438 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1439 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1440 // just hope we have one when hasLayer() returns false.
1441 if (hasLayer()) {
1442 attachStencilBufferToLayer(mSnapshot->layer);
1443 }
1444}
1445
1446void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1447 // The layer's FBO is already bound when we reach this stage
1448 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001449 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1450 // is attached after we initiated tiling. We must turn it off,
1451 // attach the new render buffer then turn tiling back on
1452 endTiling();
1453
Romain Guy8d4aeb72013-02-12 16:08:55 -08001454 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001455 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001456 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001457
Romain Guyf735c8e2013-01-31 17:45:55 -08001458 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001459 }
1460}
1461
1462void OpenGLRenderer::setStencilFromClip() {
1463 if (!mCaches.debugOverdraw) {
1464 if (!mSnapshot->clipRegion->isEmpty()) {
1465 // NOTE: The order here is important, we must set dirtyClip to false
1466 // before any draw call to avoid calling back into this method
1467 mDirtyClip = false;
1468
1469 ensureStencilBuffer();
1470
1471 mCaches.stencil.enableWrite();
1472
1473 // Clear the stencil but first make sure we restrict drawing
1474 // to the region's bounds
1475 bool resetScissor = mCaches.enableScissor();
1476 if (resetScissor) {
1477 // The scissor was not set so we now need to update it
1478 setScissorFromClip();
1479 }
1480 mCaches.stencil.clear();
1481 if (resetScissor) mCaches.disableScissor();
1482
1483 // NOTE: We could use the region contour path to generate a smaller mesh
1484 // Since we are using the stencil we could use the red book path
1485 // drawing technique. It might increase bandwidth usage though.
1486
1487 // The last parameter is important: we are not drawing in the color buffer
1488 // so we don't want to dirty the current layer, if any
1489 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1490
1491 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001492
1493 // Draw the region used to generate the stencil if the appropriate debug
1494 // mode is enabled
1495 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1496 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1497 }
Romain Guy8ce00302013-01-15 18:51:42 -08001498 } else {
1499 mCaches.stencil.disable();
1500 }
1501 }
1502}
1503
Romain Guy9d5316e2010-06-24 19:30:36 -07001504const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001505 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001506}
1507
Romain Guy8a4ac612012-07-17 17:32:48 -07001508bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1509 if (mSnapshot->isIgnored()) {
1510 return true;
1511 }
1512
1513 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001514 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001515 r.snapToPixelBoundaries();
1516
1517 Rect clipRect(*mSnapshot->clipRect);
1518 clipRect.snapToPixelBoundaries();
1519
1520 return !clipRect.intersects(r);
1521}
1522
Romain Guy35643dd2012-09-18 15:40:58 -07001523bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1524 Rect& transformed, Rect& clip) {
1525 if (mSnapshot->isIgnored()) {
1526 return true;
1527 }
1528
1529 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001530 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001531 transformed.snapToPixelBoundaries();
1532
1533 clip.set(*mSnapshot->clipRect);
1534 clip.snapToPixelBoundaries();
1535
1536 return !clip.intersects(transformed);
1537}
1538
Romain Guy672433d2013-01-04 19:05:13 -08001539bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1540 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001541 if (paint->getStyle() != SkPaint::kFill_Style) {
1542 float outset = paint->getStrokeWidth() * 0.5f;
1543 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1544 } else {
1545 return quickReject(left, top, right, bottom);
1546 }
1547}
1548
Romain Guyc7d53492010-06-25 13:41:57 -07001549bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001550 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001551 return true;
1552 }
1553
Romain Guy1d83e192010-08-17 11:37:00 -07001554 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001555 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001556 r.snapToPixelBoundaries();
1557
1558 Rect clipRect(*mSnapshot->clipRect);
1559 clipRect.snapToPixelBoundaries();
1560
Romain Guy586cae32012-07-13 15:28:31 -07001561 bool rejected = !clipRect.intersects(r);
1562 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001563 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001564 }
1565
1566 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001567}
1568
Romain Guy8ce00302013-01-15 18:51:42 -08001569void OpenGLRenderer::debugClip() {
1570#if DEBUG_CLIP_REGIONS
1571 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1572 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1573 }
1574#endif
1575}
1576
Romain Guy079ba2c2010-07-16 14:12:24 -07001577bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001578 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001579 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1580 if (clipped) {
1581 dirtyClip();
1582 }
1583 return !mSnapshot->clipRect->isEmpty();
1584 }
1585
1586 SkPath path;
1587 path.addRect(left, top, right, bottom);
1588
1589 return clipPath(&path, op);
1590}
1591
1592bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1593 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001594 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001595
1596 SkPath transformed;
1597 path->transform(transform, &transformed);
1598
1599 SkRegion clip;
1600 if (!mSnapshot->clipRegion->isEmpty()) {
1601 clip.setRegion(*mSnapshot->clipRegion);
1602 } else {
1603 Rect* bounds = mSnapshot->clipRect;
1604 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1605 }
1606
1607 SkRegion region;
1608 region.setPath(transformed, clip);
1609
1610 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001611 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001612 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001613 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001614 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001615}
1616
Romain Guy735738c2012-12-03 12:34:51 -08001617bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001618 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1619 if (clipped) {
1620 dirtyClip();
1621 }
1622 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001623}
1624
Chet Haasea23eed82012-04-12 15:19:04 -07001625Rect* OpenGLRenderer::getClipRect() {
1626 return mSnapshot->clipRect;
1627}
1628
Romain Guyf6a11b82010-06-23 17:47:49 -07001629///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001630// Drawing commands
1631///////////////////////////////////////////////////////////////////////////////
1632
Romain Guy54be1cd2011-06-13 19:04:27 -07001633void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001634 // TODO: It would be best if we could do this before quickReject()
1635 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001636 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001637 // Make sure setScissor & setStencil happen at the beginning of
1638 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001639 if (mDirtyClip) {
1640 if (mCaches.scissorEnabled) {
1641 setScissorFromClip();
1642 }
Romain Guy8ce00302013-01-15 18:51:42 -08001643 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001644 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001645
Romain Guy70ca14e2010-12-13 18:24:33 -08001646 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648 mSetShaderColor = false;
1649 mColorSet = false;
1650 mColorA = mColorR = mColorG = mColorB = 0.0f;
1651 mTextureUnit = 0;
1652 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001653
1654 // Enable debug highlight when what we're about to draw is tested against
1655 // the stencil buffer and if stencil highlight debugging is on
1656 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1657 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1658 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001659}
1660
1661void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1662 mDescription.hasTexture = true;
1663 mDescription.hasAlpha8Texture = isAlpha8;
1664}
1665
Romain Guyff316ec2013-02-13 18:39:43 -08001666void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1667 mDescription.hasTexture = true;
1668 mDescription.hasColors = true;
1669 mDescription.hasAlpha8Texture = isAlpha8;
1670}
1671
Romain Guyaa6c24c2011-04-28 18:40:04 -07001672void OpenGLRenderer::setupDrawWithExternalTexture() {
1673 mDescription.hasExternalTexture = true;
1674}
1675
Romain Guy15bc6432011-12-13 13:11:32 -08001676void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001677 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001678}
1679
Chris Craik710f46d2012-09-17 17:25:49 -07001680void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001681 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001682}
1683
Romain Guyed6fcb02011-03-21 13:11:28 -07001684void OpenGLRenderer::setupDrawPoint(float pointSize) {
1685 mDescription.isPoint = true;
1686 mDescription.pointSize = pointSize;
1687}
1688
Romain Guy8d0d4782010-12-14 20:13:35 -08001689void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1690 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001691 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1692 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1693 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001694 mColorSet = true;
1695 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1696}
1697
Romain Guy86568192010-12-14 15:55:39 -08001698void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1699 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001700 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1701 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1702 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001703 mColorSet = true;
1704 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1705}
1706
Romain Guy41210632012-07-16 17:04:24 -07001707void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1708 mCaches.fontRenderer->describe(mDescription, paint);
1709}
1710
Romain Guy70ca14e2010-12-13 18:24:33 -08001711void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1712 mColorA = a;
1713 mColorR = r;
1714 mColorG = g;
1715 mColorB = b;
1716 mColorSet = true;
1717 mSetShaderColor = mDescription.setColor(r, g, b, a);
1718}
1719
1720void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001721 if (mDrawModifiers.mShader) {
1722 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001723 }
1724}
1725
1726void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001727 if (mDrawModifiers.mColorFilter) {
1728 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001729 }
1730}
1731
Romain Guyf09ef512011-05-27 11:43:46 -07001732void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1733 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1734 mColorA = 1.0f;
1735 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001736 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001737 }
1738}
1739
Romain Guy70ca14e2010-12-13 18:24:33 -08001740void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001741 // When the blending mode is kClear_Mode, we need to use a modulate color
1742 // argb=1,0,0,0
1743 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001744 bool blend = (mColorSet && mColorA < 1.0f) ||
1745 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1746 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001747}
1748
1749void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001750 // When the blending mode is kClear_Mode, we need to use a modulate color
1751 // argb=1,0,0,0
1752 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001753 blend |= (mColorSet && mColorA < 1.0f) ||
1754 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1755 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1756 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001757}
1758
1759void OpenGLRenderer::setupDrawProgram() {
1760 useProgram(mCaches.programCache.get(mDescription));
1761}
1762
1763void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1764 mTrackDirtyRegions = false;
1765}
1766
1767void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1768 bool ignoreTransform) {
1769 mModelView.loadTranslate(left, top, 0.0f);
1770 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001771 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1772 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001773 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001774 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1776 }
1777}
1778
Chet Haase8a5cc922011-04-26 07:28:09 -07001779void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001780 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001781}
1782
Romain Guy70ca14e2010-12-13 18:24:33 -08001783void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1784 bool ignoreTransform, bool ignoreModelView) {
1785 if (!ignoreModelView) {
1786 mModelView.loadTranslate(left, top, 0.0f);
1787 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001788 } else {
1789 mModelView.loadIdentity();
1790 }
Romain Guy86568192010-12-14 15:55:39 -08001791 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1792 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001793 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001794 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001795 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001796 }
1797 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001798 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001799 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1800 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001801}
1802
Romain Guyed6fcb02011-03-21 13:11:28 -07001803void OpenGLRenderer::setupDrawPointUniforms() {
1804 int slot = mCaches.currentProgram->getUniform("pointSize");
1805 glUniform1f(slot, mDescription.pointSize);
1806}
1807
Romain Guy70ca14e2010-12-13 18:24:33 -08001808void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001809 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001810 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1811 }
1812}
1813
Romain Guy86568192010-12-14 15:55:39 -08001814void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001815 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001816 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001817 }
1818}
1819
Romain Guy70ca14e2010-12-13 18:24:33 -08001820void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001821 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001822 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001823 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001824 }
Chris Craikc3566d02013-02-04 16:16:33 -08001825 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1826 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001827 }
1828}
1829
Romain Guy8d0d4782010-12-14 20:13:35 -08001830void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001831 if (mDrawModifiers.mShader) {
1832 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001833 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001834 }
1835}
1836
Romain Guy70ca14e2010-12-13 18:24:33 -08001837void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001838 if (mDrawModifiers.mColorFilter) {
1839 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001840 }
1841}
1842
Romain Guy41210632012-07-16 17:04:24 -07001843void OpenGLRenderer::setupDrawTextGammaUniforms() {
1844 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1845}
1846
Romain Guy70ca14e2010-12-13 18:24:33 -08001847void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001848 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001849 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001850 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001851}
1852
1853void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001854 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001855 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001856 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001857}
1858
Romain Guyaa6c24c2011-04-28 18:40:04 -07001859void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1860 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001861 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001862 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001863}
1864
Romain Guy8f0095c2011-05-02 17:24:22 -07001865void OpenGLRenderer::setupDrawTextureTransform() {
1866 mDescription.hasTextureTransform = true;
1867}
1868
1869void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001870 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1871 GL_FALSE, &transform.data[0]);
1872}
1873
Romain Guy70ca14e2010-12-13 18:24:33 -08001874void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001875 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001876 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001877 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001878 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001879 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001880 }
Romain Guyd71dd362011-12-12 19:03:35 -08001881
Chris Craikcb4d6002012-09-25 12:00:29 -07001882 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001883 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001884 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001885 }
1886
1887 mCaches.unbindIndicesBuffer();
1888}
1889
Romain Guyff316ec2013-02-13 18:39:43 -08001890void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1891 bool force = mCaches.unbindMeshBuffer();
1892 GLsizei stride = sizeof(ColorTextureVertex);
1893
1894 mCaches.bindPositionVertexPointer(force, vertices, stride);
1895 if (mCaches.currentProgram->texCoords >= 0) {
1896 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1897 }
1898 int slot = mCaches.currentProgram->getAttrib("colors");
1899 if (slot >= 0) {
1900 glEnableVertexAttribArray(slot);
1901 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1902 }
1903
1904 mCaches.unbindIndicesBuffer();
1905}
1906
Romain Guy3b748a42013-04-17 18:54:38 -07001907void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1908 bool force = false;
1909 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1910 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1911 // use the default VBO found in Caches
1912 if (!vertices || vbo) {
1913 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1914 } else {
1915 force = mCaches.unbindMeshBuffer();
1916 }
1917 mCaches.bindIndicesBuffer();
1918
Chris Craikcb4d6002012-09-25 12:00:29 -07001919 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001920 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001921 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001922 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001923}
1924
Chet Haase5b0200b2011-04-13 17:58:08 -07001925void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001926 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001927 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001928 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001929}
1930
Romain Guy70ca14e2010-12-13 18:24:33 -08001931void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001932}
1933
1934///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001935// Drawing
1936///////////////////////////////////////////////////////////////////////////////
1937
Chris Craikff785832013-03-08 13:12:16 -08001938status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1939 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001940 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001941 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1942 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001943 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001944 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001945 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001946 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1947 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001948 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001949 }
1950
1951 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001952 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1953 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001954
1955 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001956 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001957
Chet Haase58d110a2013-04-10 07:43:29 -07001958 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001959 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001960
Romain Guy65549432012-03-26 16:45:05 -07001961 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001962}
1963
Chris Craikc3566d02013-02-04 16:16:33 -08001964void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001965 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001966 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001967 }
1968}
1969
Romain Guya168d732011-03-18 16:50:13 -07001970void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1971 int alpha;
1972 SkXfermode::Mode mode;
1973 getAlphaAndMode(paint, &alpha, &mode);
1974
Romain Guy886b2752013-01-04 12:26:18 -08001975 int color = paint != NULL ? paint->getColor() : 0;
1976
Romain Guya168d732011-03-18 16:50:13 -07001977 float x = left;
1978 float y = top;
1979
Romain Guy886b2752013-01-04 12:26:18 -08001980 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1981
Romain Guya168d732011-03-18 16:50:13 -07001982 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001983 if (currentTransform().isPureTranslate()) {
1984 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1985 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001986 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001987
1988 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001989 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001990 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001991 }
1992
Romain Guy3b748a42013-04-17 18:54:38 -07001993 // No need to check for a UV mapper on the texture object, only ARGB_8888
1994 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001995 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07001996 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1997 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001998}
1999
Chris Craik527a3aa2013-03-04 10:19:31 -08002000status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
2001 const Rect& bounds, SkPaint* paint) {
2002
2003 // merged draw operations don't need scissor, but clip should still be valid
2004 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
2005
2006 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002007 Texture* texture = getTexture(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002008 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002009
Chris Craik527a3aa2013-03-04 10:19:31 -08002010 const AutoTexture autoCleanup(texture);
2011
2012 int alpha;
2013 SkXfermode::Mode mode;
2014 getAlphaAndMode(paint, &alpha, &mode);
2015
2016 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2017 texture->setFilter(GL_NEAREST, true); // merged ops are always pure-translation for now
2018
2019 const float x = (int) floorf(bounds.left + 0.5f);
2020 const float y = (int) floorf(bounds.top + 0.5f);
2021 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2022 int color = paint != NULL ? paint->getColor() : 0;
2023 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2024 texture->id, paint != NULL, color, alpha, mode,
2025 &vertices[0].position[0], &vertices[0].texture[0],
2026 GL_TRIANGLES, bitmapCount * 6, true, true);
2027 } else {
2028 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2029 texture->id, alpha / 255.0f, mode, texture->blend,
2030 &vertices[0].position[0], &vertices[0].texture[0],
2031 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true);
2032 }
2033
2034 return DrawGlInfo::kStatusDrew;
2035}
2036
Chet Haase48659092012-05-31 15:21:51 -07002037status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002038 const float right = left + bitmap->width();
2039 const float bottom = top + bitmap->height();
2040
2041 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002042 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002043 }
2044
Romain Guya1d3c912011-12-13 14:55:06 -08002045 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002046 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002047 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002048 const AutoTexture autoCleanup(texture);
2049
Romain Guy211370f2012-02-01 16:10:55 -08002050 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002051 drawAlphaBitmap(texture, left, top, paint);
2052 } else {
2053 drawTextureRect(left, top, right, bottom, texture, paint);
2054 }
Chet Haase48659092012-05-31 15:21:51 -07002055
2056 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002057}
2058
Chet Haase48659092012-05-31 15:21:51 -07002059status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002060 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2061 const mat4 transform(*matrix);
2062 transform.mapRect(r);
2063
Romain Guy6926c722010-07-12 20:20:03 -07002064 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002065 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002066 }
2067
Romain Guya1d3c912011-12-13 14:55:06 -08002068 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002069 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002070 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002071 const AutoTexture autoCleanup(texture);
2072
Romain Guy5b3b3522010-10-27 18:57:51 -07002073 // This could be done in a cheaper way, all we need is pass the matrix
2074 // to the vertex shader. The save/restore is a bit overkill.
2075 save(SkCanvas::kMatrix_SaveFlag);
2076 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002077 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2078 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2079 } else {
2080 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2081 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002082 restore();
Chet Haase48659092012-05-31 15:21:51 -07002083
2084 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002085}
2086
Chet Haase48659092012-05-31 15:21:51 -07002087status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002088 const float right = left + bitmap->width();
2089 const float bottom = top + bitmap->height();
2090
2091 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002092 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002093 }
2094
2095 mCaches.activeTexture(0);
2096 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2097 const AutoTexture autoCleanup(texture);
2098
Romain Guy886b2752013-01-04 12:26:18 -08002099 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2100 drawAlphaBitmap(texture, left, top, paint);
2101 } else {
2102 drawTextureRect(left, top, right, bottom, texture, paint);
2103 }
Chet Haase48659092012-05-31 15:21:51 -07002104
2105 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002106}
2107
Chet Haase48659092012-05-31 15:21:51 -07002108status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002109 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002110 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002111 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002112 }
2113
Romain Guyb18d2d02011-02-10 15:52:54 -08002114 float left = FLT_MAX;
2115 float top = FLT_MAX;
2116 float right = FLT_MIN;
2117 float bottom = FLT_MIN;
2118
Romain Guya92bb4d2012-10-16 11:08:44 -07002119 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002120
Romain Guyff316ec2013-02-13 18:39:43 -08002121 ColorTextureVertex mesh[count];
2122 ColorTextureVertex* vertex = mesh;
2123
2124 bool cleanupColors = false;
2125 if (!colors) {
2126 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2127 colors = new int[colorsCount];
2128 memset(colors, 0xff, colorsCount * sizeof(int));
2129 cleanupColors = true;
2130 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002131
Romain Guy3b748a42013-04-17 18:54:38 -07002132 mCaches.activeTexture(0);
2133 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2134 const UvMapper& mapper(getMapper(texture));
2135
Romain Guy5a7b4662011-01-20 19:09:30 -08002136 for (int32_t y = 0; y < meshHeight; y++) {
2137 for (int32_t x = 0; x < meshWidth; x++) {
2138 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2139
2140 float u1 = float(x) / meshWidth;
2141 float u2 = float(x + 1) / meshWidth;
2142 float v1 = float(y) / meshHeight;
2143 float v2 = float(y + 1) / meshHeight;
2144
Romain Guy3b748a42013-04-17 18:54:38 -07002145 mapper.map(u1, v1, u2, v2);
2146
Romain Guy5a7b4662011-01-20 19:09:30 -08002147 int ax = i + (meshWidth + 1) * 2;
2148 int ay = ax + 1;
2149 int bx = i;
2150 int by = bx + 1;
2151 int cx = i + 2;
2152 int cy = cx + 1;
2153 int dx = i + (meshWidth + 1) * 2 + 2;
2154 int dy = dx + 1;
2155
Romain Guyff316ec2013-02-13 18:39:43 -08002156 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2157 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2158 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002159
Romain Guyff316ec2013-02-13 18:39:43 -08002160 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2161 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2162 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002163
Romain Guya92bb4d2012-10-16 11:08:44 -07002164 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2165 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2166 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2167 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002168 }
2169 }
2170
Romain Guya92bb4d2012-10-16 11:08:44 -07002171 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002172 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002173 return DrawGlInfo::kStatusDone;
2174 }
2175
Romain Guyff316ec2013-02-13 18:39:43 -08002176 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002177 texture = mCaches.textureCache.get(bitmap);
2178 if (!texture) {
2179 if (cleanupColors) delete[] colors;
2180 return DrawGlInfo::kStatusDone;
2181 }
Romain Guyff316ec2013-02-13 18:39:43 -08002182 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002183 const AutoTexture autoCleanup(texture);
2184
2185 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2186 texture->setFilter(FILTER(paint), true);
2187
2188 int alpha;
2189 SkXfermode::Mode mode;
2190 getAlphaAndMode(paint, &alpha, &mode);
2191
Romain Guyff316ec2013-02-13 18:39:43 -08002192 float a = alpha / 255.0f;
2193
Romain Guya92bb4d2012-10-16 11:08:44 -07002194 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002195 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002196 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002197
Romain Guyff316ec2013-02-13 18:39:43 -08002198 setupDraw();
2199 setupDrawWithTextureAndColor();
2200 setupDrawColor(a, a, a, a);
2201 setupDrawColorFilter();
2202 setupDrawBlending(true, mode, false);
2203 setupDrawProgram();
2204 setupDrawDirtyRegionsDisabled();
2205 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2206 setupDrawTexture(texture->id);
2207 setupDrawPureColorUniforms();
2208 setupDrawColorFilterUniforms();
2209 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2210
2211 glDrawArrays(GL_TRIANGLES, 0, count);
2212
2213 finishDrawTexture();
2214
2215 int slot = mCaches.currentProgram->getAttrib("colors");
2216 if (slot >= 0) {
2217 glDisableVertexAttribArray(slot);
2218 }
2219
2220 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002221
2222 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002223}
2224
Chet Haase48659092012-05-31 15:21:51 -07002225status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002226 float srcLeft, float srcTop, float srcRight, float srcBottom,
2227 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002228 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002229 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002230 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002231 }
2232
Romain Guya1d3c912011-12-13 14:55:06 -08002233 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002234 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002235 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002236 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002237
Romain Guy8ba548f2010-06-30 19:21:21 -07002238 const float width = texture->width;
2239 const float height = texture->height;
2240
Romain Guy3b748a42013-04-17 18:54:38 -07002241 float u1 = fmax(0.0f, srcLeft / width);
2242 float v1 = fmax(0.0f, srcTop / height);
2243 float u2 = fmin(1.0f, srcRight / width);
2244 float v2 = fmin(1.0f, srcBottom / height);
2245
2246 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002247
Romain Guy03750a02010-10-18 14:06:08 -07002248 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002249 resetDrawTextureTexCoords(u1, v1, u2, v2);
2250
Romain Guy03750a02010-10-18 14:06:08 -07002251 int alpha;
2252 SkXfermode::Mode mode;
2253 getAlphaAndMode(paint, &alpha, &mode);
2254
Romain Guyd21b6e12011-11-30 20:21:23 -08002255 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2256
Romain Guy886b2752013-01-04 12:26:18 -08002257 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2258 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002259
Romain Guy886b2752013-01-04 12:26:18 -08002260 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2261 // Apply a scale transform on the canvas only when a shader is in use
2262 // Skia handles the ratio between the dst and src rects as a scale factor
2263 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002264 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002265 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002266
Romain Guy3b753822013-03-05 10:27:35 -08002267 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2268 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2269 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002270
2271 dstRight = x + (dstRight - dstLeft);
2272 dstBottom = y + (dstBottom - dstTop);
2273
2274 dstLeft = x;
2275 dstTop = y;
2276
2277 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2278 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002279 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002280 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002281 }
2282
2283 if (CC_UNLIKELY(useScaleTransform)) {
2284 save(SkCanvas::kMatrix_SaveFlag);
2285 translate(dstLeft, dstTop);
2286 scale(scaleX, scaleY);
2287
2288 dstLeft = 0.0f;
2289 dstTop = 0.0f;
2290
2291 dstRight = srcRight - srcLeft;
2292 dstBottom = srcBottom - srcTop;
2293 }
2294
2295 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2296 int color = paint ? paint->getColor() : 0;
2297 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2298 texture->id, paint != NULL, color, alpha, mode,
2299 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2300 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2301 } else {
2302 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2303 texture->id, alpha / 255.0f, mode, texture->blend,
2304 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2305 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2306 }
2307
2308 if (CC_UNLIKELY(useScaleTransform)) {
2309 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002310 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002311
2312 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002313
2314 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002315}
2316
Romain Guy3b748a42013-04-17 18:54:38 -07002317status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002318 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002319 int alpha;
2320 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002321 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002322
Romain Guy3b748a42013-04-17 18:54:38 -07002323 return drawPatch(bitmap, patch, mCaches.assetAtlas.getEntry(bitmap),
Romain Guybe6f9dc2012-07-16 12:41:17 -07002324 left, top, right, bottom, alpha, mode);
2325}
2326
Romain Guy3b748a42013-04-17 18:54:38 -07002327status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
2328 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2329 int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002330 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002331 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002332 }
2333
Romain Guy3b748a42013-04-17 18:54:38 -07002334 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2335 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002336
Romain Guy211370f2012-02-01 16:10:55 -08002337 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002338 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002339 Texture* texture = entry ? &entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002340 if (!texture) return DrawGlInfo::kStatusDone;
2341 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002342
Romain Guya4adcf02013-02-28 12:15:35 -08002343 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2344 texture->setFilter(GL_LINEAR, true);
2345
Romain Guy3b753822013-03-05 10:27:35 -08002346 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002347 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002348 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002349 const float offsetX = left + currentTransform().getTranslateX();
2350 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002351 const size_t count = mesh->quads.size();
2352 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002353 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002354 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002355 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2356 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2357 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002358 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002359 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002360 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002361 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002362 }
2363 }
2364
Romain Guy3b748a42013-04-17 18:54:38 -07002365 alpha *= mSnapshot->alpha;
2366
Romain Guy211370f2012-02-01 16:10:55 -08002367 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002368 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2369 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002370
Romain Guy3b748a42013-04-17 18:54:38 -07002371 right = x + right - left;
2372 bottom = y + bottom - top;
2373 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2374 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2375 GL_TRIANGLES, mesh->indexCount, false, true,
2376 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002377 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002378 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2379 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2380 GL_TRIANGLES, mesh->indexCount, false, false,
2381 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002382 }
Romain Guy054dc182010-10-15 17:55:25 -07002383 }
Chet Haase48659092012-05-31 15:21:51 -07002384
2385 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002386}
2387
Chris Craik65cd6122012-12-10 17:56:27 -08002388status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2389 bool useOffset) {
2390 if (!vertexBuffer.getSize()) {
2391 // no vertices to draw
2392 return DrawGlInfo::kStatusDone;
2393 }
2394
Chris Craikcb4d6002012-09-25 12:00:29 -07002395 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002396 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2397 bool isAA = paint->isAntiAlias();
2398
Chris Craik710f46d2012-09-17 17:25:49 -07002399 setupDraw();
2400 setupDrawNoTexture();
2401 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002402 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2403 setupDrawColorFilter();
2404 setupDrawShader();
2405 setupDrawBlending(isAA, mode);
2406 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002407 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002408 setupDrawColorUniforms();
2409 setupDrawColorFilterUniforms();
2410 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002411
Chris Craik710f46d2012-09-17 17:25:49 -07002412 void* vertices = vertexBuffer.getBuffer();
2413 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002414 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002415 mCaches.resetTexCoordsVertexPointer();
2416 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002417
Chris Craik710f46d2012-09-17 17:25:49 -07002418 int alphaSlot = -1;
2419 if (isAA) {
2420 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2421 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002422
Chris Craik710f46d2012-09-17 17:25:49 -07002423 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002424 glEnableVertexAttribArray(alphaSlot);
2425 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002426 }
Romain Guy04299382012-07-18 17:15:41 -07002427
Chris Craik710f46d2012-09-17 17:25:49 -07002428 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002429
Chris Craik710f46d2012-09-17 17:25:49 -07002430 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002431 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002432 }
Chris Craik65cd6122012-12-10 17:56:27 -08002433
2434 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002435}
2436
2437/**
Chris Craik65cd6122012-12-10 17:56:27 -08002438 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2439 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2440 * screen space in all directions. However, instead of using a fragment shader to compute the
2441 * translucency of the color from its position, we simply use a varying parameter to define how far
2442 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2443 *
2444 * Doesn't yet support joins, caps, or path effects.
2445 */
2446status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2447 VertexBuffer vertexBuffer;
2448 // TODO: try clipping large paths to viewport
2449 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2450
Romain Guyc46d07a2013-03-15 19:06:39 -07002451 if (hasLayer()) {
2452 SkRect bounds = path.getBounds();
2453 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2454 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2455 }
Chris Craik65cd6122012-12-10 17:56:27 -08002456
2457 return drawVertexBuffer(vertexBuffer, paint);
2458}
2459
2460/**
2461 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2462 * and additional geometry for defining an alpha slope perimeter.
2463 *
2464 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2465 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2466 * in-shader alpha region, but found it to be taxing on some GPUs.
2467 *
2468 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2469 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002470 */
Chet Haase48659092012-05-31 15:21:51 -07002471status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002472 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002473
Chris Craik65cd6122012-12-10 17:56:27 -08002474 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002475
Chris Craik65cd6122012-12-10 17:56:27 -08002476 VertexBuffer buffer;
2477 SkRect bounds;
2478 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002479
2480 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2481 return DrawGlInfo::kStatusDone;
2482 }
2483
Romain Guy3b753822013-03-05 10:27:35 -08002484 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002485
Chris Craik65cd6122012-12-10 17:56:27 -08002486 bool useOffset = !paint->isAntiAlias();
2487 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002488}
2489
Chet Haase48659092012-05-31 15:21:51 -07002490status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2491 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002492
2493 // TODO: The paint's cap style defines whether the points are square or circular
2494 // TODO: Handle AA for round points
2495
Chet Haase5b0200b2011-04-13 17:58:08 -07002496 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002497 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002498 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002499 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002500 if (isHairLine) {
2501 // Now that we know it's hairline, we can set the effective width, to be used later
2502 strokeWidth = 1.0f;
2503 }
2504 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002505
Romain Guyed6fcb02011-03-21 13:11:28 -07002506 int alpha;
2507 SkXfermode::Mode mode;
2508 getAlphaAndMode(paint, &alpha, &mode);
2509
2510 int verticesCount = count >> 1;
2511 int generatedVerticesCount = 0;
2512
2513 TextureVertex pointsData[verticesCount];
2514 TextureVertex* vertex = &pointsData[0];
2515
Romain Guy04299382012-07-18 17:15:41 -07002516 // TODO: We should optimize this method to not generate vertices for points
2517 // that lie outside of the clip.
2518 mCaches.enableScissor();
2519
Romain Guyed6fcb02011-03-21 13:11:28 -07002520 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002521 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002522 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002523 setupDrawColor(paint->getColor(), alpha);
2524 setupDrawColorFilter();
2525 setupDrawShader();
2526 setupDrawBlending(mode);
2527 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002528 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002529 setupDrawColorUniforms();
2530 setupDrawColorFilterUniforms();
2531 setupDrawPointUniforms();
2532 setupDrawShaderIdentityUniforms();
2533 setupDrawMesh(vertex);
2534
2535 for (int i = 0; i < count; i += 2) {
2536 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2537 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002538
Chet Haase8a5cc922011-04-26 07:28:09 -07002539 float left = points[i] - halfWidth;
2540 float right = points[i] + halfWidth;
2541 float top = points[i + 1] - halfWidth;
2542 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002543
Romain Guy3b753822013-03-05 10:27:35 -08002544 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002545 }
2546
2547 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002548
2549 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002550}
2551
Chet Haase48659092012-05-31 15:21:51 -07002552status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002553 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002554 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002555
Romain Guyae88e5e2010-10-22 17:49:18 -07002556 Rect& clip(*mSnapshot->clipRect);
2557 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002558
Romain Guy3d58c032010-07-14 16:34:53 -07002559 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002560
2561 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002562}
Romain Guy9d5316e2010-06-24 19:30:36 -07002563
Chet Haase48659092012-05-31 15:21:51 -07002564status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2565 SkPaint* paint) {
2566 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002567 const AutoTexture autoCleanup(texture);
2568
2569 const float x = left + texture->left - texture->offset;
2570 const float y = top + texture->top - texture->offset;
2571
2572 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002573
2574 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002575}
2576
Chet Haase48659092012-05-31 15:21:51 -07002577status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002578 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002579 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2580 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002581 return DrawGlInfo::kStatusDone;
2582 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002583
Chris Craikcb4d6002012-09-25 12:00:29 -07002584 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002585 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002586 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002587 right - left, bottom - top, rx, ry, p);
2588 return drawShape(left, top, texture, p);
2589 }
2590
2591 SkPath path;
2592 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002593 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2594 float outset = p->getStrokeWidth() / 2;
2595 rect.outset(outset, outset);
2596 rx += outset;
2597 ry += outset;
2598 }
Chris Craik710f46d2012-09-17 17:25:49 -07002599 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002600 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002601}
2602
Chris Craik710f46d2012-09-17 17:25:49 -07002603status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002604 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002605 x + radius, y + radius, p) ||
2606 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002607 return DrawGlInfo::kStatusDone;
2608 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002609 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002610 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002611 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002612 return drawShape(x - radius, y - radius, texture, p);
2613 }
2614
2615 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002616 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2617 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2618 } else {
2619 path.addCircle(x, y, radius);
2620 }
Chris Craik65cd6122012-12-10 17:56:27 -08002621 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002622}
Romain Guy01d58e42011-01-19 21:54:02 -08002623
Chet Haase48659092012-05-31 15:21:51 -07002624status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002625 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002626 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2627 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002628 return DrawGlInfo::kStatusDone;
2629 }
Romain Guy01d58e42011-01-19 21:54:02 -08002630
Chris Craikcb4d6002012-09-25 12:00:29 -07002631 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002632 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002633 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002634 return drawShape(left, top, texture, p);
2635 }
2636
2637 SkPath path;
2638 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002639 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2640 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2641 }
Chris Craik710f46d2012-09-17 17:25:49 -07002642 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002643 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002644}
2645
Chet Haase48659092012-05-31 15:21:51 -07002646status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002647 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002648 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2649 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002650 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002651 }
2652
Chris Craik780c1282012-10-04 14:10:49 -07002653 if (fabs(sweepAngle) >= 360.0f) {
2654 return drawOval(left, top, right, bottom, p);
2655 }
2656
2657 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002658 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002659 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002660 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002661 startAngle, sweepAngle, useCenter, p);
2662 return drawShape(left, top, texture, p);
2663 }
2664
2665 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2666 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2667 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2668 }
2669
2670 SkPath path;
2671 if (useCenter) {
2672 path.moveTo(rect.centerX(), rect.centerY());
2673 }
2674 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2675 if (useCenter) {
2676 path.close();
2677 }
Chris Craik65cd6122012-12-10 17:56:27 -08002678 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002679}
2680
Romain Guycf8675e2012-10-02 12:32:25 -07002681// See SkPaintDefaults.h
2682#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2683
Chet Haase48659092012-05-31 15:21:51 -07002684status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002685 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2686 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002687 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002688 }
2689
Chris Craik710f46d2012-09-17 17:25:49 -07002690 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002691 // only fill style is supported by drawConvexPath, since others have to handle joins
2692 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2693 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2694 mCaches.activeTexture(0);
2695 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002696 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002697 return drawShape(left, top, texture, p);
2698 }
2699
2700 SkPath path;
2701 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2702 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2703 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2704 }
2705 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002706 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002707 }
2708
Romain Guy3b753822013-03-05 10:27:35 -08002709 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002710 SkPath path;
2711 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002712 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002713 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002714 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002715 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002716 }
Romain Guyc7d53492010-06-25 13:41:57 -07002717}
Romain Guy9d5316e2010-06-24 19:30:36 -07002718
Raph Levien416a8472012-07-19 22:48:17 -07002719void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2720 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2721 float x, float y) {
2722 mCaches.activeTexture(0);
2723
2724 // NOTE: The drop shadow will not perform gamma correction
2725 // if shader-based correction is enabled
2726 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2727 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002728 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002729 // If the drop shadow exceeds the max texture size or couldn't be
2730 // allocated, skip drawing
2731 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002732 const AutoTexture autoCleanup(shadow);
2733
Chris Craikc3566d02013-02-04 16:16:33 -08002734 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2735 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002736
Chris Craikc3566d02013-02-04 16:16:33 -08002737 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2738 int shadowColor = mDrawModifiers.mShadowColor;
2739 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002740 shadowColor = 0xffffffff;
2741 }
2742
2743 setupDraw();
2744 setupDrawWithTexture(true);
2745 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2746 setupDrawColorFilter();
2747 setupDrawShader();
2748 setupDrawBlending(true, mode);
2749 setupDrawProgram();
2750 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2751 setupDrawTexture(shadow->id);
2752 setupDrawPureColorUniforms();
2753 setupDrawColorFilterUniforms();
2754 setupDrawShaderUniforms();
2755 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2756
2757 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2758}
2759
Romain Guy768bffc2013-02-27 13:50:45 -08002760bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2761 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2762 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2763}
2764
Romain Guy257ae352013-03-20 16:31:12 -07002765class TextSetupFunctor: public Functor {
2766public:
2767 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2768 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2769 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2770 alpha(alpha), mode(mode), paint(paint) {
2771 }
2772 ~TextSetupFunctor() { }
2773
2774 status_t operator ()(int what, void* data) {
2775 renderer.setupDraw();
2776 renderer.setupDrawTextGamma(paint);
2777 renderer.setupDrawDirtyRegionsDisabled();
2778 renderer.setupDrawWithTexture(true);
2779 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2780 renderer.setupDrawColorFilter();
2781 renderer.setupDrawShader();
2782 renderer.setupDrawBlending(true, mode);
2783 renderer.setupDrawProgram();
2784 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2785 // Calling setupDrawTexture with the name 0 will enable the
2786 // uv attributes and increase the texture unit count
2787 // texture binding will be performed by the font renderer as
2788 // needed
2789 renderer.setupDrawTexture(0);
2790 renderer.setupDrawPureColorUniforms();
2791 renderer.setupDrawColorFilterUniforms();
2792 renderer.setupDrawShaderUniforms(pureTranslate);
2793 renderer.setupDrawTextGammaUniforms();
2794
2795 return NO_ERROR;
2796 }
2797
2798 OpenGLRenderer& renderer;
2799 float x;
2800 float y;
2801 bool pureTranslate;
2802 int alpha;
2803 SkXfermode::Mode mode;
2804 SkPaint* paint;
2805};
2806
Chet Haase48659092012-05-31 15:21:51 -07002807status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002808 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002809 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002810 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002811 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002812
Romain Guy671d6cf2012-01-18 12:39:17 -08002813 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002814 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002815 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002816 }
2817
2818 float x = 0.0f;
2819 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002820 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002821 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002822 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2823 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002824 }
2825
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002826 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002827 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002828
2829 int alpha;
2830 SkXfermode::Mode mode;
2831 getAlphaAndMode(paint, &alpha, &mode);
2832
Chris Craikc3566d02013-02-04 16:16:33 -08002833 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002834 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2835 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002836 }
2837
Romain Guy671d6cf2012-01-18 12:39:17 -08002838 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002839 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002840 if (pureTranslate && !linearFilter) {
2841 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2842 }
Romain Guy257ae352013-03-20 16:31:12 -07002843 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002844
2845 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2846 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2847
Romain Guy211370f2012-02-01 16:10:55 -08002848 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002849
Romain Guy257ae352013-03-20 16:31:12 -07002850 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002851 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002852 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002853 if (hasActiveLayer) {
2854 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002855 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002856 }
2857 dirtyLayerUnchecked(bounds, getRegion());
2858 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002859 }
Chet Haase48659092012-05-31 15:21:51 -07002860
2861 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002862}
2863
Romain Guy624234f2013-03-05 16:43:31 -08002864mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2865 mat4 fontTransform;
2866 if (CC_LIKELY(transform.isPureTranslate())) {
2867 fontTransform = mat4::identity();
2868 } else {
2869 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002870 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002871 } else {
2872 float sx, sy;
2873 currentTransform().decomposeScale(sx, sy);
2874 fontTransform.loadScale(sx, sy, 1.0f);
2875 }
2876 }
2877 return fontTransform;
2878}
2879
Romain Guyc2525952012-07-27 16:41:22 -07002880status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Chris Craik527a3aa2013-03-04 10:19:31 -08002881 float x, float y, const float* positions, SkPaint* paint, float length,
2882 DrawOpMode drawOpMode) {
2883
2884 if (drawOpMode == kDrawOpMode_Immediate &&
2885 (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint))) {
Chet Haase48659092012-05-31 15:21:51 -07002886 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002887 }
2888
Chet Haasea1cff502012-02-21 13:43:44 -08002889 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002890 switch (paint->getTextAlign()) {
2891 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002892 x -= length / 2.0f;
2893 break;
2894 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002895 x -= length;
2896 break;
2897 default:
2898 break;
2899 }
2900
Romain Guycac5fd32011-12-01 20:08:50 -08002901 SkPaint::FontMetrics metrics;
2902 paint->getFontMetrics(&metrics, 0.0f);
Chris Craik527a3aa2013-03-04 10:19:31 -08002903 if (drawOpMode == kDrawOpMode_Immediate) {
2904 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
2905 return DrawGlInfo::kStatusDone;
2906 }
2907 } else {
2908 // merged draw operations don't need scissor, but clip should still be valid
2909 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
Romain Guycac5fd32011-12-01 20:08:50 -08002910 }
2911
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002912 const float oldX = x;
2913 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002914
2915 const mat4& transform = currentTransform();
2916 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002917
Romain Guy211370f2012-02-01 16:10:55 -08002918 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002919 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2920 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002921 }
2922
Romain Guy86568192010-12-14 15:55:39 -08002923 int alpha;
2924 SkXfermode::Mode mode;
2925 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002926
Romain Guyc74f45a2013-02-26 19:10:14 -08002927 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2928
Chris Craikc3566d02013-02-04 16:16:33 -08002929 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002930 fontRenderer.setFont(paint, mat4::identity());
2931 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2932 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002933 }
2934
Romain Guy19d4dd82013-03-04 11:14:26 -08002935 const bool hasActiveLayer = hasLayer();
2936
Romain Guy624234f2013-03-05 16:43:31 -08002937 // We only pass a partial transform to the font renderer. That partial
2938 // matrix defines how glyphs are rasterized. Typically we want glyphs
2939 // to be rasterized at their final size on screen, which means the partial
2940 // matrix needs to take the scale factor into account.
2941 // When a partial matrix is used to transform glyphs during rasterization,
2942 // the mesh is generated with the inverse transform (in the case of scale,
2943 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2944 // apply the full transform matrix at draw time in the vertex shader.
2945 // Applying the full matrix in the shader is the easiest way to handle
2946 // rotation and perspective and allows us to always generated quads in the
2947 // font renderer which greatly simplifies the code, clipping in particular.
2948 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002949 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002950
Romain Guy6620c6d2010-12-06 18:07:02 -08002951 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002952 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002953 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002954
Romain Guy624234f2013-03-05 16:43:31 -08002955 // TODO: Implement better clipping for scaled/rotated text
2956 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002957 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2958
Raph Levien996e57c2012-07-23 15:22:52 -07002959 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002960 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002961
2962 // don't call issuedrawcommand, do it at end of batch
2963 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002964 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002965 SkPaint paintCopy(*paint);
2966 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2967 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002968 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002969 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002970 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002971 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002972 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002973
Chris Craik527a3aa2013-03-04 10:19:31 -08002974 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002975 if (!pureTranslate) {
2976 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002977 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002978 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002979 }
Romain Guy694b5192010-07-21 21:33:20 -07002980
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002981 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002982
2983 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002984}
2985
Chet Haase48659092012-05-31 15:21:51 -07002986status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002987 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002988 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002989 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002990 }
2991
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002992 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002993 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002994 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002995
2996 int alpha;
2997 SkXfermode::Mode mode;
2998 getAlphaAndMode(paint, &alpha, &mode);
2999
Romain Guy03d58522012-02-24 17:54:07 -08003000 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07003001 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08003002 setupDrawDirtyRegionsDisabled();
3003 setupDrawWithTexture(true);
3004 setupDrawAlpha8Color(paint->getColor(), alpha);
3005 setupDrawColorFilter();
3006 setupDrawShader();
3007 setupDrawBlending(true, mode);
3008 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08003009 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07003010 // Calling setupDrawTexture with the name 0 will enable the
3011 // uv attributes and increase the texture unit count
3012 // texture binding will be performed by the font renderer as
3013 // needed
3014 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003015 setupDrawPureColorUniforms();
3016 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003017 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003018 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003019
Romain Guy97771732012-02-28 18:17:02 -08003020 const Rect* clip = &mSnapshot->getLocalClip();
3021 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 -08003022
Romain Guy97771732012-02-28 18:17:02 -08003023 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003024
3025 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3026 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003027 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003028 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003029 dirtyLayerUnchecked(bounds, getRegion());
3030 }
Romain Guy97771732012-02-28 18:17:02 -08003031 }
Chet Haase48659092012-05-31 15:21:51 -07003032
3033 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003034}
3035
Chet Haase48659092012-05-31 15:21:51 -07003036status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3037 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003038
Romain Guya1d3c912011-12-13 14:55:06 -08003039 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003040
Romain Guyfb8b7632010-08-23 21:05:08 -07003041 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003042 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003043 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003044
Romain Guy8b55f372010-08-18 17:10:07 -07003045 const float x = texture->left - texture->offset;
3046 const float y = texture->top - texture->offset;
3047
Romain Guy01d58e42011-01-19 21:54:02 -08003048 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003049
3050 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003051}
3052
Chris Craika08f95c2013-03-15 17:24:33 -07003053status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003054 if (!layer) {
3055 return DrawGlInfo::kStatusDone;
3056 }
3057
Romain Guyb2e2f242012-10-17 18:18:35 -07003058 mat4* transform = NULL;
3059 if (layer->isTextureLayer()) {
3060 transform = &layer->getTransform();
3061 if (!transform->isIdentity()) {
3062 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003063 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003064 }
3065 }
3066
Romain Guy35643dd2012-09-18 15:40:58 -07003067 Rect transformed;
3068 Rect clip;
3069 const bool rejected = quickRejectNoScissor(x, y,
3070 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
3071
3072 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003073 if (transform && !transform->isIdentity()) {
3074 restore();
3075 }
Chet Haase48659092012-05-31 15:21:51 -07003076 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003077 }
3078
Romain Guy5bb3c732012-11-29 17:52:58 -08003079 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003080
Romain Guy87e2f7572012-09-24 11:37:12 -07003081 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08003082 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003083
Romain Guy211370f2012-02-01 16:10:55 -08003084 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003085 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3086 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003087
Romain Guyc88e3572011-01-22 00:32:12 -08003088 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003089 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3090 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003091 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003092 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003093 setupDraw();
3094 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003095 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003096 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003097 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003098 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003099 setupDrawPureColorUniforms();
3100 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003101 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003102 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3103 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3104 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003105
Romain Guyd21b6e12011-11-30 20:21:23 -08003106 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003107 setupDrawModelViewTranslate(tx, ty,
3108 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003109 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003110 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003111 setupDrawModelViewTranslate(x, y,
3112 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3113 }
Romain Guyc88e3572011-01-22 00:32:12 -08003114 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003115
Chris Craik34416ea2013-04-15 16:08:28 -07003116 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3117 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3118 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003119
Romain Guyc88e3572011-01-22 00:32:12 -08003120 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003121
3122#if DEBUG_LAYERS_AS_REGIONS
3123 drawRegionRects(layer->region);
3124#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003125 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003126
Chris Craikc3566d02013-02-04 16:16:33 -08003127 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003128
Romain Guy5bb3c732012-11-29 17:52:58 -08003129 if (layer->debugDrawUpdate) {
3130 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003131 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3132 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3133 }
Romain Guyf219da52011-01-16 12:54:25 -08003134 }
Chris Craik34416ea2013-04-15 16:08:28 -07003135 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003136
Romain Guyb2e2f242012-10-17 18:18:35 -07003137 if (transform && !transform->isIdentity()) {
3138 restore();
3139 }
3140
Chet Haase48659092012-05-31 15:21:51 -07003141 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003142}
3143
Romain Guy6926c722010-07-12 20:20:03 -07003144///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003145// Shaders
3146///////////////////////////////////////////////////////////////////////////////
3147
3148void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003149 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003150}
3151
Romain Guy06f96e22010-07-30 19:18:16 -07003152void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003153 mDrawModifiers.mShader = shader;
3154 if (mDrawModifiers.mShader) {
3155 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003156 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003157}
3158
Romain Guyd27977d2010-07-14 19:18:51 -07003159///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003160// Color filters
3161///////////////////////////////////////////////////////////////////////////////
3162
3163void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003164 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003165}
3166
3167void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003168 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003169}
3170
3171///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003172// Drop shadow
3173///////////////////////////////////////////////////////////////////////////////
3174
3175void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003176 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003177}
3178
3179void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003180 mDrawModifiers.mHasShadow = true;
3181 mDrawModifiers.mShadowRadius = radius;
3182 mDrawModifiers.mShadowDx = dx;
3183 mDrawModifiers.mShadowDy = dy;
3184 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003185}
3186
3187///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003188// Draw filters
3189///////////////////////////////////////////////////////////////////////////////
3190
3191void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003192 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3193 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003194 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003195 mDrawModifiers.mPaintFilterClearBits = 0;
3196 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003197}
3198
3199void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003200 mDrawModifiers.mHasDrawFilter = true;
3201 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3202 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003203}
3204
Chris Craika08f95c2013-03-15 17:24:33 -07003205SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003206 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003207 return paint;
3208 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003209
3210 uint32_t flags = paint->getFlags();
3211
3212 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003213 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3214 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003215
3216 return &mFilteredPaint;
3217}
3218
3219///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003220// Drawing implementation
3221///////////////////////////////////////////////////////////////////////////////
3222
Romain Guy3b748a42013-04-17 18:54:38 -07003223Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3224 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3225 if (!texture) {
3226 return mCaches.textureCache.get(bitmap);
3227 }
3228 return texture;
3229}
3230
Romain Guy01d58e42011-01-19 21:54:02 -08003231void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3232 float x, float y, SkPaint* paint) {
3233 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3234 return;
3235 }
3236
3237 int alpha;
3238 SkXfermode::Mode mode;
3239 getAlphaAndMode(paint, &alpha, &mode);
3240
3241 setupDraw();
3242 setupDrawWithTexture(true);
3243 setupDrawAlpha8Color(paint->getColor(), alpha);
3244 setupDrawColorFilter();
3245 setupDrawShader();
3246 setupDrawBlending(true, mode);
3247 setupDrawProgram();
3248 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3249 setupDrawTexture(texture->id);
3250 setupDrawPureColorUniforms();
3251 setupDrawColorFilterUniforms();
3252 setupDrawShaderUniforms();
3253 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3254
3255 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3256
3257 finishDrawTexture();
3258}
3259
Romain Guyf607bdc2010-09-10 19:20:06 -07003260// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003261#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3262#define kStdUnderline_Offset (1.0f / 9.0f)
3263#define kStdUnderline_Thickness (1.0f / 18.0f)
3264
3265void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3266 float x, float y, SkPaint* paint) {
3267 // Handle underline and strike-through
3268 uint32_t flags = paint->getFlags();
3269 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003270 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003271 float underlineWidth = length;
3272 // If length is > 0.0f, we already measured the text for the text alignment
3273 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003274 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003275 }
3276
Romain Guy211370f2012-02-01 16:10:55 -08003277 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003278 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003279 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003280
Raph Levien8b4072d2012-07-30 15:50:00 -07003281 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003282 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003283
Romain Guyf6834472011-01-23 13:32:12 -08003284 int linesCount = 0;
3285 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3286 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3287
3288 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003289 float points[pointsCount];
3290 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003291
3292 if (flags & SkPaint::kUnderlineText_Flag) {
3293 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003294 points[currentPoint++] = left;
3295 points[currentPoint++] = top;
3296 points[currentPoint++] = left + underlineWidth;
3297 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003298 }
3299
3300 if (flags & SkPaint::kStrikeThruText_Flag) {
3301 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003302 points[currentPoint++] = left;
3303 points[currentPoint++] = top;
3304 points[currentPoint++] = left + underlineWidth;
3305 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003306 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003307
Romain Guy726aeba2011-06-01 14:52:00 -07003308 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003309
Romain Guy726aeba2011-06-01 14:52:00 -07003310 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003311 }
3312 }
3313}
3314
Romain Guy672433d2013-01-04 19:05:13 -08003315status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3316 if (mSnapshot->isIgnored()) {
3317 return DrawGlInfo::kStatusDone;
3318 }
3319
Romain Guy735738c2012-12-03 12:34:51 -08003320 int color = paint->getColor();
3321 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003322 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003323 color |= 0x00ffffff;
3324 }
3325 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3326
3327 return drawColorRects(rects, count, color, mode);
3328}
3329
3330status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003331 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003332 if (count == 0) {
3333 return DrawGlInfo::kStatusDone;
3334 }
Romain Guy735738c2012-12-03 12:34:51 -08003335
Romain Guy672433d2013-01-04 19:05:13 -08003336 float left = FLT_MAX;
3337 float top = FLT_MAX;
3338 float right = FLT_MIN;
3339 float bottom = FLT_MIN;
3340
3341 int vertexCount = 0;
3342 Vertex mesh[count * 6];
3343 Vertex* vertex = mesh;
3344
Chris Craik2af46352012-11-26 18:30:17 -08003345 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003346 float l = rects[index + 0];
3347 float t = rects[index + 1];
3348 float r = rects[index + 2];
3349 float b = rects[index + 3];
3350
Romain Guy3b753822013-03-05 10:27:35 -08003351 Vertex::set(vertex++, l, b);
3352 Vertex::set(vertex++, l, t);
3353 Vertex::set(vertex++, r, t);
3354 Vertex::set(vertex++, l, b);
3355 Vertex::set(vertex++, r, t);
3356 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003357
Romain Guy3b753822013-03-05 10:27:35 -08003358 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003359
Romain Guy3b753822013-03-05 10:27:35 -08003360 left = fminf(left, l);
3361 top = fminf(top, t);
3362 right = fmaxf(right, r);
3363 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003364 }
3365
Romain Guy3b753822013-03-05 10:27:35 -08003366 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003367 return DrawGlInfo::kStatusDone;
3368 }
Romain Guy672433d2013-01-04 19:05:13 -08003369
Romain Guy672433d2013-01-04 19:05:13 -08003370 setupDraw();
3371 setupDrawNoTexture();
3372 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3373 setupDrawShader();
3374 setupDrawColorFilter();
3375 setupDrawBlending(mode);
3376 setupDrawProgram();
3377 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003378 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003379 setupDrawColorUniforms();
3380 setupDrawShaderUniforms();
3381 setupDrawColorFilterUniforms();
3382 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3383
Romain Guy8ce00302013-01-15 18:51:42 -08003384 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003385 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003386 }
3387
3388 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3389
3390 return DrawGlInfo::kStatusDrew;
3391}
3392
Romain Guy026c5e162010-06-28 17:12:22 -07003393void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003394 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003395 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003396 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003397 color |= 0x00ffffff;
3398 }
3399
Romain Guy70ca14e2010-12-13 18:24:33 -08003400 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003401 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003402 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003403 setupDrawShader();
3404 setupDrawColorFilter();
3405 setupDrawBlending(mode);
3406 setupDrawProgram();
3407 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3408 setupDrawColorUniforms();
3409 setupDrawShaderUniforms(ignoreTransform);
3410 setupDrawColorFilterUniforms();
3411 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003412
Romain Guyc95c8d62010-09-17 15:31:32 -07003413 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3414}
3415
Romain Guy82ba8142010-07-09 13:25:56 -07003416void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003417 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003418 int alpha;
3419 SkXfermode::Mode mode;
3420 getAlphaAndMode(paint, &alpha, &mode);
3421
Romain Guyd21b6e12011-11-30 20:21:23 -08003422 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003423
Romain Guy3b748a42013-04-17 18:54:38 -07003424 GLvoid* vertices = (GLvoid*) NULL;
3425 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3426
3427 if (texture->uvMapper) {
3428 vertices = &mMeshVertices[0].position[0];
3429 texCoords = &mMeshVertices[0].texture[0];
3430
3431 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3432 texture->uvMapper->map(uvs);
3433
3434 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3435 }
3436
Romain Guy3b753822013-03-05 10:27:35 -08003437 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3438 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3439 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003440
Romain Guyd21b6e12011-11-30 20:21:23 -08003441 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003442 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003443 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3444 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003445 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003446 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003447 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003448 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3449 }
3450
3451 if (texture->uvMapper) {
3452 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003453 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003454}
3455
Romain Guybd6b79b2010-06-26 00:13:53 -07003456void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003457 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3458 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003459 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003460}
3461
3462void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003463 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003464 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003465 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003466
Romain Guy746b7402010-10-26 16:27:31 -07003467 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003468 setupDrawWithTexture();
3469 setupDrawColor(alpha, alpha, alpha, alpha);
3470 setupDrawColorFilter();
3471 setupDrawBlending(blend, mode, swapSrcDst);
3472 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003473 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003474 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003475 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003476 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003477 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003478 }
Romain Guy886b2752013-01-04 12:26:18 -08003479 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003480 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003481 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003482 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003483
Romain Guy6820ac82010-09-15 18:11:50 -07003484 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003485
3486 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003487}
3488
Romain Guy3b748a42013-04-17 18:54:38 -07003489void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3490 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3491 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3492 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3493
3494 setupDraw();
3495 setupDrawWithTexture();
3496 setupDrawColor(alpha, alpha, alpha, alpha);
3497 setupDrawColorFilter();
3498 setupDrawBlending(blend, mode, swapSrcDst);
3499 setupDrawProgram();
3500 if (!dirty) setupDrawDirtyRegionsDisabled();
3501 if (!ignoreScale) {
3502 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3503 } else {
3504 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3505 }
3506 setupDrawTexture(texture);
3507 setupDrawPureColorUniforms();
3508 setupDrawColorFilterUniforms();
3509 setupDrawMeshIndices(vertices, texCoords, vbo);
3510
3511 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3512
3513 finishDrawTexture();
3514}
3515
Romain Guy886b2752013-01-04 12:26:18 -08003516void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3517 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3518 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003519 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003520
3521 setupDraw();
3522 setupDrawWithTexture(true);
3523 if (hasColor) {
3524 setupDrawAlpha8Color(color, alpha);
3525 }
3526 setupDrawColorFilter();
3527 setupDrawShader();
3528 setupDrawBlending(true, mode);
3529 setupDrawProgram();
3530 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003531 if (!ignoreScale) {
3532 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3533 } else {
3534 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3535 }
Romain Guy886b2752013-01-04 12:26:18 -08003536 setupDrawTexture(texture);
3537 setupDrawPureColorUniforms();
3538 setupDrawColorFilterUniforms();
3539 setupDrawShaderUniforms();
3540 setupDrawMesh(vertices, texCoords);
3541
3542 glDrawArrays(drawMode, 0, elementsCount);
3543
3544 finishDrawTexture();
3545}
3546
Romain Guya5aed0d2010-09-09 14:42:43 -07003547void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003548 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003549 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003550
Romain Guy82ba8142010-07-09 13:25:56 -07003551 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003552 // These blend modes are not supported by OpenGL directly and have
3553 // to be implemented using shaders. Since the shader will perform
3554 // the blending, turn blending off here
3555 // If the blend mode cannot be implemented using shaders, fall
3556 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003557 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003558 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003559 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003560 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003561
Romain Guy82bc7a72012-01-03 14:13:39 -08003562 if (mCaches.blend) {
3563 glDisable(GL_BLEND);
3564 mCaches.blend = false;
3565 }
3566
3567 return;
3568 } else {
3569 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003570 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003571 }
3572
3573 if (!mCaches.blend) {
3574 glEnable(GL_BLEND);
3575 }
3576
3577 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3578 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3579
3580 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3581 glBlendFunc(sourceMode, destMode);
3582 mCaches.lastSrcMode = sourceMode;
3583 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003584 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003585 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003586 glDisable(GL_BLEND);
3587 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003588 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003589}
3590
Romain Guy889f8d12010-07-29 14:37:42 -07003591bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003592 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003593 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003594 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003595 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003596 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003597 }
Romain Guy6926c722010-07-12 20:20:03 -07003598 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003599}
3600
Romain Guy026c5e162010-06-28 17:12:22 -07003601void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003602 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003603 TextureVertex::setUV(v++, u1, v1);
3604 TextureVertex::setUV(v++, u2, v1);
3605 TextureVertex::setUV(v++, u1, v2);
3606 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003607}
3608
Chris Craik16ecda52013-03-29 10:59:59 -07003609void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003610 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003611 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3612 // if drawing a layer, ignore the paint's alpha
3613 *alpha = mDrawModifiers.mOverrideLayerAlpha;
3614 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003615 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003616}
3617
Chris Craik16ecda52013-03-29 10:59:59 -07003618float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3619 float alpha;
3620 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3621 alpha = mDrawModifiers.mOverrideLayerAlpha;
3622 } else {
3623 alpha = layer->getAlpha() / 255.0f;
3624 }
3625 return alpha * mSnapshot->alpha;
3626}
3627
Romain Guy9d5316e2010-06-24 19:30:36 -07003628}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003629}; // namespace android