blob: 038df07e0cfdb6f482d2b765facb38b4b8d2f903 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080036#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070037#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080038#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070039
40namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070041namespace uirenderer {
42
43///////////////////////////////////////////////////////////////////////////////
44// Defines
45///////////////////////////////////////////////////////////////////////////////
46
Romain Guy759ea802010-09-16 20:49:46 -070047#define RAD_TO_DEG (180.0f / 3.14159265f)
48#define MIN_ANGLE 0.001f
49
Romain Guyf8773082012-07-12 18:01:00 -070050#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070051
Romain Guy713e1bb2012-10-16 18:44:09 -070052#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080053
Romain Guy9d5316e2010-06-24 19:30:36 -070054///////////////////////////////////////////////////////////////////////////////
55// Globals
56///////////////////////////////////////////////////////////////////////////////
57
Romain Guy889f8d12010-07-29 14:37:42 -070058/**
59 * Structure mapping Skia xfermodes to OpenGL blending factors.
60 */
61struct Blender {
62 SkXfermode::Mode mode;
63 GLenum src;
64 GLenum dst;
65}; // struct Blender
66
Romain Guy026c5e162010-06-28 17:12:22 -070067// In this array, the index of each Blender equals the value of the first
68// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
69static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070070 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
71 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
72 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
73 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
75 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
76 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
77 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
78 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
79 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
81 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050083 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070084 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070085};
Romain Guye4d01122010-06-16 18:44:05 -070086
Romain Guy87a76572010-09-13 18:11:21 -070087// This array contains the swapped version of each SkXfermode. For instance
88// this array's SrcOver blending mode is actually DstOver. You can refer to
89// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070090static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070091 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
92 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
93 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
94 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
95 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
96 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
97 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
98 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
100 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
101 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500104 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700105 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700106};
107
Romain Guyf6a11b82010-06-23 17:47:49 -0700108///////////////////////////////////////////////////////////////////////////////
109// Constructors/destructor
110///////////////////////////////////////////////////////////////////////////////
111
Romain Guy3bbacf22013-02-06 16:51:04 -0800112OpenGLRenderer::OpenGLRenderer():
113 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800114 // *set* draw modifiers to be 0
115 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700116 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700117
Romain Guyac670c02010-07-27 17:39:27 -0700118 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
119
Romain Guyae5575b2010-07-29 18:48:04 -0700120 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700121 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700122 mCountOverdraw = 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) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700225
Romain Guy96885eb2013-03-26 15:05:58 -0700226 setupFrameState(left, top, right, bottom, opaque);
227
228 // Layer renderers will start the frame immediately
229 // The framebuffer renderer will first defer the display list
230 // for each layer and wait until the first drawing command
231 // to start the frame
232 if (mSnapshot->fbo == 0) {
233 syncState();
234 updateLayers();
235 } else {
236 return startFrame();
237 }
238
239 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700240}
241
Romain Guydcfc8362013-01-03 13:08:57 -0800242void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
243 // If we know that we are going to redraw the entire framebuffer,
244 // perform a discard to let the driver know we don't need to preserve
245 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800246 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800247 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800248 const bool isFbo = getTargetFbo() == 0;
249 const GLenum attachments[] = {
250 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
251 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800252 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
253 }
254}
255
Romain Guy7c25aab2012-10-18 15:05:02 -0700256status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700257 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700258 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700259 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700260 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700261 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700262 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700263
Romain Guy7c25aab2012-10-18 15:05:02 -0700264 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700265 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700266}
267
268void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700269 if (mCaches.blend) {
270 glEnable(GL_BLEND);
271 } else {
272 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700273 }
Romain Guybb9524b2010-06-22 18:56:38 -0700274}
275
Romain Guy57b52682012-09-20 17:38:46 -0700276void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700277 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700278 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800279 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700280 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700281 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700282
Romain Guyc3fedaf2013-01-29 17:26:25 -0800283 startTiling(*clip, s->height, opaque);
284 }
285}
286
287void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
288 if (!mSuppressTiling) {
289 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800290 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292}
293
294void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700295 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700296}
297
Romain Guyb025b9c2010-09-16 14:16:48 -0700298void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700299 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700300 endTiling();
301
Romain Guyca89e2a2013-03-08 17:44:20 -0800302 // When finish() is invoked on FBO 0 we've reached the end
303 // of the current frame
304 if (getTargetFbo() == 0) {
305 mCaches.pathCache.trim();
306 }
307
Romain Guy11cb6422012-09-21 00:39:43 -0700308 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700309#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700310 GLenum status = GL_NO_ERROR;
311 while ((status = glGetError()) != GL_NO_ERROR) {
312 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
313 switch (status) {
314 case GL_INVALID_ENUM:
315 ALOGE(" GL_INVALID_ENUM");
316 break;
317 case GL_INVALID_VALUE:
318 ALOGE(" GL_INVALID_VALUE");
319 break;
320 case GL_INVALID_OPERATION:
321 ALOGE(" GL_INVALID_OPERATION");
322 break;
323 case GL_OUT_OF_MEMORY:
324 ALOGE(" Out of memory!");
325 break;
326 }
Romain Guya07105b2011-01-10 21:14:18 -0800327 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700328#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700329
Romain Guyc15008e2010-11-10 11:59:15 -0800330#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800331 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700332#else
333 if (mCaches.getDebugLevel() & kDebugMemory) {
334 mCaches.dumpMemoryUsage();
335 }
Romain Guyc15008e2010-11-10 11:59:15 -0800336#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700337 }
Romain Guy96885eb2013-03-26 15:05:58 -0700338
Romain Guy78dd96d2013-05-03 14:24:16 -0700339 if (mCountOverdraw) {
340 countOverdraw();
341 }
342
Romain Guy96885eb2013-03-26 15:05:58 -0700343 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700344}
345
Romain Guy6c319ca2011-01-11 14:29:25 -0800346void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700347 if (mCaches.currentProgram) {
348 if (mCaches.currentProgram->isInUse()) {
349 mCaches.currentProgram->remove();
350 mCaches.currentProgram = NULL;
351 }
352 }
Romain Guy50c0f092010-10-19 11:42:22 -0700353 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800354 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800355 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800356 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700357 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700358}
359
Romain Guy6c319ca2011-01-11 14:29:25 -0800360void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800361 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800362 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700363 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700364 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700365
Romain Guy3e263fa2011-12-12 16:47:48 -0800366 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
367
Chet Haase80250612012-08-15 13:46:54 -0700368 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700369 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800370 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700371 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700372
Romain Guya1d3c912011-12-13 14:55:06 -0800373 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700374
Romain Guy50c0f092010-10-19 11:42:22 -0700375 mCaches.blend = true;
376 glEnable(GL_BLEND);
377 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
378 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700379}
380
Romain Guy35643dd2012-09-18 15:40:58 -0700381void OpenGLRenderer::resumeAfterLayer() {
382 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
383 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
384 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700385 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700386
387 mCaches.resetScissor();
388 dirtyClip();
389}
390
Romain Guyba6be8a2012-04-23 18:22:09 -0700391void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700392 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700393}
394
395void OpenGLRenderer::attachFunctor(Functor* functor) {
396 mFunctors.add(functor);
397}
398
Romain Guy8f3b8e32012-03-27 16:33:45 -0700399status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
400 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700401 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800404 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700405 SortedVector<Functor*> functors(mFunctors);
406 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700407
Romain Guyba6be8a2012-04-23 18:22:09 -0700408 DrawGlInfo info;
409 info.clipLeft = 0;
410 info.clipTop = 0;
411 info.clipRight = 0;
412 info.clipBottom = 0;
413 info.isLayer = false;
414 info.width = 0;
415 info.height = 0;
416 memset(info.transform, 0, sizeof(float) * 16);
417
418 for (size_t i = 0; i < count; i++) {
419 Functor* f = functors.itemAt(i);
420 result |= (*f)(DrawGlInfo::kModeProcess, &info);
421
Chris Craikc2c95432012-04-25 15:13:52 -0700422 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700423 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
424 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700425 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700426
Chris Craikc2c95432012-04-25 15:13:52 -0700427 if (result & DrawGlInfo::kStatusInvoke) {
428 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700429 }
430 }
Chris Craikd15321b2012-11-28 14:45:04 -0800431 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700432 }
433
434 return result;
435}
436
437status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700438 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
439
Chet Haasedaf98e92011-01-10 14:10:36 -0800440 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700441 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700442
Romain Guy8a4ac612012-07-17 17:32:48 -0700443 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800444 if (mDirtyClip) {
445 setScissorFromClip();
446 }
Romain Guyd643bb52011-03-01 14:55:21 -0800447
Romain Guy80911b82011-03-16 15:30:12 -0700448 Rect clip(*mSnapshot->clipRect);
449 clip.snapToPixelBoundaries();
450
Romain Guyd643bb52011-03-01 14:55:21 -0800451 // Since we don't know what the functor will draw, let's dirty
452 // tne entire clip region
453 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800454 dirtyLayerUnchecked(clip, getRegion());
455 }
Romain Guyd643bb52011-03-01 14:55:21 -0800456
Romain Guy08aa2cb2011-03-17 11:06:57 -0700457 DrawGlInfo info;
458 info.clipLeft = clip.left;
459 info.clipTop = clip.top;
460 info.clipRight = clip.right;
461 info.clipBottom = clip.bottom;
462 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700463 info.width = getSnapshot()->viewport.getWidth();
464 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700465 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700466
Chris Craik4a2bff72013-04-16 13:50:16 -0700467 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800468
Romain Guy8f3b8e32012-03-27 16:33:45 -0700469 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700470 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800471 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700472
Chris Craik65924a32012-04-05 17:52:11 -0700473 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700474 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700475 }
Romain Guycabfcc12011-03-07 18:06:46 -0800476 }
477
Chet Haasedaf98e92011-01-10 14:10:36 -0800478 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700479 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800480}
481
Romain Guyf6a11b82010-06-23 17:47:49 -0700482///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700483// Debug
484///////////////////////////////////////////////////////////////////////////////
485
Romain Guy0f667532013-03-01 14:31:04 -0800486void OpenGLRenderer::eventMark(const char* name) const {
487 mCaches.eventMark(0, name);
488}
489
Romain Guy87e2f7572012-09-24 11:37:12 -0700490void OpenGLRenderer::startMark(const char* name) const {
491 mCaches.startMark(0, name);
492}
493
494void OpenGLRenderer::endMark() const {
495 mCaches.endMark();
496}
497
498void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
499 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
500 if (clear) {
501 mCaches.disableScissor();
502 mCaches.stencil.clear();
503 }
504 if (enable) {
505 mCaches.stencil.enableDebugWrite();
506 } else {
507 mCaches.stencil.disable();
508 }
509 }
510}
511
512void OpenGLRenderer::renderOverdraw() {
513 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700514 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700515
516 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700517 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700518 clip->right - clip->left, clip->bottom - clip->top);
519
520 mCaches.stencil.enableDebugTest(2);
521 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
522 mCaches.stencil.enableDebugTest(3);
523 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
524 mCaches.stencil.enableDebugTest(4);
525 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
526 mCaches.stencil.enableDebugTest(4, true);
527 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
528 mCaches.stencil.disable();
529 }
530}
531
Romain Guy78dd96d2013-05-03 14:24:16 -0700532void OpenGLRenderer::countOverdraw() {
533 size_t count = mWidth * mHeight;
534 uint32_t* buffer = new uint32_t[count];
535 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
536
537 size_t total = 0;
538 for (size_t i = 0; i < count; i++) {
539 total += buffer[i] & 0xff;
540 }
541
542 mOverdraw = total / float(count);
543
544 delete[] buffer;
545}
546
Romain Guy87e2f7572012-09-24 11:37:12 -0700547///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700548// Layers
549///////////////////////////////////////////////////////////////////////////////
550
551bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700552 if (layer->deferredUpdateScheduled && layer->renderer &&
553 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700554 Rect& dirty = layer->dirtyRect;
555
Romain Guy7c450aa2012-09-21 19:15:00 -0700556 if (inFrame) {
557 endTiling();
558 debugOverdraw(false, false);
559 }
Romain Guy11cb6422012-09-21 00:39:43 -0700560
Romain Guy96885eb2013-03-26 15:05:58 -0700561 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700562 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700563 } else {
564 layer->defer();
565 }
Romain Guy11cb6422012-09-21 00:39:43 -0700566
567 if (inFrame) {
568 resumeAfterLayer();
569 startTiling(mSnapshot);
570 }
571
Romain Guy5bb3c732012-11-29 17:52:58 -0800572 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700573 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700574
575 return true;
576 }
577
578 return false;
579}
580
581void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700582 // If draw deferring is enabled this method will simply defer
583 // the display list of each individual layer. The layers remain
584 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700585 int count = mLayerUpdates.size();
586 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700587 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
588 startMark("Layer Updates");
589 } else {
590 startMark("Defer Layer Updates");
591 }
Romain Guy11cb6422012-09-21 00:39:43 -0700592
Chris Craik1206b9b2013-04-04 14:46:24 -0700593 // Note: it is very important to update the layers in order
594 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700595 Layer* layer = mLayerUpdates.itemAt(i);
596 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700597 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
598 mCaches.resourceCache.decrementRefcount(layer);
599 }
Romain Guy11cb6422012-09-21 00:39:43 -0700600 }
Romain Guy11cb6422012-09-21 00:39:43 -0700601
Romain Guy96885eb2013-03-26 15:05:58 -0700602 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
603 mLayerUpdates.clear();
604 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
605 }
606 endMark();
607 }
608}
609
610void OpenGLRenderer::flushLayers() {
611 int count = mLayerUpdates.size();
612 if (count > 0) {
613 startMark("Apply Layer Updates");
614 char layerName[12];
615
Chris Craik1206b9b2013-04-04 14:46:24 -0700616 // Note: it is very important to update the layers in order
617 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700618 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700619 startMark(layerName);
620
621 Layer* layer = mLayerUpdates.itemAt(i);
622 layer->flush();
623 mCaches.resourceCache.decrementRefcount(layer);
624
Romain Guy96885eb2013-03-26 15:05:58 -0700625 endMark();
626 }
627
628 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700629 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700630
Romain Guy11cb6422012-09-21 00:39:43 -0700631 endMark();
632 }
633}
634
635void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
636 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700637 // Make sure we don't introduce duplicates.
638 // SortedVector would do this automatically but we need to respect
639 // the insertion order. The linear search is not an issue since
640 // this list is usually very short (typically one item, at most a few)
641 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
642 if (mLayerUpdates.itemAt(i) == layer) {
643 return;
644 }
645 }
Romain Guy11cb6422012-09-21 00:39:43 -0700646 mLayerUpdates.push_back(layer);
647 mCaches.resourceCache.incrementRefcount(layer);
648 }
649}
650
651void OpenGLRenderer::clearLayerUpdates() {
652 size_t count = mLayerUpdates.size();
653 if (count > 0) {
654 mCaches.resourceCache.lock();
655 for (size_t i = 0; i < count; i++) {
656 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
657 }
658 mCaches.resourceCache.unlock();
659 mLayerUpdates.clear();
660 }
661}
662
663///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700664// State management
665///////////////////////////////////////////////////////////////////////////////
666
Romain Guybb9524b2010-06-22 18:56:38 -0700667int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700668 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700669}
670
671int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700672 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700673}
674
675void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700676 if (mSaveCount > 1) {
677 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700678 }
Romain Guybb9524b2010-06-22 18:56:38 -0700679}
680
681void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700682 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700683
Romain Guy8fb95422010-08-17 18:38:51 -0700684 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700685 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700686 }
Romain Guybb9524b2010-06-22 18:56:38 -0700687}
688
Romain Guy8aef54f2010-09-01 15:13:49 -0700689int OpenGLRenderer::saveSnapshot(int flags) {
690 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700691 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700692}
693
694bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700695 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700696 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700697 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700698
Romain Guybd6b79b2010-06-26 00:13:53 -0700699 sp<Snapshot> current = mSnapshot;
700 sp<Snapshot> previous = mSnapshot->previous;
701
Romain Guyeb993562010-10-05 18:14:38 -0700702 if (restoreOrtho) {
703 Rect& r = previous->viewport;
704 glViewport(r.left, r.top, r.right, r.bottom);
705 mOrthoMatrix.load(current->orthoMatrix);
706 }
707
Romain Guy8b55f372010-08-18 17:10:07 -0700708 mSaveCount--;
709 mSnapshot = previous;
710
Romain Guy2542d192010-08-18 11:47:12 -0700711 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700712 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700713 }
Romain Guy2542d192010-08-18 11:47:12 -0700714
Romain Guy5ec99242010-11-03 16:19:08 -0700715 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700716 endMark(); // Savelayer
717 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700718 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700719 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700720 }
721
Romain Guy2542d192010-08-18 11:47:12 -0700722 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700723}
724
Romain Guyf6a11b82010-06-23 17:47:49 -0700725///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700726// Layers
727///////////////////////////////////////////////////////////////////////////////
728
729int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800730 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700731 const GLuint previousFbo = mSnapshot->fbo;
732 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700733
Romain Guyaf636eb2010-12-09 17:47:21 -0800734 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700735 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700736 }
Romain Guyd55a8612010-06-28 17:42:46 -0700737
738 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700739}
740
Chris Craikd90144d2013-03-19 15:03:48 -0700741void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
742 const Rect untransformedBounds(bounds);
743
744 currentTransform().mapRect(bounds);
745
746 // Layers only make sense if they are in the framebuffer's bounds
747 if (bounds.intersect(*mSnapshot->clipRect)) {
748 // We cannot work with sub-pixels in this case
749 bounds.snapToPixelBoundaries();
750
751 // When the layer is not an FBO, we may use glCopyTexImage so we
752 // need to make sure the layer does not extend outside the bounds
753 // of the framebuffer
754 if (!bounds.intersect(mSnapshot->previous->viewport)) {
755 bounds.setEmpty();
756 } else if (fboLayer) {
757 clip.set(bounds);
758 mat4 inverse;
759 inverse.loadInverse(currentTransform());
760 inverse.mapRect(clip);
761 clip.snapToPixelBoundaries();
762 if (clip.intersect(untransformedBounds)) {
763 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
764 bounds.set(untransformedBounds);
765 } else {
766 clip.setEmpty();
767 }
768 }
769 } else {
770 bounds.setEmpty();
771 }
772}
773
Chris Craik408eb122013-03-26 18:55:15 -0700774void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
775 bool fboLayer, int alpha) {
776 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
777 bounds.getHeight() > mCaches.maxTextureSize ||
778 (fboLayer && clip.isEmpty())) {
779 mSnapshot->empty = fboLayer;
780 } else {
781 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
782 }
783}
784
Chris Craikd90144d2013-03-19 15:03:48 -0700785int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
786 int alpha, SkXfermode::Mode mode, int flags) {
787 const GLuint previousFbo = mSnapshot->fbo;
788 const int count = saveSnapshot(flags);
789
790 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
791 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
792 // operations will be able to store and restore the current clip and transform info, and
793 // quick rejection will be correct (for display lists)
794
795 Rect bounds(left, top, right, bottom);
796 Rect clip;
797 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700798 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700799
Chris Craik408eb122013-03-26 18:55:15 -0700800 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700801 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
802 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
803 }
804 }
805
806 return count;
807}
808
809
Romain Guy1c740bc2010-09-13 18:00:09 -0700810/**
811 * Layers are viewed by Skia are slightly different than layers in image editing
812 * programs (for instance.) When a layer is created, previously created layers
813 * and the frame buffer still receive every drawing command. For instance, if a
814 * layer is created and a shape intersecting the bounds of the layers and the
815 * framebuffer is draw, the shape will be drawn on both (unless the layer was
816 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
817 *
818 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
819 * texture. Unfortunately, this is inefficient as it requires every primitive to
820 * be drawn n + 1 times, where n is the number of active layers. In practice this
821 * means, for every primitive:
822 * - Switch active frame buffer
823 * - Change viewport, clip and projection matrix
824 * - Issue the drawing
825 *
826 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700827 * To avoid this, layers are implemented in a different way here, at least in the
828 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
829 * is set. When this flag is set we can redirect all drawing operations into a
830 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700831 *
832 * This implementation relies on the frame buffer being at least RGBA 8888. When
833 * a layer is created, only a texture is created, not an FBO. The content of the
834 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700835 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700836 * buffer and drawing continues as normal. This technique therefore treats the
837 * frame buffer as a scratch buffer for the layers.
838 *
839 * To compose the layers back onto the frame buffer, each layer texture
840 * (containing the original frame buffer data) is drawn as a simple quad over
841 * the frame buffer. The trick is that the quad is set as the composition
842 * destination in the blending equation, and the frame buffer becomes the source
843 * of the composition.
844 *
845 * Drawing layers with an alpha value requires an extra step before composition.
846 * An empty quad is drawn over the layer's region in the frame buffer. This quad
847 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
848 * quad is used to multiply the colors in the frame buffer. This is achieved by
849 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
850 * GL_ZERO, GL_SRC_ALPHA.
851 *
852 * Because glCopyTexImage2D() can be slow, an alternative implementation might
853 * be use to draw a single clipped layer. The implementation described above
854 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700855 *
856 * (1) The frame buffer is actually not cleared right away. To allow the GPU
857 * to potentially optimize series of calls to glCopyTexImage2D, the frame
858 * buffer is left untouched until the first drawing operation. Only when
859 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700860 */
Chet Haased48885a2012-08-28 17:43:28 -0700861bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
862 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700863 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700864 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700865
Romain Guyeb993562010-10-05 18:14:38 -0700866 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
867
Romain Guyf607bdc2010-09-10 19:20:06 -0700868 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700869 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700870 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700871 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700872 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700873
874 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700875 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700876 return false;
877 }
Romain Guyf18fd992010-07-08 11:45:51 -0700878
Romain Guya1d3c912011-12-13 14:55:06 -0800879 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700880 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700881 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700882 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700883 }
884
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700886 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700887 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
888 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800889 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700890 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700891 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700892
Romain Guy8fb95422010-08-17 18:38:51 -0700893 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700894 mSnapshot->flags |= Snapshot::kFlagIsLayer;
895 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700896
Chris Craik7273daa2013-03-28 11:25:24 -0700897 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700898 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700899 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700900 } else {
901 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700902 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800903 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700904 if (layer->isEmpty()) {
905 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700906 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700907 layer->getWidth(), layer->getHeight(), 0);
908 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800909 } else {
910 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700911 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800912 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700913
Romain Guy54be1cd2011-06-13 19:04:27 -0700914 // Enqueue the buffer coordinates to clear the corresponding region later
915 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700916 }
Romain Guyeb993562010-10-05 18:14:38 -0700917 }
Romain Guyf86ef572010-07-01 11:05:42 -0700918
Romain Guyd55a8612010-06-28 17:42:46 -0700919 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700920}
921
Chet Haased48885a2012-08-28 17:43:28 -0700922bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800923 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700925
Chet Haased48885a2012-08-28 17:43:28 -0700926 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800927 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
928 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700929 mSnapshot->fbo = layer->getFbo();
930 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
931 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
932 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
933 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700934 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700935
Romain Guy2b7028e2012-09-19 17:25:38 -0700936 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700937 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
940 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700941
942 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700944 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700945 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700946 }
947
948 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950
Romain Guyf735c8e2013-01-31 17:45:55 -0800951 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700952
953 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700954 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800955 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700956 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700957 glClear(GL_COLOR_BUFFER_BIT);
958
959 dirtyClip();
960
961 // Change the ortho projection
962 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
963 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
964
965 return true;
966}
967
Romain Guy1c740bc2010-09-13 18:00:09 -0700968/**
969 * Read the documentation of createLayer() before doing anything in this method.
970 */
Romain Guy1d83e192010-08-17 11:37:00 -0700971void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
972 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000973 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700974 return;
975 }
976
Romain Guy8ce00302013-01-15 18:51:42 -0800977 Layer* layer = current->layer;
978 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700980
981 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700982 endTiling();
983
Romain Guye0aa84b2012-04-03 19:30:26 -0700984 // Detach the texture from the FBO
985 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800986
987 layer->removeFbo(false);
988
Romain Guyeb993562010-10-05 18:14:38 -0700989 // Unbind current FBO and restore previous one
990 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700991 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700992
993 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700994 }
995
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700997 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001000 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001001 }
1002
Romain Guy03750a02010-10-18 14:06:08 -07001003 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001004
Romain Guya1d3c912011-12-13 14:55:06 -08001005 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001006
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 // When the layer is stored in an FBO, we can save a bit of fillrate by
1008 // drawing only the dirty region
1009 if (fboLayer) {
1010 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001011 if (layer->getColorFilter()) {
1012 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001013 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001014 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001015 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001016 resetColorFilter();
1017 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 } else if (!rect.isEmpty()) {
1019 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1020 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001021 }
Romain Guy8b55f372010-08-18 17:10:07 -07001022
Romain Guy746b7402010-10-26 16:27:31 -07001023 dirtyClip();
1024
Romain Guyeb993562010-10-05 18:14:38 -07001025 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001026 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001027 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001028 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001029 }
1030}
1031
Romain Guyaa6c24c2011-04-28 18:40:04 -07001032void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001033 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001034
1035 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001036 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001037 setupDrawWithTexture();
1038 } else {
1039 setupDrawWithExternalTexture();
1040 }
1041 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001042 setupDrawColor(alpha, alpha, alpha, alpha);
1043 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001044 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001045 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001046 setupDrawPureColorUniforms();
1047 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001048 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1049 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001050 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001051 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001052 }
Romain Guy3b753822013-03-05 10:27:35 -08001053 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001054 layer->getWidth() == (uint32_t) rect.getWidth() &&
1055 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001056 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1057 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001058
Romain Guyd21b6e12011-11-30 20:21:23 -08001059 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001060 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1061 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001062 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001063 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1064 }
1065 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001066 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1067
1068 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1069
1070 finishDrawTexture();
1071}
1072
Romain Guy5b3b3522010-10-27 18:57:51 -07001073void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001075 const Rect& texCoords = layer->texCoords;
1076 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1077 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001078
Romain Guy9ace8f52011-07-07 20:50:11 -07001079 float x = rect.left;
1080 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001081 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001082 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001083 layer->getHeight() == (uint32_t) rect.getHeight();
1084
1085 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001086 // When we're swapping, the layer is already in screen coordinates
1087 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001088 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1089 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001090 }
1091
Romain Guyd21b6e12011-11-30 20:21:23 -08001092 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001093 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001094 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001095 }
1096
Chris Craik16ecda52013-03-29 10:59:59 -07001097 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001098 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001099 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001100 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001101 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1102 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001103
Romain Guyaa6c24c2011-04-28 18:40:04 -07001104 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1105 } else {
1106 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1107 drawTextureLayer(layer, rect);
1108 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1109 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001110}
1111
Chris Craik34416ea2013-04-15 16:08:28 -07001112/**
1113 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1114 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1115 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1116 * by saveLayer's restore
1117 */
1118#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1119 DRAW_COMMAND; \
1120 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1121 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1122 DRAW_COMMAND; \
1123 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1124 } \
1125 }
1126
1127#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1128
Romain Guy5b3b3522010-10-27 18:57:51 -07001129void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001130 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001131 layer->setRegionAsRect();
1132
Chris Craik34416ea2013-04-15 16:08:28 -07001133 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001134
Romain Guy5b3b3522010-10-27 18:57:51 -07001135 layer->region.clear();
1136 return;
1137 }
1138
Romain Guy8a3957d2011-09-07 17:55:15 -07001139 // TODO: See LayerRenderer.cpp::generateMesh() for important
1140 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001141 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001142 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001143 const android::Rect* rects;
1144 Region safeRegion;
1145 if (CC_LIKELY(hasRectToRectTransform())) {
1146 rects = layer->region.getArray(&count);
1147 } else {
1148 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1149 rects = safeRegion.getArray(&count);
1150 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001151
Chris Craik16ecda52013-03-29 10:59:59 -07001152 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001153 const float texX = 1.0f / float(layer->getWidth());
1154 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001155 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001156
Romain Guy8ce00302013-01-15 18:51:42 -08001157 setupDraw();
1158
1159 // We must get (and therefore bind) the region mesh buffer
1160 // after we setup drawing in case we need to mess with the
1161 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001162 TextureVertex* mesh = mCaches.getRegionMesh();
1163 GLsizei numQuads = 0;
1164
Romain Guy7230a742011-01-10 22:26:16 -08001165 setupDrawWithTexture();
1166 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001167 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001168 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001169 setupDrawProgram();
1170 setupDrawDirtyRegionsDisabled();
1171 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001172 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001173 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001174 if (currentTransform().isPureTranslate()) {
1175 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1176 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001177
Romain Guyd21b6e12011-11-30 20:21:23 -08001178 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001179 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1180 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001181 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001182 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1183 }
Romain Guy15bc6432011-12-13 13:11:32 -08001184 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001185
1186 for (size_t i = 0; i < count; i++) {
1187 const android::Rect* r = &rects[i];
1188
1189 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001190 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001191 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001192 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001193
1194 // TODO: Reject quads outside of the clip
1195 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1196 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1197 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1198 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1199
1200 numQuads++;
1201
1202 if (numQuads >= REGION_MESH_QUAD_COUNT) {
Chris Craik34416ea2013-04-15 16:08:28 -07001203 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1204 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001205 numQuads = 0;
1206 mesh = mCaches.getRegionMesh();
1207 }
1208 }
1209
1210 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001211 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1212 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001213 }
1214
Romain Guy7230a742011-01-10 22:26:16 -08001215 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001216
1217#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001218 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001219#endif
1220
1221 layer->region.clear();
1222 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001223}
1224
Romain Guy3a3133d2011-02-01 22:59:58 -08001225void OpenGLRenderer::drawRegionRects(const Region& region) {
1226#if DEBUG_LAYERS_AS_REGIONS
1227 size_t count;
1228 const android::Rect* rects = region.getArray(&count);
1229
1230 uint32_t colors[] = {
1231 0x7fff0000, 0x7f00ff00,
1232 0x7f0000ff, 0x7fff00ff,
1233 };
1234
1235 int offset = 0;
1236 int32_t top = rects[0].top;
1237
1238 for (size_t i = 0; i < count; i++) {
1239 if (top != rects[i].top) {
1240 offset ^= 0x2;
1241 top = rects[i].top;
1242 }
1243
1244 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1245 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1246 SkXfermode::kSrcOver_Mode);
1247 }
1248#endif
1249}
1250
Romain Guy8ce00302013-01-15 18:51:42 -08001251void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1252 SkXfermode::Mode mode, bool dirty) {
1253 int count = 0;
1254 Vector<float> rects;
1255
1256 SkRegion::Iterator it(region);
1257 while (!it.done()) {
1258 const SkIRect& r = it.rect();
1259 rects.push(r.fLeft);
1260 rects.push(r.fTop);
1261 rects.push(r.fRight);
1262 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001263 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001264 it.next();
1265 }
1266
Romain Guy3bbacf22013-02-06 16:51:04 -08001267 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001268}
1269
Romain Guy5b3b3522010-10-27 18:57:51 -07001270void OpenGLRenderer::dirtyLayer(const float left, const float top,
1271 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001272 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001273 Rect bounds(left, top, right, bottom);
1274 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001275 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001276 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001277}
1278
1279void OpenGLRenderer::dirtyLayer(const float left, const float top,
1280 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001281 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001282 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001283 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001284 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001285}
1286
1287void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001288 if (bounds.intersect(*mSnapshot->clipRect)) {
1289 bounds.snapToPixelBoundaries();
1290 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1291 if (!dirty.isEmpty()) {
1292 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001293 }
1294 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001295}
1296
Romain Guy54be1cd2011-06-13 19:04:27 -07001297void OpenGLRenderer::clearLayerRegions() {
1298 const size_t count = mLayers.size();
1299 if (count == 0) return;
1300
1301 if (!mSnapshot->isIgnored()) {
1302 // Doing several glScissor/glClear here can negatively impact
1303 // GPUs with a tiler architecture, instead we draw quads with
1304 // the Clear blending mode
1305
1306 // The list contains bounds that have already been clipped
1307 // against their initial clip rect, and the current clip
1308 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001309 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001310
1311 Vertex mesh[count * 6];
1312 Vertex* vertex = mesh;
1313
1314 for (uint32_t i = 0; i < count; i++) {
1315 Rect* bounds = mLayers.itemAt(i);
1316
1317 Vertex::set(vertex++, bounds->left, bounds->bottom);
1318 Vertex::set(vertex++, bounds->left, bounds->top);
1319 Vertex::set(vertex++, bounds->right, bounds->top);
1320 Vertex::set(vertex++, bounds->left, bounds->bottom);
1321 Vertex::set(vertex++, bounds->right, bounds->top);
1322 Vertex::set(vertex++, bounds->right, bounds->bottom);
1323
1324 delete bounds;
1325 }
Romain Guye67307c2013-02-11 18:01:20 -08001326 // We must clear the list of dirty rects before we
1327 // call setupDraw() to prevent stencil setup to do
1328 // the same thing again
1329 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001330
1331 setupDraw(false);
1332 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1333 setupDrawBlending(true, SkXfermode::kClear_Mode);
1334 setupDrawProgram();
1335 setupDrawPureColorUniforms();
1336 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001337 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001338
Romain Guy54be1cd2011-06-13 19:04:27 -07001339 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001340
1341 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001342 } else {
1343 for (uint32_t i = 0; i < count; i++) {
1344 delete mLayers.itemAt(i);
1345 }
Romain Guye67307c2013-02-11 18:01:20 -08001346 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001347 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001348}
1349
Romain Guybd6b79b2010-06-26 00:13:53 -07001350///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001351// State Deferral
1352///////////////////////////////////////////////////////////////////////////////
1353
Chris Craikff785832013-03-08 13:12:16 -08001354bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001355 const Rect& currentClip = *(mSnapshot->clipRect);
1356 const mat4& currentMatrix = *(mSnapshot->transform);
1357
Chris Craikff785832013-03-08 13:12:16 -08001358 if (stateDeferFlags & kStateDeferFlag_Draw) {
1359 // state has bounds initialized in local coordinates
1360 if (!state.mBounds.isEmpty()) {
1361 currentMatrix.mapRect(state.mBounds);
1362 if (!state.mBounds.intersect(currentClip)) {
1363 // quick rejected
1364 return true;
1365 }
1366 } else {
1367 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001368 }
1369 }
1370
Chris Craik527a3aa2013-03-04 10:19:31 -08001371 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1372 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001373 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001374 }
1375
Chris Craik7273daa2013-03-28 11:25:24 -07001376 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1377 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001378 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001379 state.mDrawModifiers = mDrawModifiers;
1380 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001381 return false;
1382}
1383
Chris Craik527a3aa2013-03-04 10:19:31 -08001384void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001385 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001386 mDrawModifiers = state.mDrawModifiers;
1387 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001388
Chris Craik527a3aa2013-03-04 10:19:31 -08001389 if (state.mClipValid && !skipClipRestore) {
Chris Craikff785832013-03-08 13:12:16 -08001390 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1391 dirtyClip();
1392 }
Chris Craikc3566d02013-02-04 16:16:33 -08001393}
1394
Chris Craik527a3aa2013-03-04 10:19:31 -08001395void OpenGLRenderer::setFullScreenClip() {
1396 mSnapshot->setClip(0, 0, mWidth, mHeight);
1397 dirtyClip();
1398}
1399
Chris Craikc3566d02013-02-04 16:16:33 -08001400///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001401// Transforms
1402///////////////////////////////////////////////////////////////////////////////
1403
1404void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001405 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001406}
1407
1408void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001409 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001410}
1411
1412void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001413 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001414}
1415
Romain Guy807daf72011-01-18 11:19:19 -08001416void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001417 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001418}
1419
Romain Guyf6a11b82010-06-23 17:47:49 -07001420void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001421 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001422 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001423 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001424 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001425 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001426}
1427
Chris Craikb98a0162013-02-21 11:30:22 -08001428bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001429 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001430}
1431
Romain Guyf6a11b82010-06-23 17:47:49 -07001432void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001433 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001434}
1435
1436void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001437 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001438 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001439 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001440 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001441}
1442
1443///////////////////////////////////////////////////////////////////////////////
1444// Clipping
1445///////////////////////////////////////////////////////////////////////////////
1446
Romain Guybb9524b2010-06-22 18:56:38 -07001447void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001448 Rect clip(*mSnapshot->clipRect);
1449 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001450
Romain Guy8a4ac612012-07-17 17:32:48 -07001451 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1452 clip.getWidth(), clip.getHeight())) {
1453 mDirtyClip = false;
1454 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001455}
1456
Romain Guy8ce00302013-01-15 18:51:42 -08001457void OpenGLRenderer::ensureStencilBuffer() {
1458 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1459 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1460 // just hope we have one when hasLayer() returns false.
1461 if (hasLayer()) {
1462 attachStencilBufferToLayer(mSnapshot->layer);
1463 }
1464}
1465
1466void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1467 // The layer's FBO is already bound when we reach this stage
1468 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001469 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1470 // is attached after we initiated tiling. We must turn it off,
1471 // attach the new render buffer then turn tiling back on
1472 endTiling();
1473
Romain Guy8d4aeb72013-02-12 16:08:55 -08001474 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001475 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001476 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001477
Romain Guyf735c8e2013-01-31 17:45:55 -08001478 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001479 }
1480}
1481
1482void OpenGLRenderer::setStencilFromClip() {
1483 if (!mCaches.debugOverdraw) {
1484 if (!mSnapshot->clipRegion->isEmpty()) {
1485 // NOTE: The order here is important, we must set dirtyClip to false
1486 // before any draw call to avoid calling back into this method
1487 mDirtyClip = false;
1488
1489 ensureStencilBuffer();
1490
1491 mCaches.stencil.enableWrite();
1492
1493 // Clear the stencil but first make sure we restrict drawing
1494 // to the region's bounds
1495 bool resetScissor = mCaches.enableScissor();
1496 if (resetScissor) {
1497 // The scissor was not set so we now need to update it
1498 setScissorFromClip();
1499 }
1500 mCaches.stencil.clear();
1501 if (resetScissor) mCaches.disableScissor();
1502
1503 // NOTE: We could use the region contour path to generate a smaller mesh
1504 // Since we are using the stencil we could use the red book path
1505 // drawing technique. It might increase bandwidth usage though.
1506
1507 // The last parameter is important: we are not drawing in the color buffer
1508 // so we don't want to dirty the current layer, if any
1509 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1510
1511 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001512
1513 // Draw the region used to generate the stencil if the appropriate debug
1514 // mode is enabled
1515 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1516 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1517 }
Romain Guy8ce00302013-01-15 18:51:42 -08001518 } else {
1519 mCaches.stencil.disable();
1520 }
1521 }
1522}
1523
Romain Guy9d5316e2010-06-24 19:30:36 -07001524const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001525 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001526}
1527
Romain Guy8a4ac612012-07-17 17:32:48 -07001528bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1529 if (mSnapshot->isIgnored()) {
1530 return true;
1531 }
1532
1533 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001534 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001535 r.snapToPixelBoundaries();
1536
1537 Rect clipRect(*mSnapshot->clipRect);
1538 clipRect.snapToPixelBoundaries();
1539
1540 return !clipRect.intersects(r);
1541}
1542
Romain Guy35643dd2012-09-18 15:40:58 -07001543bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1544 Rect& transformed, Rect& clip) {
1545 if (mSnapshot->isIgnored()) {
1546 return true;
1547 }
1548
1549 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001550 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001551 transformed.snapToPixelBoundaries();
1552
1553 clip.set(*mSnapshot->clipRect);
1554 clip.snapToPixelBoundaries();
1555
1556 return !clip.intersects(transformed);
1557}
1558
Romain Guy672433d2013-01-04 19:05:13 -08001559bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1560 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001561 if (paint->getStyle() != SkPaint::kFill_Style) {
1562 float outset = paint->getStrokeWidth() * 0.5f;
1563 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1564 } else {
1565 return quickReject(left, top, right, bottom);
1566 }
1567}
1568
Romain Guyc7d53492010-06-25 13:41:57 -07001569bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001570 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001571 return true;
1572 }
1573
Romain Guy1d83e192010-08-17 11:37:00 -07001574 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001575 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001576 r.snapToPixelBoundaries();
1577
1578 Rect clipRect(*mSnapshot->clipRect);
1579 clipRect.snapToPixelBoundaries();
1580
Romain Guy586cae32012-07-13 15:28:31 -07001581 bool rejected = !clipRect.intersects(r);
1582 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001583 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001584 }
1585
1586 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001587}
1588
Romain Guy8ce00302013-01-15 18:51:42 -08001589void OpenGLRenderer::debugClip() {
1590#if DEBUG_CLIP_REGIONS
1591 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1592 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1593 }
1594#endif
1595}
1596
Romain Guy079ba2c2010-07-16 14:12:24 -07001597bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001598 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001599 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1600 if (clipped) {
1601 dirtyClip();
1602 }
1603 return !mSnapshot->clipRect->isEmpty();
1604 }
1605
1606 SkPath path;
1607 path.addRect(left, top, right, bottom);
1608
1609 return clipPath(&path, op);
1610}
1611
1612bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1613 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001614 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001615
1616 SkPath transformed;
1617 path->transform(transform, &transformed);
1618
1619 SkRegion clip;
1620 if (!mSnapshot->clipRegion->isEmpty()) {
1621 clip.setRegion(*mSnapshot->clipRegion);
1622 } else {
1623 Rect* bounds = mSnapshot->clipRect;
1624 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1625 }
1626
1627 SkRegion region;
1628 region.setPath(transformed, clip);
1629
1630 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001631 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001632 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001633 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001634 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001635}
1636
Romain Guy735738c2012-12-03 12:34:51 -08001637bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001638 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1639 if (clipped) {
1640 dirtyClip();
1641 }
1642 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001643}
1644
Chet Haasea23eed82012-04-12 15:19:04 -07001645Rect* OpenGLRenderer::getClipRect() {
1646 return mSnapshot->clipRect;
1647}
1648
Romain Guyf6a11b82010-06-23 17:47:49 -07001649///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001650// Drawing commands
1651///////////////////////////////////////////////////////////////////////////////
1652
Romain Guy54be1cd2011-06-13 19:04:27 -07001653void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001654 // TODO: It would be best if we could do this before quickReject()
1655 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001656 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001657 // Make sure setScissor & setStencil happen at the beginning of
1658 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001659 if (mDirtyClip) {
1660 if (mCaches.scissorEnabled) {
1661 setScissorFromClip();
1662 }
Romain Guy8ce00302013-01-15 18:51:42 -08001663 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001664 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001665
Romain Guy70ca14e2010-12-13 18:24:33 -08001666 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001667
Romain Guy70ca14e2010-12-13 18:24:33 -08001668 mSetShaderColor = false;
1669 mColorSet = false;
1670 mColorA = mColorR = mColorG = mColorB = 0.0f;
1671 mTextureUnit = 0;
1672 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001673
1674 // Enable debug highlight when what we're about to draw is tested against
1675 // the stencil buffer and if stencil highlight debugging is on
1676 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1677 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1678 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001679
1680 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001681}
1682
1683void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1684 mDescription.hasTexture = true;
1685 mDescription.hasAlpha8Texture = isAlpha8;
1686}
1687
Romain Guyff316ec2013-02-13 18:39:43 -08001688void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1689 mDescription.hasTexture = true;
1690 mDescription.hasColors = true;
1691 mDescription.hasAlpha8Texture = isAlpha8;
1692}
1693
Romain Guyaa6c24c2011-04-28 18:40:04 -07001694void OpenGLRenderer::setupDrawWithExternalTexture() {
1695 mDescription.hasExternalTexture = true;
1696}
1697
Romain Guy15bc6432011-12-13 13:11:32 -08001698void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001699 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001700}
1701
Chris Craik710f46d2012-09-17 17:25:49 -07001702void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001703 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001704}
1705
Romain Guy8d0d4782010-12-14 20:13:35 -08001706void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1707 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001708 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1709 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1710 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001711 mColorSet = true;
1712 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1713}
1714
Romain Guy86568192010-12-14 15:55:39 -08001715void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1716 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001717 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1718 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1719 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001720 mColorSet = true;
1721 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1722}
1723
Romain Guy41210632012-07-16 17:04:24 -07001724void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1725 mCaches.fontRenderer->describe(mDescription, paint);
1726}
1727
Romain Guy70ca14e2010-12-13 18:24:33 -08001728void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1729 mColorA = a;
1730 mColorR = r;
1731 mColorG = g;
1732 mColorB = b;
1733 mColorSet = true;
1734 mSetShaderColor = mDescription.setColor(r, g, b, a);
1735}
1736
1737void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001738 if (mDrawModifiers.mShader) {
1739 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001740 }
1741}
1742
1743void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001744 if (mDrawModifiers.mColorFilter) {
1745 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001746 }
1747}
1748
Romain Guyf09ef512011-05-27 11:43:46 -07001749void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1750 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1751 mColorA = 1.0f;
1752 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001753 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001754 }
1755}
1756
Romain Guy70ca14e2010-12-13 18:24:33 -08001757void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001758 // When the blending mode is kClear_Mode, we need to use a modulate color
1759 // argb=1,0,0,0
1760 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001761 bool blend = (mColorSet && mColorA < 1.0f) ||
1762 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1763 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001764}
1765
1766void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001767 // When the blending mode is kClear_Mode, we need to use a modulate color
1768 // argb=1,0,0,0
1769 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001770 blend |= (mColorSet && mColorA < 1.0f) ||
1771 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1772 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1773 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001774}
1775
1776void OpenGLRenderer::setupDrawProgram() {
1777 useProgram(mCaches.programCache.get(mDescription));
1778}
1779
1780void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1781 mTrackDirtyRegions = false;
1782}
1783
1784void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1785 bool ignoreTransform) {
1786 mModelView.loadTranslate(left, top, 0.0f);
1787 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001788 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1789 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001790 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001791 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001792 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1793 }
1794}
1795
Chet Haase8a5cc922011-04-26 07:28:09 -07001796void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001797 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001798}
1799
Romain Guy70ca14e2010-12-13 18:24:33 -08001800void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1801 bool ignoreTransform, bool ignoreModelView) {
1802 if (!ignoreModelView) {
1803 mModelView.loadTranslate(left, top, 0.0f);
1804 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805 } else {
1806 mModelView.loadIdentity();
1807 }
Romain Guy86568192010-12-14 15:55:39 -08001808 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1809 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001810 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001811 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001812 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001813 }
1814 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001815 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001816 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1817 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001818}
1819
1820void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001821 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001822 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1823 }
1824}
1825
Romain Guy86568192010-12-14 15:55:39 -08001826void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001827 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001828 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001829 }
1830}
1831
Romain Guy70ca14e2010-12-13 18:24:33 -08001832void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001833 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001834 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001835 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001836 }
Chris Craikc3566d02013-02-04 16:16:33 -08001837 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1838 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001839 }
1840}
1841
Romain Guy8d0d4782010-12-14 20:13:35 -08001842void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001843 if (mDrawModifiers.mShader) {
1844 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001845 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001846 }
1847}
1848
Romain Guy70ca14e2010-12-13 18:24:33 -08001849void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001850 if (mDrawModifiers.mColorFilter) {
1851 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001852 }
1853}
1854
Romain Guy41210632012-07-16 17:04:24 -07001855void OpenGLRenderer::setupDrawTextGammaUniforms() {
1856 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1857}
1858
Romain Guy70ca14e2010-12-13 18:24:33 -08001859void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001860 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001861 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001862 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001863}
1864
1865void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001866 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001867 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001868 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001869}
1870
Romain Guyaa6c24c2011-04-28 18:40:04 -07001871void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1872 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001873 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001874 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001875}
1876
Romain Guy8f0095c2011-05-02 17:24:22 -07001877void OpenGLRenderer::setupDrawTextureTransform() {
1878 mDescription.hasTextureTransform = true;
1879}
1880
1881void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001882 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1883 GL_FALSE, &transform.data[0]);
1884}
1885
Romain Guy70ca14e2010-12-13 18:24:33 -08001886void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001887 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001888 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001889 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001890 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001891 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001892 }
Romain Guyd71dd362011-12-12 19:03:35 -08001893
Chris Craikcb4d6002012-09-25 12:00:29 -07001894 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001895 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001896 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001897 }
1898
1899 mCaches.unbindIndicesBuffer();
1900}
1901
Romain Guyff316ec2013-02-13 18:39:43 -08001902void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1903 bool force = mCaches.unbindMeshBuffer();
1904 GLsizei stride = sizeof(ColorTextureVertex);
1905
1906 mCaches.bindPositionVertexPointer(force, vertices, stride);
1907 if (mCaches.currentProgram->texCoords >= 0) {
1908 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1909 }
1910 int slot = mCaches.currentProgram->getAttrib("colors");
1911 if (slot >= 0) {
1912 glEnableVertexAttribArray(slot);
1913 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1914 }
1915
1916 mCaches.unbindIndicesBuffer();
1917}
1918
Romain Guy3b748a42013-04-17 18:54:38 -07001919void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1920 bool force = false;
1921 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1922 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1923 // use the default VBO found in Caches
1924 if (!vertices || vbo) {
1925 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1926 } else {
1927 force = mCaches.unbindMeshBuffer();
1928 }
1929 mCaches.bindIndicesBuffer();
1930
Chris Craikcb4d6002012-09-25 12:00:29 -07001931 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001932 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001933 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001934 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001935}
1936
Chet Haase5b0200b2011-04-13 17:58:08 -07001937void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001938 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001939 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001940 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001941}
1942
Romain Guy70ca14e2010-12-13 18:24:33 -08001943void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001944}
1945
1946///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001947// Drawing
1948///////////////////////////////////////////////////////////////////////////////
1949
Chris Craikff785832013-03-08 13:12:16 -08001950status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1951 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001952 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001953 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1954 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001955 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001956 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001957 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001958 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1959 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001960 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001961 }
1962
1963 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001964 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1965 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001966
1967 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001968 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001969
Chet Haase58d110a2013-04-10 07:43:29 -07001970 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001971 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001972
Romain Guy65549432012-03-26 16:45:05 -07001973 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001974}
1975
Chris Craikc3566d02013-02-04 16:16:33 -08001976void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001977 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001978 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001979 }
1980}
1981
Romain Guya168d732011-03-18 16:50:13 -07001982void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1983 int alpha;
1984 SkXfermode::Mode mode;
1985 getAlphaAndMode(paint, &alpha, &mode);
1986
Romain Guy886b2752013-01-04 12:26:18 -08001987 int color = paint != NULL ? paint->getColor() : 0;
1988
Romain Guya168d732011-03-18 16:50:13 -07001989 float x = left;
1990 float y = top;
1991
Romain Guy886b2752013-01-04 12:26:18 -08001992 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1993
Romain Guya168d732011-03-18 16:50:13 -07001994 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001995 if (currentTransform().isPureTranslate()) {
1996 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1997 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001998 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001999
2000 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002001 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002002 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002003 }
2004
Romain Guy3b748a42013-04-17 18:54:38 -07002005 // No need to check for a UV mapper on the texture object, only ARGB_8888
2006 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002007 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002008 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2009 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002010}
2011
Chris Craik527a3aa2013-03-04 10:19:31 -08002012status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
2013 const Rect& bounds, SkPaint* paint) {
2014
2015 // merged draw operations don't need scissor, but clip should still be valid
2016 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
2017
2018 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002019 Texture* texture = getTexture(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002020 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002021
Chris Craik527a3aa2013-03-04 10:19:31 -08002022 const AutoTexture autoCleanup(texture);
2023
2024 int alpha;
2025 SkXfermode::Mode mode;
2026 getAlphaAndMode(paint, &alpha, &mode);
2027
2028 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2029 texture->setFilter(GL_NEAREST, true); // merged ops are always pure-translation for now
2030
2031 const float x = (int) floorf(bounds.left + 0.5f);
2032 const float y = (int) floorf(bounds.top + 0.5f);
2033 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2034 int color = paint != NULL ? paint->getColor() : 0;
2035 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2036 texture->id, paint != NULL, color, alpha, mode,
2037 &vertices[0].position[0], &vertices[0].texture[0],
2038 GL_TRIANGLES, bitmapCount * 6, true, true);
2039 } else {
2040 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2041 texture->id, alpha / 255.0f, mode, texture->blend,
2042 &vertices[0].position[0], &vertices[0].texture[0],
2043 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true);
2044 }
2045
2046 return DrawGlInfo::kStatusDrew;
2047}
2048
Chet Haase48659092012-05-31 15:21:51 -07002049status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002050 const float right = left + bitmap->width();
2051 const float bottom = top + bitmap->height();
2052
2053 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002054 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002055 }
2056
Romain Guya1d3c912011-12-13 14:55:06 -08002057 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002058 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002059 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002060 const AutoTexture autoCleanup(texture);
2061
Romain Guy211370f2012-02-01 16:10:55 -08002062 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002063 drawAlphaBitmap(texture, left, top, paint);
2064 } else {
2065 drawTextureRect(left, top, right, bottom, texture, paint);
2066 }
Chet Haase48659092012-05-31 15:21:51 -07002067
2068 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002069}
2070
Chet Haase48659092012-05-31 15:21:51 -07002071status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002072 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2073 const mat4 transform(*matrix);
2074 transform.mapRect(r);
2075
Romain Guy6926c722010-07-12 20:20:03 -07002076 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002077 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002078 }
2079
Romain Guya1d3c912011-12-13 14:55:06 -08002080 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002081 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002082 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002083 const AutoTexture autoCleanup(texture);
2084
Romain Guy5b3b3522010-10-27 18:57:51 -07002085 // This could be done in a cheaper way, all we need is pass the matrix
2086 // to the vertex shader. The save/restore is a bit overkill.
2087 save(SkCanvas::kMatrix_SaveFlag);
2088 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002089 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2090 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2091 } else {
2092 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2093 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002094 restore();
Chet Haase48659092012-05-31 15:21:51 -07002095
2096 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002097}
2098
Chet Haase48659092012-05-31 15:21:51 -07002099status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002100 const float right = left + bitmap->width();
2101 const float bottom = top + bitmap->height();
2102
2103 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002104 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002105 }
2106
2107 mCaches.activeTexture(0);
2108 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2109 const AutoTexture autoCleanup(texture);
2110
Romain Guy886b2752013-01-04 12:26:18 -08002111 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2112 drawAlphaBitmap(texture, left, top, paint);
2113 } else {
2114 drawTextureRect(left, top, right, bottom, texture, paint);
2115 }
Chet Haase48659092012-05-31 15:21:51 -07002116
2117 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002118}
2119
Chet Haase48659092012-05-31 15:21:51 -07002120status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002121 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002122 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002123 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002124 }
2125
Romain Guyb18d2d02011-02-10 15:52:54 -08002126 float left = FLT_MAX;
2127 float top = FLT_MAX;
2128 float right = FLT_MIN;
2129 float bottom = FLT_MIN;
2130
Romain Guya92bb4d2012-10-16 11:08:44 -07002131 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002132
Romain Guyff316ec2013-02-13 18:39:43 -08002133 ColorTextureVertex mesh[count];
2134 ColorTextureVertex* vertex = mesh;
2135
2136 bool cleanupColors = false;
2137 if (!colors) {
2138 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2139 colors = new int[colorsCount];
2140 memset(colors, 0xff, colorsCount * sizeof(int));
2141 cleanupColors = true;
2142 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002143
Romain Guy3b748a42013-04-17 18:54:38 -07002144 mCaches.activeTexture(0);
2145 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2146 const UvMapper& mapper(getMapper(texture));
2147
Romain Guy5a7b4662011-01-20 19:09:30 -08002148 for (int32_t y = 0; y < meshHeight; y++) {
2149 for (int32_t x = 0; x < meshWidth; x++) {
2150 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2151
2152 float u1 = float(x) / meshWidth;
2153 float u2 = float(x + 1) / meshWidth;
2154 float v1 = float(y) / meshHeight;
2155 float v2 = float(y + 1) / meshHeight;
2156
Romain Guy3b748a42013-04-17 18:54:38 -07002157 mapper.map(u1, v1, u2, v2);
2158
Romain Guy5a7b4662011-01-20 19:09:30 -08002159 int ax = i + (meshWidth + 1) * 2;
2160 int ay = ax + 1;
2161 int bx = i;
2162 int by = bx + 1;
2163 int cx = i + 2;
2164 int cy = cx + 1;
2165 int dx = i + (meshWidth + 1) * 2 + 2;
2166 int dy = dx + 1;
2167
Romain Guyff316ec2013-02-13 18:39:43 -08002168 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2169 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2170 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002171
Romain Guyff316ec2013-02-13 18:39:43 -08002172 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2173 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2174 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002175
Romain Guya92bb4d2012-10-16 11:08:44 -07002176 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2177 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2178 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2179 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002180 }
2181 }
2182
Romain Guya92bb4d2012-10-16 11:08:44 -07002183 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002184 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002185 return DrawGlInfo::kStatusDone;
2186 }
2187
Romain Guyff316ec2013-02-13 18:39:43 -08002188 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002189 texture = mCaches.textureCache.get(bitmap);
2190 if (!texture) {
2191 if (cleanupColors) delete[] colors;
2192 return DrawGlInfo::kStatusDone;
2193 }
Romain Guyff316ec2013-02-13 18:39:43 -08002194 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002195 const AutoTexture autoCleanup(texture);
2196
2197 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2198 texture->setFilter(FILTER(paint), true);
2199
2200 int alpha;
2201 SkXfermode::Mode mode;
2202 getAlphaAndMode(paint, &alpha, &mode);
2203
Romain Guyff316ec2013-02-13 18:39:43 -08002204 float a = alpha / 255.0f;
2205
Romain Guya92bb4d2012-10-16 11:08:44 -07002206 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002207 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002208 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002209
Romain Guyff316ec2013-02-13 18:39:43 -08002210 setupDraw();
2211 setupDrawWithTextureAndColor();
2212 setupDrawColor(a, a, a, a);
2213 setupDrawColorFilter();
2214 setupDrawBlending(true, mode, false);
2215 setupDrawProgram();
2216 setupDrawDirtyRegionsDisabled();
2217 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2218 setupDrawTexture(texture->id);
2219 setupDrawPureColorUniforms();
2220 setupDrawColorFilterUniforms();
2221 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2222
2223 glDrawArrays(GL_TRIANGLES, 0, count);
2224
2225 finishDrawTexture();
2226
2227 int slot = mCaches.currentProgram->getAttrib("colors");
2228 if (slot >= 0) {
2229 glDisableVertexAttribArray(slot);
2230 }
2231
2232 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002233
2234 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002235}
2236
Chet Haase48659092012-05-31 15:21:51 -07002237status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002238 float srcLeft, float srcTop, float srcRight, float srcBottom,
2239 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002240 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002241 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002242 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002243 }
2244
Romain Guya1d3c912011-12-13 14:55:06 -08002245 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002246 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002247 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002248 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002249
Romain Guy8ba548f2010-06-30 19:21:21 -07002250 const float width = texture->width;
2251 const float height = texture->height;
2252
Romain Guy3b748a42013-04-17 18:54:38 -07002253 float u1 = fmax(0.0f, srcLeft / width);
2254 float v1 = fmax(0.0f, srcTop / height);
2255 float u2 = fmin(1.0f, srcRight / width);
2256 float v2 = fmin(1.0f, srcBottom / height);
2257
2258 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002259
Romain Guy03750a02010-10-18 14:06:08 -07002260 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002261 resetDrawTextureTexCoords(u1, v1, u2, v2);
2262
Romain Guy03750a02010-10-18 14:06:08 -07002263 int alpha;
2264 SkXfermode::Mode mode;
2265 getAlphaAndMode(paint, &alpha, &mode);
2266
Romain Guyd21b6e12011-11-30 20:21:23 -08002267 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2268
Romain Guy886b2752013-01-04 12:26:18 -08002269 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2270 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002271
Romain Guy886b2752013-01-04 12:26:18 -08002272 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2273 // Apply a scale transform on the canvas only when a shader is in use
2274 // Skia handles the ratio between the dst and src rects as a scale factor
2275 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002276 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002277 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002278
Romain Guy3b753822013-03-05 10:27:35 -08002279 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2280 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2281 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002282
2283 dstRight = x + (dstRight - dstLeft);
2284 dstBottom = y + (dstBottom - dstTop);
2285
2286 dstLeft = x;
2287 dstTop = y;
2288
2289 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2290 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002291 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002292 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002293 }
2294
2295 if (CC_UNLIKELY(useScaleTransform)) {
2296 save(SkCanvas::kMatrix_SaveFlag);
2297 translate(dstLeft, dstTop);
2298 scale(scaleX, scaleY);
2299
2300 dstLeft = 0.0f;
2301 dstTop = 0.0f;
2302
2303 dstRight = srcRight - srcLeft;
2304 dstBottom = srcBottom - srcTop;
2305 }
2306
2307 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2308 int color = paint ? paint->getColor() : 0;
2309 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2310 texture->id, paint != NULL, color, alpha, mode,
2311 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2312 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2313 } else {
2314 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2315 texture->id, alpha / 255.0f, mode, texture->blend,
2316 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2317 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2318 }
2319
2320 if (CC_UNLIKELY(useScaleTransform)) {
2321 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002322 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002323
2324 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002325
2326 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002327}
2328
Romain Guy3b748a42013-04-17 18:54:38 -07002329status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002330 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002331 int alpha;
2332 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002333 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002334
Romain Guy3b748a42013-04-17 18:54:38 -07002335 return drawPatch(bitmap, patch, mCaches.assetAtlas.getEntry(bitmap),
Romain Guybe6f9dc2012-07-16 12:41:17 -07002336 left, top, right, bottom, alpha, mode);
2337}
2338
Romain Guy3b748a42013-04-17 18:54:38 -07002339status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
2340 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2341 int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002342 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002343 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002344 }
2345
Romain Guy3b748a42013-04-17 18:54:38 -07002346 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2347 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002348
Romain Guy211370f2012-02-01 16:10:55 -08002349 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002350 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002351 Texture* texture = entry ? &entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002352 if (!texture) return DrawGlInfo::kStatusDone;
2353 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002354
Romain Guya4adcf02013-02-28 12:15:35 -08002355 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2356 texture->setFilter(GL_LINEAR, true);
2357
Romain Guy3b753822013-03-05 10:27:35 -08002358 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002359 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002360 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002361 const float offsetX = left + currentTransform().getTranslateX();
2362 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002363 const size_t count = mesh->quads.size();
2364 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002365 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002366 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002367 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2368 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2369 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002370 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002371 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002372 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002373 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002374 }
2375 }
2376
Romain Guy3b748a42013-04-17 18:54:38 -07002377 alpha *= mSnapshot->alpha;
2378
Romain Guy211370f2012-02-01 16:10:55 -08002379 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002380 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2381 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002382
Romain Guy3b748a42013-04-17 18:54:38 -07002383 right = x + right - left;
2384 bottom = y + bottom - top;
2385 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2386 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2387 GL_TRIANGLES, mesh->indexCount, false, true,
2388 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002389 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002390 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2391 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2392 GL_TRIANGLES, mesh->indexCount, false, false,
2393 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002394 }
Romain Guy054dc182010-10-15 17:55:25 -07002395 }
Chet Haase48659092012-05-31 15:21:51 -07002396
2397 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002398}
2399
Chris Craik65cd6122012-12-10 17:56:27 -08002400status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2401 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002402 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002403 // no vertices to draw
2404 return DrawGlInfo::kStatusDone;
2405 }
2406
Chris Craikcb4d6002012-09-25 12:00:29 -07002407 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002408 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2409 bool isAA = paint->isAntiAlias();
2410
Chris Craik710f46d2012-09-17 17:25:49 -07002411 setupDraw();
2412 setupDrawNoTexture();
2413 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002414 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2415 setupDrawColorFilter();
2416 setupDrawShader();
2417 setupDrawBlending(isAA, mode);
2418 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002419 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002420 setupDrawColorUniforms();
2421 setupDrawColorFilterUniforms();
2422 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002423
Chris Craik710f46d2012-09-17 17:25:49 -07002424 void* vertices = vertexBuffer.getBuffer();
2425 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002426 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002427 mCaches.resetTexCoordsVertexPointer();
2428 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002429
Chris Craik710f46d2012-09-17 17:25:49 -07002430 int alphaSlot = -1;
2431 if (isAA) {
2432 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2433 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002434
Chris Craik710f46d2012-09-17 17:25:49 -07002435 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002436 glEnableVertexAttribArray(alphaSlot);
2437 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002438 }
Romain Guy04299382012-07-18 17:15:41 -07002439
Chris Craik6d29c8d2013-05-08 18:35:44 -07002440 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002441
Chris Craik710f46d2012-09-17 17:25:49 -07002442 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002443 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002444 }
Chris Craik65cd6122012-12-10 17:56:27 -08002445
2446 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002447}
2448
2449/**
Chris Craik65cd6122012-12-10 17:56:27 -08002450 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2451 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2452 * screen space in all directions. However, instead of using a fragment shader to compute the
2453 * translucency of the color from its position, we simply use a varying parameter to define how far
2454 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2455 *
2456 * Doesn't yet support joins, caps, or path effects.
2457 */
2458status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2459 VertexBuffer vertexBuffer;
2460 // TODO: try clipping large paths to viewport
2461 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2462
Romain Guyc46d07a2013-03-15 19:06:39 -07002463 if (hasLayer()) {
2464 SkRect bounds = path.getBounds();
2465 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2466 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2467 }
Chris Craik65cd6122012-12-10 17:56:27 -08002468
2469 return drawVertexBuffer(vertexBuffer, paint);
2470}
2471
2472/**
2473 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2474 * and additional geometry for defining an alpha slope perimeter.
2475 *
2476 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2477 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2478 * in-shader alpha region, but found it to be taxing on some GPUs.
2479 *
2480 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2481 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002482 */
Chet Haase48659092012-05-31 15:21:51 -07002483status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002484 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002485
Chris Craik65cd6122012-12-10 17:56:27 -08002486 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002487
Chris Craik65cd6122012-12-10 17:56:27 -08002488 VertexBuffer buffer;
2489 SkRect bounds;
2490 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002491
2492 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2493 return DrawGlInfo::kStatusDone;
2494 }
2495
Romain Guy3b753822013-03-05 10:27:35 -08002496 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002497
Chris Craik65cd6122012-12-10 17:56:27 -08002498 bool useOffset = !paint->isAntiAlias();
2499 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002500}
2501
Chet Haase48659092012-05-31 15:21:51 -07002502status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002503 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002504
Chris Craik6d29c8d2013-05-08 18:35:44 -07002505 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002506
Chris Craik6d29c8d2013-05-08 18:35:44 -07002507 VertexBuffer buffer;
2508 SkRect bounds;
2509 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002510
Chris Craik6d29c8d2013-05-08 18:35:44 -07002511 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2512 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002513 }
2514
Chris Craik6d29c8d2013-05-08 18:35:44 -07002515 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002516
Chris Craik6d29c8d2013-05-08 18:35:44 -07002517 bool useOffset = !paint->isAntiAlias();
2518 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002519}
2520
Chet Haase48659092012-05-31 15:21:51 -07002521status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002522 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002523 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002524
Romain Guyae88e5e2010-10-22 17:49:18 -07002525 Rect& clip(*mSnapshot->clipRect);
2526 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002527
Romain Guy3d58c032010-07-14 16:34:53 -07002528 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002529
2530 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002531}
Romain Guy9d5316e2010-06-24 19:30:36 -07002532
Chet Haase48659092012-05-31 15:21:51 -07002533status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2534 SkPaint* paint) {
2535 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002536 const AutoTexture autoCleanup(texture);
2537
2538 const float x = left + texture->left - texture->offset;
2539 const float y = top + texture->top - texture->offset;
2540
2541 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002542
2543 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002544}
2545
Chet Haase48659092012-05-31 15:21:51 -07002546status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002547 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002548 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2549 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002550 return DrawGlInfo::kStatusDone;
2551 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002552
Chris Craikcb4d6002012-09-25 12:00:29 -07002553 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002554 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002555 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002556 right - left, bottom - top, rx, ry, p);
2557 return drawShape(left, top, texture, p);
2558 }
2559
2560 SkPath path;
2561 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002562 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2563 float outset = p->getStrokeWidth() / 2;
2564 rect.outset(outset, outset);
2565 rx += outset;
2566 ry += outset;
2567 }
Chris Craik710f46d2012-09-17 17:25:49 -07002568 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002569 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002570}
2571
Chris Craik710f46d2012-09-17 17:25:49 -07002572status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002573 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002574 x + radius, y + radius, p) ||
2575 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002576 return DrawGlInfo::kStatusDone;
2577 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002578 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002579 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002580 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002581 return drawShape(x - radius, y - radius, texture, p);
2582 }
2583
2584 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002585 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2586 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2587 } else {
2588 path.addCircle(x, y, radius);
2589 }
Chris Craik65cd6122012-12-10 17:56:27 -08002590 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002591}
Romain Guy01d58e42011-01-19 21:54:02 -08002592
Chet Haase48659092012-05-31 15:21:51 -07002593status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002594 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002595 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2596 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002597 return DrawGlInfo::kStatusDone;
2598 }
Romain Guy01d58e42011-01-19 21:54:02 -08002599
Chris Craikcb4d6002012-09-25 12:00:29 -07002600 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002601 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002602 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002603 return drawShape(left, top, texture, p);
2604 }
2605
2606 SkPath path;
2607 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002608 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2609 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2610 }
Chris Craik710f46d2012-09-17 17:25:49 -07002611 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002612 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002613}
2614
Chet Haase48659092012-05-31 15:21:51 -07002615status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002616 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002617 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2618 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002619 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002620 }
2621
Chris Craik780c1282012-10-04 14:10:49 -07002622 if (fabs(sweepAngle) >= 360.0f) {
2623 return drawOval(left, top, right, bottom, p);
2624 }
2625
2626 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002627 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002628 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002629 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002630 startAngle, sweepAngle, useCenter, p);
2631 return drawShape(left, top, texture, p);
2632 }
2633
2634 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2635 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2636 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2637 }
2638
2639 SkPath path;
2640 if (useCenter) {
2641 path.moveTo(rect.centerX(), rect.centerY());
2642 }
2643 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2644 if (useCenter) {
2645 path.close();
2646 }
Chris Craik65cd6122012-12-10 17:56:27 -08002647 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002648}
2649
Romain Guycf8675e2012-10-02 12:32:25 -07002650// See SkPaintDefaults.h
2651#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2652
Chet Haase48659092012-05-31 15:21:51 -07002653status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002654 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2655 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002656 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002657 }
2658
Chris Craik710f46d2012-09-17 17:25:49 -07002659 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002660 // only fill style is supported by drawConvexPath, since others have to handle joins
2661 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2662 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2663 mCaches.activeTexture(0);
2664 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002665 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002666 return drawShape(left, top, texture, p);
2667 }
2668
2669 SkPath path;
2670 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2671 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2672 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2673 }
2674 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002675 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002676 }
2677
Romain Guy3b753822013-03-05 10:27:35 -08002678 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002679 SkPath path;
2680 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002681 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002682 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002683 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002684 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002685 }
Romain Guyc7d53492010-06-25 13:41:57 -07002686}
Romain Guy9d5316e2010-06-24 19:30:36 -07002687
Raph Levien416a8472012-07-19 22:48:17 -07002688void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2689 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2690 float x, float y) {
2691 mCaches.activeTexture(0);
2692
2693 // NOTE: The drop shadow will not perform gamma correction
2694 // if shader-based correction is enabled
2695 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2696 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002697 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002698 // If the drop shadow exceeds the max texture size or couldn't be
2699 // allocated, skip drawing
2700 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002701 const AutoTexture autoCleanup(shadow);
2702
Chris Craikc3566d02013-02-04 16:16:33 -08002703 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2704 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002705
Chris Craikc3566d02013-02-04 16:16:33 -08002706 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2707 int shadowColor = mDrawModifiers.mShadowColor;
2708 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002709 shadowColor = 0xffffffff;
2710 }
2711
2712 setupDraw();
2713 setupDrawWithTexture(true);
2714 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2715 setupDrawColorFilter();
2716 setupDrawShader();
2717 setupDrawBlending(true, mode);
2718 setupDrawProgram();
2719 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2720 setupDrawTexture(shadow->id);
2721 setupDrawPureColorUniforms();
2722 setupDrawColorFilterUniforms();
2723 setupDrawShaderUniforms();
2724 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2725
2726 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2727}
2728
Romain Guy768bffc2013-02-27 13:50:45 -08002729bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2730 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2731 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2732}
2733
Romain Guy257ae352013-03-20 16:31:12 -07002734class TextSetupFunctor: public Functor {
2735public:
2736 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2737 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2738 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2739 alpha(alpha), mode(mode), paint(paint) {
2740 }
2741 ~TextSetupFunctor() { }
2742
2743 status_t operator ()(int what, void* data) {
2744 renderer.setupDraw();
2745 renderer.setupDrawTextGamma(paint);
2746 renderer.setupDrawDirtyRegionsDisabled();
2747 renderer.setupDrawWithTexture(true);
2748 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2749 renderer.setupDrawColorFilter();
2750 renderer.setupDrawShader();
2751 renderer.setupDrawBlending(true, mode);
2752 renderer.setupDrawProgram();
2753 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2754 // Calling setupDrawTexture with the name 0 will enable the
2755 // uv attributes and increase the texture unit count
2756 // texture binding will be performed by the font renderer as
2757 // needed
2758 renderer.setupDrawTexture(0);
2759 renderer.setupDrawPureColorUniforms();
2760 renderer.setupDrawColorFilterUniforms();
2761 renderer.setupDrawShaderUniforms(pureTranslate);
2762 renderer.setupDrawTextGammaUniforms();
2763
2764 return NO_ERROR;
2765 }
2766
2767 OpenGLRenderer& renderer;
2768 float x;
2769 float y;
2770 bool pureTranslate;
2771 int alpha;
2772 SkXfermode::Mode mode;
2773 SkPaint* paint;
2774};
2775
Chet Haase48659092012-05-31 15:21:51 -07002776status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002777 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002778 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002779 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002780 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002781
Romain Guy671d6cf2012-01-18 12:39:17 -08002782 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002783 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002784 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002785 }
2786
2787 float x = 0.0f;
2788 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002789 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002790 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002791 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2792 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002793 }
2794
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002795 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002796 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002797
2798 int alpha;
2799 SkXfermode::Mode mode;
2800 getAlphaAndMode(paint, &alpha, &mode);
2801
Chris Craikc3566d02013-02-04 16:16:33 -08002802 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002803 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2804 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002805 }
2806
Romain Guy671d6cf2012-01-18 12:39:17 -08002807 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002808 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002809 if (pureTranslate && !linearFilter) {
2810 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2811 }
Romain Guy257ae352013-03-20 16:31:12 -07002812 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002813
2814 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2815 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2816
Romain Guy211370f2012-02-01 16:10:55 -08002817 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002818
Romain Guy257ae352013-03-20 16:31:12 -07002819 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002820 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002821 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002822 if (hasActiveLayer) {
2823 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002824 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002825 }
2826 dirtyLayerUnchecked(bounds, getRegion());
2827 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002828 }
Chet Haase48659092012-05-31 15:21:51 -07002829
2830 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002831}
2832
Romain Guy624234f2013-03-05 16:43:31 -08002833mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2834 mat4 fontTransform;
2835 if (CC_LIKELY(transform.isPureTranslate())) {
2836 fontTransform = mat4::identity();
2837 } else {
2838 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002839 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002840 } else {
2841 float sx, sy;
2842 currentTransform().decomposeScale(sx, sy);
2843 fontTransform.loadScale(sx, sy, 1.0f);
2844 }
2845 }
2846 return fontTransform;
2847}
2848
Romain Guyc2525952012-07-27 16:41:22 -07002849status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Chris Craik527a3aa2013-03-04 10:19:31 -08002850 float x, float y, const float* positions, SkPaint* paint, float length,
2851 DrawOpMode drawOpMode) {
2852
2853 if (drawOpMode == kDrawOpMode_Immediate &&
2854 (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint))) {
Chet Haase48659092012-05-31 15:21:51 -07002855 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002856 }
2857
Chet Haasea1cff502012-02-21 13:43:44 -08002858 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002859 switch (paint->getTextAlign()) {
2860 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002861 x -= length / 2.0f;
2862 break;
2863 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002864 x -= length;
2865 break;
2866 default:
2867 break;
2868 }
2869
Romain Guycac5fd32011-12-01 20:08:50 -08002870 SkPaint::FontMetrics metrics;
2871 paint->getFontMetrics(&metrics, 0.0f);
Chris Craik527a3aa2013-03-04 10:19:31 -08002872 if (drawOpMode == kDrawOpMode_Immediate) {
2873 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
2874 return DrawGlInfo::kStatusDone;
2875 }
2876 } else {
2877 // merged draw operations don't need scissor, but clip should still be valid
2878 mCaches.setScissorEnabled(mScissorOptimizationDisabled);
Romain Guycac5fd32011-12-01 20:08:50 -08002879 }
2880
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002881 const float oldX = x;
2882 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002883
2884 const mat4& transform = currentTransform();
2885 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002886
Romain Guy211370f2012-02-01 16:10:55 -08002887 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002888 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2889 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002890 }
2891
Romain Guy86568192010-12-14 15:55:39 -08002892 int alpha;
2893 SkXfermode::Mode mode;
2894 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002895
Romain Guyc74f45a2013-02-26 19:10:14 -08002896 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2897
Chris Craikc3566d02013-02-04 16:16:33 -08002898 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002899 fontRenderer.setFont(paint, mat4::identity());
2900 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2901 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002902 }
2903
Romain Guy19d4dd82013-03-04 11:14:26 -08002904 const bool hasActiveLayer = hasLayer();
2905
Romain Guy624234f2013-03-05 16:43:31 -08002906 // We only pass a partial transform to the font renderer. That partial
2907 // matrix defines how glyphs are rasterized. Typically we want glyphs
2908 // to be rasterized at their final size on screen, which means the partial
2909 // matrix needs to take the scale factor into account.
2910 // When a partial matrix is used to transform glyphs during rasterization,
2911 // the mesh is generated with the inverse transform (in the case of scale,
2912 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2913 // apply the full transform matrix at draw time in the vertex shader.
2914 // Applying the full matrix in the shader is the easiest way to handle
2915 // rotation and perspective and allows us to always generated quads in the
2916 // font renderer which greatly simplifies the code, clipping in particular.
2917 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002918 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002919
Romain Guy6620c6d2010-12-06 18:07:02 -08002920 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002921 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002922 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002923
Romain Guy624234f2013-03-05 16:43:31 -08002924 // TODO: Implement better clipping for scaled/rotated text
2925 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002926 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2927
Raph Levien996e57c2012-07-23 15:22:52 -07002928 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002929 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002930
2931 // don't call issuedrawcommand, do it at end of batch
2932 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002933 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002934 SkPaint paintCopy(*paint);
2935 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2936 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002937 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002938 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002939 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik527a3aa2013-03-04 10:19:31 -08002940 positions, hasActiveLayer ? &bounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002941 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002942
Chris Craik527a3aa2013-03-04 10:19:31 -08002943 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002944 if (!pureTranslate) {
2945 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002946 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002947 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002948 }
Romain Guy694b5192010-07-21 21:33:20 -07002949
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002950 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002951
2952 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002953}
2954
Chet Haase48659092012-05-31 15:21:51 -07002955status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002956 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002957 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002958 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002959 }
2960
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002961 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002962 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002963 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002964
2965 int alpha;
2966 SkXfermode::Mode mode;
2967 getAlphaAndMode(paint, &alpha, &mode);
2968
Romain Guy03d58522012-02-24 17:54:07 -08002969 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002970 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002971 setupDrawDirtyRegionsDisabled();
2972 setupDrawWithTexture(true);
2973 setupDrawAlpha8Color(paint->getColor(), alpha);
2974 setupDrawColorFilter();
2975 setupDrawShader();
2976 setupDrawBlending(true, mode);
2977 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002978 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002979 // Calling setupDrawTexture with the name 0 will enable the
2980 // uv attributes and increase the texture unit count
2981 // texture binding will be performed by the font renderer as
2982 // needed
2983 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002984 setupDrawPureColorUniforms();
2985 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002986 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002987 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002988
Romain Guy97771732012-02-28 18:17:02 -08002989 const Rect* clip = &mSnapshot->getLocalClip();
2990 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 -08002991
Romain Guy97771732012-02-28 18:17:02 -08002992 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002993
2994 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2995 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002996 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002997 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002998 dirtyLayerUnchecked(bounds, getRegion());
2999 }
Romain Guy97771732012-02-28 18:17:02 -08003000 }
Chet Haase48659092012-05-31 15:21:51 -07003001
3002 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003003}
3004
Chet Haase48659092012-05-31 15:21:51 -07003005status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3006 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003007
Romain Guya1d3c912011-12-13 14:55:06 -08003008 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003009
Romain Guyfb8b7632010-08-23 21:05:08 -07003010 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003011 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003012 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003013
Romain Guy8b55f372010-08-18 17:10:07 -07003014 const float x = texture->left - texture->offset;
3015 const float y = texture->top - texture->offset;
3016
Romain Guy01d58e42011-01-19 21:54:02 -08003017 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003018
3019 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003020}
3021
Chris Craika08f95c2013-03-15 17:24:33 -07003022status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003023 if (!layer) {
3024 return DrawGlInfo::kStatusDone;
3025 }
3026
Romain Guyb2e2f242012-10-17 18:18:35 -07003027 mat4* transform = NULL;
3028 if (layer->isTextureLayer()) {
3029 transform = &layer->getTransform();
3030 if (!transform->isIdentity()) {
3031 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003032 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003033 }
3034 }
3035
Romain Guy35643dd2012-09-18 15:40:58 -07003036 Rect transformed;
3037 Rect clip;
3038 const bool rejected = quickRejectNoScissor(x, y,
3039 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
3040
3041 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003042 if (transform && !transform->isIdentity()) {
3043 restore();
3044 }
Chet Haase48659092012-05-31 15:21:51 -07003045 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003046 }
3047
Romain Guy5bb3c732012-11-29 17:52:58 -08003048 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003049
Romain Guy87e2f7572012-09-24 11:37:12 -07003050 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08003051 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003052
Romain Guy211370f2012-02-01 16:10:55 -08003053 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003054 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3055 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003056
Romain Guyc88e3572011-01-22 00:32:12 -08003057 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003058 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3059 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003060 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003061 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003062 setupDraw();
3063 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003064 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003065 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003066 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003067 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003068 setupDrawPureColorUniforms();
3069 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003070 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003071 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3072 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3073 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003074
Romain Guyd21b6e12011-11-30 20:21:23 -08003075 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003076 setupDrawModelViewTranslate(tx, ty,
3077 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003078 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003079 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003080 setupDrawModelViewTranslate(x, y,
3081 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3082 }
Romain Guyc88e3572011-01-22 00:32:12 -08003083 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003084
Chris Craik34416ea2013-04-15 16:08:28 -07003085 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3086 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3087 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003088
Romain Guyc88e3572011-01-22 00:32:12 -08003089 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003090
3091#if DEBUG_LAYERS_AS_REGIONS
3092 drawRegionRects(layer->region);
3093#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003094 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003095
Chris Craikc3566d02013-02-04 16:16:33 -08003096 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003097
Romain Guy5bb3c732012-11-29 17:52:58 -08003098 if (layer->debugDrawUpdate) {
3099 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003100 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3101 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3102 }
Romain Guyf219da52011-01-16 12:54:25 -08003103 }
Chris Craik34416ea2013-04-15 16:08:28 -07003104 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003105
Romain Guyb2e2f242012-10-17 18:18:35 -07003106 if (transform && !transform->isIdentity()) {
3107 restore();
3108 }
3109
Chet Haase48659092012-05-31 15:21:51 -07003110 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003111}
3112
Romain Guy6926c722010-07-12 20:20:03 -07003113///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003114// Shaders
3115///////////////////////////////////////////////////////////////////////////////
3116
3117void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003118 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003119}
3120
Romain Guy06f96e22010-07-30 19:18:16 -07003121void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003122 mDrawModifiers.mShader = shader;
3123 if (mDrawModifiers.mShader) {
3124 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003125 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003126}
3127
Romain Guyd27977d2010-07-14 19:18:51 -07003128///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003129// Color filters
3130///////////////////////////////////////////////////////////////////////////////
3131
3132void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003133 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003134}
3135
3136void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003137 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003138}
3139
3140///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003141// Drop shadow
3142///////////////////////////////////////////////////////////////////////////////
3143
3144void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003145 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003146}
3147
3148void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003149 mDrawModifiers.mHasShadow = true;
3150 mDrawModifiers.mShadowRadius = radius;
3151 mDrawModifiers.mShadowDx = dx;
3152 mDrawModifiers.mShadowDy = dy;
3153 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003154}
3155
3156///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003157// Draw filters
3158///////////////////////////////////////////////////////////////////////////////
3159
3160void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003161 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3162 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003163 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003164 mDrawModifiers.mPaintFilterClearBits = 0;
3165 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003166}
3167
3168void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003169 mDrawModifiers.mHasDrawFilter = true;
3170 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3171 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003172}
3173
Chris Craika08f95c2013-03-15 17:24:33 -07003174SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003175 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003176 return paint;
3177 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003178
3179 uint32_t flags = paint->getFlags();
3180
3181 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003182 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3183 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003184
3185 return &mFilteredPaint;
3186}
3187
3188///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003189// Drawing implementation
3190///////////////////////////////////////////////////////////////////////////////
3191
Romain Guy3b748a42013-04-17 18:54:38 -07003192Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3193 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3194 if (!texture) {
3195 return mCaches.textureCache.get(bitmap);
3196 }
3197 return texture;
3198}
3199
Romain Guy01d58e42011-01-19 21:54:02 -08003200void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3201 float x, float y, SkPaint* paint) {
3202 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3203 return;
3204 }
3205
3206 int alpha;
3207 SkXfermode::Mode mode;
3208 getAlphaAndMode(paint, &alpha, &mode);
3209
3210 setupDraw();
3211 setupDrawWithTexture(true);
3212 setupDrawAlpha8Color(paint->getColor(), alpha);
3213 setupDrawColorFilter();
3214 setupDrawShader();
3215 setupDrawBlending(true, mode);
3216 setupDrawProgram();
3217 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3218 setupDrawTexture(texture->id);
3219 setupDrawPureColorUniforms();
3220 setupDrawColorFilterUniforms();
3221 setupDrawShaderUniforms();
3222 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3223
3224 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3225
3226 finishDrawTexture();
3227}
3228
Romain Guyf607bdc2010-09-10 19:20:06 -07003229// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003230#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3231#define kStdUnderline_Offset (1.0f / 9.0f)
3232#define kStdUnderline_Thickness (1.0f / 18.0f)
3233
3234void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3235 float x, float y, SkPaint* paint) {
3236 // Handle underline and strike-through
3237 uint32_t flags = paint->getFlags();
3238 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003239 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003240 float underlineWidth = length;
3241 // If length is > 0.0f, we already measured the text for the text alignment
3242 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003243 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003244 }
3245
Romain Guy211370f2012-02-01 16:10:55 -08003246 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003247 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003248 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003249
Raph Levien8b4072d2012-07-30 15:50:00 -07003250 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003251 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003252
Romain Guyf6834472011-01-23 13:32:12 -08003253 int linesCount = 0;
3254 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3255 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3256
3257 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003258 float points[pointsCount];
3259 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003260
3261 if (flags & SkPaint::kUnderlineText_Flag) {
3262 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003263 points[currentPoint++] = left;
3264 points[currentPoint++] = top;
3265 points[currentPoint++] = left + underlineWidth;
3266 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003267 }
3268
3269 if (flags & SkPaint::kStrikeThruText_Flag) {
3270 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003271 points[currentPoint++] = left;
3272 points[currentPoint++] = top;
3273 points[currentPoint++] = left + underlineWidth;
3274 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003275 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003276
Romain Guy726aeba2011-06-01 14:52:00 -07003277 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003278
Romain Guy726aeba2011-06-01 14:52:00 -07003279 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003280 }
3281 }
3282}
3283
Romain Guy672433d2013-01-04 19:05:13 -08003284status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3285 if (mSnapshot->isIgnored()) {
3286 return DrawGlInfo::kStatusDone;
3287 }
3288
Romain Guy735738c2012-12-03 12:34:51 -08003289 int color = paint->getColor();
3290 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003291 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003292 color |= 0x00ffffff;
3293 }
3294 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3295
3296 return drawColorRects(rects, count, color, mode);
3297}
3298
3299status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003300 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003301 if (count == 0) {
3302 return DrawGlInfo::kStatusDone;
3303 }
Romain Guy735738c2012-12-03 12:34:51 -08003304
Romain Guy672433d2013-01-04 19:05:13 -08003305 float left = FLT_MAX;
3306 float top = FLT_MAX;
3307 float right = FLT_MIN;
3308 float bottom = FLT_MIN;
3309
3310 int vertexCount = 0;
3311 Vertex mesh[count * 6];
3312 Vertex* vertex = mesh;
3313
Chris Craik2af46352012-11-26 18:30:17 -08003314 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003315 float l = rects[index + 0];
3316 float t = rects[index + 1];
3317 float r = rects[index + 2];
3318 float b = rects[index + 3];
3319
Romain Guy3b753822013-03-05 10:27:35 -08003320 Vertex::set(vertex++, l, b);
3321 Vertex::set(vertex++, l, t);
3322 Vertex::set(vertex++, r, t);
3323 Vertex::set(vertex++, l, b);
3324 Vertex::set(vertex++, r, t);
3325 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003326
Romain Guy3b753822013-03-05 10:27:35 -08003327 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003328
Romain Guy3b753822013-03-05 10:27:35 -08003329 left = fminf(left, l);
3330 top = fminf(top, t);
3331 right = fmaxf(right, r);
3332 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003333 }
3334
Romain Guy3b753822013-03-05 10:27:35 -08003335 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003336 return DrawGlInfo::kStatusDone;
3337 }
Romain Guy672433d2013-01-04 19:05:13 -08003338
Romain Guy672433d2013-01-04 19:05:13 -08003339 setupDraw();
3340 setupDrawNoTexture();
3341 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3342 setupDrawShader();
3343 setupDrawColorFilter();
3344 setupDrawBlending(mode);
3345 setupDrawProgram();
3346 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003347 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003348 setupDrawColorUniforms();
3349 setupDrawShaderUniforms();
3350 setupDrawColorFilterUniforms();
3351 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3352
Romain Guy8ce00302013-01-15 18:51:42 -08003353 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003354 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003355 }
3356
3357 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3358
3359 return DrawGlInfo::kStatusDrew;
3360}
3361
Romain Guy026c5e162010-06-28 17:12:22 -07003362void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003363 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003364 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003365 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003366 color |= 0x00ffffff;
3367 }
3368
Romain Guy70ca14e2010-12-13 18:24:33 -08003369 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003370 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003371 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003372 setupDrawShader();
3373 setupDrawColorFilter();
3374 setupDrawBlending(mode);
3375 setupDrawProgram();
3376 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3377 setupDrawColorUniforms();
3378 setupDrawShaderUniforms(ignoreTransform);
3379 setupDrawColorFilterUniforms();
3380 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003381
Romain Guyc95c8d62010-09-17 15:31:32 -07003382 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3383}
3384
Romain Guy82ba8142010-07-09 13:25:56 -07003385void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003386 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003387 int alpha;
3388 SkXfermode::Mode mode;
3389 getAlphaAndMode(paint, &alpha, &mode);
3390
Romain Guyd21b6e12011-11-30 20:21:23 -08003391 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003392
Romain Guy3b748a42013-04-17 18:54:38 -07003393 GLvoid* vertices = (GLvoid*) NULL;
3394 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3395
3396 if (texture->uvMapper) {
3397 vertices = &mMeshVertices[0].position[0];
3398 texCoords = &mMeshVertices[0].texture[0];
3399
3400 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3401 texture->uvMapper->map(uvs);
3402
3403 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3404 }
3405
Romain Guy3b753822013-03-05 10:27:35 -08003406 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3407 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3408 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003409
Romain Guyd21b6e12011-11-30 20:21:23 -08003410 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003411 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003412 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3413 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003414 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003415 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003416 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003417 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3418 }
3419
3420 if (texture->uvMapper) {
3421 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003422 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003423}
3424
Romain Guybd6b79b2010-06-26 00:13:53 -07003425void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003426 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3427 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003428 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003429}
3430
3431void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003432 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003433 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003434 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003435
Romain Guy746b7402010-10-26 16:27:31 -07003436 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003437 setupDrawWithTexture();
3438 setupDrawColor(alpha, alpha, alpha, alpha);
3439 setupDrawColorFilter();
3440 setupDrawBlending(blend, mode, swapSrcDst);
3441 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003442 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003443 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003444 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003445 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003446 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003447 }
Romain Guy886b2752013-01-04 12:26:18 -08003448 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003449 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003450 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003451 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003452
Romain Guy6820ac82010-09-15 18:11:50 -07003453 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003454
3455 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003456}
3457
Romain Guy3b748a42013-04-17 18:54:38 -07003458void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3459 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3460 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3461 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3462
3463 setupDraw();
3464 setupDrawWithTexture();
3465 setupDrawColor(alpha, alpha, alpha, alpha);
3466 setupDrawColorFilter();
3467 setupDrawBlending(blend, mode, swapSrcDst);
3468 setupDrawProgram();
3469 if (!dirty) setupDrawDirtyRegionsDisabled();
3470 if (!ignoreScale) {
3471 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3472 } else {
3473 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3474 }
3475 setupDrawTexture(texture);
3476 setupDrawPureColorUniforms();
3477 setupDrawColorFilterUniforms();
3478 setupDrawMeshIndices(vertices, texCoords, vbo);
3479
3480 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3481
3482 finishDrawTexture();
3483}
3484
Romain Guy886b2752013-01-04 12:26:18 -08003485void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3486 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3487 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003488 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003489
3490 setupDraw();
3491 setupDrawWithTexture(true);
3492 if (hasColor) {
3493 setupDrawAlpha8Color(color, alpha);
3494 }
3495 setupDrawColorFilter();
3496 setupDrawShader();
3497 setupDrawBlending(true, mode);
3498 setupDrawProgram();
3499 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003500 if (!ignoreScale) {
3501 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3502 } else {
3503 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3504 }
Romain Guy886b2752013-01-04 12:26:18 -08003505 setupDrawTexture(texture);
3506 setupDrawPureColorUniforms();
3507 setupDrawColorFilterUniforms();
3508 setupDrawShaderUniforms();
3509 setupDrawMesh(vertices, texCoords);
3510
3511 glDrawArrays(drawMode, 0, elementsCount);
3512
3513 finishDrawTexture();
3514}
3515
Romain Guya5aed0d2010-09-09 14:42:43 -07003516void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003517 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003518 if (mCountOverdraw) {
3519 if (!mCaches.blend) glEnable(GL_BLEND);
3520 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3521 glBlendFunc(GL_ONE, GL_ONE);
3522 }
3523
3524 mCaches.blend = true;
3525 mCaches.lastSrcMode = GL_ONE;
3526 mCaches.lastDstMode = GL_ONE;
3527
3528 return;
3529 }
3530
Romain Guy82ba8142010-07-09 13:25:56 -07003531 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003532
Romain Guy82ba8142010-07-09 13:25:56 -07003533 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003534 // These blend modes are not supported by OpenGL directly and have
3535 // to be implemented using shaders. Since the shader will perform
3536 // the blending, turn blending off here
3537 // If the blend mode cannot be implemented using shaders, fall
3538 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003539 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003540 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003541 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003542 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003543
Romain Guy82bc7a72012-01-03 14:13:39 -08003544 if (mCaches.blend) {
3545 glDisable(GL_BLEND);
3546 mCaches.blend = false;
3547 }
3548
3549 return;
3550 } else {
3551 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003552 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003553 }
3554
3555 if (!mCaches.blend) {
3556 glEnable(GL_BLEND);
3557 }
3558
3559 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3560 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3561
3562 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3563 glBlendFunc(sourceMode, destMode);
3564 mCaches.lastSrcMode = sourceMode;
3565 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003566 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003567 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003568 glDisable(GL_BLEND);
3569 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003570 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003571}
3572
Romain Guy889f8d12010-07-29 14:37:42 -07003573bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003574 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003575 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003576 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003577 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003578 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003579 }
Romain Guy6926c722010-07-12 20:20:03 -07003580 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003581}
3582
Romain Guy026c5e162010-06-28 17:12:22 -07003583void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003584 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003585 TextureVertex::setUV(v++, u1, v1);
3586 TextureVertex::setUV(v++, u2, v1);
3587 TextureVertex::setUV(v++, u1, v2);
3588 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003589}
3590
Chris Craik16ecda52013-03-29 10:59:59 -07003591void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003592 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003593 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3594 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003595 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003596 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003597 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003598}
3599
Chris Craik16ecda52013-03-29 10:59:59 -07003600float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3601 float alpha;
3602 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3603 alpha = mDrawModifiers.mOverrideLayerAlpha;
3604 } else {
3605 alpha = layer->getAlpha() / 255.0f;
3606 }
3607 return alpha * mSnapshot->alpha;
3608}
3609
Romain Guy9d5316e2010-06-24 19:30:36 -07003610}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003611}; // namespace android