blob: 5ac06cf041139bb2215cc48624dbe1168c26ae0a [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 Guy8aa195d2013-06-04 18:00:09 -0700374 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700375
Romain Guy50c0f092010-10-19 11:42:22 -0700376 mCaches.blend = true;
377 glEnable(GL_BLEND);
378 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
379 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700380}
381
Romain Guy35643dd2012-09-18 15:40:58 -0700382void OpenGLRenderer::resumeAfterLayer() {
383 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
384 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
385 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700386 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700387
388 mCaches.resetScissor();
389 dirtyClip();
390}
391
Romain Guyba6be8a2012-04-23 18:22:09 -0700392void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700393 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700394}
395
396void OpenGLRenderer::attachFunctor(Functor* functor) {
397 mFunctors.add(functor);
398}
399
Romain Guy8f3b8e32012-03-27 16:33:45 -0700400status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
401 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700402 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700403
Romain Guyba6be8a2012-04-23 18:22:09 -0700404 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800405 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700406 SortedVector<Functor*> functors(mFunctors);
407 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700408
Romain Guyba6be8a2012-04-23 18:22:09 -0700409 DrawGlInfo info;
410 info.clipLeft = 0;
411 info.clipTop = 0;
412 info.clipRight = 0;
413 info.clipBottom = 0;
414 info.isLayer = false;
415 info.width = 0;
416 info.height = 0;
417 memset(info.transform, 0, sizeof(float) * 16);
418
419 for (size_t i = 0; i < count; i++) {
420 Functor* f = functors.itemAt(i);
421 result |= (*f)(DrawGlInfo::kModeProcess, &info);
422
Chris Craikc2c95432012-04-25 15:13:52 -0700423 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700424 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
425 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700426 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700427
Chris Craikc2c95432012-04-25 15:13:52 -0700428 if (result & DrawGlInfo::kStatusInvoke) {
429 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700430 }
431 }
Chris Craikd15321b2012-11-28 14:45:04 -0800432 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700433 }
434
435 return result;
436}
437
438status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700439 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
440
Chet Haasedaf98e92011-01-10 14:10:36 -0800441 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700442 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700443
Romain Guy8a4ac612012-07-17 17:32:48 -0700444 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800445 if (mDirtyClip) {
446 setScissorFromClip();
447 }
Romain Guyd643bb52011-03-01 14:55:21 -0800448
Romain Guy80911b82011-03-16 15:30:12 -0700449 Rect clip(*mSnapshot->clipRect);
450 clip.snapToPixelBoundaries();
451
Romain Guyd643bb52011-03-01 14:55:21 -0800452 // Since we don't know what the functor will draw, let's dirty
453 // tne entire clip region
454 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800455 dirtyLayerUnchecked(clip, getRegion());
456 }
Romain Guyd643bb52011-03-01 14:55:21 -0800457
Romain Guy08aa2cb2011-03-17 11:06:57 -0700458 DrawGlInfo info;
459 info.clipLeft = clip.left;
460 info.clipTop = clip.top;
461 info.clipRight = clip.right;
462 info.clipBottom = clip.bottom;
463 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700464 info.width = getSnapshot()->viewport.getWidth();
465 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700466 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700467
Chris Craik4a2bff72013-04-16 13:50:16 -0700468 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800469
Romain Guy8f3b8e32012-03-27 16:33:45 -0700470 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700471 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800472 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700473
Chris Craik65924a32012-04-05 17:52:11 -0700474 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700475 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700476 }
Romain Guycabfcc12011-03-07 18:06:46 -0800477 }
478
Chet Haasedaf98e92011-01-10 14:10:36 -0800479 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700480 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800481}
482
Romain Guyf6a11b82010-06-23 17:47:49 -0700483///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700484// Debug
485///////////////////////////////////////////////////////////////////////////////
486
Romain Guy0f667532013-03-01 14:31:04 -0800487void OpenGLRenderer::eventMark(const char* name) const {
488 mCaches.eventMark(0, name);
489}
490
Romain Guy87e2f7572012-09-24 11:37:12 -0700491void OpenGLRenderer::startMark(const char* name) const {
492 mCaches.startMark(0, name);
493}
494
495void OpenGLRenderer::endMark() const {
496 mCaches.endMark();
497}
498
499void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
500 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
501 if (clear) {
502 mCaches.disableScissor();
503 mCaches.stencil.clear();
504 }
505 if (enable) {
506 mCaches.stencil.enableDebugWrite();
507 } else {
508 mCaches.stencil.disable();
509 }
510 }
511}
512
513void OpenGLRenderer::renderOverdraw() {
514 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700515 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700516
517 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700518 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700519 clip->right - clip->left, clip->bottom - clip->top);
520
521 mCaches.stencil.enableDebugTest(2);
522 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
523 mCaches.stencil.enableDebugTest(3);
524 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
525 mCaches.stencil.enableDebugTest(4);
526 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
527 mCaches.stencil.enableDebugTest(4, true);
528 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
529 mCaches.stencil.disable();
530 }
531}
532
Romain Guy78dd96d2013-05-03 14:24:16 -0700533void OpenGLRenderer::countOverdraw() {
534 size_t count = mWidth * mHeight;
535 uint32_t* buffer = new uint32_t[count];
536 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
537
538 size_t total = 0;
539 for (size_t i = 0; i < count; i++) {
540 total += buffer[i] & 0xff;
541 }
542
543 mOverdraw = total / float(count);
544
545 delete[] buffer;
546}
547
Romain Guy87e2f7572012-09-24 11:37:12 -0700548///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700549// Layers
550///////////////////////////////////////////////////////////////////////////////
551
552bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700553 if (layer->deferredUpdateScheduled && layer->renderer &&
554 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700555 Rect& dirty = layer->dirtyRect;
556
Romain Guy7c450aa2012-09-21 19:15:00 -0700557 if (inFrame) {
558 endTiling();
559 debugOverdraw(false, false);
560 }
Romain Guy11cb6422012-09-21 00:39:43 -0700561
Romain Guy96885eb2013-03-26 15:05:58 -0700562 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700563 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700564 } else {
565 layer->defer();
566 }
Romain Guy11cb6422012-09-21 00:39:43 -0700567
568 if (inFrame) {
569 resumeAfterLayer();
570 startTiling(mSnapshot);
571 }
572
Romain Guy5bb3c732012-11-29 17:52:58 -0800573 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700574 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700575
576 return true;
577 }
578
579 return false;
580}
581
582void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700583 // If draw deferring is enabled this method will simply defer
584 // the display list of each individual layer. The layers remain
585 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700586 int count = mLayerUpdates.size();
587 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700588 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
589 startMark("Layer Updates");
590 } else {
591 startMark("Defer Layer Updates");
592 }
Romain Guy11cb6422012-09-21 00:39:43 -0700593
Chris Craik1206b9b2013-04-04 14:46:24 -0700594 // Note: it is very important to update the layers in order
595 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700596 Layer* layer = mLayerUpdates.itemAt(i);
597 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700598 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
599 mCaches.resourceCache.decrementRefcount(layer);
600 }
Romain Guy11cb6422012-09-21 00:39:43 -0700601 }
Romain Guy11cb6422012-09-21 00:39:43 -0700602
Romain Guy96885eb2013-03-26 15:05:58 -0700603 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
604 mLayerUpdates.clear();
605 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
606 }
607 endMark();
608 }
609}
610
611void OpenGLRenderer::flushLayers() {
612 int count = mLayerUpdates.size();
613 if (count > 0) {
614 startMark("Apply Layer Updates");
615 char layerName[12];
616
Chris Craik1206b9b2013-04-04 14:46:24 -0700617 // Note: it is very important to update the layers in order
618 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700619 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700620 startMark(layerName);
621
622 Layer* layer = mLayerUpdates.itemAt(i);
623 layer->flush();
624 mCaches.resourceCache.decrementRefcount(layer);
625
Romain Guy96885eb2013-03-26 15:05:58 -0700626 endMark();
627 }
628
629 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700630 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700631
Romain Guy11cb6422012-09-21 00:39:43 -0700632 endMark();
633 }
634}
635
636void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
637 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700638 // Make sure we don't introduce duplicates.
639 // SortedVector would do this automatically but we need to respect
640 // the insertion order. The linear search is not an issue since
641 // this list is usually very short (typically one item, at most a few)
642 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
643 if (mLayerUpdates.itemAt(i) == layer) {
644 return;
645 }
646 }
Romain Guy11cb6422012-09-21 00:39:43 -0700647 mLayerUpdates.push_back(layer);
648 mCaches.resourceCache.incrementRefcount(layer);
649 }
650}
651
652void OpenGLRenderer::clearLayerUpdates() {
653 size_t count = mLayerUpdates.size();
654 if (count > 0) {
655 mCaches.resourceCache.lock();
656 for (size_t i = 0; i < count; i++) {
657 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
658 }
659 mCaches.resourceCache.unlock();
660 mLayerUpdates.clear();
661 }
662}
663
664///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700665// State management
666///////////////////////////////////////////////////////////////////////////////
667
Romain Guybb9524b2010-06-22 18:56:38 -0700668int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700669 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700670}
671
672int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700673 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700674}
675
676void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700677 if (mSaveCount > 1) {
678 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700679 }
Romain Guybb9524b2010-06-22 18:56:38 -0700680}
681
682void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700683 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700684
Romain Guy8fb95422010-08-17 18:38:51 -0700685 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700686 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700687 }
Romain Guybb9524b2010-06-22 18:56:38 -0700688}
689
Romain Guy8aef54f2010-09-01 15:13:49 -0700690int OpenGLRenderer::saveSnapshot(int flags) {
691 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700692 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700693}
694
695bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700696 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700697 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700698 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700699
Romain Guybd6b79b2010-06-26 00:13:53 -0700700 sp<Snapshot> current = mSnapshot;
701 sp<Snapshot> previous = mSnapshot->previous;
702
Romain Guyeb993562010-10-05 18:14:38 -0700703 if (restoreOrtho) {
704 Rect& r = previous->viewport;
705 glViewport(r.left, r.top, r.right, r.bottom);
706 mOrthoMatrix.load(current->orthoMatrix);
707 }
708
Romain Guy8b55f372010-08-18 17:10:07 -0700709 mSaveCount--;
710 mSnapshot = previous;
711
Romain Guy2542d192010-08-18 11:47:12 -0700712 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700713 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700714 }
Romain Guy2542d192010-08-18 11:47:12 -0700715
Romain Guy5ec99242010-11-03 16:19:08 -0700716 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700717 endMark(); // Savelayer
718 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700719 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700720 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700721 }
722
Romain Guy2542d192010-08-18 11:47:12 -0700723 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
Romain Guyf6a11b82010-06-23 17:47:49 -0700726///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700727// Layers
728///////////////////////////////////////////////////////////////////////////////
729
730int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800731 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700732 const GLuint previousFbo = mSnapshot->fbo;
733 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700734
Romain Guyaf636eb2010-12-09 17:47:21 -0800735 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700736 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700737 }
Romain Guyd55a8612010-06-28 17:42:46 -0700738
739 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700740}
741
Chris Craikd90144d2013-03-19 15:03:48 -0700742void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
743 const Rect untransformedBounds(bounds);
744
745 currentTransform().mapRect(bounds);
746
747 // Layers only make sense if they are in the framebuffer's bounds
748 if (bounds.intersect(*mSnapshot->clipRect)) {
749 // We cannot work with sub-pixels in this case
750 bounds.snapToPixelBoundaries();
751
752 // When the layer is not an FBO, we may use glCopyTexImage so we
753 // need to make sure the layer does not extend outside the bounds
754 // of the framebuffer
755 if (!bounds.intersect(mSnapshot->previous->viewport)) {
756 bounds.setEmpty();
757 } else if (fboLayer) {
758 clip.set(bounds);
759 mat4 inverse;
760 inverse.loadInverse(currentTransform());
761 inverse.mapRect(clip);
762 clip.snapToPixelBoundaries();
763 if (clip.intersect(untransformedBounds)) {
764 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
765 bounds.set(untransformedBounds);
766 } else {
767 clip.setEmpty();
768 }
769 }
770 } else {
771 bounds.setEmpty();
772 }
773}
774
Chris Craik408eb122013-03-26 18:55:15 -0700775void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
776 bool fboLayer, int alpha) {
777 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
778 bounds.getHeight() > mCaches.maxTextureSize ||
779 (fboLayer && clip.isEmpty())) {
780 mSnapshot->empty = fboLayer;
781 } else {
782 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
783 }
784}
785
Chris Craikd90144d2013-03-19 15:03:48 -0700786int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
787 int alpha, SkXfermode::Mode mode, int flags) {
788 const GLuint previousFbo = mSnapshot->fbo;
789 const int count = saveSnapshot(flags);
790
791 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
792 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
793 // operations will be able to store and restore the current clip and transform info, and
794 // quick rejection will be correct (for display lists)
795
796 Rect bounds(left, top, right, bottom);
797 Rect clip;
798 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700799 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700800
Chris Craik408eb122013-03-26 18:55:15 -0700801 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700802 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
803 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
804 }
805 }
806
807 return count;
808}
809
810
Romain Guy1c740bc2010-09-13 18:00:09 -0700811/**
812 * Layers are viewed by Skia are slightly different than layers in image editing
813 * programs (for instance.) When a layer is created, previously created layers
814 * and the frame buffer still receive every drawing command. For instance, if a
815 * layer is created and a shape intersecting the bounds of the layers and the
816 * framebuffer is draw, the shape will be drawn on both (unless the layer was
817 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
818 *
819 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
820 * texture. Unfortunately, this is inefficient as it requires every primitive to
821 * be drawn n + 1 times, where n is the number of active layers. In practice this
822 * means, for every primitive:
823 * - Switch active frame buffer
824 * - Change viewport, clip and projection matrix
825 * - Issue the drawing
826 *
827 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700828 * To avoid this, layers are implemented in a different way here, at least in the
829 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
830 * is set. When this flag is set we can redirect all drawing operations into a
831 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700832 *
833 * This implementation relies on the frame buffer being at least RGBA 8888. When
834 * a layer is created, only a texture is created, not an FBO. The content of the
835 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700836 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700837 * buffer and drawing continues as normal. This technique therefore treats the
838 * frame buffer as a scratch buffer for the layers.
839 *
840 * To compose the layers back onto the frame buffer, each layer texture
841 * (containing the original frame buffer data) is drawn as a simple quad over
842 * the frame buffer. The trick is that the quad is set as the composition
843 * destination in the blending equation, and the frame buffer becomes the source
844 * of the composition.
845 *
846 * Drawing layers with an alpha value requires an extra step before composition.
847 * An empty quad is drawn over the layer's region in the frame buffer. This quad
848 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
849 * quad is used to multiply the colors in the frame buffer. This is achieved by
850 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
851 * GL_ZERO, GL_SRC_ALPHA.
852 *
853 * Because glCopyTexImage2D() can be slow, an alternative implementation might
854 * be use to draw a single clipped layer. The implementation described above
855 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700856 *
857 * (1) The frame buffer is actually not cleared right away. To allow the GPU
858 * to potentially optimize series of calls to glCopyTexImage2D, the frame
859 * buffer is left untouched until the first drawing operation. Only when
860 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700861 */
Chet Haased48885a2012-08-28 17:43:28 -0700862bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
863 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700864 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700865 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700866
Romain Guyeb993562010-10-05 18:14:38 -0700867 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
868
Romain Guyf607bdc2010-09-10 19:20:06 -0700869 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700870 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700871 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700872 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700873 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700874
875 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700876 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700877 return false;
878 }
Romain Guyf18fd992010-07-08 11:45:51 -0700879
Romain Guya1d3c912011-12-13 14:55:06 -0800880 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700881 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700882 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700883 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700884 }
885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700887 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
889 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800890 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700891 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700892 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700893
Romain Guy8fb95422010-08-17 18:38:51 -0700894 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700895 mSnapshot->flags |= Snapshot::kFlagIsLayer;
896 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700897
Chris Craik7273daa2013-03-28 11:25:24 -0700898 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700899 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700900 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700901 } else {
902 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800904 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 if (layer->isEmpty()) {
906 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700907 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700908 layer->getWidth(), layer->getHeight(), 0);
909 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800910 } else {
911 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700912 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800913 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700914
Romain Guy54be1cd2011-06-13 19:04:27 -0700915 // Enqueue the buffer coordinates to clear the corresponding region later
916 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700917 }
Romain Guyeb993562010-10-05 18:14:38 -0700918 }
Romain Guyf86ef572010-07-01 11:05:42 -0700919
Romain Guyd55a8612010-06-28 17:42:46 -0700920 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700921}
922
Chet Haased48885a2012-08-28 17:43:28 -0700923bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800924 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700925 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700926
Chet Haased48885a2012-08-28 17:43:28 -0700927 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800928 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
929 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700930 mSnapshot->fbo = layer->getFbo();
931 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
932 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
933 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
934 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700935 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700936
Romain Guy2b7028e2012-09-19 17:25:38 -0700937 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700938 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700939 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
941 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700942
943 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700944 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700945 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700946 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700947 }
948
949 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700950 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700951
Romain Guyf735c8e2013-01-31 17:45:55 -0800952 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700953
954 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700955 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800956 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700957 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700958 glClear(GL_COLOR_BUFFER_BIT);
959
960 dirtyClip();
961
962 // Change the ortho projection
963 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
964 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
965
966 return true;
967}
968
Romain Guy1c740bc2010-09-13 18:00:09 -0700969/**
970 * Read the documentation of createLayer() before doing anything in this method.
971 */
Romain Guy1d83e192010-08-17 11:37:00 -0700972void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
973 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000974 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700975 return;
976 }
977
Romain Guy8ce00302013-01-15 18:51:42 -0800978 Layer* layer = current->layer;
979 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700981
982 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700983 endTiling();
984
Romain Guye0aa84b2012-04-03 19:30:26 -0700985 // Detach the texture from the FBO
986 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800987
988 layer->removeFbo(false);
989
Romain Guyeb993562010-10-05 18:14:38 -0700990 // Unbind current FBO and restore previous one
991 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700992 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700993
994 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700995 }
996
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700998 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001000 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001001 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001002 }
1003
Romain Guy03750a02010-10-18 14:06:08 -07001004 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001005
Romain Guya1d3c912011-12-13 14:55:06 -08001006 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001007
Romain Guy5b3b3522010-10-27 18:57:51 -07001008 // When the layer is stored in an FBO, we can save a bit of fillrate by
1009 // drawing only the dirty region
1010 if (fboLayer) {
1011 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 if (layer->getColorFilter()) {
1013 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001014 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001015 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001017 resetColorFilter();
1018 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 } else if (!rect.isEmpty()) {
1020 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1021 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001022 }
Romain Guy8b55f372010-08-18 17:10:07 -07001023
Romain Guy746b7402010-10-26 16:27:31 -07001024 dirtyClip();
1025
Romain Guyeb993562010-10-05 18:14:38 -07001026 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001027 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001028 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001029 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001030 }
1031}
1032
Romain Guyaa6c24c2011-04-28 18:40:04 -07001033void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001034 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001035
1036 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001037 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001038 setupDrawWithTexture();
1039 } else {
1040 setupDrawWithExternalTexture();
1041 }
1042 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001043 setupDrawColor(alpha, alpha, alpha, alpha);
1044 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001045 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001046 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001047 setupDrawPureColorUniforms();
1048 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001049 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1050 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001051 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001052 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001053 }
Romain Guy3b753822013-03-05 10:27:35 -08001054 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001055 layer->getWidth() == (uint32_t) rect.getWidth() &&
1056 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001057 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1058 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001059
Romain Guyd21b6e12011-11-30 20:21:23 -08001060 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001061 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1062 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001063 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001064 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1065 }
1066 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001067 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1068
1069 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1070
1071 finishDrawTexture();
1072}
1073
Romain Guy5b3b3522010-10-27 18:57:51 -07001074void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001076 const Rect& texCoords = layer->texCoords;
1077 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1078 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001079
Romain Guy9ace8f52011-07-07 20:50:11 -07001080 float x = rect.left;
1081 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001082 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001083 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001084 layer->getHeight() == (uint32_t) rect.getHeight();
1085
1086 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001087 // When we're swapping, the layer is already in screen coordinates
1088 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001089 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1090 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001091 }
1092
Romain Guyd21b6e12011-11-30 20:21:23 -08001093 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001094 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001095 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001096 }
1097
Chris Craik16ecda52013-03-29 10:59:59 -07001098 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001099 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001100 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001101 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001102 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1103 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001104
Romain Guyaa6c24c2011-04-28 18:40:04 -07001105 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1106 } else {
1107 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1108 drawTextureLayer(layer, rect);
1109 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1110 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001111}
1112
Chris Craik34416ea2013-04-15 16:08:28 -07001113/**
1114 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1115 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1116 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1117 * by saveLayer's restore
1118 */
1119#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1120 DRAW_COMMAND; \
1121 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1122 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1123 DRAW_COMMAND; \
1124 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1125 } \
1126 }
1127
1128#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1129
Romain Guy5b3b3522010-10-27 18:57:51 -07001130void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001131 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001132 layer->setRegionAsRect();
1133
Chris Craik34416ea2013-04-15 16:08:28 -07001134 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001135
Romain Guy5b3b3522010-10-27 18:57:51 -07001136 layer->region.clear();
1137 return;
1138 }
1139
Romain Guy8a3957d2011-09-07 17:55:15 -07001140 // TODO: See LayerRenderer.cpp::generateMesh() for important
1141 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001142 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001143 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001144 const android::Rect* rects;
1145 Region safeRegion;
1146 if (CC_LIKELY(hasRectToRectTransform())) {
1147 rects = layer->region.getArray(&count);
1148 } else {
1149 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1150 rects = safeRegion.getArray(&count);
1151 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001152
Chris Craik16ecda52013-03-29 10:59:59 -07001153 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001154 const float texX = 1.0f / float(layer->getWidth());
1155 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001156 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Romain Guy8ce00302013-01-15 18:51:42 -08001158 setupDraw();
1159
1160 // We must get (and therefore bind) the region mesh buffer
1161 // after we setup drawing in case we need to mess with the
1162 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001163 TextureVertex* mesh = mCaches.getRegionMesh();
1164 GLsizei numQuads = 0;
1165
Romain Guy7230a742011-01-10 22:26:16 -08001166 setupDrawWithTexture();
1167 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001168 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001169 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001170 setupDrawProgram();
1171 setupDrawDirtyRegionsDisabled();
1172 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001173 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001174 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001175 if (currentTransform().isPureTranslate()) {
1176 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1177 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001178
Romain Guyd21b6e12011-11-30 20:21:23 -08001179 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001180 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1181 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001182 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001183 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1184 }
Romain Guy15bc6432011-12-13 13:11:32 -08001185 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001186
1187 for (size_t i = 0; i < count; i++) {
1188 const android::Rect* r = &rects[i];
1189
1190 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001191 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001192 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001193 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001194
1195 // TODO: Reject quads outside of the clip
1196 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1197 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1198 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1199 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1200
1201 numQuads++;
1202
1203 if (numQuads >= REGION_MESH_QUAD_COUNT) {
Chris Craik34416ea2013-04-15 16:08:28 -07001204 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1205 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001206 numQuads = 0;
1207 mesh = mCaches.getRegionMesh();
1208 }
1209 }
1210
1211 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001212 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1213 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001214 }
1215
Romain Guy7230a742011-01-10 22:26:16 -08001216 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001217
1218#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001219 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001220#endif
1221
1222 layer->region.clear();
1223 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001224}
1225
Romain Guy3a3133d2011-02-01 22:59:58 -08001226void OpenGLRenderer::drawRegionRects(const Region& region) {
1227#if DEBUG_LAYERS_AS_REGIONS
1228 size_t count;
1229 const android::Rect* rects = region.getArray(&count);
1230
1231 uint32_t colors[] = {
1232 0x7fff0000, 0x7f00ff00,
1233 0x7f0000ff, 0x7fff00ff,
1234 };
1235
1236 int offset = 0;
1237 int32_t top = rects[0].top;
1238
1239 for (size_t i = 0; i < count; i++) {
1240 if (top != rects[i].top) {
1241 offset ^= 0x2;
1242 top = rects[i].top;
1243 }
1244
1245 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1246 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1247 SkXfermode::kSrcOver_Mode);
1248 }
1249#endif
1250}
1251
Romain Guy8ce00302013-01-15 18:51:42 -08001252void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1253 SkXfermode::Mode mode, bool dirty) {
1254 int count = 0;
1255 Vector<float> rects;
1256
1257 SkRegion::Iterator it(region);
1258 while (!it.done()) {
1259 const SkIRect& r = it.rect();
1260 rects.push(r.fLeft);
1261 rects.push(r.fTop);
1262 rects.push(r.fRight);
1263 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001264 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001265 it.next();
1266 }
1267
Romain Guy3bbacf22013-02-06 16:51:04 -08001268 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001269}
1270
Romain Guy5b3b3522010-10-27 18:57:51 -07001271void OpenGLRenderer::dirtyLayer(const float left, const float top,
1272 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001273 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001274 Rect bounds(left, top, right, bottom);
1275 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001276 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001277 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001278}
1279
1280void OpenGLRenderer::dirtyLayer(const float left, const float top,
1281 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001282 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001283 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001284 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001285 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001286}
1287
1288void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001289 if (bounds.intersect(*mSnapshot->clipRect)) {
1290 bounds.snapToPixelBoundaries();
1291 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1292 if (!dirty.isEmpty()) {
1293 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001294 }
1295 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001296}
1297
Romain Guy54be1cd2011-06-13 19:04:27 -07001298void OpenGLRenderer::clearLayerRegions() {
1299 const size_t count = mLayers.size();
1300 if (count == 0) return;
1301
1302 if (!mSnapshot->isIgnored()) {
1303 // Doing several glScissor/glClear here can negatively impact
1304 // GPUs with a tiler architecture, instead we draw quads with
1305 // the Clear blending mode
1306
1307 // The list contains bounds that have already been clipped
1308 // against their initial clip rect, and the current clip
1309 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001310 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001311
1312 Vertex mesh[count * 6];
1313 Vertex* vertex = mesh;
1314
1315 for (uint32_t i = 0; i < count; i++) {
1316 Rect* bounds = mLayers.itemAt(i);
1317
1318 Vertex::set(vertex++, bounds->left, bounds->bottom);
1319 Vertex::set(vertex++, bounds->left, bounds->top);
1320 Vertex::set(vertex++, bounds->right, bounds->top);
1321 Vertex::set(vertex++, bounds->left, bounds->bottom);
1322 Vertex::set(vertex++, bounds->right, bounds->top);
1323 Vertex::set(vertex++, bounds->right, bounds->bottom);
1324
1325 delete bounds;
1326 }
Romain Guye67307c2013-02-11 18:01:20 -08001327 // We must clear the list of dirty rects before we
1328 // call setupDraw() to prevent stencil setup to do
1329 // the same thing again
1330 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001331
1332 setupDraw(false);
1333 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1334 setupDrawBlending(true, SkXfermode::kClear_Mode);
1335 setupDrawProgram();
1336 setupDrawPureColorUniforms();
1337 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001338 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001339
Romain Guy54be1cd2011-06-13 19:04:27 -07001340 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001341
1342 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001343 } else {
1344 for (uint32_t i = 0; i < count; i++) {
1345 delete mLayers.itemAt(i);
1346 }
Romain Guye67307c2013-02-11 18:01:20 -08001347 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001348 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001349}
1350
Romain Guybd6b79b2010-06-26 00:13:53 -07001351///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001352// State Deferral
1353///////////////////////////////////////////////////////////////////////////////
1354
Chris Craikff785832013-03-08 13:12:16 -08001355bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001356 const Rect& currentClip = *(mSnapshot->clipRect);
1357 const mat4& currentMatrix = *(mSnapshot->transform);
1358
Chris Craikff785832013-03-08 13:12:16 -08001359 if (stateDeferFlags & kStateDeferFlag_Draw) {
1360 // state has bounds initialized in local coordinates
1361 if (!state.mBounds.isEmpty()) {
1362 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001363 Rect clippedBounds(state.mBounds);
1364 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001365 // quick rejected
1366 return true;
1367 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001368
1369 state.mClipSideFlags = kClipSide_Unclipped;
1370 if (!currentClip.contains(state.mBounds)) {
1371 int& flags = state.mClipSideFlags;
1372 // op partially clipped, so record which sides are clipped for clip-aware merging
1373 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1374 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1375 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1376 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1377 }
1378 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001379 } else {
Romain Guy2db5e992013-05-21 15:29:59 -07001380 // If we don't have bounds, let's assume we're clipped
1381 // to prevent merging
Chris Craik28ce94a2013-05-31 11:38:03 -07001382 state.mClipSideFlags = kClipSide_Full;
Chris Craikff785832013-03-08 13:12:16 -08001383 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001384 }
1385 }
1386
Chris Craik527a3aa2013-03-04 10:19:31 -08001387 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1388 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001389 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001390 }
1391
Chris Craik7273daa2013-03-28 11:25:24 -07001392 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1393 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001394 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001395 state.mDrawModifiers = mDrawModifiers;
1396 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001397 return false;
1398}
1399
Chris Craik527a3aa2013-03-04 10:19:31 -08001400void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001401 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001402 mDrawModifiers = state.mDrawModifiers;
1403 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001404
Chris Craik527a3aa2013-03-04 10:19:31 -08001405 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001406 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1407 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001408 dirtyClip();
1409 }
Chris Craikc3566d02013-02-04 16:16:33 -08001410}
1411
Chris Craik28ce94a2013-05-31 11:38:03 -07001412/**
1413 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1414 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1415 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1416 *
1417 * This method should be called when restoreDisplayState() won't be restoring the clip
1418 */
1419void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1420 if (clipRect != NULL) {
1421 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1422 } else {
1423 mSnapshot->setClip(0, 0, mWidth, mHeight);
1424 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001425 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001426 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001427}
1428
Chris Craikc3566d02013-02-04 16:16:33 -08001429///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001430// Transforms
1431///////////////////////////////////////////////////////////////////////////////
1432
1433void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001434 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001435}
1436
1437void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001438 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001439}
1440
1441void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001442 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001443}
1444
Romain Guy807daf72011-01-18 11:19:19 -08001445void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001446 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001447}
1448
Romain Guyf6a11b82010-06-23 17:47:49 -07001449void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001450 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001451 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001452 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001453 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001454 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001455}
1456
Chris Craikb98a0162013-02-21 11:30:22 -08001457bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001458 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001459}
1460
Romain Guyf6a11b82010-06-23 17:47:49 -07001461void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001462 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001463}
1464
1465void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001466 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001467 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001468 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001469 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001470}
1471
1472///////////////////////////////////////////////////////////////////////////////
1473// Clipping
1474///////////////////////////////////////////////////////////////////////////////
1475
Romain Guybb9524b2010-06-22 18:56:38 -07001476void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001477 Rect clip(*mSnapshot->clipRect);
1478 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001479
Romain Guy8a4ac612012-07-17 17:32:48 -07001480 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1481 clip.getWidth(), clip.getHeight())) {
1482 mDirtyClip = false;
1483 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001484}
1485
Romain Guy8ce00302013-01-15 18:51:42 -08001486void OpenGLRenderer::ensureStencilBuffer() {
1487 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1488 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1489 // just hope we have one when hasLayer() returns false.
1490 if (hasLayer()) {
1491 attachStencilBufferToLayer(mSnapshot->layer);
1492 }
1493}
1494
1495void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1496 // The layer's FBO is already bound when we reach this stage
1497 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001498 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1499 // is attached after we initiated tiling. We must turn it off,
1500 // attach the new render buffer then turn tiling back on
1501 endTiling();
1502
Romain Guy8d4aeb72013-02-12 16:08:55 -08001503 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001504 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001505 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001506
Romain Guyf735c8e2013-01-31 17:45:55 -08001507 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001508 }
1509}
1510
1511void OpenGLRenderer::setStencilFromClip() {
1512 if (!mCaches.debugOverdraw) {
1513 if (!mSnapshot->clipRegion->isEmpty()) {
1514 // NOTE: The order here is important, we must set dirtyClip to false
1515 // before any draw call to avoid calling back into this method
1516 mDirtyClip = false;
1517
1518 ensureStencilBuffer();
1519
1520 mCaches.stencil.enableWrite();
1521
1522 // Clear the stencil but first make sure we restrict drawing
1523 // to the region's bounds
1524 bool resetScissor = mCaches.enableScissor();
1525 if (resetScissor) {
1526 // The scissor was not set so we now need to update it
1527 setScissorFromClip();
1528 }
1529 mCaches.stencil.clear();
1530 if (resetScissor) mCaches.disableScissor();
1531
1532 // NOTE: We could use the region contour path to generate a smaller mesh
1533 // Since we are using the stencil we could use the red book path
1534 // drawing technique. It might increase bandwidth usage though.
1535
1536 // The last parameter is important: we are not drawing in the color buffer
1537 // so we don't want to dirty the current layer, if any
1538 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1539
1540 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001541
1542 // Draw the region used to generate the stencil if the appropriate debug
1543 // mode is enabled
1544 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1545 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1546 }
Romain Guy8ce00302013-01-15 18:51:42 -08001547 } else {
1548 mCaches.stencil.disable();
1549 }
1550 }
1551}
1552
Romain Guy9d5316e2010-06-24 19:30:36 -07001553const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001554 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001555}
1556
Romain Guy8a4ac612012-07-17 17:32:48 -07001557bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1558 if (mSnapshot->isIgnored()) {
1559 return true;
1560 }
1561
1562 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001563 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001564 r.snapToPixelBoundaries();
1565
1566 Rect clipRect(*mSnapshot->clipRect);
1567 clipRect.snapToPixelBoundaries();
1568
1569 return !clipRect.intersects(r);
1570}
1571
Romain Guy35643dd2012-09-18 15:40:58 -07001572bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1573 Rect& transformed, Rect& clip) {
1574 if (mSnapshot->isIgnored()) {
1575 return true;
1576 }
1577
1578 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001579 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001580 transformed.snapToPixelBoundaries();
1581
1582 clip.set(*mSnapshot->clipRect);
1583 clip.snapToPixelBoundaries();
1584
1585 return !clip.intersects(transformed);
1586}
1587
Romain Guy672433d2013-01-04 19:05:13 -08001588bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1589 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001590 if (paint->getStyle() != SkPaint::kFill_Style) {
1591 float outset = paint->getStrokeWidth() * 0.5f;
1592 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1593 } else {
1594 return quickReject(left, top, right, bottom);
1595 }
1596}
1597
Romain Guyc7d53492010-06-25 13:41:57 -07001598bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001599 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001600 return true;
1601 }
1602
Romain Guy1d83e192010-08-17 11:37:00 -07001603 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001604 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001605 r.snapToPixelBoundaries();
1606
1607 Rect clipRect(*mSnapshot->clipRect);
1608 clipRect.snapToPixelBoundaries();
1609
Romain Guy586cae32012-07-13 15:28:31 -07001610 bool rejected = !clipRect.intersects(r);
1611 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001612 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001613 }
1614
1615 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001616}
1617
Romain Guy8ce00302013-01-15 18:51:42 -08001618void OpenGLRenderer::debugClip() {
1619#if DEBUG_CLIP_REGIONS
1620 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1621 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1622 }
1623#endif
1624}
1625
Romain Guy079ba2c2010-07-16 14:12:24 -07001626bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001627 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001628 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1629 if (clipped) {
1630 dirtyClip();
1631 }
1632 return !mSnapshot->clipRect->isEmpty();
1633 }
1634
1635 SkPath path;
1636 path.addRect(left, top, right, bottom);
1637
1638 return clipPath(&path, op);
1639}
1640
1641bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1642 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001643 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001644
1645 SkPath transformed;
1646 path->transform(transform, &transformed);
1647
1648 SkRegion clip;
1649 if (!mSnapshot->clipRegion->isEmpty()) {
1650 clip.setRegion(*mSnapshot->clipRegion);
1651 } else {
1652 Rect* bounds = mSnapshot->clipRect;
1653 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1654 }
1655
1656 SkRegion region;
1657 region.setPath(transformed, clip);
1658
1659 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001660 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001661 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001662 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001663 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001664}
1665
Romain Guy735738c2012-12-03 12:34:51 -08001666bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001667 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1668 if (clipped) {
1669 dirtyClip();
1670 }
1671 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001672}
1673
Chet Haasea23eed82012-04-12 15:19:04 -07001674Rect* OpenGLRenderer::getClipRect() {
1675 return mSnapshot->clipRect;
1676}
1677
Romain Guyf6a11b82010-06-23 17:47:49 -07001678///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001679// Drawing commands
1680///////////////////////////////////////////////////////////////////////////////
1681
Romain Guy54be1cd2011-06-13 19:04:27 -07001682void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001683 // TODO: It would be best if we could do this before quickReject()
1684 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001685 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001686 // Make sure setScissor & setStencil happen at the beginning of
1687 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001688 if (mDirtyClip) {
1689 if (mCaches.scissorEnabled) {
1690 setScissorFromClip();
1691 }
Romain Guy8ce00302013-01-15 18:51:42 -08001692 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001693 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001694
Romain Guy70ca14e2010-12-13 18:24:33 -08001695 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001696
Romain Guy70ca14e2010-12-13 18:24:33 -08001697 mSetShaderColor = false;
1698 mColorSet = false;
1699 mColorA = mColorR = mColorG = mColorB = 0.0f;
1700 mTextureUnit = 0;
1701 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001702
1703 // Enable debug highlight when what we're about to draw is tested against
1704 // the stencil buffer and if stencil highlight debugging is on
1705 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1706 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1707 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001708
1709 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001710}
1711
1712void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1713 mDescription.hasTexture = true;
1714 mDescription.hasAlpha8Texture = isAlpha8;
1715}
1716
Romain Guyff316ec2013-02-13 18:39:43 -08001717void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1718 mDescription.hasTexture = true;
1719 mDescription.hasColors = true;
1720 mDescription.hasAlpha8Texture = isAlpha8;
1721}
1722
Romain Guyaa6c24c2011-04-28 18:40:04 -07001723void OpenGLRenderer::setupDrawWithExternalTexture() {
1724 mDescription.hasExternalTexture = true;
1725}
1726
Romain Guy15bc6432011-12-13 13:11:32 -08001727void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001728 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001729}
1730
Chris Craik710f46d2012-09-17 17:25:49 -07001731void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001732 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001733}
1734
Romain Guy8d0d4782010-12-14 20:13:35 -08001735void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1736 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001737 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1738 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1739 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001740 mColorSet = true;
1741 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1742}
1743
Romain Guy86568192010-12-14 15:55:39 -08001744void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1745 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001746 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1747 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1748 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001749 mColorSet = true;
1750 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1751}
1752
Romain Guy41210632012-07-16 17:04:24 -07001753void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1754 mCaches.fontRenderer->describe(mDescription, paint);
1755}
1756
Romain Guy70ca14e2010-12-13 18:24:33 -08001757void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1758 mColorA = a;
1759 mColorR = r;
1760 mColorG = g;
1761 mColorB = b;
1762 mColorSet = true;
1763 mSetShaderColor = mDescription.setColor(r, g, b, a);
1764}
1765
1766void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001767 if (mDrawModifiers.mShader) {
1768 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001769 }
1770}
1771
1772void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001773 if (mDrawModifiers.mColorFilter) {
1774 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001775 }
1776}
1777
Romain Guyf09ef512011-05-27 11:43:46 -07001778void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1779 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1780 mColorA = 1.0f;
1781 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001782 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001783 }
1784}
1785
Romain Guy70ca14e2010-12-13 18:24:33 -08001786void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001787 // When the blending mode is kClear_Mode, we need to use a modulate color
1788 // argb=1,0,0,0
1789 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001790 bool blend = (mColorSet && mColorA < 1.0f) ||
1791 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1792 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001793}
1794
1795void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001796 // When the blending mode is kClear_Mode, we need to use a modulate color
1797 // argb=1,0,0,0
1798 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001799 blend |= (mColorSet && mColorA < 1.0f) ||
1800 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1801 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1802 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001803}
1804
1805void OpenGLRenderer::setupDrawProgram() {
1806 useProgram(mCaches.programCache.get(mDescription));
1807}
1808
1809void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1810 mTrackDirtyRegions = false;
1811}
1812
1813void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1814 bool ignoreTransform) {
1815 mModelView.loadTranslate(left, top, 0.0f);
1816 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001817 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1818 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001819 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001820 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001821 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1822 }
1823}
1824
Chet Haase8a5cc922011-04-26 07:28:09 -07001825void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001826 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001827}
1828
Romain Guy70ca14e2010-12-13 18:24:33 -08001829void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1830 bool ignoreTransform, bool ignoreModelView) {
1831 if (!ignoreModelView) {
1832 mModelView.loadTranslate(left, top, 0.0f);
1833 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001834 } else {
1835 mModelView.loadIdentity();
1836 }
Romain Guy86568192010-12-14 15:55:39 -08001837 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1838 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001839 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001840 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001841 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001842 }
1843 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001844 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001845 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1846 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001847}
1848
1849void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001850 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1852 }
1853}
1854
Romain Guy86568192010-12-14 15:55:39 -08001855void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001856 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001857 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001858 }
1859}
1860
Romain Guy70ca14e2010-12-13 18:24:33 -08001861void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001862 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001863 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001864 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001865 }
Chris Craikc3566d02013-02-04 16:16:33 -08001866 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1867 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001868 }
1869}
1870
Romain Guy8d0d4782010-12-14 20:13:35 -08001871void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001872 if (mDrawModifiers.mShader) {
1873 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001874 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001875 }
1876}
1877
Romain Guy70ca14e2010-12-13 18:24:33 -08001878void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001879 if (mDrawModifiers.mColorFilter) {
1880 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001881 }
1882}
1883
Romain Guy41210632012-07-16 17:04:24 -07001884void OpenGLRenderer::setupDrawTextGammaUniforms() {
1885 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1886}
1887
Romain Guy70ca14e2010-12-13 18:24:33 -08001888void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001889 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001890 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001891 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001892}
1893
1894void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001895 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001896 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001897 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001898}
1899
Romain Guyaa6c24c2011-04-28 18:40:04 -07001900void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1901 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001902 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001903 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001904}
1905
Romain Guy8f0095c2011-05-02 17:24:22 -07001906void OpenGLRenderer::setupDrawTextureTransform() {
1907 mDescription.hasTextureTransform = true;
1908}
1909
1910void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001911 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1912 GL_FALSE, &transform.data[0]);
1913}
1914
Romain Guy70ca14e2010-12-13 18:24:33 -08001915void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001916 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001917 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001918 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001919 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001920 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001921 }
Romain Guyd71dd362011-12-12 19:03:35 -08001922
Chris Craikcb4d6002012-09-25 12:00:29 -07001923 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001924 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001925 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001926 }
1927
1928 mCaches.unbindIndicesBuffer();
1929}
1930
Romain Guyff316ec2013-02-13 18:39:43 -08001931void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1932 bool force = mCaches.unbindMeshBuffer();
1933 GLsizei stride = sizeof(ColorTextureVertex);
1934
1935 mCaches.bindPositionVertexPointer(force, vertices, stride);
1936 if (mCaches.currentProgram->texCoords >= 0) {
1937 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1938 }
1939 int slot = mCaches.currentProgram->getAttrib("colors");
1940 if (slot >= 0) {
1941 glEnableVertexAttribArray(slot);
1942 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1943 }
1944
1945 mCaches.unbindIndicesBuffer();
1946}
1947
Romain Guy3b748a42013-04-17 18:54:38 -07001948void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1949 bool force = false;
1950 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1951 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1952 // use the default VBO found in Caches
1953 if (!vertices || vbo) {
1954 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1955 } else {
1956 force = mCaches.unbindMeshBuffer();
1957 }
1958 mCaches.bindIndicesBuffer();
1959
Chris Craikcb4d6002012-09-25 12:00:29 -07001960 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001961 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001962 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001963 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001964}
1965
Chet Haase5b0200b2011-04-13 17:58:08 -07001966void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001967 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001968 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001969 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001970}
1971
Romain Guy70ca14e2010-12-13 18:24:33 -08001972void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001973}
1974
1975///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001976// Drawing
1977///////////////////////////////////////////////////////////////////////////////
1978
Chris Craikff785832013-03-08 13:12:16 -08001979status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1980 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001981 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001982 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1983 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001984 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001985 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001986 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001987 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1988 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001989 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001990 }
1991
Chris Craik28ce94a2013-05-31 11:38:03 -07001992 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
1993 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001994 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1995 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001996
1997 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001998 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001999
Chet Haase58d110a2013-04-10 07:43:29 -07002000 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002001 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002002
Romain Guy65549432012-03-26 16:45:05 -07002003 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002004}
2005
Chris Craikc3566d02013-02-04 16:16:33 -08002006void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002007 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002008 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002009 }
2010}
2011
Romain Guya168d732011-03-18 16:50:13 -07002012void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2013 int alpha;
2014 SkXfermode::Mode mode;
2015 getAlphaAndMode(paint, &alpha, &mode);
2016
Romain Guy886b2752013-01-04 12:26:18 -08002017 int color = paint != NULL ? paint->getColor() : 0;
2018
Romain Guya168d732011-03-18 16:50:13 -07002019 float x = left;
2020 float y = top;
2021
Romain Guy886b2752013-01-04 12:26:18 -08002022 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2023
Romain Guya168d732011-03-18 16:50:13 -07002024 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002025 if (currentTransform().isPureTranslate()) {
2026 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2027 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002028 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002029
2030 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002031 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002032 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002033 }
2034
Romain Guy3b748a42013-04-17 18:54:38 -07002035 // No need to check for a UV mapper on the texture object, only ARGB_8888
2036 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002037 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002038 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2039 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002040}
2041
Chris Craik527a3aa2013-03-04 10:19:31 -08002042status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, int bitmapCount, TextureVertex* vertices,
Romain Guy2db5e992013-05-21 15:29:59 -07002043 bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002044 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002045 Texture* texture = getTexture(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002046 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002047
Chris Craik527a3aa2013-03-04 10:19:31 -08002048 const AutoTexture autoCleanup(texture);
2049
2050 int alpha;
2051 SkXfermode::Mode mode;
2052 getAlphaAndMode(paint, &alpha, &mode);
2053
2054 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002055 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002056
2057 const float x = (int) floorf(bounds.left + 0.5f);
2058 const float y = (int) floorf(bounds.top + 0.5f);
2059 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2060 int color = paint != NULL ? paint->getColor() : 0;
2061 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2062 texture->id, paint != NULL, color, alpha, mode,
2063 &vertices[0].position[0], &vertices[0].texture[0],
2064 GL_TRIANGLES, bitmapCount * 6, true, true);
2065 } else {
2066 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2067 texture->id, alpha / 255.0f, mode, texture->blend,
2068 &vertices[0].position[0], &vertices[0].texture[0],
2069 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true);
2070 }
2071
2072 return DrawGlInfo::kStatusDrew;
2073}
2074
Chet Haase48659092012-05-31 15:21:51 -07002075status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002076 const float right = left + bitmap->width();
2077 const float bottom = top + bitmap->height();
2078
2079 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002080 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002081 }
2082
Romain Guya1d3c912011-12-13 14:55:06 -08002083 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002084 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002085 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002086 const AutoTexture autoCleanup(texture);
2087
Romain Guy211370f2012-02-01 16:10:55 -08002088 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002089 drawAlphaBitmap(texture, left, top, paint);
2090 } else {
2091 drawTextureRect(left, top, right, bottom, texture, paint);
2092 }
Chet Haase48659092012-05-31 15:21:51 -07002093
2094 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002095}
2096
Chet Haase48659092012-05-31 15:21:51 -07002097status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002098 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2099 const mat4 transform(*matrix);
2100 transform.mapRect(r);
2101
Romain Guy6926c722010-07-12 20:20:03 -07002102 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002103 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002104 }
2105
Romain Guya1d3c912011-12-13 14:55:06 -08002106 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002107 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002108 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002109 const AutoTexture autoCleanup(texture);
2110
Romain Guy5b3b3522010-10-27 18:57:51 -07002111 // This could be done in a cheaper way, all we need is pass the matrix
2112 // to the vertex shader. The save/restore is a bit overkill.
2113 save(SkCanvas::kMatrix_SaveFlag);
2114 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002115 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2116 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2117 } else {
2118 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2119 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002120 restore();
Chet Haase48659092012-05-31 15:21:51 -07002121
2122 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002123}
2124
Chet Haase48659092012-05-31 15:21:51 -07002125status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002126 const float right = left + bitmap->width();
2127 const float bottom = top + bitmap->height();
2128
2129 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002130 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002131 }
2132
2133 mCaches.activeTexture(0);
2134 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2135 const AutoTexture autoCleanup(texture);
2136
Romain Guy886b2752013-01-04 12:26:18 -08002137 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2138 drawAlphaBitmap(texture, left, top, paint);
2139 } else {
2140 drawTextureRect(left, top, right, bottom, texture, paint);
2141 }
Chet Haase48659092012-05-31 15:21:51 -07002142
2143 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002144}
2145
Chet Haase48659092012-05-31 15:21:51 -07002146status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002147 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002148 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002149 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002150 }
2151
Romain Guyb18d2d02011-02-10 15:52:54 -08002152 float left = FLT_MAX;
2153 float top = FLT_MAX;
2154 float right = FLT_MIN;
2155 float bottom = FLT_MIN;
2156
Romain Guya92bb4d2012-10-16 11:08:44 -07002157 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002158
Romain Guyff316ec2013-02-13 18:39:43 -08002159 ColorTextureVertex mesh[count];
2160 ColorTextureVertex* vertex = mesh;
2161
2162 bool cleanupColors = false;
2163 if (!colors) {
2164 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2165 colors = new int[colorsCount];
2166 memset(colors, 0xff, colorsCount * sizeof(int));
2167 cleanupColors = true;
2168 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002169
Romain Guy3b748a42013-04-17 18:54:38 -07002170 mCaches.activeTexture(0);
2171 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2172 const UvMapper& mapper(getMapper(texture));
2173
Romain Guy5a7b4662011-01-20 19:09:30 -08002174 for (int32_t y = 0; y < meshHeight; y++) {
2175 for (int32_t x = 0; x < meshWidth; x++) {
2176 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2177
2178 float u1 = float(x) / meshWidth;
2179 float u2 = float(x + 1) / meshWidth;
2180 float v1 = float(y) / meshHeight;
2181 float v2 = float(y + 1) / meshHeight;
2182
Romain Guy3b748a42013-04-17 18:54:38 -07002183 mapper.map(u1, v1, u2, v2);
2184
Romain Guy5a7b4662011-01-20 19:09:30 -08002185 int ax = i + (meshWidth + 1) * 2;
2186 int ay = ax + 1;
2187 int bx = i;
2188 int by = bx + 1;
2189 int cx = i + 2;
2190 int cy = cx + 1;
2191 int dx = i + (meshWidth + 1) * 2 + 2;
2192 int dy = dx + 1;
2193
Romain Guyff316ec2013-02-13 18:39:43 -08002194 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2195 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2196 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002197
Romain Guyff316ec2013-02-13 18:39:43 -08002198 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2199 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2200 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002201
Romain Guya92bb4d2012-10-16 11:08:44 -07002202 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2203 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2204 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2205 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002206 }
2207 }
2208
Romain Guya92bb4d2012-10-16 11:08:44 -07002209 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002210 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002211 return DrawGlInfo::kStatusDone;
2212 }
2213
Romain Guyff316ec2013-02-13 18:39:43 -08002214 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002215 texture = mCaches.textureCache.get(bitmap);
2216 if (!texture) {
2217 if (cleanupColors) delete[] colors;
2218 return DrawGlInfo::kStatusDone;
2219 }
Romain Guyff316ec2013-02-13 18:39:43 -08002220 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002221 const AutoTexture autoCleanup(texture);
2222
2223 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2224 texture->setFilter(FILTER(paint), true);
2225
2226 int alpha;
2227 SkXfermode::Mode mode;
2228 getAlphaAndMode(paint, &alpha, &mode);
2229
Romain Guyff316ec2013-02-13 18:39:43 -08002230 float a = alpha / 255.0f;
2231
Romain Guya92bb4d2012-10-16 11:08:44 -07002232 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002233 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002234 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002235
Romain Guyff316ec2013-02-13 18:39:43 -08002236 setupDraw();
2237 setupDrawWithTextureAndColor();
2238 setupDrawColor(a, a, a, a);
2239 setupDrawColorFilter();
2240 setupDrawBlending(true, mode, false);
2241 setupDrawProgram();
2242 setupDrawDirtyRegionsDisabled();
2243 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2244 setupDrawTexture(texture->id);
2245 setupDrawPureColorUniforms();
2246 setupDrawColorFilterUniforms();
2247 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2248
2249 glDrawArrays(GL_TRIANGLES, 0, count);
2250
2251 finishDrawTexture();
2252
2253 int slot = mCaches.currentProgram->getAttrib("colors");
2254 if (slot >= 0) {
2255 glDisableVertexAttribArray(slot);
2256 }
2257
2258 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002259
2260 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002261}
2262
Chet Haase48659092012-05-31 15:21:51 -07002263status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002264 float srcLeft, float srcTop, float srcRight, float srcBottom,
2265 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002266 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002267 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002268 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002269 }
2270
Romain Guya1d3c912011-12-13 14:55:06 -08002271 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002272 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002273 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002274 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002275
Romain Guy8ba548f2010-06-30 19:21:21 -07002276 const float width = texture->width;
2277 const float height = texture->height;
2278
Romain Guy3b748a42013-04-17 18:54:38 -07002279 float u1 = fmax(0.0f, srcLeft / width);
2280 float v1 = fmax(0.0f, srcTop / height);
2281 float u2 = fmin(1.0f, srcRight / width);
2282 float v2 = fmin(1.0f, srcBottom / height);
2283
2284 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002285
Romain Guy03750a02010-10-18 14:06:08 -07002286 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002287 resetDrawTextureTexCoords(u1, v1, u2, v2);
2288
Romain Guy03750a02010-10-18 14:06:08 -07002289 int alpha;
2290 SkXfermode::Mode mode;
2291 getAlphaAndMode(paint, &alpha, &mode);
2292
Romain Guyd21b6e12011-11-30 20:21:23 -08002293 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2294
Romain Guy886b2752013-01-04 12:26:18 -08002295 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2296 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002297
Romain Guy886b2752013-01-04 12:26:18 -08002298 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2299 // Apply a scale transform on the canvas only when a shader is in use
2300 // Skia handles the ratio between the dst and src rects as a scale factor
2301 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002302 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002303 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002304
Romain Guy3b753822013-03-05 10:27:35 -08002305 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2306 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2307 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002308
2309 dstRight = x + (dstRight - dstLeft);
2310 dstBottom = y + (dstBottom - dstTop);
2311
2312 dstLeft = x;
2313 dstTop = y;
2314
2315 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2316 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002317 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002318 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002319 }
2320
2321 if (CC_UNLIKELY(useScaleTransform)) {
2322 save(SkCanvas::kMatrix_SaveFlag);
2323 translate(dstLeft, dstTop);
2324 scale(scaleX, scaleY);
2325
2326 dstLeft = 0.0f;
2327 dstTop = 0.0f;
2328
2329 dstRight = srcRight - srcLeft;
2330 dstBottom = srcBottom - srcTop;
2331 }
2332
2333 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2334 int color = paint ? paint->getColor() : 0;
2335 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2336 texture->id, paint != NULL, color, alpha, mode,
2337 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2338 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2339 } else {
2340 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2341 texture->id, alpha / 255.0f, mode, texture->blend,
2342 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2343 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2344 }
2345
2346 if (CC_UNLIKELY(useScaleTransform)) {
2347 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002348 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002349
2350 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002351
2352 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002353}
2354
Romain Guy3b748a42013-04-17 18:54:38 -07002355status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002356 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002357 int alpha;
2358 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002359 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002360
Romain Guy6926c722010-07-12 20:20:03 -07002361 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002362 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002363 }
2364
Romain Guy4c2547f2013-06-11 16:19:24 -07002365 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002366 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2367 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002368
Romain Guy4c2547f2013-06-11 16:19:24 -07002369 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, alpha, mode);
2370}
2371
2372status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh,
2373 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2374 int alpha, SkXfermode::Mode mode) {
2375
2376 if (quickReject(left, top, right, bottom)) {
2377 return DrawGlInfo::kStatusDone;
2378 }
2379
Romain Guy211370f2012-02-01 16:10:55 -08002380 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002381 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002382 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002383 if (!texture) return DrawGlInfo::kStatusDone;
2384 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002385
Romain Guya4adcf02013-02-28 12:15:35 -08002386 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2387 texture->setFilter(GL_LINEAR, true);
2388
Romain Guy3b753822013-03-05 10:27:35 -08002389 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002390 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002391 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002392 const float offsetX = left + currentTransform().getTranslateX();
2393 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002394 const size_t count = mesh->quads.size();
2395 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002396 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002397 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002398 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2399 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2400 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002401 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002402 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002403 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002404 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002405 }
2406 }
2407
Romain Guy3b748a42013-04-17 18:54:38 -07002408 alpha *= mSnapshot->alpha;
2409
Romain Guy211370f2012-02-01 16:10:55 -08002410 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002411 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2412 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002413
Romain Guy3b748a42013-04-17 18:54:38 -07002414 right = x + right - left;
2415 bottom = y + bottom - top;
2416 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2417 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2418 GL_TRIANGLES, mesh->indexCount, false, true,
2419 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002420 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002421 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2422 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2423 GL_TRIANGLES, mesh->indexCount, false, false,
2424 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002425 }
Romain Guy054dc182010-10-15 17:55:25 -07002426 }
Chet Haase48659092012-05-31 15:21:51 -07002427
2428 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002429}
2430
Chris Craik65cd6122012-12-10 17:56:27 -08002431status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2432 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002433 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002434 // no vertices to draw
2435 return DrawGlInfo::kStatusDone;
2436 }
2437
Chris Craikcb4d6002012-09-25 12:00:29 -07002438 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002439 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2440 bool isAA = paint->isAntiAlias();
2441
Chris Craik710f46d2012-09-17 17:25:49 -07002442 setupDraw();
2443 setupDrawNoTexture();
2444 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002445 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2446 setupDrawColorFilter();
2447 setupDrawShader();
2448 setupDrawBlending(isAA, mode);
2449 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002450 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002451 setupDrawColorUniforms();
2452 setupDrawColorFilterUniforms();
2453 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002454
Chris Craik710f46d2012-09-17 17:25:49 -07002455 void* vertices = vertexBuffer.getBuffer();
2456 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002457 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002458 mCaches.resetTexCoordsVertexPointer();
2459 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002460
Chris Craik710f46d2012-09-17 17:25:49 -07002461 int alphaSlot = -1;
2462 if (isAA) {
2463 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2464 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002465
Chris Craik710f46d2012-09-17 17:25:49 -07002466 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002467 glEnableVertexAttribArray(alphaSlot);
2468 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002469 }
Romain Guy04299382012-07-18 17:15:41 -07002470
Chris Craik6d29c8d2013-05-08 18:35:44 -07002471 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002472
Chris Craik710f46d2012-09-17 17:25:49 -07002473 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002474 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002475 }
Chris Craik65cd6122012-12-10 17:56:27 -08002476
2477 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002478}
2479
2480/**
Chris Craik65cd6122012-12-10 17:56:27 -08002481 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2482 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2483 * screen space in all directions. However, instead of using a fragment shader to compute the
2484 * translucency of the color from its position, we simply use a varying parameter to define how far
2485 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2486 *
2487 * Doesn't yet support joins, caps, or path effects.
2488 */
2489status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2490 VertexBuffer vertexBuffer;
2491 // TODO: try clipping large paths to viewport
2492 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2493
Romain Guyc46d07a2013-03-15 19:06:39 -07002494 if (hasLayer()) {
2495 SkRect bounds = path.getBounds();
2496 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2497 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2498 }
Chris Craik65cd6122012-12-10 17:56:27 -08002499
2500 return drawVertexBuffer(vertexBuffer, paint);
2501}
2502
2503/**
2504 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2505 * and additional geometry for defining an alpha slope perimeter.
2506 *
2507 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2508 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2509 * in-shader alpha region, but found it to be taxing on some GPUs.
2510 *
2511 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2512 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002513 */
Chet Haase48659092012-05-31 15:21:51 -07002514status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002515 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002516
Chris Craik65cd6122012-12-10 17:56:27 -08002517 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002518
Chris Craik65cd6122012-12-10 17:56:27 -08002519 VertexBuffer buffer;
2520 SkRect bounds;
2521 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002522
2523 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2524 return DrawGlInfo::kStatusDone;
2525 }
2526
Romain Guy3b753822013-03-05 10:27:35 -08002527 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002528
Chris Craik65cd6122012-12-10 17:56:27 -08002529 bool useOffset = !paint->isAntiAlias();
2530 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002531}
2532
Chet Haase48659092012-05-31 15:21:51 -07002533status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002534 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002535
Chris Craik6d29c8d2013-05-08 18:35:44 -07002536 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002537
Chris Craik6d29c8d2013-05-08 18:35:44 -07002538 VertexBuffer buffer;
2539 SkRect bounds;
2540 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002541
Chris Craik6d29c8d2013-05-08 18:35:44 -07002542 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2543 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002544 }
2545
Chris Craik6d29c8d2013-05-08 18:35:44 -07002546 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002547
Chris Craik6d29c8d2013-05-08 18:35:44 -07002548 bool useOffset = !paint->isAntiAlias();
2549 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002550}
2551
Chet Haase48659092012-05-31 15:21:51 -07002552status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002553 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002554 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002555
Romain Guyae88e5e2010-10-22 17:49:18 -07002556 Rect& clip(*mSnapshot->clipRect);
2557 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002558
Romain Guy3d58c032010-07-14 16:34:53 -07002559 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002560
2561 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002562}
Romain Guy9d5316e2010-06-24 19:30:36 -07002563
Chet Haase48659092012-05-31 15:21:51 -07002564status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2565 SkPaint* paint) {
2566 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002567 const AutoTexture autoCleanup(texture);
2568
2569 const float x = left + texture->left - texture->offset;
2570 const float y = top + texture->top - texture->offset;
2571
2572 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002573
2574 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002575}
2576
Chet Haase48659092012-05-31 15:21:51 -07002577status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002578 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002579 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2580 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002581 return DrawGlInfo::kStatusDone;
2582 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002583
Chris Craikcb4d6002012-09-25 12:00:29 -07002584 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002585 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002586 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002587 right - left, bottom - top, rx, ry, p);
2588 return drawShape(left, top, texture, p);
2589 }
2590
2591 SkPath path;
2592 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002593 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2594 float outset = p->getStrokeWidth() / 2;
2595 rect.outset(outset, outset);
2596 rx += outset;
2597 ry += outset;
2598 }
Chris Craik710f46d2012-09-17 17:25:49 -07002599 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002600 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002601}
2602
Chris Craik710f46d2012-09-17 17:25:49 -07002603status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002604 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002605 x + radius, y + radius, p) ||
2606 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002607 return DrawGlInfo::kStatusDone;
2608 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002609 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002610 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002611 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002612 return drawShape(x - radius, y - radius, texture, p);
2613 }
2614
2615 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002616 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2617 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2618 } else {
2619 path.addCircle(x, y, radius);
2620 }
Chris Craik65cd6122012-12-10 17:56:27 -08002621 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002622}
Romain Guy01d58e42011-01-19 21:54:02 -08002623
Chet Haase48659092012-05-31 15:21:51 -07002624status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002625 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002626 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2627 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002628 return DrawGlInfo::kStatusDone;
2629 }
Romain Guy01d58e42011-01-19 21:54:02 -08002630
Chris Craikcb4d6002012-09-25 12:00:29 -07002631 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002632 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002633 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002634 return drawShape(left, top, texture, p);
2635 }
2636
2637 SkPath path;
2638 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002639 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2640 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2641 }
Chris Craik710f46d2012-09-17 17:25:49 -07002642 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002643 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002644}
2645
Chet Haase48659092012-05-31 15:21:51 -07002646status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002647 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002648 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2649 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002650 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002651 }
2652
Chris Craik780c1282012-10-04 14:10:49 -07002653 if (fabs(sweepAngle) >= 360.0f) {
2654 return drawOval(left, top, right, bottom, p);
2655 }
2656
2657 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002658 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002659 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002660 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002661 startAngle, sweepAngle, useCenter, p);
2662 return drawShape(left, top, texture, p);
2663 }
2664
2665 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2666 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2667 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2668 }
2669
2670 SkPath path;
2671 if (useCenter) {
2672 path.moveTo(rect.centerX(), rect.centerY());
2673 }
2674 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2675 if (useCenter) {
2676 path.close();
2677 }
Chris Craik65cd6122012-12-10 17:56:27 -08002678 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002679}
2680
Romain Guycf8675e2012-10-02 12:32:25 -07002681// See SkPaintDefaults.h
2682#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2683
Chet Haase48659092012-05-31 15:21:51 -07002684status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002685 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2686 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002687 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002688 }
2689
Chris Craik710f46d2012-09-17 17:25:49 -07002690 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002691 // only fill style is supported by drawConvexPath, since others have to handle joins
2692 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2693 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2694 mCaches.activeTexture(0);
2695 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002696 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002697 return drawShape(left, top, texture, p);
2698 }
2699
2700 SkPath path;
2701 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2702 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2703 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2704 }
2705 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002706 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002707 }
2708
Romain Guy3b753822013-03-05 10:27:35 -08002709 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002710 SkPath path;
2711 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002712 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002713 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002714 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002715 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002716 }
Romain Guyc7d53492010-06-25 13:41:57 -07002717}
Romain Guy9d5316e2010-06-24 19:30:36 -07002718
Raph Levien416a8472012-07-19 22:48:17 -07002719void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2720 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2721 float x, float y) {
2722 mCaches.activeTexture(0);
2723
2724 // NOTE: The drop shadow will not perform gamma correction
2725 // if shader-based correction is enabled
2726 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2727 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002728 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002729 // If the drop shadow exceeds the max texture size or couldn't be
2730 // allocated, skip drawing
2731 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002732 const AutoTexture autoCleanup(shadow);
2733
Chris Craikc3566d02013-02-04 16:16:33 -08002734 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2735 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002736
Chris Craikc3566d02013-02-04 16:16:33 -08002737 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2738 int shadowColor = mDrawModifiers.mShadowColor;
2739 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002740 shadowColor = 0xffffffff;
2741 }
2742
2743 setupDraw();
2744 setupDrawWithTexture(true);
2745 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2746 setupDrawColorFilter();
2747 setupDrawShader();
2748 setupDrawBlending(true, mode);
2749 setupDrawProgram();
2750 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2751 setupDrawTexture(shadow->id);
2752 setupDrawPureColorUniforms();
2753 setupDrawColorFilterUniforms();
2754 setupDrawShaderUniforms();
2755 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2756
2757 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2758}
2759
Romain Guy768bffc2013-02-27 13:50:45 -08002760bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2761 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2762 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2763}
2764
Romain Guy257ae352013-03-20 16:31:12 -07002765class TextSetupFunctor: public Functor {
2766public:
2767 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2768 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2769 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2770 alpha(alpha), mode(mode), paint(paint) {
2771 }
2772 ~TextSetupFunctor() { }
2773
2774 status_t operator ()(int what, void* data) {
2775 renderer.setupDraw();
2776 renderer.setupDrawTextGamma(paint);
2777 renderer.setupDrawDirtyRegionsDisabled();
2778 renderer.setupDrawWithTexture(true);
2779 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2780 renderer.setupDrawColorFilter();
2781 renderer.setupDrawShader();
2782 renderer.setupDrawBlending(true, mode);
2783 renderer.setupDrawProgram();
2784 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2785 // Calling setupDrawTexture with the name 0 will enable the
2786 // uv attributes and increase the texture unit count
2787 // texture binding will be performed by the font renderer as
2788 // needed
2789 renderer.setupDrawTexture(0);
2790 renderer.setupDrawPureColorUniforms();
2791 renderer.setupDrawColorFilterUniforms();
2792 renderer.setupDrawShaderUniforms(pureTranslate);
2793 renderer.setupDrawTextGammaUniforms();
2794
2795 return NO_ERROR;
2796 }
2797
2798 OpenGLRenderer& renderer;
2799 float x;
2800 float y;
2801 bool pureTranslate;
2802 int alpha;
2803 SkXfermode::Mode mode;
2804 SkPaint* paint;
2805};
2806
Chet Haase48659092012-05-31 15:21:51 -07002807status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002808 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002809 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002810 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002811 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002812
Romain Guy671d6cf2012-01-18 12:39:17 -08002813 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002814 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002815 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002816 }
2817
2818 float x = 0.0f;
2819 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002820 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002821 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002822 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2823 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002824 }
2825
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002826 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002827 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002828
2829 int alpha;
2830 SkXfermode::Mode mode;
2831 getAlphaAndMode(paint, &alpha, &mode);
2832
Chris Craikc3566d02013-02-04 16:16:33 -08002833 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002834 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2835 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002836 }
2837
Romain Guy671d6cf2012-01-18 12:39:17 -08002838 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002839 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002840 if (pureTranslate && !linearFilter) {
2841 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2842 }
Romain Guy257ae352013-03-20 16:31:12 -07002843 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002844
2845 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2846 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2847
Romain Guy211370f2012-02-01 16:10:55 -08002848 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002849
Romain Guy257ae352013-03-20 16:31:12 -07002850 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002851 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002852 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002853 if (hasActiveLayer) {
2854 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002855 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002856 }
2857 dirtyLayerUnchecked(bounds, getRegion());
2858 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002859 }
Chet Haase48659092012-05-31 15:21:51 -07002860
2861 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002862}
2863
Romain Guy624234f2013-03-05 16:43:31 -08002864mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2865 mat4 fontTransform;
2866 if (CC_LIKELY(transform.isPureTranslate())) {
2867 fontTransform = mat4::identity();
2868 } else {
2869 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002870 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002871 } else {
2872 float sx, sy;
2873 currentTransform().decomposeScale(sx, sy);
2874 fontTransform.loadScale(sx, sy, 1.0f);
2875 }
2876 }
2877 return fontTransform;
2878}
2879
Chris Craik41541822013-05-03 16:35:54 -07002880status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2881 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002882 DrawOpMode drawOpMode) {
2883
Chris Craik527a3aa2013-03-04 10:19:31 -08002884 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002885 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2886 // drawing as ops from DeferredDisplayList are already filtered for these
2887 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2888 quickReject(bounds)) {
2889 return DrawGlInfo::kStatusDone;
2890 }
Romain Guycac5fd32011-12-01 20:08:50 -08002891 }
2892
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002893 const float oldX = x;
2894 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002895
2896 const mat4& transform = currentTransform();
2897 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002898
Romain Guy211370f2012-02-01 16:10:55 -08002899 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002900 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2901 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002902 }
2903
Romain Guy86568192010-12-14 15:55:39 -08002904 int alpha;
2905 SkXfermode::Mode mode;
2906 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002907
Romain Guyc74f45a2013-02-26 19:10:14 -08002908 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2909
Chris Craikc3566d02013-02-04 16:16:33 -08002910 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002911 fontRenderer.setFont(paint, mat4::identity());
2912 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2913 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002914 }
2915
Romain Guy19d4dd82013-03-04 11:14:26 -08002916 const bool hasActiveLayer = hasLayer();
2917
Romain Guy624234f2013-03-05 16:43:31 -08002918 // We only pass a partial transform to the font renderer. That partial
2919 // matrix defines how glyphs are rasterized. Typically we want glyphs
2920 // to be rasterized at their final size on screen, which means the partial
2921 // matrix needs to take the scale factor into account.
2922 // When a partial matrix is used to transform glyphs during rasterization,
2923 // the mesh is generated with the inverse transform (in the case of scale,
2924 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2925 // apply the full transform matrix at draw time in the vertex shader.
2926 // Applying the full matrix in the shader is the easiest way to handle
2927 // rotation and perspective and allows us to always generated quads in the
2928 // font renderer which greatly simplifies the code, clipping in particular.
2929 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002930 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002931
Romain Guy6620c6d2010-12-06 18:07:02 -08002932 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002933 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002934 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002935
Romain Guy624234f2013-03-05 16:43:31 -08002936 // TODO: Implement better clipping for scaled/rotated text
2937 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002938 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07002939
Raph Levien996e57c2012-07-23 15:22:52 -07002940 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002941 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002942
2943 // don't call issuedrawcommand, do it at end of batch
2944 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002945 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002946 SkPaint paintCopy(*paint);
2947 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2948 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002949 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002950 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002951 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002952 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002953 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002954
Chris Craik527a3aa2013-03-04 10:19:31 -08002955 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002956 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002957 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002958 }
Chris Craik41541822013-05-03 16:35:54 -07002959 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002960 }
Romain Guy694b5192010-07-21 21:33:20 -07002961
Chris Craik41541822013-05-03 16:35:54 -07002962 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002963
2964 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002965}
2966
Chet Haase48659092012-05-31 15:21:51 -07002967status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002968 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002969 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002970 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002971 }
2972
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002973 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002974 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002975 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002976
2977 int alpha;
2978 SkXfermode::Mode mode;
2979 getAlphaAndMode(paint, &alpha, &mode);
2980
Romain Guy03d58522012-02-24 17:54:07 -08002981 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002982 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002983 setupDrawDirtyRegionsDisabled();
2984 setupDrawWithTexture(true);
2985 setupDrawAlpha8Color(paint->getColor(), alpha);
2986 setupDrawColorFilter();
2987 setupDrawShader();
2988 setupDrawBlending(true, mode);
2989 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002990 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002991 // Calling setupDrawTexture with the name 0 will enable the
2992 // uv attributes and increase the texture unit count
2993 // texture binding will be performed by the font renderer as
2994 // needed
2995 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002996 setupDrawPureColorUniforms();
2997 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002998 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002999 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003000
Romain Guy97771732012-02-28 18:17:02 -08003001 const Rect* clip = &mSnapshot->getLocalClip();
3002 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 -08003003
Romain Guy97771732012-02-28 18:17:02 -08003004 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003005
3006 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3007 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003008 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003009 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003010 dirtyLayerUnchecked(bounds, getRegion());
3011 }
Romain Guy97771732012-02-28 18:17:02 -08003012 }
Chet Haase48659092012-05-31 15:21:51 -07003013
3014 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003015}
3016
Chet Haase48659092012-05-31 15:21:51 -07003017status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3018 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003019
Romain Guya1d3c912011-12-13 14:55:06 -08003020 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003021
Romain Guyfb8b7632010-08-23 21:05:08 -07003022 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003023 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003024 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003025
Romain Guy8b55f372010-08-18 17:10:07 -07003026 const float x = texture->left - texture->offset;
3027 const float y = texture->top - texture->offset;
3028
Romain Guy01d58e42011-01-19 21:54:02 -08003029 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003030
3031 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003032}
3033
Chris Craika08f95c2013-03-15 17:24:33 -07003034status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003035 if (!layer) {
3036 return DrawGlInfo::kStatusDone;
3037 }
3038
Romain Guyb2e2f242012-10-17 18:18:35 -07003039 mat4* transform = NULL;
3040 if (layer->isTextureLayer()) {
3041 transform = &layer->getTransform();
3042 if (!transform->isIdentity()) {
3043 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003044 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003045 }
3046 }
3047
Romain Guy35643dd2012-09-18 15:40:58 -07003048 Rect transformed;
3049 Rect clip;
3050 const bool rejected = quickRejectNoScissor(x, y,
3051 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
3052
3053 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003054 if (transform && !transform->isIdentity()) {
3055 restore();
3056 }
Chet Haase48659092012-05-31 15:21:51 -07003057 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003058 }
3059
Romain Guy5bb3c732012-11-29 17:52:58 -08003060 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003061
Romain Guy87e2f7572012-09-24 11:37:12 -07003062 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08003063 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003064
Romain Guy211370f2012-02-01 16:10:55 -08003065 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003066 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3067 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003068
Romain Guyc88e3572011-01-22 00:32:12 -08003069 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003070 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3071 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003072 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003073 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003074 setupDraw();
3075 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003076 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003077 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003078 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003079 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003080 setupDrawPureColorUniforms();
3081 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003082 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003083 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3084 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3085 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003086
Romain Guyd21b6e12011-11-30 20:21:23 -08003087 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003088 setupDrawModelViewTranslate(tx, ty,
3089 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003090 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003091 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003092 setupDrawModelViewTranslate(x, y,
3093 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3094 }
Romain Guyc88e3572011-01-22 00:32:12 -08003095 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003096
Chris Craik34416ea2013-04-15 16:08:28 -07003097 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3098 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3099 GL_UNSIGNED_SHORT, layer->meshIndices));
Romain Guyf219da52011-01-16 12:54:25 -08003100
Romain Guyc88e3572011-01-22 00:32:12 -08003101 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003102
3103#if DEBUG_LAYERS_AS_REGIONS
3104 drawRegionRects(layer->region);
3105#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003106 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003107
Chris Craikc3566d02013-02-04 16:16:33 -08003108 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003109
Romain Guy5bb3c732012-11-29 17:52:58 -08003110 if (layer->debugDrawUpdate) {
3111 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003112 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3113 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3114 }
Romain Guyf219da52011-01-16 12:54:25 -08003115 }
Chris Craik34416ea2013-04-15 16:08:28 -07003116 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003117
Romain Guyb2e2f242012-10-17 18:18:35 -07003118 if (transform && !transform->isIdentity()) {
3119 restore();
3120 }
3121
Chet Haase48659092012-05-31 15:21:51 -07003122 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003123}
3124
Romain Guy6926c722010-07-12 20:20:03 -07003125///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003126// Shaders
3127///////////////////////////////////////////////////////////////////////////////
3128
3129void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003130 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003131}
3132
Romain Guy06f96e22010-07-30 19:18:16 -07003133void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003134 mDrawModifiers.mShader = shader;
3135 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003136 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003137 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003138}
3139
Romain Guyd27977d2010-07-14 19:18:51 -07003140///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003141// Color filters
3142///////////////////////////////////////////////////////////////////////////////
3143
3144void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003145 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003146}
3147
3148void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003149 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003150}
3151
3152///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003153// Drop shadow
3154///////////////////////////////////////////////////////////////////////////////
3155
3156void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003157 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003158}
3159
3160void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003161 mDrawModifiers.mHasShadow = true;
3162 mDrawModifiers.mShadowRadius = radius;
3163 mDrawModifiers.mShadowDx = dx;
3164 mDrawModifiers.mShadowDy = dy;
3165 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003166}
3167
3168///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003169// Draw filters
3170///////////////////////////////////////////////////////////////////////////////
3171
3172void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003173 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3174 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003175 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003176 mDrawModifiers.mPaintFilterClearBits = 0;
3177 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003178}
3179
3180void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003181 mDrawModifiers.mHasDrawFilter = true;
3182 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3183 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003184}
3185
Chris Craika08f95c2013-03-15 17:24:33 -07003186SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003187 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003188 return paint;
3189 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003190
3191 uint32_t flags = paint->getFlags();
3192
3193 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003194 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3195 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003196
3197 return &mFilteredPaint;
3198}
3199
3200///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003201// Drawing implementation
3202///////////////////////////////////////////////////////////////////////////////
3203
Romain Guy3b748a42013-04-17 18:54:38 -07003204Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3205 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3206 if (!texture) {
3207 return mCaches.textureCache.get(bitmap);
3208 }
3209 return texture;
3210}
3211
Romain Guy01d58e42011-01-19 21:54:02 -08003212void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3213 float x, float y, SkPaint* paint) {
3214 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3215 return;
3216 }
3217
3218 int alpha;
3219 SkXfermode::Mode mode;
3220 getAlphaAndMode(paint, &alpha, &mode);
3221
3222 setupDraw();
3223 setupDrawWithTexture(true);
3224 setupDrawAlpha8Color(paint->getColor(), alpha);
3225 setupDrawColorFilter();
3226 setupDrawShader();
3227 setupDrawBlending(true, mode);
3228 setupDrawProgram();
3229 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3230 setupDrawTexture(texture->id);
3231 setupDrawPureColorUniforms();
3232 setupDrawColorFilterUniforms();
3233 setupDrawShaderUniforms();
3234 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3235
3236 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3237
3238 finishDrawTexture();
3239}
3240
Romain Guyf607bdc2010-09-10 19:20:06 -07003241// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003242#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3243#define kStdUnderline_Offset (1.0f / 9.0f)
3244#define kStdUnderline_Thickness (1.0f / 18.0f)
3245
Chris Craik41541822013-05-03 16:35:54 -07003246void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003247 float x, float y, SkPaint* paint) {
3248 // Handle underline and strike-through
3249 uint32_t flags = paint->getFlags();
3250 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003251 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003252
Romain Guy211370f2012-02-01 16:10:55 -08003253 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003254 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003255 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003256
Raph Levien8b4072d2012-07-30 15:50:00 -07003257 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003258 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003259
Romain Guyf6834472011-01-23 13:32:12 -08003260 int linesCount = 0;
3261 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3262 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3263
3264 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003265 float points[pointsCount];
3266 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003267
3268 if (flags & SkPaint::kUnderlineText_Flag) {
3269 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003270 points[currentPoint++] = left;
3271 points[currentPoint++] = top;
3272 points[currentPoint++] = left + underlineWidth;
3273 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003274 }
3275
3276 if (flags & SkPaint::kStrikeThruText_Flag) {
3277 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003278 points[currentPoint++] = left;
3279 points[currentPoint++] = top;
3280 points[currentPoint++] = left + underlineWidth;
3281 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003282 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003283
Romain Guy726aeba2011-06-01 14:52:00 -07003284 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003285
Romain Guy726aeba2011-06-01 14:52:00 -07003286 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003287 }
3288 }
3289}
3290
Romain Guy672433d2013-01-04 19:05:13 -08003291status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3292 if (mSnapshot->isIgnored()) {
3293 return DrawGlInfo::kStatusDone;
3294 }
3295
Romain Guy735738c2012-12-03 12:34:51 -08003296 int color = paint->getColor();
3297 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003298 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003299 color |= 0x00ffffff;
3300 }
3301 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3302
3303 return drawColorRects(rects, count, color, mode);
3304}
3305
3306status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003307 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003308 if (count == 0) {
3309 return DrawGlInfo::kStatusDone;
3310 }
Romain Guy735738c2012-12-03 12:34:51 -08003311
Romain Guy672433d2013-01-04 19:05:13 -08003312 float left = FLT_MAX;
3313 float top = FLT_MAX;
3314 float right = FLT_MIN;
3315 float bottom = FLT_MIN;
3316
3317 int vertexCount = 0;
3318 Vertex mesh[count * 6];
3319 Vertex* vertex = mesh;
3320
Chris Craik2af46352012-11-26 18:30:17 -08003321 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003322 float l = rects[index + 0];
3323 float t = rects[index + 1];
3324 float r = rects[index + 2];
3325 float b = rects[index + 3];
3326
Romain Guy3b753822013-03-05 10:27:35 -08003327 Vertex::set(vertex++, l, b);
3328 Vertex::set(vertex++, l, t);
3329 Vertex::set(vertex++, r, t);
3330 Vertex::set(vertex++, l, b);
3331 Vertex::set(vertex++, r, t);
3332 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003333
Romain Guy3b753822013-03-05 10:27:35 -08003334 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003335
Romain Guy3b753822013-03-05 10:27:35 -08003336 left = fminf(left, l);
3337 top = fminf(top, t);
3338 right = fmaxf(right, r);
3339 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003340 }
3341
Romain Guy3b753822013-03-05 10:27:35 -08003342 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003343 return DrawGlInfo::kStatusDone;
3344 }
Romain Guy672433d2013-01-04 19:05:13 -08003345
Romain Guy672433d2013-01-04 19:05:13 -08003346 setupDraw();
3347 setupDrawNoTexture();
3348 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3349 setupDrawShader();
3350 setupDrawColorFilter();
3351 setupDrawBlending(mode);
3352 setupDrawProgram();
3353 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003354 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003355 setupDrawColorUniforms();
3356 setupDrawShaderUniforms();
3357 setupDrawColorFilterUniforms();
3358 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3359
Romain Guy8ce00302013-01-15 18:51:42 -08003360 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003361 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003362 }
3363
3364 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3365
3366 return DrawGlInfo::kStatusDrew;
3367}
3368
Romain Guy026c5e162010-06-28 17:12:22 -07003369void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003370 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003371 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003372 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003373 color |= 0x00ffffff;
3374 }
3375
Romain Guy70ca14e2010-12-13 18:24:33 -08003376 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003377 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003378 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003379 setupDrawShader();
3380 setupDrawColorFilter();
3381 setupDrawBlending(mode);
3382 setupDrawProgram();
3383 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3384 setupDrawColorUniforms();
3385 setupDrawShaderUniforms(ignoreTransform);
3386 setupDrawColorFilterUniforms();
3387 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003388
Romain Guyc95c8d62010-09-17 15:31:32 -07003389 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3390}
3391
Romain Guy82ba8142010-07-09 13:25:56 -07003392void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003393 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003394 int alpha;
3395 SkXfermode::Mode mode;
3396 getAlphaAndMode(paint, &alpha, &mode);
3397
Romain Guyd21b6e12011-11-30 20:21:23 -08003398 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003399
Romain Guy3b748a42013-04-17 18:54:38 -07003400 GLvoid* vertices = (GLvoid*) NULL;
3401 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3402
3403 if (texture->uvMapper) {
3404 vertices = &mMeshVertices[0].position[0];
3405 texCoords = &mMeshVertices[0].texture[0];
3406
3407 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3408 texture->uvMapper->map(uvs);
3409
3410 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3411 }
3412
Romain Guy3b753822013-03-05 10:27:35 -08003413 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3414 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3415 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003416
Romain Guyd21b6e12011-11-30 20:21:23 -08003417 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003418 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003419 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3420 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003421 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003422 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003423 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003424 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3425 }
3426
3427 if (texture->uvMapper) {
3428 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003429 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003430}
3431
Romain Guybd6b79b2010-06-26 00:13:53 -07003432void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003433 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3434 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003435 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003436}
3437
3438void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003439 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003440 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003441 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003442
Romain Guy746b7402010-10-26 16:27:31 -07003443 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003444 setupDrawWithTexture();
3445 setupDrawColor(alpha, alpha, alpha, alpha);
3446 setupDrawColorFilter();
3447 setupDrawBlending(blend, mode, swapSrcDst);
3448 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003449 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003450 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003451 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003452 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003453 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003454 }
Romain Guy886b2752013-01-04 12:26:18 -08003455 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003456 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003457 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003458 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003459
Romain Guy6820ac82010-09-15 18:11:50 -07003460 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003461
3462 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003463}
3464
Romain Guy3b748a42013-04-17 18:54:38 -07003465void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3466 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3467 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3468 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3469
3470 setupDraw();
3471 setupDrawWithTexture();
3472 setupDrawColor(alpha, alpha, alpha, alpha);
3473 setupDrawColorFilter();
3474 setupDrawBlending(blend, mode, swapSrcDst);
3475 setupDrawProgram();
3476 if (!dirty) setupDrawDirtyRegionsDisabled();
3477 if (!ignoreScale) {
3478 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3479 } else {
3480 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3481 }
3482 setupDrawTexture(texture);
3483 setupDrawPureColorUniforms();
3484 setupDrawColorFilterUniforms();
3485 setupDrawMeshIndices(vertices, texCoords, vbo);
3486
3487 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3488
3489 finishDrawTexture();
3490}
3491
Romain Guy886b2752013-01-04 12:26:18 -08003492void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3493 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3494 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003495 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003496
3497 setupDraw();
3498 setupDrawWithTexture(true);
3499 if (hasColor) {
3500 setupDrawAlpha8Color(color, alpha);
3501 }
3502 setupDrawColorFilter();
3503 setupDrawShader();
3504 setupDrawBlending(true, mode);
3505 setupDrawProgram();
3506 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003507 if (!ignoreScale) {
3508 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3509 } else {
3510 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3511 }
Romain Guy886b2752013-01-04 12:26:18 -08003512 setupDrawTexture(texture);
3513 setupDrawPureColorUniforms();
3514 setupDrawColorFilterUniforms();
3515 setupDrawShaderUniforms();
3516 setupDrawMesh(vertices, texCoords);
3517
3518 glDrawArrays(drawMode, 0, elementsCount);
3519
3520 finishDrawTexture();
3521}
3522
Romain Guya5aed0d2010-09-09 14:42:43 -07003523void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003524 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003525 if (mCountOverdraw) {
3526 if (!mCaches.blend) glEnable(GL_BLEND);
3527 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3528 glBlendFunc(GL_ONE, GL_ONE);
3529 }
3530
3531 mCaches.blend = true;
3532 mCaches.lastSrcMode = GL_ONE;
3533 mCaches.lastDstMode = GL_ONE;
3534
3535 return;
3536 }
3537
Romain Guy82ba8142010-07-09 13:25:56 -07003538 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003539
Romain Guy82ba8142010-07-09 13:25:56 -07003540 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003541 // These blend modes are not supported by OpenGL directly and have
3542 // to be implemented using shaders. Since the shader will perform
3543 // the blending, turn blending off here
3544 // If the blend mode cannot be implemented using shaders, fall
3545 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003546 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003547 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003548 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003549 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003550
Romain Guy82bc7a72012-01-03 14:13:39 -08003551 if (mCaches.blend) {
3552 glDisable(GL_BLEND);
3553 mCaches.blend = false;
3554 }
3555
3556 return;
3557 } else {
3558 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003559 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003560 }
3561
3562 if (!mCaches.blend) {
3563 glEnable(GL_BLEND);
3564 }
3565
3566 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3567 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3568
3569 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3570 glBlendFunc(sourceMode, destMode);
3571 mCaches.lastSrcMode = sourceMode;
3572 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003573 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003574 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003575 glDisable(GL_BLEND);
3576 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003577 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003578}
3579
Romain Guy889f8d12010-07-29 14:37:42 -07003580bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003581 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003582 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003583 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003584 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003585 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003586 }
Romain Guy6926c722010-07-12 20:20:03 -07003587 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003588}
3589
Romain Guy026c5e162010-06-28 17:12:22 -07003590void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003591 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003592 TextureVertex::setUV(v++, u1, v1);
3593 TextureVertex::setUV(v++, u2, v1);
3594 TextureVertex::setUV(v++, u1, v2);
3595 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003596}
3597
Chris Craik16ecda52013-03-29 10:59:59 -07003598void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003599 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003600 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3601 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003602 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003603 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003604 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003605}
3606
Chris Craik16ecda52013-03-29 10:59:59 -07003607float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3608 float alpha;
3609 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3610 alpha = mDrawModifiers.mOverrideLayerAlpha;
3611 } else {
3612 alpha = layer->getAlpha() / 255.0f;
3613 }
3614 return alpha * mSnapshot->alpha;
3615}
3616
Romain Guy9d5316e2010-06-24 19:30:36 -07003617}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003618}; // namespace android