blob: 1138998432a6eff15aab5d0143cad1d37364cfc8 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy03d58522012-02-24 17:54:07 -080024#include <SkPathMeasure.h>
Romain Guy694b5192010-07-21 21:33:20 -070025#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070026
Romain Guye4d01122010-06-16 18:44:05 -070027#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070028#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070029
Romain Guy08aa2cb2011-03-17 11:06:57 -070030#include <private/hwui/DrawGlInfo.h>
31
Romain Guy5b3b3522010-10-27 18:57:51 -070032#include <ui/Rect.h>
33
Romain Guy85bf02f2010-06-22 13:11:24 -070034#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080035#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080036#include "DisplayListRenderer.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
110// Constructors/destructor
111///////////////////////////////////////////////////////////////////////////////
112
Romain Guy3bbacf22013-02-06 16:51:04 -0800113OpenGLRenderer::OpenGLRenderer():
114 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700115 mDrawModifiers.mShader = NULL;
116 mDrawModifiers.mColorFilter = NULL;
Chris Craik16ecda52013-03-29 10:59:59 -0700117 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Chris Craikd90144d2013-03-19 15:03:48 -0700118 mDrawModifiers.mHasShadow = false;
119 mDrawModifiers.mHasDrawFilter = false;
Romain Guy026c5e162010-06-28 17:12:22 -0700120
Romain Guyac670c02010-07-27 17:39:27 -0700121 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
122
Romain Guyae5575b2010-07-29 18:48:04 -0700123 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700124 mFrameStarted = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700125
126 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700127}
128
Romain Guy85bf02f2010-06-22 13:11:24 -0700129OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700130 // The context has already been destroyed at this point, do not call
131 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700132}
133
Romain Guy87e2f7572012-09-24 11:37:12 -0700134void OpenGLRenderer::initProperties() {
135 char property[PROPERTY_VALUE_MAX];
136 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
137 mScissorOptimizationDisabled = !strcasecmp(property, "true");
138 INIT_LOGD(" Scissor optimization %s",
139 mScissorOptimizationDisabled ? "disabled" : "enabled");
140 } else {
141 INIT_LOGD(" Scissor optimization enabled");
142 }
Romain Guy13631f32012-01-30 17:41:55 -0800143}
144
145///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700146// Setup
147///////////////////////////////////////////////////////////////////////////////
148
Romain Guyef359272013-01-31 19:07:29 -0800149void OpenGLRenderer::setName(const char* name) {
150 if (name) {
151 mName.setTo(name);
152 } else {
153 mName.clear();
154 }
155}
156
157const char* OpenGLRenderer::getName() const {
158 return mName.string();
159}
160
Romain Guy49c5fc02012-05-15 11:10:01 -0700161bool OpenGLRenderer::isDeferred() {
162 return false;
163}
164
Romain Guy85bf02f2010-06-22 13:11:24 -0700165void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700166 initViewport(width, height);
167
168 glDisable(GL_DITHER);
169 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
170
171 glEnableVertexAttribArray(Program::kBindingPosition);
172}
173
174void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700175 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700176
177 mWidth = width;
178 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700179
180 mFirstSnapshot->height = height;
181 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700182}
183
Romain Guy96885eb2013-03-26 15:05:58 -0700184void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800185 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800186 mCaches.clearGarbage();
187
Romain Guy96885eb2013-03-26 15:05:58 -0700188 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700189 mSnapshot = new Snapshot(mFirstSnapshot,
190 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800191 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700192 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700193
Romain Guy7d7b5492011-01-24 16:33:45 -0800194 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700195 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700196}
197
198status_t OpenGLRenderer::startFrame() {
199 if (mFrameStarted) return DrawGlInfo::kStatusDone;
200 mFrameStarted = true;
201
Romain Guy41308e22012-10-22 20:02:43 -0700202 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700203
Romain Guy96885eb2013-03-26 15:05:58 -0700204 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700205
Romain Guy96885eb2013-03-26 15:05:58 -0700206 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800207
Romain Guy54c1a642012-09-27 17:55:46 -0700208 // Functors break the tiling extension in pretty spectacular ways
209 // This ensures we don't use tiling when a functor is going to be
210 // invoked during the frame
211 mSuppressTiling = mCaches.hasRegisteredFunctors();
212
Chris Craik5f803622013-03-21 14:39:04 -0700213 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700214
Romain Guy7c450aa2012-09-21 19:15:00 -0700215 debugOverdraw(true, true);
216
Romain Guy96885eb2013-03-26 15:05:58 -0700217 return clear(mTilingClip.left, mTilingClip.top,
218 mTilingClip.right, mTilingClip.bottom, mOpaque);
219}
220
221status_t OpenGLRenderer::prepare(bool opaque) {
222 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
223}
224
225status_t OpenGLRenderer::prepareDirty(float left, float top,
226 float right, float bottom, bool opaque) {
227 setupFrameState(left, top, right, bottom, opaque);
228
229 // Layer renderers will start the frame immediately
230 // The framebuffer renderer will first defer the display list
231 // for each layer and wait until the first drawing command
232 // to start the frame
233 if (mSnapshot->fbo == 0) {
234 syncState();
235 updateLayers();
236 } else {
237 return startFrame();
238 }
239
240 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700241}
242
Romain Guydcfc8362013-01-03 13:08:57 -0800243void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
244 // If we know that we are going to redraw the entire framebuffer,
245 // perform a discard to let the driver know we don't need to preserve
246 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800247 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800248 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800249 const bool isFbo = getTargetFbo() == 0;
250 const GLenum attachments[] = {
251 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
252 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800253 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
254 }
255}
256
Romain Guy7c25aab2012-10-18 15:05:02 -0700257status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy6b7bd242010-10-06 19:49:23 -0700258 if (!opaque) {
Romain Guy586cae32012-07-13 15:28:31 -0700259 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700260 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700262 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700263 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700264
Romain Guy7c25aab2012-10-18 15:05:02 -0700265 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700266 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700267}
268
269void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700270 if (mCaches.blend) {
271 glEnable(GL_BLEND);
272 } else {
273 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700274 }
Romain Guybb9524b2010-06-22 18:56:38 -0700275}
276
Romain Guy57b52682012-09-20 17:38:46 -0700277void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700278 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700279 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800280 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700281 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700282 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700283
Romain Guyc3fedaf2013-01-29 17:26:25 -0800284 startTiling(*clip, s->height, opaque);
285 }
286}
287
288void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
289 if (!mSuppressTiling) {
290 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800291 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700292 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700293}
294
295void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700296 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700297}
298
Romain Guyb025b9c2010-09-16 14:16:48 -0700299void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700300 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700301 endTiling();
302
Romain Guyca89e2a2013-03-08 17:44:20 -0800303 // When finish() is invoked on FBO 0 we've reached the end
304 // of the current frame
305 if (getTargetFbo() == 0) {
306 mCaches.pathCache.trim();
307 }
308
Romain Guy11cb6422012-09-21 00:39:43 -0700309 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700310#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700311 GLenum status = GL_NO_ERROR;
312 while ((status = glGetError()) != GL_NO_ERROR) {
313 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
314 switch (status) {
315 case GL_INVALID_ENUM:
316 ALOGE(" GL_INVALID_ENUM");
317 break;
318 case GL_INVALID_VALUE:
319 ALOGE(" GL_INVALID_VALUE");
320 break;
321 case GL_INVALID_OPERATION:
322 ALOGE(" GL_INVALID_OPERATION");
323 break;
324 case GL_OUT_OF_MEMORY:
325 ALOGE(" Out of memory!");
326 break;
327 }
Romain Guya07105b2011-01-10 21:14:18 -0800328 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700329#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700330
Romain Guyc15008e2010-11-10 11:59:15 -0800331#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800332 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700333#else
334 if (mCaches.getDebugLevel() & kDebugMemory) {
335 mCaches.dumpMemoryUsage();
336 }
Romain Guyc15008e2010-11-10 11:59:15 -0800337#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700338 }
Romain Guy96885eb2013-03-26 15:05:58 -0700339
340 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700341}
342
Romain Guy6c319ca2011-01-11 14:29:25 -0800343void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700344 if (mCaches.currentProgram) {
345 if (mCaches.currentProgram->isInUse()) {
346 mCaches.currentProgram->remove();
347 mCaches.currentProgram = NULL;
348 }
349 }
Romain Guy50c0f092010-10-19 11:42:22 -0700350 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800351 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800352 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800353 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700354 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700355}
356
Romain Guy6c319ca2011-01-11 14:29:25 -0800357void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800358 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800359 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700360 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700361 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700362
Romain Guy3e263fa2011-12-12 16:47:48 -0800363 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
364
Chet Haase80250612012-08-15 13:46:54 -0700365 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700366 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800367 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700368 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700369
Romain Guya1d3c912011-12-13 14:55:06 -0800370 mCaches.activeTexture(0);
Romain Guyf607bdc2010-09-10 19:20:06 -0700371
Romain Guy50c0f092010-10-19 11:42:22 -0700372 mCaches.blend = true;
373 glEnable(GL_BLEND);
374 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
375 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700376}
377
Romain Guy35643dd2012-09-18 15:40:58 -0700378void OpenGLRenderer::resumeAfterLayer() {
379 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
380 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
381 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700382 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700383
384 mCaches.resetScissor();
385 dirtyClip();
386}
387
Romain Guyba6be8a2012-04-23 18:22:09 -0700388void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700389 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700390}
391
392void OpenGLRenderer::attachFunctor(Functor* functor) {
393 mFunctors.add(functor);
394}
395
Romain Guy8f3b8e32012-03-27 16:33:45 -0700396status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
397 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700398 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700399
Romain Guyba6be8a2012-04-23 18:22:09 -0700400 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800401 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700402 SortedVector<Functor*> functors(mFunctors);
403 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700404
Romain Guyba6be8a2012-04-23 18:22:09 -0700405 DrawGlInfo info;
406 info.clipLeft = 0;
407 info.clipTop = 0;
408 info.clipRight = 0;
409 info.clipBottom = 0;
410 info.isLayer = false;
411 info.width = 0;
412 info.height = 0;
413 memset(info.transform, 0, sizeof(float) * 16);
414
415 for (size_t i = 0; i < count; i++) {
416 Functor* f = functors.itemAt(i);
417 result |= (*f)(DrawGlInfo::kModeProcess, &info);
418
Chris Craikc2c95432012-04-25 15:13:52 -0700419 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
421 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700422 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700423
Chris Craikc2c95432012-04-25 15:13:52 -0700424 if (result & DrawGlInfo::kStatusInvoke) {
425 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700426 }
427 }
Chris Craikd15321b2012-11-28 14:45:04 -0800428 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700429 }
430
431 return result;
432}
433
434status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700435 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
436
Chet Haasedaf98e92011-01-10 14:10:36 -0800437 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700438 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700439
Romain Guy8a4ac612012-07-17 17:32:48 -0700440 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800441 if (mDirtyClip) {
442 setScissorFromClip();
443 }
Romain Guyd643bb52011-03-01 14:55:21 -0800444
Romain Guy80911b82011-03-16 15:30:12 -0700445 Rect clip(*mSnapshot->clipRect);
446 clip.snapToPixelBoundaries();
447
Romain Guyd643bb52011-03-01 14:55:21 -0800448 // Since we don't know what the functor will draw, let's dirty
449 // tne entire clip region
450 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800451 dirtyLayerUnchecked(clip, getRegion());
452 }
Romain Guyd643bb52011-03-01 14:55:21 -0800453
Romain Guy08aa2cb2011-03-17 11:06:57 -0700454 DrawGlInfo info;
455 info.clipLeft = clip.left;
456 info.clipTop = clip.top;
457 info.clipRight = clip.right;
458 info.clipBottom = clip.bottom;
459 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700460 info.width = getSnapshot()->viewport.getWidth();
461 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700462 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700463
Chet Haase48659092012-05-31 15:21:51 -0700464 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info) | DrawGlInfo::kStatusDrew;
Romain Guycabfcc12011-03-07 18:06:46 -0800465
Romain Guy8f3b8e32012-03-27 16:33:45 -0700466 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700467 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800468 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700469
Chris Craik65924a32012-04-05 17:52:11 -0700470 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700471 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700472 }
Romain Guycabfcc12011-03-07 18:06:46 -0800473 }
474
Chet Haasedaf98e92011-01-10 14:10:36 -0800475 resume();
Romain Guy65549432012-03-26 16:45:05 -0700476 return result;
Chet Haasedaf98e92011-01-10 14:10:36 -0800477}
478
Romain Guyf6a11b82010-06-23 17:47:49 -0700479///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700480// Debug
481///////////////////////////////////////////////////////////////////////////////
482
Romain Guy0f667532013-03-01 14:31:04 -0800483void OpenGLRenderer::eventMark(const char* name) const {
484 mCaches.eventMark(0, name);
485}
486
Romain Guy87e2f7572012-09-24 11:37:12 -0700487void OpenGLRenderer::startMark(const char* name) const {
488 mCaches.startMark(0, name);
489}
490
491void OpenGLRenderer::endMark() const {
492 mCaches.endMark();
493}
494
495void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
496 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
497 if (clear) {
498 mCaches.disableScissor();
499 mCaches.stencil.clear();
500 }
501 if (enable) {
502 mCaches.stencil.enableDebugWrite();
503 } else {
504 mCaches.stencil.disable();
505 }
506 }
507}
508
509void OpenGLRenderer::renderOverdraw() {
510 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700511 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700512
513 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700514 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700515 clip->right - clip->left, clip->bottom - clip->top);
516
517 mCaches.stencil.enableDebugTest(2);
518 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
519 mCaches.stencil.enableDebugTest(3);
520 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
521 mCaches.stencil.enableDebugTest(4);
522 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
523 mCaches.stencil.enableDebugTest(4, true);
524 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
525 mCaches.stencil.disable();
526 }
527}
528
529///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700530// Layers
531///////////////////////////////////////////////////////////////////////////////
532
533bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700534 if (layer->deferredUpdateScheduled && layer->renderer &&
535 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy11cb6422012-09-21 00:39:43 -0700536 Rect& dirty = layer->dirtyRect;
537
Romain Guy7c450aa2012-09-21 19:15:00 -0700538 if (inFrame) {
539 endTiling();
540 debugOverdraw(false, false);
541 }
Romain Guy11cb6422012-09-21 00:39:43 -0700542
Romain Guy96885eb2013-03-26 15:05:58 -0700543 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700544 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700545 } else {
546 layer->defer();
547 }
Romain Guy11cb6422012-09-21 00:39:43 -0700548
549 if (inFrame) {
550 resumeAfterLayer();
551 startTiling(mSnapshot);
552 }
553
Romain Guy5bb3c732012-11-29 17:52:58 -0800554 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Romain Guy11cb6422012-09-21 00:39:43 -0700555
556 return true;
557 }
558
559 return false;
560}
561
562void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700563 // If draw deferring is enabled this method will simply defer
564 // the display list of each individual layer. The layers remain
565 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700566 int count = mLayerUpdates.size();
567 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700568 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
569 startMark("Layer Updates");
570 } else {
571 startMark("Defer Layer Updates");
572 }
Romain Guy11cb6422012-09-21 00:39:43 -0700573
574 // Note: it is very important to update the layers in reverse order
575 for (int i = count - 1; i >= 0; i--) {
576 Layer* layer = mLayerUpdates.itemAt(i);
577 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
579 mCaches.resourceCache.decrementRefcount(layer);
580 }
Romain Guy11cb6422012-09-21 00:39:43 -0700581 }
Romain Guy11cb6422012-09-21 00:39:43 -0700582
Romain Guy96885eb2013-03-26 15:05:58 -0700583 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
584 mLayerUpdates.clear();
585 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
586 }
587 endMark();
588 }
589}
590
591void OpenGLRenderer::flushLayers() {
592 int count = mLayerUpdates.size();
593 if (count > 0) {
594 startMark("Apply Layer Updates");
595 char layerName[12];
596
597 // Note: it is very important to update the layers in reverse order
598 for (int i = count - 1; i >= 0; i--) {
599 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700600 startMark(layerName);
601
602 Layer* layer = mLayerUpdates.itemAt(i);
603 layer->flush();
604 mCaches.resourceCache.decrementRefcount(layer);
605
Romain Guy96885eb2013-03-26 15:05:58 -0700606 endMark();
607 }
608
609 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700610 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700611
Romain Guy11cb6422012-09-21 00:39:43 -0700612 endMark();
613 }
614}
615
616void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
617 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700618 // Make sure we don't introduce duplicates.
619 // SortedVector would do this automatically but we need to respect
620 // the insertion order. The linear search is not an issue since
621 // this list is usually very short (typically one item, at most a few)
622 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
623 if (mLayerUpdates.itemAt(i) == layer) {
624 return;
625 }
626 }
Romain Guy11cb6422012-09-21 00:39:43 -0700627 mLayerUpdates.push_back(layer);
628 mCaches.resourceCache.incrementRefcount(layer);
629 }
630}
631
632void OpenGLRenderer::clearLayerUpdates() {
633 size_t count = mLayerUpdates.size();
634 if (count > 0) {
635 mCaches.resourceCache.lock();
636 for (size_t i = 0; i < count; i++) {
637 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
638 }
639 mCaches.resourceCache.unlock();
640 mLayerUpdates.clear();
641 }
642}
643
644///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700645// State management
646///////////////////////////////////////////////////////////////////////////////
647
Romain Guybb9524b2010-06-22 18:56:38 -0700648int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700649 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700650}
651
652int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700653 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700654}
655
656void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700657 if (mSaveCount > 1) {
658 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700659 }
Romain Guybb9524b2010-06-22 18:56:38 -0700660}
661
662void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700663 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700664
Romain Guy8fb95422010-08-17 18:38:51 -0700665 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700666 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700667 }
Romain Guybb9524b2010-06-22 18:56:38 -0700668}
669
Romain Guy8aef54f2010-09-01 15:13:49 -0700670int OpenGLRenderer::saveSnapshot(int flags) {
671 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700672 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700673}
674
675bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700676 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700677 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700678 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700679
Romain Guybd6b79b2010-06-26 00:13:53 -0700680 sp<Snapshot> current = mSnapshot;
681 sp<Snapshot> previous = mSnapshot->previous;
682
Romain Guyeb993562010-10-05 18:14:38 -0700683 if (restoreOrtho) {
684 Rect& r = previous->viewport;
685 glViewport(r.left, r.top, r.right, r.bottom);
686 mOrthoMatrix.load(current->orthoMatrix);
687 }
688
Romain Guy8b55f372010-08-18 17:10:07 -0700689 mSaveCount--;
690 mSnapshot = previous;
691
Romain Guy2542d192010-08-18 11:47:12 -0700692 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700693 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700694 }
Romain Guy2542d192010-08-18 11:47:12 -0700695
Romain Guy5ec99242010-11-03 16:19:08 -0700696 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700697 endMark(); // Savelayer
698 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700699 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700700 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700701 }
702
Romain Guy2542d192010-08-18 11:47:12 -0700703 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700704}
705
Romain Guyf6a11b82010-06-23 17:47:49 -0700706///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700707// Layers
708///////////////////////////////////////////////////////////////////////////////
709
710int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800711 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700712 const GLuint previousFbo = mSnapshot->fbo;
713 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700714
Romain Guyaf636eb2010-12-09 17:47:21 -0800715 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700716 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700717 }
Romain Guyd55a8612010-06-28 17:42:46 -0700718
719 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700720}
721
Chris Craikd90144d2013-03-19 15:03:48 -0700722void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
723 const Rect untransformedBounds(bounds);
724
725 currentTransform().mapRect(bounds);
726
727 // Layers only make sense if they are in the framebuffer's bounds
728 if (bounds.intersect(*mSnapshot->clipRect)) {
729 // We cannot work with sub-pixels in this case
730 bounds.snapToPixelBoundaries();
731
732 // When the layer is not an FBO, we may use glCopyTexImage so we
733 // need to make sure the layer does not extend outside the bounds
734 // of the framebuffer
735 if (!bounds.intersect(mSnapshot->previous->viewport)) {
736 bounds.setEmpty();
737 } else if (fboLayer) {
738 clip.set(bounds);
739 mat4 inverse;
740 inverse.loadInverse(currentTransform());
741 inverse.mapRect(clip);
742 clip.snapToPixelBoundaries();
743 if (clip.intersect(untransformedBounds)) {
744 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
745 bounds.set(untransformedBounds);
746 } else {
747 clip.setEmpty();
748 }
749 }
750 } else {
751 bounds.setEmpty();
752 }
753}
754
Chris Craik408eb122013-03-26 18:55:15 -0700755void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
756 bool fboLayer, int alpha) {
757 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
758 bounds.getHeight() > mCaches.maxTextureSize ||
759 (fboLayer && clip.isEmpty())) {
760 mSnapshot->empty = fboLayer;
761 } else {
762 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
763 }
764}
765
Chris Craikd90144d2013-03-19 15:03:48 -0700766int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
767 int alpha, SkXfermode::Mode mode, int flags) {
768 const GLuint previousFbo = mSnapshot->fbo;
769 const int count = saveSnapshot(flags);
770
771 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
772 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
773 // operations will be able to store and restore the current clip and transform info, and
774 // quick rejection will be correct (for display lists)
775
776 Rect bounds(left, top, right, bottom);
777 Rect clip;
778 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700779 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700780
Chris Craik408eb122013-03-26 18:55:15 -0700781 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700782 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
783 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
784 }
785 }
786
787 return count;
788}
789
790
Romain Guy1c740bc2010-09-13 18:00:09 -0700791/**
792 * Layers are viewed by Skia are slightly different than layers in image editing
793 * programs (for instance.) When a layer is created, previously created layers
794 * and the frame buffer still receive every drawing command. For instance, if a
795 * layer is created and a shape intersecting the bounds of the layers and the
796 * framebuffer is draw, the shape will be drawn on both (unless the layer was
797 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
798 *
799 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
800 * texture. Unfortunately, this is inefficient as it requires every primitive to
801 * be drawn n + 1 times, where n is the number of active layers. In practice this
802 * means, for every primitive:
803 * - Switch active frame buffer
804 * - Change viewport, clip and projection matrix
805 * - Issue the drawing
806 *
807 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700808 * To avoid this, layers are implemented in a different way here, at least in the
809 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
810 * is set. When this flag is set we can redirect all drawing operations into a
811 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700812 *
813 * This implementation relies on the frame buffer being at least RGBA 8888. When
814 * a layer is created, only a texture is created, not an FBO. The content of the
815 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700816 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700817 * buffer and drawing continues as normal. This technique therefore treats the
818 * frame buffer as a scratch buffer for the layers.
819 *
820 * To compose the layers back onto the frame buffer, each layer texture
821 * (containing the original frame buffer data) is drawn as a simple quad over
822 * the frame buffer. The trick is that the quad is set as the composition
823 * destination in the blending equation, and the frame buffer becomes the source
824 * of the composition.
825 *
826 * Drawing layers with an alpha value requires an extra step before composition.
827 * An empty quad is drawn over the layer's region in the frame buffer. This quad
828 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
829 * quad is used to multiply the colors in the frame buffer. This is achieved by
830 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
831 * GL_ZERO, GL_SRC_ALPHA.
832 *
833 * Because glCopyTexImage2D() can be slow, an alternative implementation might
834 * be use to draw a single clipped layer. The implementation described above
835 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700836 *
837 * (1) The frame buffer is actually not cleared right away. To allow the GPU
838 * to potentially optimize series of calls to glCopyTexImage2D, the frame
839 * buffer is left untouched until the first drawing operation. Only when
840 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700841 */
Chet Haased48885a2012-08-28 17:43:28 -0700842bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
843 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700844 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700845 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700846
Romain Guyeb993562010-10-05 18:14:38 -0700847 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
848
Romain Guyf607bdc2010-09-10 19:20:06 -0700849 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700850 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700851 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700852 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700853 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700854
855 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700856 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700857 return false;
858 }
Romain Guyf18fd992010-07-08 11:45:51 -0700859
Romain Guya1d3c912011-12-13 14:55:06 -0800860 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700861 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700862 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700863 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700864 }
865
Romain Guy9ace8f52011-07-07 20:50:11 -0700866 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700867 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700868 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
869 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800870 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700871 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700872 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700873
Romain Guy8fb95422010-08-17 18:38:51 -0700874 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700875 mSnapshot->flags |= Snapshot::kFlagIsLayer;
876 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700877
Chris Craik7273daa2013-03-28 11:25:24 -0700878 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700879 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700880 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700881 } else {
882 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700883 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800884 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700885 if (layer->isEmpty()) {
886 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
Chet Haased48885a2012-08-28 17:43:28 -0700887 bounds.left, mSnapshot->height - bounds.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 layer->getWidth(), layer->getHeight(), 0);
889 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800890 } else {
891 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
Chet Haased48885a2012-08-28 17:43:28 -0700892 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
Romain Guy514fb182011-01-19 14:38:29 -0800893 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700894
Romain Guy54be1cd2011-06-13 19:04:27 -0700895 // Enqueue the buffer coordinates to clear the corresponding region later
896 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700897 }
Romain Guyeb993562010-10-05 18:14:38 -0700898 }
Romain Guyf86ef572010-07-01 11:05:42 -0700899
Romain Guyd55a8612010-06-28 17:42:46 -0700900 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700901}
902
Chet Haased48885a2012-08-28 17:43:28 -0700903bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800904 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700906
Chet Haased48885a2012-08-28 17:43:28 -0700907 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800908 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
909 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700910 mSnapshot->fbo = layer->getFbo();
911 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
912 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
913 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
914 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700915 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700916
Romain Guy2b7028e2012-09-19 17:25:38 -0700917 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700918 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700919 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700920 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
921 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700922
923 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 if (layer->isEmpty()) {
925 layer->allocateTexture(GL_RGBA, GL_UNSIGNED_BYTE);
926 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700927 }
928
929 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700930 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700931
Romain Guyf735c8e2013-01-31 17:45:55 -0800932 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700933
934 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700935 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800936 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700937 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700938 glClear(GL_COLOR_BUFFER_BIT);
939
940 dirtyClip();
941
942 // Change the ortho projection
943 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
944 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
945
946 return true;
947}
948
Romain Guy1c740bc2010-09-13 18:00:09 -0700949/**
950 * Read the documentation of createLayer() before doing anything in this method.
951 */
Romain Guy1d83e192010-08-17 11:37:00 -0700952void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
953 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +0000954 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700955 return;
956 }
957
Romain Guy8ce00302013-01-15 18:51:42 -0800958 Layer* layer = current->layer;
959 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -0700960 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700961
962 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700963 endTiling();
964
Romain Guye0aa84b2012-04-03 19:30:26 -0700965 // Detach the texture from the FBO
966 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800967
968 layer->removeFbo(false);
969
Romain Guyeb993562010-10-05 18:14:38 -0700970 // Unbind current FBO and restore previous one
971 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700972 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700973
974 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -0700975 }
976
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -0700978 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700982 }
983
Romain Guy03750a02010-10-18 14:06:08 -0700984 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700985
Romain Guya1d3c912011-12-13 14:55:06 -0800986 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700987
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 // When the layer is stored in an FBO, we can save a bit of fillrate by
989 // drawing only the dirty region
990 if (fboLayer) {
991 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 if (layer->getColorFilter()) {
993 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -0800994 }
Romain Guy5b3b3522010-10-27 18:57:51 -0700995 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -0800997 resetColorFilter();
998 }
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 } else if (!rect.isEmpty()) {
1000 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1001 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002 }
Romain Guy8b55f372010-08-18 17:10:07 -07001003
Romain Guy746b7402010-10-26 16:27:31 -07001004 dirtyClip();
1005
Romain Guyeb993562010-10-05 18:14:38 -07001006 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001007 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001008 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001009 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001010 }
1011}
1012
Romain Guyaa6c24c2011-04-28 18:40:04 -07001013void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Chris Craike83569c2013-03-20 16:57:09 -07001014 float alpha = layer->getAlpha() / 255.0f * mSnapshot->alpha;
Romain Guyaa6c24c2011-04-28 18:40:04 -07001015
1016 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001018 setupDrawWithTexture();
1019 } else {
1020 setupDrawWithExternalTexture();
1021 }
1022 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001023 setupDrawColor(alpha, alpha, alpha, alpha);
1024 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001025 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001026 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001027 setupDrawPureColorUniforms();
1028 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001029 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1030 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001031 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001032 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001033 }
Romain Guy3b753822013-03-05 10:27:35 -08001034 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001035 layer->getWidth() == (uint32_t) rect.getWidth() &&
1036 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001037 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1038 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001039
Romain Guyd21b6e12011-11-30 20:21:23 -08001040 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001041 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1042 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001043 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001044 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1045 }
1046 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001047 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1048
1049 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1050
1051 finishDrawTexture();
1052}
1053
Romain Guy5b3b3522010-10-27 18:57:51 -07001054void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001056 const Rect& texCoords = layer->texCoords;
1057 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1058 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001059
Romain Guy9ace8f52011-07-07 20:50:11 -07001060 float x = rect.left;
1061 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001062 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001063 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001064 layer->getHeight() == (uint32_t) rect.getHeight();
1065
1066 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001067 // When we're swapping, the layer is already in screen coordinates
1068 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001069 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1070 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 }
1072
Romain Guyd21b6e12011-11-30 20:21:23 -08001073 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001075 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001076 }
1077
Chris Craik16ecda52013-03-29 10:59:59 -07001078 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001079 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001080 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001081 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1083 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001084
Romain Guyaa6c24c2011-04-28 18:40:04 -07001085 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1086 } else {
1087 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1088 drawTextureLayer(layer, rect);
1089 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1090 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001091}
1092
1093void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001094 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001095 layer->setRegionAsRect();
1096
Romain Guy40667672011-03-18 14:34:03 -07001097 composeLayerRect(layer, layer->regionRect);
Romain Guy9fc27812011-04-27 14:21:41 -07001098
Romain Guy5b3b3522010-10-27 18:57:51 -07001099 layer->region.clear();
1100 return;
1101 }
1102
Romain Guy8a3957d2011-09-07 17:55:15 -07001103 // TODO: See LayerRenderer.cpp::generateMesh() for important
1104 // information about this implementation
Romain Guy211370f2012-02-01 16:10:55 -08001105 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001106 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001107 const android::Rect* rects;
1108 Region safeRegion;
1109 if (CC_LIKELY(hasRectToRectTransform())) {
1110 rects = layer->region.getArray(&count);
1111 } else {
1112 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1113 rects = safeRegion.getArray(&count);
1114 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001115
Chris Craik16ecda52013-03-29 10:59:59 -07001116 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001117 const float texX = 1.0f / float(layer->getWidth());
1118 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001119 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001120
Romain Guy8ce00302013-01-15 18:51:42 -08001121 setupDraw();
1122
1123 // We must get (and therefore bind) the region mesh buffer
1124 // after we setup drawing in case we need to mess with the
1125 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001126 TextureVertex* mesh = mCaches.getRegionMesh();
1127 GLsizei numQuads = 0;
1128
Romain Guy7230a742011-01-10 22:26:16 -08001129 setupDrawWithTexture();
1130 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001131 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001132 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001133 setupDrawProgram();
1134 setupDrawDirtyRegionsDisabled();
1135 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001136 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001137 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001138 if (currentTransform().isPureTranslate()) {
1139 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1140 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001141
Romain Guyd21b6e12011-11-30 20:21:23 -08001142 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001143 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1144 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001145 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001146 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1147 }
Romain Guy15bc6432011-12-13 13:11:32 -08001148 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001149
1150 for (size_t i = 0; i < count; i++) {
1151 const android::Rect* r = &rects[i];
1152
1153 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001154 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001155 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001156 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
1158 // TODO: Reject quads outside of the clip
1159 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1160 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1161 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1162 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1163
1164 numQuads++;
1165
1166 if (numQuads >= REGION_MESH_QUAD_COUNT) {
1167 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1168 numQuads = 0;
1169 mesh = mCaches.getRegionMesh();
1170 }
1171 }
1172
1173 if (numQuads > 0) {
1174 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, NULL);
1175 }
1176
Romain Guy7230a742011-01-10 22:26:16 -08001177 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001178
1179#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001180 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001181#endif
1182
1183 layer->region.clear();
1184 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001185}
1186
Romain Guy3a3133d2011-02-01 22:59:58 -08001187void OpenGLRenderer::drawRegionRects(const Region& region) {
1188#if DEBUG_LAYERS_AS_REGIONS
1189 size_t count;
1190 const android::Rect* rects = region.getArray(&count);
1191
1192 uint32_t colors[] = {
1193 0x7fff0000, 0x7f00ff00,
1194 0x7f0000ff, 0x7fff00ff,
1195 };
1196
1197 int offset = 0;
1198 int32_t top = rects[0].top;
1199
1200 for (size_t i = 0; i < count; i++) {
1201 if (top != rects[i].top) {
1202 offset ^= 0x2;
1203 top = rects[i].top;
1204 }
1205
1206 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1207 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1208 SkXfermode::kSrcOver_Mode);
1209 }
1210#endif
1211}
1212
Romain Guy8ce00302013-01-15 18:51:42 -08001213void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1214 SkXfermode::Mode mode, bool dirty) {
1215 int count = 0;
1216 Vector<float> rects;
1217
1218 SkRegion::Iterator it(region);
1219 while (!it.done()) {
1220 const SkIRect& r = it.rect();
1221 rects.push(r.fLeft);
1222 rects.push(r.fTop);
1223 rects.push(r.fRight);
1224 rects.push(r.fBottom);
Chris Craik2af46352012-11-26 18:30:17 -08001225 count += 4;
Romain Guy8ce00302013-01-15 18:51:42 -08001226 it.next();
1227 }
1228
Romain Guy3bbacf22013-02-06 16:51:04 -08001229 drawColorRects(rects.array(), count, color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001230}
1231
Romain Guy5b3b3522010-10-27 18:57:51 -07001232void OpenGLRenderer::dirtyLayer(const float left, const float top,
1233 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001234 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001235 Rect bounds(left, top, right, bottom);
1236 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001237 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001238 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001239}
1240
1241void OpenGLRenderer::dirtyLayer(const float left, const float top,
1242 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001243 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001244 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001245 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001246 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001247}
1248
1249void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001250 if (bounds.intersect(*mSnapshot->clipRect)) {
1251 bounds.snapToPixelBoundaries();
1252 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1253 if (!dirty.isEmpty()) {
1254 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001255 }
1256 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001257}
1258
Romain Guy54be1cd2011-06-13 19:04:27 -07001259void OpenGLRenderer::clearLayerRegions() {
1260 const size_t count = mLayers.size();
1261 if (count == 0) return;
1262
1263 if (!mSnapshot->isIgnored()) {
1264 // Doing several glScissor/glClear here can negatively impact
1265 // GPUs with a tiler architecture, instead we draw quads with
1266 // the Clear blending mode
1267
1268 // The list contains bounds that have already been clipped
1269 // against their initial clip rect, and the current clip
1270 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001271 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001272
1273 Vertex mesh[count * 6];
1274 Vertex* vertex = mesh;
1275
1276 for (uint32_t i = 0; i < count; i++) {
1277 Rect* bounds = mLayers.itemAt(i);
1278
1279 Vertex::set(vertex++, bounds->left, bounds->bottom);
1280 Vertex::set(vertex++, bounds->left, bounds->top);
1281 Vertex::set(vertex++, bounds->right, bounds->top);
1282 Vertex::set(vertex++, bounds->left, bounds->bottom);
1283 Vertex::set(vertex++, bounds->right, bounds->top);
1284 Vertex::set(vertex++, bounds->right, bounds->bottom);
1285
1286 delete bounds;
1287 }
Romain Guye67307c2013-02-11 18:01:20 -08001288 // We must clear the list of dirty rects before we
1289 // call setupDraw() to prevent stencil setup to do
1290 // the same thing again
1291 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001292
1293 setupDraw(false);
1294 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1295 setupDrawBlending(true, SkXfermode::kClear_Mode);
1296 setupDrawProgram();
1297 setupDrawPureColorUniforms();
1298 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy39d252a2011-12-12 18:14:06 -08001299 setupDrawVertices(&mesh[0].position[0]);
Romain Guy54be1cd2011-06-13 19:04:27 -07001300
Romain Guy54be1cd2011-06-13 19:04:27 -07001301 glDrawArrays(GL_TRIANGLES, 0, count * 6);
Romain Guy8a4ac612012-07-17 17:32:48 -07001302
1303 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001304 } else {
1305 for (uint32_t i = 0; i < count; i++) {
1306 delete mLayers.itemAt(i);
1307 }
Romain Guye67307c2013-02-11 18:01:20 -08001308 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001309 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001310}
1311
Romain Guybd6b79b2010-06-26 00:13:53 -07001312///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001313// State Deferral
1314///////////////////////////////////////////////////////////////////////////////
1315
Chris Craikff785832013-03-08 13:12:16 -08001316bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001317 const Rect& currentClip = *(mSnapshot->clipRect);
1318 const mat4& currentMatrix = *(mSnapshot->transform);
1319
Chris Craikff785832013-03-08 13:12:16 -08001320 if (stateDeferFlags & kStateDeferFlag_Draw) {
1321 // state has bounds initialized in local coordinates
1322 if (!state.mBounds.isEmpty()) {
1323 currentMatrix.mapRect(state.mBounds);
1324 if (!state.mBounds.intersect(currentClip)) {
1325 // quick rejected
1326 return true;
1327 }
1328 } else {
1329 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001330 }
1331 }
1332
Chris Craikff785832013-03-08 13:12:16 -08001333 if (stateDeferFlags & kStateDeferFlag_Clip) {
1334 state.mClip.set(currentClip);
1335 } else {
1336 state.mClip.setEmpty();
1337 }
1338
Chris Craik7273daa2013-03-28 11:25:24 -07001339 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1340 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001341 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001342 state.mDrawModifiers = mDrawModifiers;
1343 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001344 return false;
1345}
1346
Chris Craik7273daa2013-03-28 11:25:24 -07001347void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state) {
Romain Guy3b753822013-03-05 10:27:35 -08001348 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001349 mDrawModifiers = state.mDrawModifiers;
1350 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001351
Chris Craikd90144d2013-03-19 15:03:48 -07001352 if (!state.mClip.isEmpty()) {
Chris Craikff785832013-03-08 13:12:16 -08001353 mSnapshot->setClip(state.mClip.left, state.mClip.top, state.mClip.right, state.mClip.bottom);
1354 dirtyClip();
1355 }
Chris Craikc3566d02013-02-04 16:16:33 -08001356}
1357
1358///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001359// Transforms
1360///////////////////////////////////////////////////////////////////////////////
1361
1362void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy3b753822013-03-05 10:27:35 -08001363 currentTransform().translate(dx, dy, 0.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001364}
1365
1366void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001367 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001368}
1369
1370void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001371 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001372}
1373
Romain Guy807daf72011-01-18 11:19:19 -08001374void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001375 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001376}
1377
Romain Guyf6a11b82010-06-23 17:47:49 -07001378void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001379 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001380 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001381 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001382 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001383 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001384}
1385
Chris Craikb98a0162013-02-21 11:30:22 -08001386bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001387 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001388}
1389
Romain Guyf6a11b82010-06-23 17:47:49 -07001390void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001391 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001392}
1393
1394void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001395 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001396 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001397 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001398 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001399}
1400
1401///////////////////////////////////////////////////////////////////////////////
1402// Clipping
1403///////////////////////////////////////////////////////////////////////////////
1404
Romain Guybb9524b2010-06-22 18:56:38 -07001405void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001406 Rect clip(*mSnapshot->clipRect);
1407 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001408
Romain Guy8a4ac612012-07-17 17:32:48 -07001409 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1410 clip.getWidth(), clip.getHeight())) {
1411 mDirtyClip = false;
1412 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001413}
1414
Romain Guy8ce00302013-01-15 18:51:42 -08001415void OpenGLRenderer::ensureStencilBuffer() {
1416 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1417 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1418 // just hope we have one when hasLayer() returns false.
1419 if (hasLayer()) {
1420 attachStencilBufferToLayer(mSnapshot->layer);
1421 }
1422}
1423
1424void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1425 // The layer's FBO is already bound when we reach this stage
1426 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001427 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1428 // is attached after we initiated tiling. We must turn it off,
1429 // attach the new render buffer then turn tiling back on
1430 endTiling();
1431
Romain Guy8d4aeb72013-02-12 16:08:55 -08001432 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001433 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001434 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001435
Romain Guyf735c8e2013-01-31 17:45:55 -08001436 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001437 }
1438}
1439
1440void OpenGLRenderer::setStencilFromClip() {
1441 if (!mCaches.debugOverdraw) {
1442 if (!mSnapshot->clipRegion->isEmpty()) {
1443 // NOTE: The order here is important, we must set dirtyClip to false
1444 // before any draw call to avoid calling back into this method
1445 mDirtyClip = false;
1446
1447 ensureStencilBuffer();
1448
1449 mCaches.stencil.enableWrite();
1450
1451 // Clear the stencil but first make sure we restrict drawing
1452 // to the region's bounds
1453 bool resetScissor = mCaches.enableScissor();
1454 if (resetScissor) {
1455 // The scissor was not set so we now need to update it
1456 setScissorFromClip();
1457 }
1458 mCaches.stencil.clear();
1459 if (resetScissor) mCaches.disableScissor();
1460
1461 // NOTE: We could use the region contour path to generate a smaller mesh
1462 // Since we are using the stencil we could use the red book path
1463 // drawing technique. It might increase bandwidth usage though.
1464
1465 // The last parameter is important: we are not drawing in the color buffer
1466 // so we don't want to dirty the current layer, if any
1467 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1468
1469 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001470
1471 // Draw the region used to generate the stencil if the appropriate debug
1472 // mode is enabled
1473 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1474 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1475 }
Romain Guy8ce00302013-01-15 18:51:42 -08001476 } else {
1477 mCaches.stencil.disable();
1478 }
1479 }
1480}
1481
Romain Guy9d5316e2010-06-24 19:30:36 -07001482const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001483 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001484}
1485
Romain Guy8a4ac612012-07-17 17:32:48 -07001486bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom) {
1487 if (mSnapshot->isIgnored()) {
1488 return true;
1489 }
1490
1491 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001492 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001493 r.snapToPixelBoundaries();
1494
1495 Rect clipRect(*mSnapshot->clipRect);
1496 clipRect.snapToPixelBoundaries();
1497
1498 return !clipRect.intersects(r);
1499}
1500
Romain Guy35643dd2012-09-18 15:40:58 -07001501bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1502 Rect& transformed, Rect& clip) {
1503 if (mSnapshot->isIgnored()) {
1504 return true;
1505 }
1506
1507 transformed.set(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001508 currentTransform().mapRect(transformed);
Romain Guy35643dd2012-09-18 15:40:58 -07001509 transformed.snapToPixelBoundaries();
1510
1511 clip.set(*mSnapshot->clipRect);
1512 clip.snapToPixelBoundaries();
1513
1514 return !clip.intersects(transformed);
1515}
1516
Romain Guy672433d2013-01-04 19:05:13 -08001517bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1518 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001519 if (paint->getStyle() != SkPaint::kFill_Style) {
1520 float outset = paint->getStrokeWidth() * 0.5f;
1521 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1522 } else {
1523 return quickReject(left, top, right, bottom);
1524 }
1525}
1526
Romain Guyc7d53492010-06-25 13:41:57 -07001527bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001528 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001529 return true;
1530 }
1531
Romain Guy1d83e192010-08-17 11:37:00 -07001532 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001533 currentTransform().mapRect(r);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001534 r.snapToPixelBoundaries();
1535
1536 Rect clipRect(*mSnapshot->clipRect);
1537 clipRect.snapToPixelBoundaries();
1538
Romain Guy586cae32012-07-13 15:28:31 -07001539 bool rejected = !clipRect.intersects(r);
1540 if (!isDeferred() && !rejected) {
Romain Guy87e2f7572012-09-24 11:37:12 -07001541 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clipRect.contains(r));
Romain Guy586cae32012-07-13 15:28:31 -07001542 }
1543
1544 return rejected;
Romain Guyc7d53492010-06-25 13:41:57 -07001545}
1546
Romain Guy8ce00302013-01-15 18:51:42 -08001547void OpenGLRenderer::debugClip() {
1548#if DEBUG_CLIP_REGIONS
1549 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1550 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1551 }
1552#endif
1553}
1554
Romain Guy079ba2c2010-07-16 14:12:24 -07001555bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001556 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001557 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1558 if (clipped) {
1559 dirtyClip();
1560 }
1561 return !mSnapshot->clipRect->isEmpty();
1562 }
1563
1564 SkPath path;
1565 path.addRect(left, top, right, bottom);
1566
1567 return clipPath(&path, op);
1568}
1569
1570bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1571 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001572 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001573
1574 SkPath transformed;
1575 path->transform(transform, &transformed);
1576
1577 SkRegion clip;
1578 if (!mSnapshot->clipRegion->isEmpty()) {
1579 clip.setRegion(*mSnapshot->clipRegion);
1580 } else {
1581 Rect* bounds = mSnapshot->clipRect;
1582 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1583 }
1584
1585 SkRegion region;
1586 region.setPath(transformed, clip);
1587
1588 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001589 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001590 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001591 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001592 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001593}
1594
Romain Guy735738c2012-12-03 12:34:51 -08001595bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001596 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1597 if (clipped) {
1598 dirtyClip();
1599 }
1600 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001601}
1602
Chet Haasea23eed82012-04-12 15:19:04 -07001603Rect* OpenGLRenderer::getClipRect() {
1604 return mSnapshot->clipRect;
1605}
1606
Romain Guyf6a11b82010-06-23 17:47:49 -07001607///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001608// Drawing commands
1609///////////////////////////////////////////////////////////////////////////////
1610
Romain Guy54be1cd2011-06-13 19:04:27 -07001611void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001612 // TODO: It would be best if we could do this before quickReject()
1613 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001614 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001615 // Make sure setScissor & setStencil happen at the beginning of
1616 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001617 if (mDirtyClip) {
1618 if (mCaches.scissorEnabled) {
1619 setScissorFromClip();
1620 }
Romain Guy8ce00302013-01-15 18:51:42 -08001621 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001622 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001623
Romain Guy70ca14e2010-12-13 18:24:33 -08001624 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001625
Romain Guy70ca14e2010-12-13 18:24:33 -08001626 mSetShaderColor = false;
1627 mColorSet = false;
1628 mColorA = mColorR = mColorG = mColorB = 0.0f;
1629 mTextureUnit = 0;
1630 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001631
1632 // Enable debug highlight when what we're about to draw is tested against
1633 // the stencil buffer and if stencil highlight debugging is on
1634 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1635 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1636 mCaches.stencil.isTestEnabled();
Romain Guy70ca14e2010-12-13 18:24:33 -08001637}
1638
1639void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1640 mDescription.hasTexture = true;
1641 mDescription.hasAlpha8Texture = isAlpha8;
1642}
1643
Romain Guyff316ec2013-02-13 18:39:43 -08001644void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1645 mDescription.hasTexture = true;
1646 mDescription.hasColors = true;
1647 mDescription.hasAlpha8Texture = isAlpha8;
1648}
1649
Romain Guyaa6c24c2011-04-28 18:40:04 -07001650void OpenGLRenderer::setupDrawWithExternalTexture() {
1651 mDescription.hasExternalTexture = true;
1652}
1653
Romain Guy15bc6432011-12-13 13:11:32 -08001654void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001655 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001656}
1657
Chris Craik710f46d2012-09-17 17:25:49 -07001658void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001659 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001660}
1661
Romain Guyed6fcb02011-03-21 13:11:28 -07001662void OpenGLRenderer::setupDrawPoint(float pointSize) {
1663 mDescription.isPoint = true;
1664 mDescription.pointSize = pointSize;
1665}
1666
Romain Guy8d0d4782010-12-14 20:13:35 -08001667void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1668 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001669 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1670 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1671 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001672 mColorSet = true;
1673 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1674}
1675
Romain Guy86568192010-12-14 15:55:39 -08001676void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1677 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001678 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1679 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1680 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001681 mColorSet = true;
1682 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1683}
1684
Romain Guy41210632012-07-16 17:04:24 -07001685void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1686 mCaches.fontRenderer->describe(mDescription, paint);
1687}
1688
Romain Guy70ca14e2010-12-13 18:24:33 -08001689void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1690 mColorA = a;
1691 mColorR = r;
1692 mColorG = g;
1693 mColorB = b;
1694 mColorSet = true;
1695 mSetShaderColor = mDescription.setColor(r, g, b, a);
1696}
1697
1698void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001699 if (mDrawModifiers.mShader) {
1700 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001701 }
1702}
1703
1704void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001705 if (mDrawModifiers.mColorFilter) {
1706 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001707 }
1708}
1709
Romain Guyf09ef512011-05-27 11:43:46 -07001710void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1711 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1712 mColorA = 1.0f;
1713 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001714 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001715 }
1716}
1717
Romain Guy70ca14e2010-12-13 18:24:33 -08001718void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001719 // When the blending mode is kClear_Mode, we need to use a modulate color
1720 // argb=1,0,0,0
1721 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001722 bool blend = (mColorSet && mColorA < 1.0f) ||
1723 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1724 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001725}
1726
1727void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001728 // When the blending mode is kClear_Mode, we need to use a modulate color
1729 // argb=1,0,0,0
1730 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001731 blend |= (mColorSet && mColorA < 1.0f) ||
1732 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1733 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1734 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001735}
1736
1737void OpenGLRenderer::setupDrawProgram() {
1738 useProgram(mCaches.programCache.get(mDescription));
1739}
1740
1741void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1742 mTrackDirtyRegions = false;
1743}
1744
1745void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1746 bool ignoreTransform) {
1747 mModelView.loadTranslate(left, top, 0.0f);
1748 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001749 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1750 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001752 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1754 }
1755}
1756
Chet Haase8a5cc922011-04-26 07:28:09 -07001757void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001758 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001759}
1760
Romain Guy70ca14e2010-12-13 18:24:33 -08001761void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1762 bool ignoreTransform, bool ignoreModelView) {
1763 if (!ignoreModelView) {
1764 mModelView.loadTranslate(left, top, 0.0f);
1765 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001766 } else {
1767 mModelView.loadIdentity();
1768 }
Romain Guy86568192010-12-14 15:55:39 -08001769 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1770 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001771 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001772 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001773 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001774 }
1775 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001776 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001777 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1778 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001779}
1780
Romain Guyed6fcb02011-03-21 13:11:28 -07001781void OpenGLRenderer::setupDrawPointUniforms() {
1782 int slot = mCaches.currentProgram->getUniform("pointSize");
1783 glUniform1f(slot, mDescription.pointSize);
1784}
1785
Romain Guy70ca14e2010-12-13 18:24:33 -08001786void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001787 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001788 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1789 }
1790}
1791
Romain Guy86568192010-12-14 15:55:39 -08001792void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001793 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001794 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001795 }
1796}
1797
Romain Guy70ca14e2010-12-13 18:24:33 -08001798void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001799 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001800 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001801 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001802 }
Chris Craikc3566d02013-02-04 16:16:33 -08001803 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1804 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805 }
1806}
1807
Romain Guy8d0d4782010-12-14 20:13:35 -08001808void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001809 if (mDrawModifiers.mShader) {
1810 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001811 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001812 }
1813}
1814
Romain Guy70ca14e2010-12-13 18:24:33 -08001815void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001816 if (mDrawModifiers.mColorFilter) {
1817 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001818 }
1819}
1820
Romain Guy41210632012-07-16 17:04:24 -07001821void OpenGLRenderer::setupDrawTextGammaUniforms() {
1822 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1823}
1824
Romain Guy70ca14e2010-12-13 18:24:33 -08001825void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001826 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001827 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001828 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001829}
1830
1831void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001832 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001833 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001834 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001835}
1836
Romain Guyaa6c24c2011-04-28 18:40:04 -07001837void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1838 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001839 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001840 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001841}
1842
Romain Guy8f0095c2011-05-02 17:24:22 -07001843void OpenGLRenderer::setupDrawTextureTransform() {
1844 mDescription.hasTextureTransform = true;
1845}
1846
1847void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001848 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1849 GL_FALSE, &transform.data[0]);
1850}
1851
Romain Guy70ca14e2010-12-13 18:24:33 -08001852void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001853 bool force = false;
Romain Guy70ca14e2010-12-13 18:24:33 -08001854 if (!vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001855 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001856 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001857 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001858 }
Romain Guyd71dd362011-12-12 19:03:35 -08001859
Chris Craikcb4d6002012-09-25 12:00:29 -07001860 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001861 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001862 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001863 }
1864
1865 mCaches.unbindIndicesBuffer();
1866}
1867
Romain Guyff316ec2013-02-13 18:39:43 -08001868void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1869 bool force = mCaches.unbindMeshBuffer();
1870 GLsizei stride = sizeof(ColorTextureVertex);
1871
1872 mCaches.bindPositionVertexPointer(force, vertices, stride);
1873 if (mCaches.currentProgram->texCoords >= 0) {
1874 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1875 }
1876 int slot = mCaches.currentProgram->getAttrib("colors");
1877 if (slot >= 0) {
1878 glEnableVertexAttribArray(slot);
1879 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1880 }
1881
1882 mCaches.unbindIndicesBuffer();
1883}
1884
Romain Guy15bc6432011-12-13 13:11:32 -08001885void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords) {
1886 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001887 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001888 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001889 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001890 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001891}
1892
Chet Haase5b0200b2011-04-13 17:58:08 -07001893void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001894 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001895 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy15bc6432011-12-13 13:11:32 -08001896 mCaches.unbindIndicesBuffer();
Chet Haase5b0200b2011-04-13 17:58:08 -07001897}
1898
Romain Guy70ca14e2010-12-13 18:24:33 -08001899void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08001900}
1901
1902///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001903// Drawing
1904///////////////////////////////////////////////////////////////////////////////
1905
Chris Craikff785832013-03-08 13:12:16 -08001906status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1907 int32_t replayFlags) {
Romain Guy0fe478e2010-11-08 12:08:41 -08001908 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1909 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001910 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07001911 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Romain Guy96885eb2013-03-26 15:05:58 -07001912 startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001913 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1914 displayList->replay(replayStruct, 0);
1915 return replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001916 }
1917
1918 DeferredDisplayList deferredList;
Chris Craikff785832013-03-08 13:12:16 -08001919 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1920 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001921
1922 flushLayers();
1923 startFrame();
1924
Chris Craikff785832013-03-08 13:12:16 -08001925 return deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08001926 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001927
Romain Guy65549432012-03-26 16:45:05 -07001928 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001929}
1930
Chris Craikc3566d02013-02-04 16:16:33 -08001931void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07001932 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08001933 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07001934 }
1935}
1936
Romain Guya168d732011-03-18 16:50:13 -07001937void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1938 int alpha;
1939 SkXfermode::Mode mode;
1940 getAlphaAndMode(paint, &alpha, &mode);
1941
Romain Guy886b2752013-01-04 12:26:18 -08001942 int color = paint != NULL ? paint->getColor() : 0;
1943
Romain Guya168d732011-03-18 16:50:13 -07001944 float x = left;
1945 float y = top;
1946
Romain Guy886b2752013-01-04 12:26:18 -08001947 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1948
Romain Guya168d732011-03-18 16:50:13 -07001949 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001950 if (currentTransform().isPureTranslate()) {
1951 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1952 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001953 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001954
1955 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001956 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001957 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001958 }
1959
Romain Guy886b2752013-01-04 12:26:18 -08001960 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
1961 paint != NULL, color, alpha, mode, (GLvoid*) NULL,
1962 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001963}
1964
Chet Haase48659092012-05-31 15:21:51 -07001965status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001966 const float right = left + bitmap->width();
1967 const float bottom = top + bitmap->height();
1968
1969 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001970 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001971 }
1972
Romain Guya1d3c912011-12-13 14:55:06 -08001973 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001974 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001975 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001976 const AutoTexture autoCleanup(texture);
1977
Romain Guy211370f2012-02-01 16:10:55 -08001978 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001979 drawAlphaBitmap(texture, left, top, paint);
1980 } else {
1981 drawTextureRect(left, top, right, bottom, texture, paint);
1982 }
Chet Haase48659092012-05-31 15:21:51 -07001983
1984 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001985}
1986
Chet Haase48659092012-05-31 15:21:51 -07001987status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001988 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1989 const mat4 transform(*matrix);
1990 transform.mapRect(r);
1991
Romain Guy6926c722010-07-12 20:20:03 -07001992 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001993 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001994 }
1995
Romain Guya1d3c912011-12-13 14:55:06 -08001996 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07001997 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001998 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001999 const AutoTexture autoCleanup(texture);
2000
Romain Guy5b3b3522010-10-27 18:57:51 -07002001 // This could be done in a cheaper way, all we need is pass the matrix
2002 // to the vertex shader. The save/restore is a bit overkill.
2003 save(SkCanvas::kMatrix_SaveFlag);
2004 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002005 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2006 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2007 } else {
2008 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2009 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002010 restore();
Chet Haase48659092012-05-31 15:21:51 -07002011
2012 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002013}
2014
Chet Haase48659092012-05-31 15:21:51 -07002015status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002016 const float right = left + bitmap->width();
2017 const float bottom = top + bitmap->height();
2018
2019 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002020 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002021 }
2022
2023 mCaches.activeTexture(0);
2024 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2025 const AutoTexture autoCleanup(texture);
2026
Romain Guy886b2752013-01-04 12:26:18 -08002027 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2028 drawAlphaBitmap(texture, left, top, paint);
2029 } else {
2030 drawTextureRect(left, top, right, bottom, texture, paint);
2031 }
Chet Haase48659092012-05-31 15:21:51 -07002032
2033 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002034}
2035
Chet Haase48659092012-05-31 15:21:51 -07002036status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002037 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002038 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002039 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002040 }
2041
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 float left = FLT_MAX;
2043 float top = FLT_MAX;
2044 float right = FLT_MIN;
2045 float bottom = FLT_MIN;
2046
Romain Guya92bb4d2012-10-16 11:08:44 -07002047 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002048
Romain Guyff316ec2013-02-13 18:39:43 -08002049 ColorTextureVertex mesh[count];
2050 ColorTextureVertex* vertex = mesh;
2051
2052 bool cleanupColors = false;
2053 if (!colors) {
2054 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2055 colors = new int[colorsCount];
2056 memset(colors, 0xff, colorsCount * sizeof(int));
2057 cleanupColors = true;
2058 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002059
Romain Guy5a7b4662011-01-20 19:09:30 -08002060 for (int32_t y = 0; y < meshHeight; y++) {
2061 for (int32_t x = 0; x < meshWidth; x++) {
2062 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2063
2064 float u1 = float(x) / meshWidth;
2065 float u2 = float(x + 1) / meshWidth;
2066 float v1 = float(y) / meshHeight;
2067 float v2 = float(y + 1) / meshHeight;
2068
2069 int ax = i + (meshWidth + 1) * 2;
2070 int ay = ax + 1;
2071 int bx = i;
2072 int by = bx + 1;
2073 int cx = i + 2;
2074 int cy = cx + 1;
2075 int dx = i + (meshWidth + 1) * 2 + 2;
2076 int dy = dx + 1;
2077
Romain Guyff316ec2013-02-13 18:39:43 -08002078 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2079 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2080 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002081
Romain Guyff316ec2013-02-13 18:39:43 -08002082 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2083 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2084 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002085
Romain Guya92bb4d2012-10-16 11:08:44 -07002086 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2087 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2088 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2089 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002090 }
2091 }
2092
Romain Guya92bb4d2012-10-16 11:08:44 -07002093 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002094 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002095 return DrawGlInfo::kStatusDone;
2096 }
2097
2098 mCaches.activeTexture(0);
2099 Texture* texture = mCaches.textureCache.get(bitmap);
Romain Guyff316ec2013-02-13 18:39:43 -08002100 if (!texture) {
2101 if (cleanupColors) delete[] colors;
2102 return DrawGlInfo::kStatusDone;
2103 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002104 const AutoTexture autoCleanup(texture);
2105
2106 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2107 texture->setFilter(FILTER(paint), true);
2108
2109 int alpha;
2110 SkXfermode::Mode mode;
2111 getAlphaAndMode(paint, &alpha, &mode);
2112
Romain Guyff316ec2013-02-13 18:39:43 -08002113 float a = alpha / 255.0f;
2114
Romain Guya92bb4d2012-10-16 11:08:44 -07002115 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002116 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002117 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002118
Romain Guyff316ec2013-02-13 18:39:43 -08002119 setupDraw();
2120 setupDrawWithTextureAndColor();
2121 setupDrawColor(a, a, a, a);
2122 setupDrawColorFilter();
2123 setupDrawBlending(true, mode, false);
2124 setupDrawProgram();
2125 setupDrawDirtyRegionsDisabled();
2126 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2127 setupDrawTexture(texture->id);
2128 setupDrawPureColorUniforms();
2129 setupDrawColorFilterUniforms();
2130 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2131
2132 glDrawArrays(GL_TRIANGLES, 0, count);
2133
2134 finishDrawTexture();
2135
2136 int slot = mCaches.currentProgram->getAttrib("colors");
2137 if (slot >= 0) {
2138 glDisableVertexAttribArray(slot);
2139 }
2140
2141 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002142
2143 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002144}
2145
Chet Haase48659092012-05-31 15:21:51 -07002146status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002147 float srcLeft, float srcTop, float srcRight, float srcBottom,
2148 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002149 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002150 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002151 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002152 }
2153
Romain Guya1d3c912011-12-13 14:55:06 -08002154 mCaches.activeTexture(0);
Romain Guy8164c2d2010-10-25 18:03:28 -07002155 Texture* texture = mCaches.textureCache.get(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002156 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002157 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002158
Romain Guy8ba548f2010-06-30 19:21:21 -07002159 const float width = texture->width;
2160 const float height = texture->height;
2161
Romain Guy68169722011-08-22 17:33:33 -07002162 const float u1 = fmax(0.0f, srcLeft / width);
2163 const float v1 = fmax(0.0f, srcTop / height);
2164 const float u2 = fmin(1.0f, srcRight / width);
2165 const float v2 = fmin(1.0f, srcBottom / height);
Romain Guy8ba548f2010-06-30 19:21:21 -07002166
Romain Guy03750a02010-10-18 14:06:08 -07002167 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002168 resetDrawTextureTexCoords(u1, v1, u2, v2);
2169
Romain Guy03750a02010-10-18 14:06:08 -07002170 int alpha;
2171 SkXfermode::Mode mode;
2172 getAlphaAndMode(paint, &alpha, &mode);
2173
Romain Guyd21b6e12011-11-30 20:21:23 -08002174 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2175
Romain Guy886b2752013-01-04 12:26:18 -08002176 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2177 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002178
Romain Guy886b2752013-01-04 12:26:18 -08002179 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2180 // Apply a scale transform on the canvas only when a shader is in use
2181 // Skia handles the ratio between the dst and src rects as a scale factor
2182 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002183 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002184 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002185
Romain Guy3b753822013-03-05 10:27:35 -08002186 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2187 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2188 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002189
2190 dstRight = x + (dstRight - dstLeft);
2191 dstBottom = y + (dstBottom - dstTop);
2192
2193 dstLeft = x;
2194 dstTop = y;
2195
2196 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2197 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002198 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002199 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002200 }
2201
2202 if (CC_UNLIKELY(useScaleTransform)) {
2203 save(SkCanvas::kMatrix_SaveFlag);
2204 translate(dstLeft, dstTop);
2205 scale(scaleX, scaleY);
2206
2207 dstLeft = 0.0f;
2208 dstTop = 0.0f;
2209
2210 dstRight = srcRight - srcLeft;
2211 dstBottom = srcBottom - srcTop;
2212 }
2213
2214 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2215 int color = paint ? paint->getColor() : 0;
2216 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2217 texture->id, paint != NULL, color, alpha, mode,
2218 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2219 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2220 } else {
2221 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2222 texture->id, alpha / 255.0f, mode, texture->blend,
2223 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2224 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2225 }
2226
2227 if (CC_UNLIKELY(useScaleTransform)) {
2228 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002229 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002230
2231 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002232
2233 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002234}
2235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
Romain Guy4bb94202010-10-12 15:59:26 -07002237 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
Chet Haase5c13d892010-10-08 08:37:55 -07002238 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guybe6f9dc2012-07-16 12:41:17 -07002239 int alpha;
2240 SkXfermode::Mode mode;
Chris Craik16ecda52013-03-29 10:59:59 -07002241 getAlphaAndMode(paint, &alpha, &mode);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002242
2243 return drawPatch(bitmap, xDivs, yDivs, colors, width, height, numColors,
2244 left, top, right, bottom, alpha, mode);
2245}
2246
2247status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
2248 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors,
2249 float left, float top, float right, float bottom, int alpha, SkXfermode::Mode mode) {
Romain Guy6926c722010-07-12 20:20:03 -07002250 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002251 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002252 }
2253
Romain Guybe6f9dc2012-07-16 12:41:17 -07002254 alpha *= mSnapshot->alpha;
2255
Romain Guy2728f962010-10-08 18:36:15 -07002256 const Patch* mesh = mCaches.patchCache.get(bitmap->width(), bitmap->height(),
Romain Guy4bb94202010-10-12 15:59:26 -07002257 right - left, bottom - top, xDivs, yDivs, colors, width, height, numColors);
Romain Guyf7f93552010-07-08 19:17:03 -07002258
Romain Guy211370f2012-02-01 16:10:55 -08002259 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002260 mCaches.activeTexture(0);
2261 Texture* texture = mCaches.textureCache.get(bitmap);
2262 if (!texture) return DrawGlInfo::kStatusDone;
2263 const AutoTexture autoCleanup(texture);
2264 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2265 texture->setFilter(GL_LINEAR, true);
2266
Romain Guy3b753822013-03-05 10:27:35 -08002267 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002268 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002269 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002270 const float offsetX = left + currentTransform().getTranslateX();
2271 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002272 const size_t count = mesh->quads.size();
2273 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002274 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002275 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002276 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2277 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2278 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002279 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002280 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002281 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002282 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002283 }
2284 }
2285
Romain Guy211370f2012-02-01 16:10:55 -08002286 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002287 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2288 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002289
2290 drawTextureMesh(x, y, x + right - left, y + bottom - top, texture->id, alpha / 255.0f,
2291 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2292 GL_TRIANGLES, mesh->verticesCount, false, true, mesh->meshBuffer,
2293 true, !mesh->hasEmptyQuads);
2294 } else {
2295 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2296 mode, texture->blend, (GLvoid*) 0, (GLvoid*) gMeshTextureOffset,
2297 GL_TRIANGLES, mesh->verticesCount, false, false, mesh->meshBuffer,
2298 true, !mesh->hasEmptyQuads);
2299 }
Romain Guy054dc182010-10-15 17:55:25 -07002300 }
Chet Haase48659092012-05-31 15:21:51 -07002301
2302 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002303}
2304
Chris Craik65cd6122012-12-10 17:56:27 -08002305status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2306 bool useOffset) {
2307 if (!vertexBuffer.getSize()) {
2308 // no vertices to draw
2309 return DrawGlInfo::kStatusDone;
2310 }
2311
Chris Craikcb4d6002012-09-25 12:00:29 -07002312 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002313 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2314 bool isAA = paint->isAntiAlias();
2315
Chris Craik710f46d2012-09-17 17:25:49 -07002316 setupDraw();
2317 setupDrawNoTexture();
2318 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002319 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2320 setupDrawColorFilter();
2321 setupDrawShader();
2322 setupDrawBlending(isAA, mode);
2323 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002324 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002325 setupDrawColorUniforms();
2326 setupDrawColorFilterUniforms();
2327 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002328
Chris Craik710f46d2012-09-17 17:25:49 -07002329 void* vertices = vertexBuffer.getBuffer();
2330 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002331 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002332 mCaches.resetTexCoordsVertexPointer();
2333 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002334
Chris Craik710f46d2012-09-17 17:25:49 -07002335 int alphaSlot = -1;
2336 if (isAA) {
2337 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2338 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002339
Chris Craik710f46d2012-09-17 17:25:49 -07002340 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002341 glEnableVertexAttribArray(alphaSlot);
2342 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002343 }
Romain Guy04299382012-07-18 17:15:41 -07002344
Chris Craik710f46d2012-09-17 17:25:49 -07002345 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getSize());
Romain Guy04299382012-07-18 17:15:41 -07002346
Chris Craik710f46d2012-09-17 17:25:49 -07002347 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002348 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002349 }
Chris Craik65cd6122012-12-10 17:56:27 -08002350
2351 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002352}
2353
2354/**
Chris Craik65cd6122012-12-10 17:56:27 -08002355 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2356 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2357 * screen space in all directions. However, instead of using a fragment shader to compute the
2358 * translucency of the color from its position, we simply use a varying parameter to define how far
2359 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2360 *
2361 * Doesn't yet support joins, caps, or path effects.
2362 */
2363status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2364 VertexBuffer vertexBuffer;
2365 // TODO: try clipping large paths to viewport
2366 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2367
Romain Guyc46d07a2013-03-15 19:06:39 -07002368 if (hasLayer()) {
2369 SkRect bounds = path.getBounds();
2370 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2371 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2372 }
Chris Craik65cd6122012-12-10 17:56:27 -08002373
2374 return drawVertexBuffer(vertexBuffer, paint);
2375}
2376
2377/**
2378 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2379 * and additional geometry for defining an alpha slope perimeter.
2380 *
2381 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2382 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2383 * in-shader alpha region, but found it to be taxing on some GPUs.
2384 *
2385 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2386 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002387 */
Chet Haase48659092012-05-31 15:21:51 -07002388status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002389 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002390
Chris Craik65cd6122012-12-10 17:56:27 -08002391 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002392
Chris Craik65cd6122012-12-10 17:56:27 -08002393 VertexBuffer buffer;
2394 SkRect bounds;
2395 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002396
2397 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2398 return DrawGlInfo::kStatusDone;
2399 }
2400
Romain Guy3b753822013-03-05 10:27:35 -08002401 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002402
Chris Craik65cd6122012-12-10 17:56:27 -08002403 bool useOffset = !paint->isAntiAlias();
2404 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002405}
2406
Chet Haase48659092012-05-31 15:21:51 -07002407status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
2408 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002409
2410 // TODO: The paint's cap style defines whether the points are square or circular
2411 // TODO: Handle AA for round points
2412
Chet Haase5b0200b2011-04-13 17:58:08 -07002413 // A stroke width of 0 has a special meaning in Skia:
Romain Guyed6fcb02011-03-21 13:11:28 -07002414 // it draws an unscaled 1px point
Chet Haase8a5cc922011-04-26 07:28:09 -07002415 float strokeWidth = paint->getStrokeWidth();
Romain Guyed6fcb02011-03-21 13:11:28 -07002416 const bool isHairLine = paint->getStrokeWidth() == 0.0f;
Chet Haase8a5cc922011-04-26 07:28:09 -07002417 if (isHairLine) {
2418 // Now that we know it's hairline, we can set the effective width, to be used later
2419 strokeWidth = 1.0f;
2420 }
2421 const float halfWidth = strokeWidth / 2;
Romain Guyd71ff91d2013-02-08 13:46:40 -08002422
Romain Guyed6fcb02011-03-21 13:11:28 -07002423 int alpha;
2424 SkXfermode::Mode mode;
2425 getAlphaAndMode(paint, &alpha, &mode);
2426
2427 int verticesCount = count >> 1;
2428 int generatedVerticesCount = 0;
2429
2430 TextureVertex pointsData[verticesCount];
2431 TextureVertex* vertex = &pointsData[0];
2432
Romain Guy04299382012-07-18 17:15:41 -07002433 // TODO: We should optimize this method to not generate vertices for points
2434 // that lie outside of the clip.
2435 mCaches.enableScissor();
2436
Romain Guyed6fcb02011-03-21 13:11:28 -07002437 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08002438 setupDrawNoTexture();
Chet Haase8a5cc922011-04-26 07:28:09 -07002439 setupDrawPoint(strokeWidth);
Romain Guyed6fcb02011-03-21 13:11:28 -07002440 setupDrawColor(paint->getColor(), alpha);
2441 setupDrawColorFilter();
2442 setupDrawShader();
2443 setupDrawBlending(mode);
2444 setupDrawProgram();
Chet Haase8a5cc922011-04-26 07:28:09 -07002445 setupDrawModelViewIdentity(true);
Romain Guyed6fcb02011-03-21 13:11:28 -07002446 setupDrawColorUniforms();
2447 setupDrawColorFilterUniforms();
2448 setupDrawPointUniforms();
2449 setupDrawShaderIdentityUniforms();
2450 setupDrawMesh(vertex);
2451
2452 for (int i = 0; i < count; i += 2) {
2453 TextureVertex::set(vertex++, points[i], points[i + 1], 0.0f, 0.0f);
2454 generatedVerticesCount++;
Romain Guy7b631422012-04-04 11:38:54 -07002455
Chet Haase8a5cc922011-04-26 07:28:09 -07002456 float left = points[i] - halfWidth;
2457 float right = points[i] + halfWidth;
2458 float top = points[i + 1] - halfWidth;
2459 float bottom = points [i + 1] + halfWidth;
Romain Guy7b631422012-04-04 11:38:54 -07002460
Romain Guy3b753822013-03-05 10:27:35 -08002461 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyed6fcb02011-03-21 13:11:28 -07002462 }
2463
2464 glDrawArrays(GL_POINTS, 0, generatedVerticesCount);
Chet Haase48659092012-05-31 15:21:51 -07002465
2466 return DrawGlInfo::kStatusDrew;
Romain Guyed6fcb02011-03-21 13:11:28 -07002467}
2468
Chet Haase48659092012-05-31 15:21:51 -07002469status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002470 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002471 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002472
Romain Guyae88e5e2010-10-22 17:49:18 -07002473 Rect& clip(*mSnapshot->clipRect);
2474 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002475
Romain Guy3d58c032010-07-14 16:34:53 -07002476 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002477
2478 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002479}
Romain Guy9d5316e2010-06-24 19:30:36 -07002480
Chet Haase48659092012-05-31 15:21:51 -07002481status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2482 SkPaint* paint) {
2483 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002484 const AutoTexture autoCleanup(texture);
2485
2486 const float x = left + texture->left - texture->offset;
2487 const float y = top + texture->top - texture->offset;
2488
2489 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002490
2491 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002492}
2493
Chet Haase48659092012-05-31 15:21:51 -07002494status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002495 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002496 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2497 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002498 return DrawGlInfo::kStatusDone;
2499 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002500
Chris Craikcb4d6002012-09-25 12:00:29 -07002501 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002502 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002503 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002504 right - left, bottom - top, rx, ry, p);
2505 return drawShape(left, top, texture, p);
2506 }
2507
2508 SkPath path;
2509 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002510 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2511 float outset = p->getStrokeWidth() / 2;
2512 rect.outset(outset, outset);
2513 rx += outset;
2514 ry += outset;
2515 }
Chris Craik710f46d2012-09-17 17:25:49 -07002516 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002517 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002518}
2519
Chris Craik710f46d2012-09-17 17:25:49 -07002520status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002521 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002522 x + radius, y + radius, p) ||
2523 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002524 return DrawGlInfo::kStatusDone;
2525 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002526 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002527 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002528 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002529 return drawShape(x - radius, y - radius, texture, p);
2530 }
2531
2532 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002533 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2534 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2535 } else {
2536 path.addCircle(x, y, radius);
2537 }
Chris Craik65cd6122012-12-10 17:56:27 -08002538 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002539}
Romain Guy01d58e42011-01-19 21:54:02 -08002540
Chet Haase48659092012-05-31 15:21:51 -07002541status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002542 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002543 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2544 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002545 return DrawGlInfo::kStatusDone;
2546 }
Romain Guy01d58e42011-01-19 21:54:02 -08002547
Chris Craikcb4d6002012-09-25 12:00:29 -07002548 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002549 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002550 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002551 return drawShape(left, top, texture, p);
2552 }
2553
2554 SkPath path;
2555 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002556 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2557 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2558 }
Chris Craik710f46d2012-09-17 17:25:49 -07002559 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002560 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002561}
2562
Chet Haase48659092012-05-31 15:21:51 -07002563status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002564 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002565 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2566 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002567 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002568 }
2569
Chris Craik780c1282012-10-04 14:10:49 -07002570 if (fabs(sweepAngle) >= 360.0f) {
2571 return drawOval(left, top, right, bottom, p);
2572 }
2573
2574 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002575 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002576 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002577 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002578 startAngle, sweepAngle, useCenter, p);
2579 return drawShape(left, top, texture, p);
2580 }
2581
2582 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2583 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2584 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2585 }
2586
2587 SkPath path;
2588 if (useCenter) {
2589 path.moveTo(rect.centerX(), rect.centerY());
2590 }
2591 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2592 if (useCenter) {
2593 path.close();
2594 }
Chris Craik65cd6122012-12-10 17:56:27 -08002595 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002596}
2597
Romain Guycf8675e2012-10-02 12:32:25 -07002598// See SkPaintDefaults.h
2599#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2600
Chet Haase48659092012-05-31 15:21:51 -07002601status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002602 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2603 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002604 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002605 }
2606
Chris Craik710f46d2012-09-17 17:25:49 -07002607 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002608 // only fill style is supported by drawConvexPath, since others have to handle joins
2609 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2610 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2611 mCaches.activeTexture(0);
2612 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002613 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002614 return drawShape(left, top, texture, p);
2615 }
2616
2617 SkPath path;
2618 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2619 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2620 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2621 }
2622 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002623 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002624 }
2625
Romain Guy3b753822013-03-05 10:27:35 -08002626 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002627 SkPath path;
2628 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002629 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002630 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002631 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002632 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002633 }
Romain Guyc7d53492010-06-25 13:41:57 -07002634}
Romain Guy9d5316e2010-06-24 19:30:36 -07002635
Raph Levien416a8472012-07-19 22:48:17 -07002636void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2637 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2638 float x, float y) {
2639 mCaches.activeTexture(0);
2640
2641 // NOTE: The drop shadow will not perform gamma correction
2642 // if shader-based correction is enabled
2643 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2644 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002645 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Raph Levien416a8472012-07-19 22:48:17 -07002646 const AutoTexture autoCleanup(shadow);
2647
Chris Craikc3566d02013-02-04 16:16:33 -08002648 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2649 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002650
Chris Craikc3566d02013-02-04 16:16:33 -08002651 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2652 int shadowColor = mDrawModifiers.mShadowColor;
2653 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002654 shadowColor = 0xffffffff;
2655 }
2656
2657 setupDraw();
2658 setupDrawWithTexture(true);
2659 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2660 setupDrawColorFilter();
2661 setupDrawShader();
2662 setupDrawBlending(true, mode);
2663 setupDrawProgram();
2664 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2665 setupDrawTexture(shadow->id);
2666 setupDrawPureColorUniforms();
2667 setupDrawColorFilterUniforms();
2668 setupDrawShaderUniforms();
2669 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2670
2671 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2672}
2673
Romain Guy768bffc2013-02-27 13:50:45 -08002674bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2675 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2676 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2677}
2678
Romain Guy257ae352013-03-20 16:31:12 -07002679class TextSetupFunctor: public Functor {
2680public:
2681 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2682 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2683 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2684 alpha(alpha), mode(mode), paint(paint) {
2685 }
2686 ~TextSetupFunctor() { }
2687
2688 status_t operator ()(int what, void* data) {
2689 renderer.setupDraw();
2690 renderer.setupDrawTextGamma(paint);
2691 renderer.setupDrawDirtyRegionsDisabled();
2692 renderer.setupDrawWithTexture(true);
2693 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2694 renderer.setupDrawColorFilter();
2695 renderer.setupDrawShader();
2696 renderer.setupDrawBlending(true, mode);
2697 renderer.setupDrawProgram();
2698 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2699 // Calling setupDrawTexture with the name 0 will enable the
2700 // uv attributes and increase the texture unit count
2701 // texture binding will be performed by the font renderer as
2702 // needed
2703 renderer.setupDrawTexture(0);
2704 renderer.setupDrawPureColorUniforms();
2705 renderer.setupDrawColorFilterUniforms();
2706 renderer.setupDrawShaderUniforms(pureTranslate);
2707 renderer.setupDrawTextGammaUniforms();
2708
2709 return NO_ERROR;
2710 }
2711
2712 OpenGLRenderer& renderer;
2713 float x;
2714 float y;
2715 bool pureTranslate;
2716 int alpha;
2717 SkXfermode::Mode mode;
2718 SkPaint* paint;
2719};
2720
Chet Haase48659092012-05-31 15:21:51 -07002721status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002722 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002723 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002724 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002725 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002726
Romain Guy671d6cf2012-01-18 12:39:17 -08002727 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002728 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002729 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002730 }
2731
2732 float x = 0.0f;
2733 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002734 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002735 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002736 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2737 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002738 }
2739
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002740 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002741 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002742
2743 int alpha;
2744 SkXfermode::Mode mode;
2745 getAlphaAndMode(paint, &alpha, &mode);
2746
Chris Craikc3566d02013-02-04 16:16:33 -08002747 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002748 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2749 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002750 }
2751
Romain Guy671d6cf2012-01-18 12:39:17 -08002752 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002753 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002754 if (pureTranslate && !linearFilter) {
2755 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2756 }
Romain Guy257ae352013-03-20 16:31:12 -07002757 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002758
2759 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2760 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2761
Romain Guy211370f2012-02-01 16:10:55 -08002762 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002763
Romain Guy257ae352013-03-20 16:31:12 -07002764 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002765 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002766 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002767 if (hasActiveLayer) {
2768 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002769 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002770 }
2771 dirtyLayerUnchecked(bounds, getRegion());
2772 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002773 }
Chet Haase48659092012-05-31 15:21:51 -07002774
2775 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002776}
2777
Romain Guy624234f2013-03-05 16:43:31 -08002778mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2779 mat4 fontTransform;
2780 if (CC_LIKELY(transform.isPureTranslate())) {
2781 fontTransform = mat4::identity();
2782 } else {
2783 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002784 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002785 } else {
2786 float sx, sy;
2787 currentTransform().decomposeScale(sx, sy);
2788 fontTransform.loadScale(sx, sy, 1.0f);
2789 }
2790 }
2791 return fontTransform;
2792}
2793
Romain Guyc2525952012-07-27 16:41:22 -07002794status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
Raph Levien996e57c2012-07-23 15:22:52 -07002795 float x, float y, const float* positions, SkPaint* paint, float length) {
Romain Guy768bffc2013-02-27 13:50:45 -08002796 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002797 return DrawGlInfo::kStatusDone;
Romain Guye8e62a42010-07-23 18:55:21 -07002798 }
2799
Chet Haasea1cff502012-02-21 13:43:44 -08002800 if (length < 0.0f) length = paint->measureText(text, bytesCount);
Romain Guye8e62a42010-07-23 18:55:21 -07002801 switch (paint->getTextAlign()) {
2802 case SkPaint::kCenter_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002803 x -= length / 2.0f;
2804 break;
2805 case SkPaint::kRight_Align:
Romain Guye8e62a42010-07-23 18:55:21 -07002806 x -= length;
2807 break;
2808 default:
2809 break;
2810 }
2811
Romain Guycac5fd32011-12-01 20:08:50 -08002812 SkPaint::FontMetrics metrics;
2813 paint->getFontMetrics(&metrics, 0.0f);
Romain Guy33f6beb2012-02-16 19:24:51 -08002814 if (quickReject(x, y + metrics.fTop, x + length, y + metrics.fBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002815 return DrawGlInfo::kStatusDone;
Romain Guycac5fd32011-12-01 20:08:50 -08002816 }
2817
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002818 const float oldX = x;
2819 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002820
2821 const mat4& transform = currentTransform();
2822 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002823
Romain Guy211370f2012-02-01 16:10:55 -08002824 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002825 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2826 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002827 }
2828
Romain Guy86568192010-12-14 15:55:39 -08002829 int alpha;
2830 SkXfermode::Mode mode;
2831 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002832
Romain Guyc74f45a2013-02-26 19:10:14 -08002833 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2834
Chris Craikc3566d02013-02-04 16:16:33 -08002835 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002836 fontRenderer.setFont(paint, mat4::identity());
2837 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2838 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002839 }
2840
Romain Guy19d4dd82013-03-04 11:14:26 -08002841 const bool hasActiveLayer = hasLayer();
2842
Romain Guy624234f2013-03-05 16:43:31 -08002843 // We only pass a partial transform to the font renderer. That partial
2844 // matrix defines how glyphs are rasterized. Typically we want glyphs
2845 // to be rasterized at their final size on screen, which means the partial
2846 // matrix needs to take the scale factor into account.
2847 // When a partial matrix is used to transform glyphs during rasterization,
2848 // the mesh is generated with the inverse transform (in the case of scale,
2849 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2850 // apply the full transform matrix at draw time in the vertex shader.
2851 // Applying the full matrix in the shader is the easiest way to handle
2852 // rotation and perspective and allows us to always generated quads in the
2853 // font renderer which greatly simplifies the code, clipping in particular.
2854 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002855 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002856
Romain Guy6620c6d2010-12-06 18:07:02 -08002857 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002858 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002859 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002860
Romain Guy624234f2013-03-05 16:43:31 -08002861 // TODO: Implement better clipping for scaled/rotated text
2862 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Romain Guy5b3b3522010-10-27 18:57:51 -07002863 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2864
Raph Levien996e57c2012-07-23 15:22:52 -07002865 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07002866 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guya3dc55f2012-09-28 13:55:44 -07002867 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002868 SkPaint paintCopy(*paint);
2869 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2870 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002871 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002872 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002873 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002874 positions, hasActiveLayer ? &bounds : NULL, &functor);
Raph Levien996e57c2012-07-23 15:22:52 -07002875 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002876
2877 if (status && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002878 if (!pureTranslate) {
2879 transform.mapRect(bounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002880 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002881 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002882 }
Romain Guy694b5192010-07-21 21:33:20 -07002883
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002884 drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002885
2886 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002887}
2888
Chet Haase48659092012-05-31 15:21:51 -07002889status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002890 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002891 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002892 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002893 }
2894
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002895 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002896 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002897 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002898
2899 int alpha;
2900 SkXfermode::Mode mode;
2901 getAlphaAndMode(paint, &alpha, &mode);
2902
Romain Guy03d58522012-02-24 17:54:07 -08002903 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07002904 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08002905 setupDrawDirtyRegionsDisabled();
2906 setupDrawWithTexture(true);
2907 setupDrawAlpha8Color(paint->getColor(), alpha);
2908 setupDrawColorFilter();
2909 setupDrawShader();
2910 setupDrawBlending(true, mode);
2911 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08002912 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07002913 // Calling setupDrawTexture with the name 0 will enable the
2914 // uv attributes and increase the texture unit count
2915 // texture binding will be performed by the font renderer as
2916 // needed
2917 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08002918 setupDrawPureColorUniforms();
2919 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08002920 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07002921 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08002922
Romain Guy97771732012-02-28 18:17:02 -08002923 const Rect* clip = &mSnapshot->getLocalClip();
2924 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 -08002925
Romain Guy97771732012-02-28 18:17:02 -08002926 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002927
2928 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
2929 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08002930 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002931 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002932 dirtyLayerUnchecked(bounds, getRegion());
2933 }
Romain Guy97771732012-02-28 18:17:02 -08002934 }
Chet Haase48659092012-05-31 15:21:51 -07002935
2936 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002937}
2938
Chet Haase48659092012-05-31 15:21:51 -07002939status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2940 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002941
Romain Guya1d3c912011-12-13 14:55:06 -08002942 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002943
Romain Guyfb8b7632010-08-23 21:05:08 -07002944 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002945 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002946 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002947
Romain Guy8b55f372010-08-18 17:10:07 -07002948 const float x = texture->left - texture->offset;
2949 const float y = texture->top - texture->offset;
2950
Romain Guy01d58e42011-01-19 21:54:02 -08002951 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002952
2953 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002954}
2955
Chris Craika08f95c2013-03-15 17:24:33 -07002956status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002957 if (!layer) {
2958 return DrawGlInfo::kStatusDone;
2959 }
2960
Romain Guyb2e2f242012-10-17 18:18:35 -07002961 mat4* transform = NULL;
2962 if (layer->isTextureLayer()) {
2963 transform = &layer->getTransform();
2964 if (!transform->isIdentity()) {
2965 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08002966 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002967 }
2968 }
2969
Romain Guy35643dd2012-09-18 15:40:58 -07002970 Rect transformed;
2971 Rect clip;
2972 const bool rejected = quickRejectNoScissor(x, y,
2973 x + layer->layer.getWidth(), y + layer->layer.getHeight(), transformed, clip);
2974
2975 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002976 if (transform && !transform->isIdentity()) {
2977 restore();
2978 }
Chet Haase48659092012-05-31 15:21:51 -07002979 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002980 }
2981
Romain Guy5bb3c732012-11-29 17:52:58 -08002982 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002983
Romain Guy87e2f7572012-09-24 11:37:12 -07002984 mCaches.setScissorEnabled(mScissorOptimizationDisabled || !clip.contains(transformed));
Romain Guya1d3c912011-12-13 14:55:06 -08002985 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002986
Romain Guy211370f2012-02-01 16:10:55 -08002987 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002988 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2989 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002990
Romain Guyc88e3572011-01-22 00:32:12 -08002991 if (layer->region.isRect()) {
Romain Guy40667672011-03-18 14:34:03 -07002992 composeLayerRect(layer, layer->regionRect);
Romain Guyc88e3572011-01-22 00:32:12 -08002993 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07002994 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002995 setupDraw();
2996 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002997 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002998 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002999 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003000 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003001 setupDrawPureColorUniforms();
3002 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003003 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003004 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3005 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3006 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003007
Romain Guyd21b6e12011-11-30 20:21:23 -08003008 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003009 setupDrawModelViewTranslate(tx, ty,
3010 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003011 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003012 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003013 setupDrawModelViewTranslate(x, y,
3014 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3015 }
Romain Guyc88e3572011-01-22 00:32:12 -08003016 setupDrawMesh(&layer->mesh[0].position[0], &layer->mesh[0].texture[0]);
Romain Guyf219da52011-01-16 12:54:25 -08003017
Romain Guyc88e3572011-01-22 00:32:12 -08003018 glDrawElements(GL_TRIANGLES, layer->meshElementCount,
3019 GL_UNSIGNED_SHORT, layer->meshIndices);
Romain Guyf219da52011-01-16 12:54:25 -08003020
Romain Guyc88e3572011-01-22 00:32:12 -08003021 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003022
3023#if DEBUG_LAYERS_AS_REGIONS
3024 drawRegionRects(layer->region);
3025#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003026 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003027
Chris Craikc3566d02013-02-04 16:16:33 -08003028 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003029
Romain Guy5bb3c732012-11-29 17:52:58 -08003030 if (layer->debugDrawUpdate) {
3031 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003032 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3033 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3034 }
Romain Guyf219da52011-01-16 12:54:25 -08003035 }
Chet Haase48659092012-05-31 15:21:51 -07003036
Romain Guyb2e2f242012-10-17 18:18:35 -07003037 if (transform && !transform->isIdentity()) {
3038 restore();
3039 }
3040
Chet Haase48659092012-05-31 15:21:51 -07003041 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003042}
3043
Romain Guy6926c722010-07-12 20:20:03 -07003044///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003045// Shaders
3046///////////////////////////////////////////////////////////////////////////////
3047
3048void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003049 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003050}
3051
Romain Guy06f96e22010-07-30 19:18:16 -07003052void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003053 mDrawModifiers.mShader = shader;
3054 if (mDrawModifiers.mShader) {
3055 mDrawModifiers.mShader->set(&mCaches.textureCache, &mCaches.gradientCache);
Romain Guy06f96e22010-07-30 19:18:16 -07003056 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003057}
3058
Romain Guyd27977d2010-07-14 19:18:51 -07003059///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003060// Color filters
3061///////////////////////////////////////////////////////////////////////////////
3062
3063void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003064 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003065}
3066
3067void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003068 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003069}
3070
3071///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003072// Drop shadow
3073///////////////////////////////////////////////////////////////////////////////
3074
3075void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003076 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003077}
3078
3079void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003080 mDrawModifiers.mHasShadow = true;
3081 mDrawModifiers.mShadowRadius = radius;
3082 mDrawModifiers.mShadowDx = dx;
3083 mDrawModifiers.mShadowDy = dy;
3084 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003085}
3086
3087///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003088// Draw filters
3089///////////////////////////////////////////////////////////////////////////////
3090
3091void OpenGLRenderer::resetPaintFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003092 mDrawModifiers.mHasDrawFilter = false;
Romain Guy5ff9df62012-01-23 17:09:05 -08003093}
3094
3095void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003096 mDrawModifiers.mHasDrawFilter = true;
3097 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3098 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003099}
3100
Chris Craika08f95c2013-03-15 17:24:33 -07003101SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003102 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003103 return paint;
3104 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003105
3106 uint32_t flags = paint->getFlags();
3107
3108 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003109 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3110 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003111
3112 return &mFilteredPaint;
3113}
3114
3115///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003116// Drawing implementation
3117///////////////////////////////////////////////////////////////////////////////
3118
Romain Guy01d58e42011-01-19 21:54:02 -08003119void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3120 float x, float y, SkPaint* paint) {
3121 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3122 return;
3123 }
3124
3125 int alpha;
3126 SkXfermode::Mode mode;
3127 getAlphaAndMode(paint, &alpha, &mode);
3128
3129 setupDraw();
3130 setupDrawWithTexture(true);
3131 setupDrawAlpha8Color(paint->getColor(), alpha);
3132 setupDrawColorFilter();
3133 setupDrawShader();
3134 setupDrawBlending(true, mode);
3135 setupDrawProgram();
3136 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3137 setupDrawTexture(texture->id);
3138 setupDrawPureColorUniforms();
3139 setupDrawColorFilterUniforms();
3140 setupDrawShaderUniforms();
3141 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3142
3143 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3144
3145 finishDrawTexture();
3146}
3147
Romain Guyf607bdc2010-09-10 19:20:06 -07003148// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003149#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3150#define kStdUnderline_Offset (1.0f / 9.0f)
3151#define kStdUnderline_Thickness (1.0f / 18.0f)
3152
3153void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float length,
3154 float x, float y, SkPaint* paint) {
3155 // Handle underline and strike-through
3156 uint32_t flags = paint->getFlags();
3157 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003158 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003159 float underlineWidth = length;
3160 // If length is > 0.0f, we already measured the text for the text alignment
3161 if (length <= 0.0f) {
Romain Guy726aeba2011-06-01 14:52:00 -07003162 underlineWidth = paintCopy.measureText(text, bytesCount);
Romain Guy0a417492010-08-16 20:26:20 -07003163 }
3164
Romain Guy211370f2012-02-01 16:10:55 -08003165 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003166 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003167 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003168
Raph Levien8b4072d2012-07-30 15:50:00 -07003169 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003170 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003171
Romain Guyf6834472011-01-23 13:32:12 -08003172 int linesCount = 0;
3173 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3174 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3175
3176 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003177 float points[pointsCount];
3178 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003179
3180 if (flags & SkPaint::kUnderlineText_Flag) {
3181 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003182 points[currentPoint++] = left;
3183 points[currentPoint++] = top;
3184 points[currentPoint++] = left + underlineWidth;
3185 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003186 }
3187
3188 if (flags & SkPaint::kStrikeThruText_Flag) {
3189 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003190 points[currentPoint++] = left;
3191 points[currentPoint++] = top;
3192 points[currentPoint++] = left + underlineWidth;
3193 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003194 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003195
Romain Guy726aeba2011-06-01 14:52:00 -07003196 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003197
Romain Guy726aeba2011-06-01 14:52:00 -07003198 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003199 }
3200 }
3201}
3202
Romain Guy672433d2013-01-04 19:05:13 -08003203status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3204 if (mSnapshot->isIgnored()) {
3205 return DrawGlInfo::kStatusDone;
3206 }
3207
Romain Guy735738c2012-12-03 12:34:51 -08003208 int color = paint->getColor();
3209 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003210 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003211 color |= 0x00ffffff;
3212 }
3213 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3214
3215 return drawColorRects(rects, count, color, mode);
3216}
3217
3218status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003219 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003220 if (count == 0) {
3221 return DrawGlInfo::kStatusDone;
3222 }
Romain Guy735738c2012-12-03 12:34:51 -08003223
Romain Guy672433d2013-01-04 19:05:13 -08003224 float left = FLT_MAX;
3225 float top = FLT_MAX;
3226 float right = FLT_MIN;
3227 float bottom = FLT_MIN;
3228
3229 int vertexCount = 0;
3230 Vertex mesh[count * 6];
3231 Vertex* vertex = mesh;
3232
Chris Craik2af46352012-11-26 18:30:17 -08003233 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003234 float l = rects[index + 0];
3235 float t = rects[index + 1];
3236 float r = rects[index + 2];
3237 float b = rects[index + 3];
3238
Romain Guy3b753822013-03-05 10:27:35 -08003239 Vertex::set(vertex++, l, b);
3240 Vertex::set(vertex++, l, t);
3241 Vertex::set(vertex++, r, t);
3242 Vertex::set(vertex++, l, b);
3243 Vertex::set(vertex++, r, t);
3244 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003245
Romain Guy3b753822013-03-05 10:27:35 -08003246 vertexCount += 6;
Romain Guy672433d2013-01-04 19:05:13 -08003247
Romain Guy3b753822013-03-05 10:27:35 -08003248 left = fminf(left, l);
3249 top = fminf(top, t);
3250 right = fmaxf(right, r);
3251 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003252 }
3253
Romain Guy3b753822013-03-05 10:27:35 -08003254 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003255 return DrawGlInfo::kStatusDone;
3256 }
Romain Guy672433d2013-01-04 19:05:13 -08003257
Romain Guy672433d2013-01-04 19:05:13 -08003258 setupDraw();
3259 setupDrawNoTexture();
3260 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3261 setupDrawShader();
3262 setupDrawColorFilter();
3263 setupDrawBlending(mode);
3264 setupDrawProgram();
3265 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003266 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003267 setupDrawColorUniforms();
3268 setupDrawShaderUniforms();
3269 setupDrawColorFilterUniforms();
3270 setupDrawVertices((GLvoid*) &mesh[0].position[0]);
3271
Romain Guy8ce00302013-01-15 18:51:42 -08003272 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003273 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003274 }
3275
3276 glDrawArrays(GL_TRIANGLES, 0, vertexCount);
3277
3278 return DrawGlInfo::kStatusDrew;
3279}
3280
Romain Guy026c5e162010-06-28 17:12:22 -07003281void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003282 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003283 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003284 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003285 color |= 0x00ffffff;
3286 }
3287
Romain Guy70ca14e2010-12-13 18:24:33 -08003288 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003289 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003290 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003291 setupDrawShader();
3292 setupDrawColorFilter();
3293 setupDrawBlending(mode);
3294 setupDrawProgram();
3295 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3296 setupDrawColorUniforms();
3297 setupDrawShaderUniforms(ignoreTransform);
3298 setupDrawColorFilterUniforms();
3299 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003300
Romain Guyc95c8d62010-09-17 15:31:32 -07003301 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3302}
3303
Romain Guy82ba8142010-07-09 13:25:56 -07003304void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003305 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003306 int alpha;
3307 SkXfermode::Mode mode;
3308 getAlphaAndMode(paint, &alpha, &mode);
3309
Romain Guyd21b6e12011-11-30 20:21:23 -08003310 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003311
Romain Guy3b753822013-03-05 10:27:35 -08003312 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3313 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3314 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003315
Romain Guyd21b6e12011-11-30 20:21:23 -08003316 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003317 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
3318 alpha / 255.0f, mode, texture->blend, (GLvoid*) NULL,
3319 (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount, false, true);
3320 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003321 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003322 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
3323 texture->blend, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
3324 GL_TRIANGLE_STRIP, gMeshCount);
3325 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003326}
3327
Romain Guybd6b79b2010-06-26 00:13:53 -07003328void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003329 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3330 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003331 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003332}
3333
3334void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003335 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003336 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003337 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003338
Romain Guy746b7402010-10-26 16:27:31 -07003339 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003340 setupDrawWithTexture();
3341 setupDrawColor(alpha, alpha, alpha, alpha);
3342 setupDrawColorFilter();
3343 setupDrawBlending(blend, mode, swapSrcDst);
3344 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003345 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003346 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003347 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003348 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003349 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003350 }
Romain Guy886b2752013-01-04 12:26:18 -08003351 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003352 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003353 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003354 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003355
Romain Guy6820ac82010-09-15 18:11:50 -07003356 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003357
3358 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003359}
3360
Romain Guy886b2752013-01-04 12:26:18 -08003361void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3362 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3363 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3364 bool ignoreTransform, bool dirty) {
3365
3366 setupDraw();
3367 setupDrawWithTexture(true);
3368 if (hasColor) {
3369 setupDrawAlpha8Color(color, alpha);
3370 }
3371 setupDrawColorFilter();
3372 setupDrawShader();
3373 setupDrawBlending(true, mode);
3374 setupDrawProgram();
3375 if (!dirty) setupDrawDirtyRegionsDisabled();
3376 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3377 setupDrawTexture(texture);
3378 setupDrawPureColorUniforms();
3379 setupDrawColorFilterUniforms();
3380 setupDrawShaderUniforms();
3381 setupDrawMesh(vertices, texCoords);
3382
3383 glDrawArrays(drawMode, 0, elementsCount);
3384
3385 finishDrawTexture();
3386}
3387
Romain Guya5aed0d2010-09-09 14:42:43 -07003388void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003389 ProgramDescription& description, bool swapSrcDst) {
Romain Guy82ba8142010-07-09 13:25:56 -07003390 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003391
Romain Guy82ba8142010-07-09 13:25:56 -07003392 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003393 // These blend modes are not supported by OpenGL directly and have
3394 // to be implemented using shaders. Since the shader will perform
3395 // the blending, turn blending off here
3396 // If the blend mode cannot be implemented using shaders, fall
3397 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003398 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003399 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003400 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003401 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003402
Romain Guy82bc7a72012-01-03 14:13:39 -08003403 if (mCaches.blend) {
3404 glDisable(GL_BLEND);
3405 mCaches.blend = false;
3406 }
3407
3408 return;
3409 } else {
3410 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003411 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003412 }
3413
3414 if (!mCaches.blend) {
3415 glEnable(GL_BLEND);
3416 }
3417
3418 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3419 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3420
3421 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3422 glBlendFunc(sourceMode, destMode);
3423 mCaches.lastSrcMode = sourceMode;
3424 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003425 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003426 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003427 glDisable(GL_BLEND);
3428 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003429 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003430}
3431
Romain Guy889f8d12010-07-29 14:37:42 -07003432bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003433 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003434 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003435 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003436 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003437 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003438 }
Romain Guy6926c722010-07-12 20:20:03 -07003439 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003440}
3441
Romain Guy026c5e162010-06-28 17:12:22 -07003442void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003443 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003444 TextureVertex::setUV(v++, u1, v1);
3445 TextureVertex::setUV(v++, u2, v1);
3446 TextureVertex::setUV(v++, u1, v2);
3447 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003448}
3449
Chris Craik16ecda52013-03-29 10:59:59 -07003450void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003451 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003452 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3453 // if drawing a layer, ignore the paint's alpha
3454 *alpha = mDrawModifiers.mOverrideLayerAlpha;
3455 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003456 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003457}
3458
Chris Craik16ecda52013-03-29 10:59:59 -07003459float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3460 float alpha;
3461 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3462 alpha = mDrawModifiers.mOverrideLayerAlpha;
3463 } else {
3464 alpha = layer->getAlpha() / 255.0f;
3465 }
3466 return alpha * mSnapshot->alpha;
3467}
3468
Romain Guy9d5316e2010-06-24 19:30:36 -07003469}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003470}; // namespace android