blob: 238d9a4955c6a88da9ab479644abc4b64c69c0f6 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.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///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700183 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guyd643bb52011-03-01 14:55:21 -0800454
Romain Guy80911b82011-03-16 15:30:12 -0700455 Rect clip(*mSnapshot->clipRect);
456 clip.snapToPixelBoundaries();
457
Romain Guyd643bb52011-03-01 14:55:21 -0800458 // Since we don't know what the functor will draw, let's dirty
459 // tne entire clip region
460 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800461 dirtyLayerUnchecked(clip, getRegion());
462 }
Romain Guyd643bb52011-03-01 14:55:21 -0800463
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 DrawGlInfo info;
465 info.clipLeft = clip.left;
466 info.clipTop = clip.top;
467 info.clipRight = clip.right;
468 info.clipBottom = clip.bottom;
469 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700470 info.width = getSnapshot()->viewport.getWidth();
471 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700473
Chris Craik54f574a2013-08-26 11:23:46 -0700474 // setup GL state for functor
475 if (mDirtyClip) {
476 setScissorFromClip();
477 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
478 }
479 mCaches.enableScissor();
480 interrupt();
481
482 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700483 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800484
Romain Guy8f3b8e32012-03-27 16:33:45 -0700485 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700486 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800487 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700488
Chris Craik65924a32012-04-05 17:52:11 -0700489 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700490 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700491 }
Romain Guycabfcc12011-03-07 18:06:46 -0800492 }
493
Chet Haasedaf98e92011-01-10 14:10:36 -0800494 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700495 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800496}
497
Romain Guyf6a11b82010-06-23 17:47:49 -0700498///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700499// Debug
500///////////////////////////////////////////////////////////////////////////////
501
Romain Guy0f6675332013-03-01 14:31:04 -0800502void OpenGLRenderer::eventMark(const char* name) const {
503 mCaches.eventMark(0, name);
504}
505
Romain Guy87e2f7572012-09-24 11:37:12 -0700506void OpenGLRenderer::startMark(const char* name) const {
507 mCaches.startMark(0, name);
508}
509
510void OpenGLRenderer::endMark() const {
511 mCaches.endMark();
512}
513
514void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
515 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
516 if (clear) {
517 mCaches.disableScissor();
518 mCaches.stencil.clear();
519 }
520 if (enable) {
521 mCaches.stencil.enableDebugWrite();
522 } else {
523 mCaches.stencil.disable();
524 }
525 }
526}
527
528void OpenGLRenderer::renderOverdraw() {
529 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700530 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700531
532 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700533 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700534 clip->right - clip->left, clip->bottom - clip->top);
535
Romain Guy627c6fd2013-08-21 11:53:18 -0700536 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700537 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
539
540 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700541 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700542 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
543
544 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700545 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700546 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
547
548 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700549 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700550 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
551
Romain Guy87e2f7572012-09-24 11:37:12 -0700552 mCaches.stencil.disable();
553 }
554}
555
Romain Guy78dd96d2013-05-03 14:24:16 -0700556void OpenGLRenderer::countOverdraw() {
557 size_t count = mWidth * mHeight;
558 uint32_t* buffer = new uint32_t[count];
559 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
560
561 size_t total = 0;
562 for (size_t i = 0; i < count; i++) {
563 total += buffer[i] & 0xff;
564 }
565
566 mOverdraw = total / float(count);
567
568 delete[] buffer;
569}
570
Romain Guy87e2f7572012-09-24 11:37:12 -0700571///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700572// Layers
573///////////////////////////////////////////////////////////////////////////////
574
575bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700576 if (layer->deferredUpdateScheduled && layer->renderer &&
577 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700578 ATRACE_CALL();
579
Romain Guy11cb6422012-09-21 00:39:43 -0700580 Rect& dirty = layer->dirtyRect;
581
Romain Guy7c450aa2012-09-21 19:15:00 -0700582 if (inFrame) {
583 endTiling();
584 debugOverdraw(false, false);
585 }
Romain Guy11cb6422012-09-21 00:39:43 -0700586
Romain Guy96885eb2013-03-26 15:05:58 -0700587 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700588 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700589 } else {
590 layer->defer();
591 }
Romain Guy11cb6422012-09-21 00:39:43 -0700592
593 if (inFrame) {
594 resumeAfterLayer();
595 startTiling(mSnapshot);
596 }
597
Romain Guy5bb3c732012-11-29 17:52:58 -0800598 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700599 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700600
601 return true;
602 }
603
604 return false;
605}
606
607void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700608 // If draw deferring is enabled this method will simply defer
609 // the display list of each individual layer. The layers remain
610 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700611 int count = mLayerUpdates.size();
612 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700613 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
614 startMark("Layer Updates");
615 } else {
616 startMark("Defer Layer Updates");
617 }
Romain Guy11cb6422012-09-21 00:39:43 -0700618
Chris Craik1206b9b2013-04-04 14:46:24 -0700619 // Note: it is very important to update the layers in order
620 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700621 Layer* layer = mLayerUpdates.itemAt(i);
622 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700623 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
624 mCaches.resourceCache.decrementRefcount(layer);
625 }
Romain Guy11cb6422012-09-21 00:39:43 -0700626 }
Romain Guy11cb6422012-09-21 00:39:43 -0700627
Romain Guy96885eb2013-03-26 15:05:58 -0700628 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
629 mLayerUpdates.clear();
630 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
631 }
632 endMark();
633 }
634}
635
636void OpenGLRenderer::flushLayers() {
637 int count = mLayerUpdates.size();
638 if (count > 0) {
639 startMark("Apply Layer Updates");
640 char layerName[12];
641
Chris Craik1206b9b2013-04-04 14:46:24 -0700642 // Note: it is very important to update the layers in order
643 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700644 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700645 startMark(layerName);
646
Romain Guy40543602013-06-12 15:31:28 -0700647 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700648 Layer* layer = mLayerUpdates.itemAt(i);
649 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700650 ATRACE_END();
651
Romain Guy02b49b72013-03-29 12:37:16 -0700652 mCaches.resourceCache.decrementRefcount(layer);
653
Romain Guy96885eb2013-03-26 15:05:58 -0700654 endMark();
655 }
656
657 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700658 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700659
Romain Guy11cb6422012-09-21 00:39:43 -0700660 endMark();
661 }
662}
663
664void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
665 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700666 // Make sure we don't introduce duplicates.
667 // SortedVector would do this automatically but we need to respect
668 // the insertion order. The linear search is not an issue since
669 // this list is usually very short (typically one item, at most a few)
670 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
671 if (mLayerUpdates.itemAt(i) == layer) {
672 return;
673 }
674 }
Romain Guy11cb6422012-09-21 00:39:43 -0700675 mLayerUpdates.push_back(layer);
676 mCaches.resourceCache.incrementRefcount(layer);
677 }
678}
679
Romain Guye93482f2013-06-17 13:14:51 -0700680void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
681 if (layer) {
682 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
683 if (mLayerUpdates.itemAt(i) == layer) {
684 mLayerUpdates.removeAt(i);
685 mCaches.resourceCache.decrementRefcount(layer);
686 break;
687 }
688 }
689 }
690}
691
Romain Guy11cb6422012-09-21 00:39:43 -0700692void OpenGLRenderer::clearLayerUpdates() {
693 size_t count = mLayerUpdates.size();
694 if (count > 0) {
695 mCaches.resourceCache.lock();
696 for (size_t i = 0; i < count; i++) {
697 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
698 }
699 mCaches.resourceCache.unlock();
700 mLayerUpdates.clear();
701 }
702}
703
Romain Guy40543602013-06-12 15:31:28 -0700704void OpenGLRenderer::flushLayerUpdates() {
705 syncState();
706 updateLayers();
707 flushLayers();
708 // Wait for all the layer updates to be executed
709 AutoFence fence;
710}
711
Romain Guy11cb6422012-09-21 00:39:43 -0700712///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700713// State management
714///////////////////////////////////////////////////////////////////////////////
715
Romain Guybb9524b2010-06-22 18:56:38 -0700716int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700717 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700718}
719
720int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700721 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700722}
723
724void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700725 if (mSaveCount > 1) {
726 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700727 }
Romain Guybb9524b2010-06-22 18:56:38 -0700728}
729
730void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700731 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700732
Romain Guy8fb95422010-08-17 18:38:51 -0700733 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700734 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700735 }
Romain Guybb9524b2010-06-22 18:56:38 -0700736}
737
Romain Guy8aef54f2010-09-01 15:13:49 -0700738int OpenGLRenderer::saveSnapshot(int flags) {
739 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700740 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700741}
742
743bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700744 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700745 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700746 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700747
Romain Guybd6b79b2010-06-26 00:13:53 -0700748 sp<Snapshot> current = mSnapshot;
749 sp<Snapshot> previous = mSnapshot->previous;
750
Romain Guyeb993562010-10-05 18:14:38 -0700751 if (restoreOrtho) {
752 Rect& r = previous->viewport;
753 glViewport(r.left, r.top, r.right, r.bottom);
754 mOrthoMatrix.load(current->orthoMatrix);
755 }
756
Romain Guy8b55f372010-08-18 17:10:07 -0700757 mSaveCount--;
758 mSnapshot = previous;
759
Romain Guy2542d192010-08-18 11:47:12 -0700760 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700761 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700762 }
Romain Guy2542d192010-08-18 11:47:12 -0700763
Romain Guy5ec99242010-11-03 16:19:08 -0700764 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700765 endMark(); // Savelayer
766 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700767 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700768 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700769 }
770
Romain Guy2542d192010-08-18 11:47:12 -0700771 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700772}
773
Romain Guyf6a11b82010-06-23 17:47:49 -0700774///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700775// Layers
776///////////////////////////////////////////////////////////////////////////////
777
778int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800779 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700780 const GLuint previousFbo = mSnapshot->fbo;
781 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700782
Romain Guyaf636eb2010-12-09 17:47:21 -0800783 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700784 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700785 }
Romain Guyd55a8612010-06-28 17:42:46 -0700786
787 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700788}
789
Chris Craikd90144d2013-03-19 15:03:48 -0700790void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
791 const Rect untransformedBounds(bounds);
792
793 currentTransform().mapRect(bounds);
794
795 // Layers only make sense if they are in the framebuffer's bounds
796 if (bounds.intersect(*mSnapshot->clipRect)) {
797 // We cannot work with sub-pixels in this case
798 bounds.snapToPixelBoundaries();
799
800 // When the layer is not an FBO, we may use glCopyTexImage so we
801 // need to make sure the layer does not extend outside the bounds
802 // of the framebuffer
803 if (!bounds.intersect(mSnapshot->previous->viewport)) {
804 bounds.setEmpty();
805 } else if (fboLayer) {
806 clip.set(bounds);
807 mat4 inverse;
808 inverse.loadInverse(currentTransform());
809 inverse.mapRect(clip);
810 clip.snapToPixelBoundaries();
811 if (clip.intersect(untransformedBounds)) {
812 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
813 bounds.set(untransformedBounds);
814 } else {
815 clip.setEmpty();
816 }
817 }
818 } else {
819 bounds.setEmpty();
820 }
821}
822
Chris Craik408eb122013-03-26 18:55:15 -0700823void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
824 bool fboLayer, int alpha) {
825 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
826 bounds.getHeight() > mCaches.maxTextureSize ||
827 (fboLayer && clip.isEmpty())) {
828 mSnapshot->empty = fboLayer;
829 } else {
830 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
831 }
832}
833
Chris Craikd90144d2013-03-19 15:03:48 -0700834int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
835 int alpha, SkXfermode::Mode mode, int flags) {
836 const GLuint previousFbo = mSnapshot->fbo;
837 const int count = saveSnapshot(flags);
838
839 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
840 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
841 // operations will be able to store and restore the current clip and transform info, and
842 // quick rejection will be correct (for display lists)
843
844 Rect bounds(left, top, right, bottom);
845 Rect clip;
846 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700847 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700848
Chris Craik408eb122013-03-26 18:55:15 -0700849 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700850 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
851 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700852 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700853 }
854 }
855
856 return count;
857}
858
859
Romain Guy1c740bc2010-09-13 18:00:09 -0700860/**
861 * Layers are viewed by Skia are slightly different than layers in image editing
862 * programs (for instance.) When a layer is created, previously created layers
863 * and the frame buffer still receive every drawing command. For instance, if a
864 * layer is created and a shape intersecting the bounds of the layers and the
865 * framebuffer is draw, the shape will be drawn on both (unless the layer was
866 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
867 *
868 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
869 * texture. Unfortunately, this is inefficient as it requires every primitive to
870 * be drawn n + 1 times, where n is the number of active layers. In practice this
871 * means, for every primitive:
872 * - Switch active frame buffer
873 * - Change viewport, clip and projection matrix
874 * - Issue the drawing
875 *
876 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700877 * To avoid this, layers are implemented in a different way here, at least in the
878 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
879 * is set. When this flag is set we can redirect all drawing operations into a
880 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700881 *
882 * This implementation relies on the frame buffer being at least RGBA 8888. When
883 * a layer is created, only a texture is created, not an FBO. The content of the
884 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700885 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700886 * buffer and drawing continues as normal. This technique therefore treats the
887 * frame buffer as a scratch buffer for the layers.
888 *
889 * To compose the layers back onto the frame buffer, each layer texture
890 * (containing the original frame buffer data) is drawn as a simple quad over
891 * the frame buffer. The trick is that the quad is set as the composition
892 * destination in the blending equation, and the frame buffer becomes the source
893 * of the composition.
894 *
895 * Drawing layers with an alpha value requires an extra step before composition.
896 * An empty quad is drawn over the layer's region in the frame buffer. This quad
897 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
898 * quad is used to multiply the colors in the frame buffer. This is achieved by
899 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
900 * GL_ZERO, GL_SRC_ALPHA.
901 *
902 * Because glCopyTexImage2D() can be slow, an alternative implementation might
903 * be use to draw a single clipped layer. The implementation described above
904 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700905 *
906 * (1) The frame buffer is actually not cleared right away. To allow the GPU
907 * to potentially optimize series of calls to glCopyTexImage2D, the frame
908 * buffer is left untouched until the first drawing operation. Only when
909 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700910 */
Chet Haased48885a2012-08-28 17:43:28 -0700911bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
912 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700913 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700915
Romain Guyeb993562010-10-05 18:14:38 -0700916 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
917
Romain Guyf607bdc2010-09-10 19:20:06 -0700918 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700919 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700920 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700921 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700922 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700923
924 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700925 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700926 return false;
927 }
Romain Guyf18fd992010-07-08 11:45:51 -0700928
Romain Guya1d3c912011-12-13 14:55:06 -0800929 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700930 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700931 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700932 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700933 }
934
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
938 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800939 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700940 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700941 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700942
Romain Guy8fb95422010-08-17 18:38:51 -0700943 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700944 mSnapshot->flags |= Snapshot::kFlagIsLayer;
945 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700946
Chris Craik7273daa2013-03-28 11:25:24 -0700947 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700948 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700949 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700950 } else {
951 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800953 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700955 // Workaround for some GL drivers. When reading pixels lying outside
956 // of the window we should get undefined values for those pixels.
957 // Unfortunately some drivers will turn the entire target texture black
958 // when reading outside of the window.
959 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
960 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800962 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700963
Romain Guyb254c242013-06-27 17:15:24 -0700964 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
965 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
966
Romain Guy54be1cd2011-06-13 19:04:27 -0700967 // Enqueue the buffer coordinates to clear the corresponding region later
968 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700969 }
Romain Guyeb993562010-10-05 18:14:38 -0700970 }
Romain Guyf86ef572010-07-01 11:05:42 -0700971
Romain Guyd55a8612010-06-28 17:42:46 -0700972 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700973}
974
Chet Haased48885a2012-08-28 17:43:28 -0700975bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800976 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
Chet Haased48885a2012-08-28 17:43:28 -0700979 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800980 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
981 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700982 mSnapshot->fbo = layer->getFbo();
983 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
984 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
985 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
986 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700987 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988
Romain Guy2b7028e2012-09-19 17:25:38 -0700989 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700990 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700991 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
993 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
995 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700997 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 }
1000
1001 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
Romain Guyf735c8e2013-01-31 17:45:55 -08001004 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
1006 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001007 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001008 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001009 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 glClear(GL_COLOR_BUFFER_BIT);
1011
1012 dirtyClip();
1013
1014 // Change the ortho projection
1015 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1016 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1017
1018 return true;
1019}
1020
Romain Guy1c740bc2010-09-13 18:00:09 -07001021/**
1022 * Read the documentation of createLayer() before doing anything in this method.
1023 */
Romain Guy1d83e192010-08-17 11:37:00 -07001024void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1025 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001026 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001027 return;
1028 }
1029
Romain Guy8ce00302013-01-15 18:51:42 -08001030 Layer* layer = current->layer;
1031 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001032 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001033
Chris Craik39a908c2013-06-13 14:39:01 -07001034 bool clipRequired = false;
1035 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1036 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1037
Romain Guyeb993562010-10-05 18:14:38 -07001038 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001039 endTiling();
1040
Romain Guye0aa84b2012-04-03 19:30:26 -07001041 // Detach the texture from the FBO
1042 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001043
1044 layer->removeFbo(false);
1045
Romain Guyeb993562010-10-05 18:14:38 -07001046 // Unbind current FBO and restore previous one
1047 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001048 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001049
1050 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001051 }
1052
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001054 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001056 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001058 }
1059
Romain Guy03750a02010-10-18 14:06:08 -07001060 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001061
Romain Guya1d3c912011-12-13 14:55:06 -08001062 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001063
Romain Guy5b3b3522010-10-27 18:57:51 -07001064 // When the layer is stored in an FBO, we can save a bit of fillrate by
1065 // drawing only the dirty region
1066 if (fboLayer) {
1067 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001068 if (layer->getColorFilter()) {
1069 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001070 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001071 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001073 resetColorFilter();
1074 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 } else if (!rect.isEmpty()) {
1076 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1077 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001078 }
Romain Guy8b55f372010-08-18 17:10:07 -07001079
Romain Guy746b7402010-10-26 16:27:31 -07001080 dirtyClip();
1081
Romain Guyeb993562010-10-05 18:14:38 -07001082 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001083 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001084 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001085 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001086 }
1087}
1088
Romain Guyaa6c24c2011-04-28 18:40:04 -07001089void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001090 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001091
1092 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001093 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001094 setupDrawWithTexture();
1095 } else {
1096 setupDrawWithExternalTexture();
1097 }
1098 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001099 setupDrawColor(alpha, alpha, alpha, alpha);
1100 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001101 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001102 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001103 setupDrawPureColorUniforms();
1104 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001105 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1106 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001107 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001108 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001109 }
Romain Guy3b753822013-03-05 10:27:35 -08001110 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001111 layer->getWidth() == (uint32_t) rect.getWidth() &&
1112 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001113 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1114 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001115
Romain Guyd21b6e12011-11-30 20:21:23 -08001116 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001117 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1118 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001119 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001120 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1121 }
1122 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001123 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1124
1125 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001126}
1127
Romain Guy5b3b3522010-10-27 18:57:51 -07001128void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001129 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001130 const Rect& texCoords = layer->texCoords;
1131 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1132 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001133
Romain Guy9ace8f52011-07-07 20:50:11 -07001134 float x = rect.left;
1135 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001136 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001137 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001138 layer->getHeight() == (uint32_t) rect.getHeight();
1139
1140 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001141 // When we're swapping, the layer is already in screen coordinates
1142 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001143 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1144 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001145 }
1146
Romain Guyd21b6e12011-11-30 20:21:23 -08001147 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001148 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001149 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001150 }
1151
Chris Craik16ecda52013-03-29 10:59:59 -07001152 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001153 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001154 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001155 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1157 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001158
Romain Guyaa6c24c2011-04-28 18:40:04 -07001159 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1160 } else {
1161 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1162 drawTextureLayer(layer, rect);
1163 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1164 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001165}
1166
Chris Craik34416ea2013-04-15 16:08:28 -07001167/**
1168 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1169 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1170 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1171 * by saveLayer's restore
1172 */
1173#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1174 DRAW_COMMAND; \
1175 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1176 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1177 DRAW_COMMAND; \
1178 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1179 } \
1180 }
1181
1182#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1183
Romain Guy5b3b3522010-10-27 18:57:51 -07001184void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001185 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001186 layer->setRegionAsRect();
1187
Chris Craik34416ea2013-04-15 16:08:28 -07001188 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001189
Romain Guy5b3b3522010-10-27 18:57:51 -07001190 layer->region.clear();
1191 return;
1192 }
1193
Romain Guy211370f2012-02-01 16:10:55 -08001194 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001195 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001196 const android::Rect* rects;
1197 Region safeRegion;
1198 if (CC_LIKELY(hasRectToRectTransform())) {
1199 rects = layer->region.getArray(&count);
1200 } else {
1201 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1202 rects = safeRegion.getArray(&count);
1203 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001204
Chris Craik16ecda52013-03-29 10:59:59 -07001205 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001206 const float texX = 1.0f / float(layer->getWidth());
1207 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001208 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001209
Romain Guy8ce00302013-01-15 18:51:42 -08001210 setupDraw();
1211
1212 // We must get (and therefore bind) the region mesh buffer
1213 // after we setup drawing in case we need to mess with the
1214 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001215 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001216 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001217
Romain Guy7230a742011-01-10 22:26:16 -08001218 setupDrawWithTexture();
1219 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001220 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001221 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001222 setupDrawProgram();
1223 setupDrawDirtyRegionsDisabled();
1224 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001225 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001226 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001227 if (currentTransform().isPureTranslate()) {
1228 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1229 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001230
Romain Guyd21b6e12011-11-30 20:21:23 -08001231 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001232 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1233 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001234 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001235 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1236 }
Romain Guy15bc6432011-12-13 13:11:32 -08001237 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001238
1239 for (size_t i = 0; i < count; i++) {
1240 const android::Rect* r = &rects[i];
1241
1242 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001243 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001244 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001245 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001246
1247 // TODO: Reject quads outside of the clip
1248 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1249 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1250 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1251 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1252
1253 numQuads++;
1254
Romain Guy31e08e92013-06-18 15:53:53 -07001255 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001256 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1257 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001258 numQuads = 0;
1259 mesh = mCaches.getRegionMesh();
1260 }
1261 }
1262
1263 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001264 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1265 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001266 }
1267
Romain Guy5b3b3522010-10-27 18:57:51 -07001268#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001269 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001270#endif
1271
1272 layer->region.clear();
1273 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001274}
1275
Romain Guy3a3133d2011-02-01 22:59:58 -08001276void OpenGLRenderer::drawRegionRects(const Region& region) {
1277#if DEBUG_LAYERS_AS_REGIONS
1278 size_t count;
1279 const android::Rect* rects = region.getArray(&count);
1280
1281 uint32_t colors[] = {
1282 0x7fff0000, 0x7f00ff00,
1283 0x7f0000ff, 0x7fff00ff,
1284 };
1285
1286 int offset = 0;
1287 int32_t top = rects[0].top;
1288
1289 for (size_t i = 0; i < count; i++) {
1290 if (top != rects[i].top) {
1291 offset ^= 0x2;
1292 top = rects[i].top;
1293 }
1294
1295 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1296 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1297 SkXfermode::kSrcOver_Mode);
1298 }
1299#endif
1300}
1301
Romain Guy8ce00302013-01-15 18:51:42 -08001302void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1303 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001304 Vector<float> rects;
1305
1306 SkRegion::Iterator it(region);
1307 while (!it.done()) {
1308 const SkIRect& r = it.rect();
1309 rects.push(r.fLeft);
1310 rects.push(r.fTop);
1311 rects.push(r.fRight);
1312 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001313 it.next();
1314 }
1315
Romain Guy448455f2013-07-22 13:57:50 -07001316 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001317}
1318
Romain Guy5b3b3522010-10-27 18:57:51 -07001319void OpenGLRenderer::dirtyLayer(const float left, const float top,
1320 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001321 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001322 Rect bounds(left, top, right, bottom);
1323 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001324 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001325 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001326}
1327
1328void OpenGLRenderer::dirtyLayer(const float left, const float top,
1329 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001330 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001331 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001332 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001333 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001334}
1335
1336void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001337 if (bounds.intersect(*mSnapshot->clipRect)) {
1338 bounds.snapToPixelBoundaries();
1339 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1340 if (!dirty.isEmpty()) {
1341 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001342 }
1343 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001344}
1345
Romain Guy448455f2013-07-22 13:57:50 -07001346void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1347 GLsizei elementsCount = quadsCount * 6;
1348 while (elementsCount > 0) {
1349 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1350
1351 setupDrawIndexedVertices(&mesh[0].position[0]);
1352 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1353
1354 elementsCount -= drawCount;
1355 // Though there are 4 vertices in a quad, we use 6 indices per
1356 // quad to draw with GL_TRIANGLES
1357 mesh += (drawCount / 6) * 4;
1358 }
1359}
1360
Romain Guy54be1cd2011-06-13 19:04:27 -07001361void OpenGLRenderer::clearLayerRegions() {
1362 const size_t count = mLayers.size();
1363 if (count == 0) return;
1364
1365 if (!mSnapshot->isIgnored()) {
1366 // Doing several glScissor/glClear here can negatively impact
1367 // GPUs with a tiler architecture, instead we draw quads with
1368 // the Clear blending mode
1369
1370 // The list contains bounds that have already been clipped
1371 // against their initial clip rect, and the current clip
1372 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001373 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001374
Romain Guy448455f2013-07-22 13:57:50 -07001375 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001376 Vertex* vertex = mesh;
1377
1378 for (uint32_t i = 0; i < count; i++) {
1379 Rect* bounds = mLayers.itemAt(i);
1380
Romain Guy54be1cd2011-06-13 19:04:27 -07001381 Vertex::set(vertex++, bounds->left, bounds->top);
1382 Vertex::set(vertex++, bounds->right, bounds->top);
1383 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001384 Vertex::set(vertex++, bounds->right, bounds->bottom);
1385
1386 delete bounds;
1387 }
Romain Guye67307c2013-02-11 18:01:20 -08001388 // We must clear the list of dirty rects before we
1389 // call setupDraw() to prevent stencil setup to do
1390 // the same thing again
1391 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001392
1393 setupDraw(false);
1394 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1395 setupDrawBlending(true, SkXfermode::kClear_Mode);
1396 setupDrawProgram();
1397 setupDrawPureColorUniforms();
1398 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1399
Romain Guy448455f2013-07-22 13:57:50 -07001400 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001401
1402 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001403 } else {
1404 for (uint32_t i = 0; i < count; i++) {
1405 delete mLayers.itemAt(i);
1406 }
Romain Guye67307c2013-02-11 18:01:20 -08001407 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001408 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001409}
1410
Romain Guybd6b79b2010-06-26 00:13:53 -07001411///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001412// State Deferral
1413///////////////////////////////////////////////////////////////////////////////
1414
Chris Craikff785832013-03-08 13:12:16 -08001415bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001416 const Rect& currentClip = *(mSnapshot->clipRect);
1417 const mat4& currentMatrix = *(mSnapshot->transform);
1418
Chris Craikff785832013-03-08 13:12:16 -08001419 if (stateDeferFlags & kStateDeferFlag_Draw) {
1420 // state has bounds initialized in local coordinates
1421 if (!state.mBounds.isEmpty()) {
1422 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001423 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001424 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1425 // is used, it should more closely duplicate the quickReject logic (in how it uses
1426 // snapToPixelBoundaries)
1427
Chris Craik28ce94a2013-05-31 11:38:03 -07001428 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001429 // quick rejected
1430 return true;
1431 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001432
Chris Craika02c4ed2013-06-14 13:43:58 -07001433 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001434 if (!currentClip.contains(state.mBounds)) {
1435 int& flags = state.mClipSideFlags;
1436 // op partially clipped, so record which sides are clipped for clip-aware merging
1437 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1438 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1439 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1440 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1441 }
1442 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001443 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001444 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1445 // overdraw avoidance (since we don't know what it overlaps)
1446 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001447 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001448 }
1449 }
1450
Chris Craik527a3aa2013-03-04 10:19:31 -08001451 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1452 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001453 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001454 }
1455
Chris Craik7273daa2013-03-28 11:25:24 -07001456 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1457 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001458 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001459 state.mDrawModifiers = mDrawModifiers;
1460 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001461 return false;
1462}
1463
Chris Craik527a3aa2013-03-04 10:19:31 -08001464void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001465 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001466 mDrawModifiers = state.mDrawModifiers;
1467 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001468
Chris Craik527a3aa2013-03-04 10:19:31 -08001469 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001470 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1471 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001472 dirtyClip();
1473 }
Chris Craikc3566d02013-02-04 16:16:33 -08001474}
1475
Chris Craik28ce94a2013-05-31 11:38:03 -07001476/**
1477 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1478 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1479 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1480 *
1481 * This method should be called when restoreDisplayState() won't be restoring the clip
1482 */
1483void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1484 if (clipRect != NULL) {
1485 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1486 } else {
1487 mSnapshot->setClip(0, 0, mWidth, mHeight);
1488 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001489 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001490 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001491}
1492
Chris Craikc3566d02013-02-04 16:16:33 -08001493///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001494// Transforms
1495///////////////////////////////////////////////////////////////////////////////
1496
1497void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001498 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001499}
1500
1501void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001502 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001503}
1504
1505void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001506 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001507}
1508
Romain Guy807daf72011-01-18 11:19:19 -08001509void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001510 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001511}
1512
Romain Guyf6a11b82010-06-23 17:47:49 -07001513void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001514 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001515 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001516 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001517 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001518 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001519}
1520
Chris Craikb98a0162013-02-21 11:30:22 -08001521bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001522 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001523}
1524
Romain Guyf6a11b82010-06-23 17:47:49 -07001525void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001526 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001527}
1528
1529void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001530 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001531 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001532 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001533 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001534}
1535
1536///////////////////////////////////////////////////////////////////////////////
1537// Clipping
1538///////////////////////////////////////////////////////////////////////////////
1539
Romain Guybb9524b2010-06-22 18:56:38 -07001540void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001541 Rect clip(*mSnapshot->clipRect);
1542 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001543
Romain Guy8a4ac612012-07-17 17:32:48 -07001544 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1545 clip.getWidth(), clip.getHeight())) {
1546 mDirtyClip = false;
1547 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001548}
1549
Romain Guy8ce00302013-01-15 18:51:42 -08001550void OpenGLRenderer::ensureStencilBuffer() {
1551 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1552 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1553 // just hope we have one when hasLayer() returns false.
1554 if (hasLayer()) {
1555 attachStencilBufferToLayer(mSnapshot->layer);
1556 }
1557}
1558
1559void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1560 // The layer's FBO is already bound when we reach this stage
1561 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001562 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1563 // is attached after we initiated tiling. We must turn it off,
1564 // attach the new render buffer then turn tiling back on
1565 endTiling();
1566
Romain Guy8d4aeb72013-02-12 16:08:55 -08001567 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001568 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001569 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001570
Romain Guyf735c8e2013-01-31 17:45:55 -08001571 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001572 }
1573}
1574
1575void OpenGLRenderer::setStencilFromClip() {
1576 if (!mCaches.debugOverdraw) {
1577 if (!mSnapshot->clipRegion->isEmpty()) {
1578 // NOTE: The order here is important, we must set dirtyClip to false
1579 // before any draw call to avoid calling back into this method
1580 mDirtyClip = false;
1581
1582 ensureStencilBuffer();
1583
1584 mCaches.stencil.enableWrite();
1585
1586 // Clear the stencil but first make sure we restrict drawing
1587 // to the region's bounds
1588 bool resetScissor = mCaches.enableScissor();
1589 if (resetScissor) {
1590 // The scissor was not set so we now need to update it
1591 setScissorFromClip();
1592 }
1593 mCaches.stencil.clear();
1594 if (resetScissor) mCaches.disableScissor();
1595
1596 // NOTE: We could use the region contour path to generate a smaller mesh
1597 // Since we are using the stencil we could use the red book path
1598 // drawing technique. It might increase bandwidth usage though.
1599
1600 // The last parameter is important: we are not drawing in the color buffer
1601 // so we don't want to dirty the current layer, if any
1602 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1603
1604 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001605
1606 // Draw the region used to generate the stencil if the appropriate debug
1607 // mode is enabled
1608 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1609 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1610 }
Romain Guy8ce00302013-01-15 18:51:42 -08001611 } else {
1612 mCaches.stencil.disable();
1613 }
1614 }
1615}
1616
Romain Guy9d5316e2010-06-24 19:30:36 -07001617const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001618 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001619}
1620
Chris Craik39a908c2013-06-13 14:39:01 -07001621bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001622 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001623 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001624 return true;
1625 }
1626
1627 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001628 currentTransform().mapRect(r);
Chris Craik5e49b302013-07-30 19:05:20 -07001629
1630 if (snapOut) {
1631 // snapOut is generally used to account for 1 pixel ramp (in window coordinates)
1632 // outside of the provided rect boundaries in tessellated AA geometry
1633 r.snapOutToPixelBoundaries();
1634 } else {
1635 r.snapToPixelBoundaries();
1636 }
Romain Guy8a4ac612012-07-17 17:32:48 -07001637
1638 Rect clipRect(*mSnapshot->clipRect);
1639 clipRect.snapToPixelBoundaries();
1640
Chris Craik39a908c2013-06-13 14:39:01 -07001641 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001642
Chris Craik39a908c2013-06-13 14:39:01 -07001643 if (clipRequired) *clipRequired = !clipRect.contains(r);
1644 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001645}
1646
Romain Guy672433d2013-01-04 19:05:13 -08001647bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1648 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001649 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1650 // the final mapped rect to ensure correct clipping behavior for the ramp.
1651 bool snapOut = paint->isAntiAlias();
1652
Chris Craikcb4d6002012-09-25 12:00:29 -07001653 if (paint->getStyle() != SkPaint::kFill_Style) {
1654 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001655 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001656 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001657 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001658 }
1659}
1660
Chris Craik5e49b302013-07-30 19:05:20 -07001661bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001662 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001663 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001664 return true;
1665 }
1666
Chris Craik39a908c2013-06-13 14:39:01 -07001667 if (!isDeferred()) {
1668 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001669 }
Chris Craik39a908c2013-06-13 14:39:01 -07001670 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001671}
1672
Romain Guy8ce00302013-01-15 18:51:42 -08001673void OpenGLRenderer::debugClip() {
1674#if DEBUG_CLIP_REGIONS
1675 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1676 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1677 }
1678#endif
1679}
1680
Romain Guy079ba2c2010-07-16 14:12:24 -07001681bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001682 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001683 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1684 if (clipped) {
1685 dirtyClip();
1686 }
1687 return !mSnapshot->clipRect->isEmpty();
1688 }
1689
1690 SkPath path;
1691 path.addRect(left, top, right, bottom);
1692
Romain Guyb7b93e02013-08-01 15:29:25 -07001693 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001694}
1695
1696bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1697 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001698 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001699
1700 SkPath transformed;
1701 path->transform(transform, &transformed);
1702
1703 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001704 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1705 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001706 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001707 if (mSnapshot->previous == mFirstSnapshot) {
1708 clip.setRect(0, 0, mWidth, mHeight);
1709 } else {
1710 Rect* bounds = mSnapshot->previous->clipRect;
1711 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1712 }
Romain Guy8ce00302013-01-15 18:51:42 -08001713 }
1714
1715 SkRegion region;
1716 region.setPath(transformed, clip);
1717
1718 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001719 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001720 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001721 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001722 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001723}
1724
Romain Guy735738c2012-12-03 12:34:51 -08001725bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001726 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1727 if (clipped) {
1728 dirtyClip();
1729 }
1730 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001731}
1732
Chet Haasea23eed82012-04-12 15:19:04 -07001733Rect* OpenGLRenderer::getClipRect() {
1734 return mSnapshot->clipRect;
1735}
1736
Romain Guyf6a11b82010-06-23 17:47:49 -07001737///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001738// Drawing commands
1739///////////////////////////////////////////////////////////////////////////////
1740
Romain Guy54be1cd2011-06-13 19:04:27 -07001741void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001742 // TODO: It would be best if we could do this before quickReject()
1743 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001744 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001745 // Make sure setScissor & setStencil happen at the beginning of
1746 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001747 if (mDirtyClip) {
1748 if (mCaches.scissorEnabled) {
1749 setScissorFromClip();
1750 }
Romain Guy8ce00302013-01-15 18:51:42 -08001751 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001752 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001753
Romain Guy70ca14e2010-12-13 18:24:33 -08001754 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001755
Romain Guy70ca14e2010-12-13 18:24:33 -08001756 mSetShaderColor = false;
1757 mColorSet = false;
1758 mColorA = mColorR = mColorG = mColorB = 0.0f;
1759 mTextureUnit = 0;
1760 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001761
1762 // Enable debug highlight when what we're about to draw is tested against
1763 // the stencil buffer and if stencil highlight debugging is on
1764 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1765 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1766 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001767
1768 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001769}
1770
1771void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1772 mDescription.hasTexture = true;
1773 mDescription.hasAlpha8Texture = isAlpha8;
1774}
1775
Romain Guyff316ec2013-02-13 18:39:43 -08001776void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1777 mDescription.hasTexture = true;
1778 mDescription.hasColors = true;
1779 mDescription.hasAlpha8Texture = isAlpha8;
1780}
1781
Romain Guyaa6c24c2011-04-28 18:40:04 -07001782void OpenGLRenderer::setupDrawWithExternalTexture() {
1783 mDescription.hasExternalTexture = true;
1784}
1785
Romain Guy15bc6432011-12-13 13:11:32 -08001786void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001787 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001788}
1789
Chris Craik710f46d2012-09-17 17:25:49 -07001790void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001791 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001792}
1793
Romain Guy8d0d4782010-12-14 20:13:35 -08001794void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1795 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001796 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1797 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1798 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001799 mColorSet = true;
1800 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1801}
1802
Romain Guy86568192010-12-14 15:55:39 -08001803void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1804 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001805 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1806 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1807 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001808 mColorSet = true;
1809 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1810}
1811
Romain Guy41210632012-07-16 17:04:24 -07001812void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1813 mCaches.fontRenderer->describe(mDescription, paint);
1814}
1815
Romain Guy70ca14e2010-12-13 18:24:33 -08001816void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1817 mColorA = a;
1818 mColorR = r;
1819 mColorG = g;
1820 mColorB = b;
1821 mColorSet = true;
1822 mSetShaderColor = mDescription.setColor(r, g, b, a);
1823}
1824
1825void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001826 if (mDrawModifiers.mShader) {
1827 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001828 }
1829}
1830
1831void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001832 if (mDrawModifiers.mColorFilter) {
1833 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001834 }
1835}
1836
Romain Guyf09ef512011-05-27 11:43:46 -07001837void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1838 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1839 mColorA = 1.0f;
1840 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001841 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001842 }
1843}
1844
Romain Guy70ca14e2010-12-13 18:24:33 -08001845void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001846 // When the blending mode is kClear_Mode, we need to use a modulate color
1847 // argb=1,0,0,0
1848 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001849 bool blend = (mColorSet && mColorA < 1.0f) ||
1850 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1851 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001852}
1853
1854void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001855 // When the blending mode is kClear_Mode, we need to use a modulate color
1856 // argb=1,0,0,0
1857 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001858 blend |= (mColorSet && mColorA < 1.0f) ||
1859 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1860 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1861 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001862}
1863
1864void OpenGLRenderer::setupDrawProgram() {
1865 useProgram(mCaches.programCache.get(mDescription));
1866}
1867
1868void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1869 mTrackDirtyRegions = false;
1870}
1871
1872void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1873 bool ignoreTransform) {
1874 mModelView.loadTranslate(left, top, 0.0f);
1875 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001876 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1877 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001878 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001879 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001880 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1881 }
1882}
1883
Chet Haase8a5cc922011-04-26 07:28:09 -07001884void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001885 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001886}
1887
Romain Guy70ca14e2010-12-13 18:24:33 -08001888void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1889 bool ignoreTransform, bool ignoreModelView) {
1890 if (!ignoreModelView) {
1891 mModelView.loadTranslate(left, top, 0.0f);
1892 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001893 } else {
1894 mModelView.loadIdentity();
1895 }
Romain Guy86568192010-12-14 15:55:39 -08001896 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1897 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001898 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001899 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001900 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001901 }
1902 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001903 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001904 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1905 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001906}
1907
1908void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001909 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001910 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1911 }
1912}
1913
Romain Guy86568192010-12-14 15:55:39 -08001914void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001915 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001916 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001917 }
1918}
1919
Romain Guy70ca14e2010-12-13 18:24:33 -08001920void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001921 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001922 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001923 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001924 }
Chris Craikc3566d02013-02-04 16:16:33 -08001925 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1926 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001927 }
1928}
1929
Romain Guy8d0d4782010-12-14 20:13:35 -08001930void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001931 if (mDrawModifiers.mShader) {
1932 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001933 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001934 }
1935}
1936
Romain Guy70ca14e2010-12-13 18:24:33 -08001937void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001938 if (mDrawModifiers.mColorFilter) {
1939 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001940 }
1941}
1942
Romain Guy41210632012-07-16 17:04:24 -07001943void OpenGLRenderer::setupDrawTextGammaUniforms() {
1944 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1945}
1946
Romain Guy70ca14e2010-12-13 18:24:33 -08001947void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001948 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001949 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001950 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001951}
1952
1953void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001954 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001955 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001956 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001957}
1958
Romain Guyaa6c24c2011-04-28 18:40:04 -07001959void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1960 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001961 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001962 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001963}
1964
Romain Guy8f0095c2011-05-02 17:24:22 -07001965void OpenGLRenderer::setupDrawTextureTransform() {
1966 mDescription.hasTextureTransform = true;
1967}
1968
1969void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001970 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1971 GL_FALSE, &transform.data[0]);
1972}
1973
Romain Guy70ca14e2010-12-13 18:24:33 -08001974void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001975 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001976 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001977 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001978 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001979 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001980 }
Romain Guyd71dd362011-12-12 19:03:35 -08001981
Chris Craikcb4d6002012-09-25 12:00:29 -07001982 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001983 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001984 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001985 }
1986
1987 mCaches.unbindIndicesBuffer();
1988}
1989
Romain Guyff316ec2013-02-13 18:39:43 -08001990void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1991 bool force = mCaches.unbindMeshBuffer();
1992 GLsizei stride = sizeof(ColorTextureVertex);
1993
1994 mCaches.bindPositionVertexPointer(force, vertices, stride);
1995 if (mCaches.currentProgram->texCoords >= 0) {
1996 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1997 }
1998 int slot = mCaches.currentProgram->getAttrib("colors");
1999 if (slot >= 0) {
2000 glEnableVertexAttribArray(slot);
2001 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2002 }
2003
2004 mCaches.unbindIndicesBuffer();
2005}
2006
Romain Guy3b748a42013-04-17 18:54:38 -07002007void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2008 bool force = false;
2009 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2010 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2011 // use the default VBO found in Caches
2012 if (!vertices || vbo) {
2013 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2014 } else {
2015 force = mCaches.unbindMeshBuffer();
2016 }
2017 mCaches.bindIndicesBuffer();
2018
Chris Craikcb4d6002012-09-25 12:00:29 -07002019 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002020 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002021 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002022 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002023}
2024
Romain Guy448455f2013-07-22 13:57:50 -07002025void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002026 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002027 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002028 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002029}
2030
Romain Guy70ca14e2010-12-13 18:24:33 -08002031///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002032// Drawing
2033///////////////////////////////////////////////////////////////////////////////
2034
Chris Craikff785832013-03-08 13:12:16 -08002035status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2036 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002037 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002038 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2039 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002040 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002041 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002042 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002043 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2044 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002045 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002046 }
2047
Chris Craik28ce94a2013-05-31 11:38:03 -07002048 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2049 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002050 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2051 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002052
2053 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002054 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002055
Chet Haase58d110a2013-04-10 07:43:29 -07002056 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002057 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002058
Romain Guy65549432012-03-26 16:45:05 -07002059 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002060}
2061
Chris Craikc3566d02013-02-04 16:16:33 -08002062void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002063 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002064 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002065 }
2066}
2067
Romain Guya168d732011-03-18 16:50:13 -07002068void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2069 int alpha;
2070 SkXfermode::Mode mode;
2071 getAlphaAndMode(paint, &alpha, &mode);
2072
Romain Guy886b2752013-01-04 12:26:18 -08002073 int color = paint != NULL ? paint->getColor() : 0;
2074
Romain Guya168d732011-03-18 16:50:13 -07002075 float x = left;
2076 float y = top;
2077
Romain Guy886b2752013-01-04 12:26:18 -08002078 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2079
Romain Guya168d732011-03-18 16:50:13 -07002080 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002081 if (currentTransform().isPureTranslate()) {
2082 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2083 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002084 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002085
2086 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002087 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002088 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002089 }
2090
Romain Guy3b748a42013-04-17 18:54:38 -07002091 // No need to check for a UV mapper on the texture object, only ARGB_8888
2092 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002093 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002094 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2095 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002096}
2097
Romain Guy03c00b52013-06-20 18:30:28 -07002098/**
2099 * Important note: this method is intended to draw batches of bitmaps and
2100 * will not set the scissor enable or dirty the current layer, if any.
2101 * The caller is responsible for properly dirtying the current layer.
2102 */
Romain Guy55b6f952013-06-27 15:27:09 -07002103status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2104 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002105 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002106 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002107 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002108
Chris Craik527a3aa2013-03-04 10:19:31 -08002109 const AutoTexture autoCleanup(texture);
2110
2111 int alpha;
2112 SkXfermode::Mode mode;
2113 getAlphaAndMode(paint, &alpha, &mode);
2114
2115 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002116 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002117
2118 const float x = (int) floorf(bounds.left + 0.5f);
2119 const float y = (int) floorf(bounds.top + 0.5f);
2120 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2121 int color = paint != NULL ? paint->getColor() : 0;
2122 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2123 texture->id, paint != NULL, color, alpha, mode,
2124 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002125 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002126 } else {
2127 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2128 texture->id, alpha / 255.0f, mode, texture->blend,
2129 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002130 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002131 }
2132
2133 return DrawGlInfo::kStatusDrew;
2134}
2135
Chet Haase48659092012-05-31 15:21:51 -07002136status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002137 const float right = left + bitmap->width();
2138 const float bottom = top + bitmap->height();
2139
2140 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002141 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002142 }
2143
Romain Guya1d3c912011-12-13 14:55:06 -08002144 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002145 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002146 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002147 const AutoTexture autoCleanup(texture);
2148
Romain Guy211370f2012-02-01 16:10:55 -08002149 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002150 drawAlphaBitmap(texture, left, top, paint);
2151 } else {
2152 drawTextureRect(left, top, right, bottom, texture, paint);
2153 }
Chet Haase48659092012-05-31 15:21:51 -07002154
2155 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002156}
2157
Chet Haase48659092012-05-31 15:21:51 -07002158status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002159 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2160 const mat4 transform(*matrix);
2161 transform.mapRect(r);
2162
Romain Guy6926c722010-07-12 20:20:03 -07002163 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002164 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002165 }
2166
Romain Guya1d3c912011-12-13 14:55:06 -08002167 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002168 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002169 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002170 const AutoTexture autoCleanup(texture);
2171
Romain Guy5b3b3522010-10-27 18:57:51 -07002172 // This could be done in a cheaper way, all we need is pass the matrix
2173 // to the vertex shader. The save/restore is a bit overkill.
2174 save(SkCanvas::kMatrix_SaveFlag);
2175 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002176 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2177 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2178 } else {
2179 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2180 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002181 restore();
Chet Haase48659092012-05-31 15:21:51 -07002182
2183 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002184}
2185
Chet Haase48659092012-05-31 15:21:51 -07002186status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002187 const float right = left + bitmap->width();
2188 const float bottom = top + bitmap->height();
2189
2190 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002191 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002192 }
2193
2194 mCaches.activeTexture(0);
2195 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2196 const AutoTexture autoCleanup(texture);
2197
Romain Guy886b2752013-01-04 12:26:18 -08002198 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2199 drawAlphaBitmap(texture, left, top, paint);
2200 } else {
2201 drawTextureRect(left, top, right, bottom, texture, paint);
2202 }
Chet Haase48659092012-05-31 15:21:51 -07002203
2204 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002205}
2206
Chet Haase48659092012-05-31 15:21:51 -07002207status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002208 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002209 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002210 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002211 }
2212
Chris Craik39a908c2013-06-13 14:39:01 -07002213 // TODO: use quickReject on bounds from vertices
2214 mCaches.enableScissor();
2215
Romain Guyb18d2d02011-02-10 15:52:54 -08002216 float left = FLT_MAX;
2217 float top = FLT_MAX;
2218 float right = FLT_MIN;
2219 float bottom = FLT_MIN;
2220
Romain Guya92bb4d2012-10-16 11:08:44 -07002221 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002222
Romain Guyff316ec2013-02-13 18:39:43 -08002223 ColorTextureVertex mesh[count];
2224 ColorTextureVertex* vertex = mesh;
2225
2226 bool cleanupColors = false;
2227 if (!colors) {
2228 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2229 colors = new int[colorsCount];
2230 memset(colors, 0xff, colorsCount * sizeof(int));
2231 cleanupColors = true;
2232 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002233
Romain Guy3b748a42013-04-17 18:54:38 -07002234 mCaches.activeTexture(0);
2235 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2236 const UvMapper& mapper(getMapper(texture));
2237
Romain Guy5a7b4662011-01-20 19:09:30 -08002238 for (int32_t y = 0; y < meshHeight; y++) {
2239 for (int32_t x = 0; x < meshWidth; x++) {
2240 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2241
2242 float u1 = float(x) / meshWidth;
2243 float u2 = float(x + 1) / meshWidth;
2244 float v1 = float(y) / meshHeight;
2245 float v2 = float(y + 1) / meshHeight;
2246
Romain Guy3b748a42013-04-17 18:54:38 -07002247 mapper.map(u1, v1, u2, v2);
2248
Romain Guy5a7b4662011-01-20 19:09:30 -08002249 int ax = i + (meshWidth + 1) * 2;
2250 int ay = ax + 1;
2251 int bx = i;
2252 int by = bx + 1;
2253 int cx = i + 2;
2254 int cy = cx + 1;
2255 int dx = i + (meshWidth + 1) * 2 + 2;
2256 int dy = dx + 1;
2257
Romain Guyff316ec2013-02-13 18:39:43 -08002258 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2259 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2260 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002261
Romain Guyff316ec2013-02-13 18:39:43 -08002262 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2263 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2264 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002265
Romain Guya92bb4d2012-10-16 11:08:44 -07002266 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2267 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2268 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2269 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002270 }
2271 }
2272
Romain Guya92bb4d2012-10-16 11:08:44 -07002273 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002274 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002275 return DrawGlInfo::kStatusDone;
2276 }
2277
Romain Guyff316ec2013-02-13 18:39:43 -08002278 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002279 texture = mCaches.textureCache.get(bitmap);
2280 if (!texture) {
2281 if (cleanupColors) delete[] colors;
2282 return DrawGlInfo::kStatusDone;
2283 }
Romain Guyff316ec2013-02-13 18:39:43 -08002284 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002285 const AutoTexture autoCleanup(texture);
2286
2287 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2288 texture->setFilter(FILTER(paint), true);
2289
2290 int alpha;
2291 SkXfermode::Mode mode;
2292 getAlphaAndMode(paint, &alpha, &mode);
2293
Romain Guyff316ec2013-02-13 18:39:43 -08002294 float a = alpha / 255.0f;
2295
Romain Guya92bb4d2012-10-16 11:08:44 -07002296 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002297 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002298 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002299
Romain Guyff316ec2013-02-13 18:39:43 -08002300 setupDraw();
2301 setupDrawWithTextureAndColor();
2302 setupDrawColor(a, a, a, a);
2303 setupDrawColorFilter();
2304 setupDrawBlending(true, mode, false);
2305 setupDrawProgram();
2306 setupDrawDirtyRegionsDisabled();
2307 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2308 setupDrawTexture(texture->id);
2309 setupDrawPureColorUniforms();
2310 setupDrawColorFilterUniforms();
2311 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2312
2313 glDrawArrays(GL_TRIANGLES, 0, count);
2314
Romain Guyff316ec2013-02-13 18:39:43 -08002315 int slot = mCaches.currentProgram->getAttrib("colors");
2316 if (slot >= 0) {
2317 glDisableVertexAttribArray(slot);
2318 }
2319
2320 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002321
2322 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002323}
2324
Chet Haase48659092012-05-31 15:21:51 -07002325status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002326 float srcLeft, float srcTop, float srcRight, float srcBottom,
2327 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002328 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002329 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002330 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002331 }
2332
Romain Guya1d3c912011-12-13 14:55:06 -08002333 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002334 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002335 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002336 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002337
Romain Guy8ba548f2010-06-30 19:21:21 -07002338 const float width = texture->width;
2339 const float height = texture->height;
2340
Romain Guy3b748a42013-04-17 18:54:38 -07002341 float u1 = fmax(0.0f, srcLeft / width);
2342 float v1 = fmax(0.0f, srcTop / height);
2343 float u2 = fmin(1.0f, srcRight / width);
2344 float v2 = fmin(1.0f, srcBottom / height);
2345
2346 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002347
Romain Guy03750a02010-10-18 14:06:08 -07002348 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002349 resetDrawTextureTexCoords(u1, v1, u2, v2);
2350
Romain Guy03750a02010-10-18 14:06:08 -07002351 int alpha;
2352 SkXfermode::Mode mode;
2353 getAlphaAndMode(paint, &alpha, &mode);
2354
Romain Guyd21b6e12011-11-30 20:21:23 -08002355 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2356
Romain Guy886b2752013-01-04 12:26:18 -08002357 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2358 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002359
Romain Guy886b2752013-01-04 12:26:18 -08002360 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2361 // Apply a scale transform on the canvas only when a shader is in use
2362 // Skia handles the ratio between the dst and src rects as a scale factor
2363 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002364 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002365 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002366
Romain Guy3b753822013-03-05 10:27:35 -08002367 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2368 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2369 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002370
2371 dstRight = x + (dstRight - dstLeft);
2372 dstBottom = y + (dstBottom - dstTop);
2373
2374 dstLeft = x;
2375 dstTop = y;
2376
2377 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2378 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002379 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002380 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002381 }
2382
2383 if (CC_UNLIKELY(useScaleTransform)) {
2384 save(SkCanvas::kMatrix_SaveFlag);
2385 translate(dstLeft, dstTop);
2386 scale(scaleX, scaleY);
2387
2388 dstLeft = 0.0f;
2389 dstTop = 0.0f;
2390
2391 dstRight = srcRight - srcLeft;
2392 dstBottom = srcBottom - srcTop;
2393 }
2394
2395 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2396 int color = paint ? paint->getColor() : 0;
2397 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2398 texture->id, paint != NULL, color, alpha, mode,
2399 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2400 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2401 } else {
2402 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2403 texture->id, alpha / 255.0f, mode, texture->blend,
2404 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2405 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2406 }
2407
2408 if (CC_UNLIKELY(useScaleTransform)) {
2409 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002410 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002411
2412 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002413
2414 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002415}
2416
Romain Guy3b748a42013-04-17 18:54:38 -07002417status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002418 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002419 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002420 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002421 }
2422
Romain Guy4c2547f2013-06-11 16:19:24 -07002423 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002424 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2425 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002426
Romain Guy03c00b52013-06-20 18:30:28 -07002427 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002428}
2429
Romain Guy03c00b52013-06-20 18:30:28 -07002430status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2431 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002432 if (quickReject(left, top, right, bottom)) {
2433 return DrawGlInfo::kStatusDone;
2434 }
2435
Romain Guy211370f2012-02-01 16:10:55 -08002436 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002437 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002438 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002439 if (!texture) return DrawGlInfo::kStatusDone;
2440 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002441
Romain Guya4adcf02013-02-28 12:15:35 -08002442 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2443 texture->setFilter(GL_LINEAR, true);
2444
Romain Guy03c00b52013-06-20 18:30:28 -07002445 int alpha;
2446 SkXfermode::Mode mode;
2447 getAlphaAndMode(paint, &alpha, &mode);
2448
Romain Guy3b753822013-03-05 10:27:35 -08002449 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002450 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002451 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002452 const float offsetX = left + currentTransform().getTranslateX();
2453 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002454 const size_t count = mesh->quads.size();
2455 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002456 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002457 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002458 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2459 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2460 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002461 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002462 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002463 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002464 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002465 }
2466 }
2467
Romain Guy211370f2012-02-01 16:10:55 -08002468 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002469 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2470 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002471
Romain Guy3b748a42013-04-17 18:54:38 -07002472 right = x + right - left;
2473 bottom = y + bottom - top;
2474 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2475 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2476 GL_TRIANGLES, mesh->indexCount, false, true,
2477 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002478 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002479 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2480 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2481 GL_TRIANGLES, mesh->indexCount, false, false,
2482 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002483 }
Romain Guy054dc182010-10-15 17:55:25 -07002484 }
Chet Haase48659092012-05-31 15:21:51 -07002485
2486 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002487}
2488
Romain Guy03c00b52013-06-20 18:30:28 -07002489/**
2490 * Important note: this method is intended to draw batches of 9-patch objects and
2491 * will not set the scissor enable or dirty the current layer, if any.
2492 * The caller is responsible for properly dirtying the current layer.
2493 */
2494status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2495 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2496 mCaches.activeTexture(0);
2497 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2498 if (!texture) return DrawGlInfo::kStatusDone;
2499 const AutoTexture autoCleanup(texture);
2500
2501 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2502 texture->setFilter(GL_LINEAR, true);
2503
2504 int alpha;
2505 SkXfermode::Mode mode;
2506 getAlphaAndMode(paint, &alpha, &mode);
2507
2508 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2509 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2510 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2511
2512 return DrawGlInfo::kStatusDrew;
2513}
2514
Chris Craik65cd6122012-12-10 17:56:27 -08002515status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2516 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002517 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002518 // no vertices to draw
2519 return DrawGlInfo::kStatusDone;
2520 }
2521
Chris Craikcb4d6002012-09-25 12:00:29 -07002522 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002523 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2524 bool isAA = paint->isAntiAlias();
2525
Chris Craik710f46d2012-09-17 17:25:49 -07002526 setupDraw();
2527 setupDrawNoTexture();
2528 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002529 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2530 setupDrawColorFilter();
2531 setupDrawShader();
2532 setupDrawBlending(isAA, mode);
2533 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002534 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002535 setupDrawColorUniforms();
2536 setupDrawColorFilterUniforms();
2537 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002538
Chris Craik710f46d2012-09-17 17:25:49 -07002539 void* vertices = vertexBuffer.getBuffer();
2540 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002542 mCaches.resetTexCoordsVertexPointer();
2543 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002544
Chris Craik710f46d2012-09-17 17:25:49 -07002545 int alphaSlot = -1;
2546 if (isAA) {
2547 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2548 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002549
Chris Craik710f46d2012-09-17 17:25:49 -07002550 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002551 glEnableVertexAttribArray(alphaSlot);
2552 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002553 }
Romain Guy04299382012-07-18 17:15:41 -07002554
Chris Craik6d29c8d2013-05-08 18:35:44 -07002555 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002556
Chris Craik710f46d2012-09-17 17:25:49 -07002557 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002558 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002559 }
Chris Craik65cd6122012-12-10 17:56:27 -08002560
2561 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002562}
2563
2564/**
Chris Craik65cd6122012-12-10 17:56:27 -08002565 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2566 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2567 * screen space in all directions. However, instead of using a fragment shader to compute the
2568 * translucency of the color from its position, we simply use a varying parameter to define how far
2569 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2570 *
2571 * Doesn't yet support joins, caps, or path effects.
2572 */
2573status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2574 VertexBuffer vertexBuffer;
2575 // TODO: try clipping large paths to viewport
2576 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2577
Romain Guyc46d07a2013-03-15 19:06:39 -07002578 if (hasLayer()) {
2579 SkRect bounds = path.getBounds();
2580 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2581 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2582 }
Chris Craik65cd6122012-12-10 17:56:27 -08002583
2584 return drawVertexBuffer(vertexBuffer, paint);
2585}
2586
2587/**
2588 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2589 * and additional geometry for defining an alpha slope perimeter.
2590 *
2591 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2592 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2593 * in-shader alpha region, but found it to be taxing on some GPUs.
2594 *
2595 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2596 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002597 */
Chet Haase48659092012-05-31 15:21:51 -07002598status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002599 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002600
Chris Craik65cd6122012-12-10 17:56:27 -08002601 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002602
Chris Craik65cd6122012-12-10 17:56:27 -08002603 VertexBuffer buffer;
2604 SkRect bounds;
2605 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002606
2607 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2608 return DrawGlInfo::kStatusDone;
2609 }
2610
Romain Guy3b753822013-03-05 10:27:35 -08002611 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002612
Chris Craik65cd6122012-12-10 17:56:27 -08002613 bool useOffset = !paint->isAntiAlias();
2614 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002615}
2616
Chet Haase48659092012-05-31 15:21:51 -07002617status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002618 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002619
Chris Craik6d29c8d2013-05-08 18:35:44 -07002620 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002621
Chris Craik6d29c8d2013-05-08 18:35:44 -07002622 VertexBuffer buffer;
2623 SkRect bounds;
2624 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002625
Chris Craik6d29c8d2013-05-08 18:35:44 -07002626 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2627 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002628 }
2629
Chris Craik6d29c8d2013-05-08 18:35:44 -07002630 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002631
Chris Craik6d29c8d2013-05-08 18:35:44 -07002632 bool useOffset = !paint->isAntiAlias();
2633 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002634}
2635
Chet Haase48659092012-05-31 15:21:51 -07002636status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002637 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002638 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002639
Romain Guyae88e5e2010-10-22 17:49:18 -07002640 Rect& clip(*mSnapshot->clipRect);
2641 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002642
Romain Guy3d58c032010-07-14 16:34:53 -07002643 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002644
2645 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002646}
Romain Guy9d5316e2010-06-24 19:30:36 -07002647
Chet Haase48659092012-05-31 15:21:51 -07002648status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2649 SkPaint* paint) {
2650 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002651 const AutoTexture autoCleanup(texture);
2652
2653 const float x = left + texture->left - texture->offset;
2654 const float y = top + texture->top - texture->offset;
2655
2656 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002657
2658 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002659}
2660
Chet Haase48659092012-05-31 15:21:51 -07002661status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002662 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002663 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2664 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002665 return DrawGlInfo::kStatusDone;
2666 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002667
Chris Craikcb4d6002012-09-25 12:00:29 -07002668 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002669 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002670 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002671 right - left, bottom - top, rx, ry, p);
2672 return drawShape(left, top, texture, p);
2673 }
2674
2675 SkPath path;
2676 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002677 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2678 float outset = p->getStrokeWidth() / 2;
2679 rect.outset(outset, outset);
2680 rx += outset;
2681 ry += outset;
2682 }
Chris Craik710f46d2012-09-17 17:25:49 -07002683 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002684 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002685}
2686
Chris Craik710f46d2012-09-17 17:25:49 -07002687status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002688 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002689 x + radius, y + radius, p) ||
2690 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002691 return DrawGlInfo::kStatusDone;
2692 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002693 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002694 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002695 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002696 return drawShape(x - radius, y - radius, texture, p);
2697 }
2698
2699 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002700 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2701 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2702 } else {
2703 path.addCircle(x, y, radius);
2704 }
Chris Craik65cd6122012-12-10 17:56:27 -08002705 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002706}
Romain Guy01d58e42011-01-19 21:54:02 -08002707
Chet Haase48659092012-05-31 15:21:51 -07002708status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002709 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002710 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2711 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002712 return DrawGlInfo::kStatusDone;
2713 }
Romain Guy01d58e42011-01-19 21:54:02 -08002714
Chris Craikcb4d6002012-09-25 12:00:29 -07002715 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002716 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002717 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002718 return drawShape(left, top, texture, p);
2719 }
2720
2721 SkPath path;
2722 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002723 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2724 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2725 }
Chris Craik710f46d2012-09-17 17:25:49 -07002726 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002727 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002728}
2729
Chet Haase48659092012-05-31 15:21:51 -07002730status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002731 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002732 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2733 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002734 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002735 }
2736
Chris Craik780c1282012-10-04 14:10:49 -07002737 if (fabs(sweepAngle) >= 360.0f) {
2738 return drawOval(left, top, right, bottom, p);
2739 }
2740
2741 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002742 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002743 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002744 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002745 startAngle, sweepAngle, useCenter, p);
2746 return drawShape(left, top, texture, p);
2747 }
2748
2749 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2750 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2751 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2752 }
2753
2754 SkPath path;
2755 if (useCenter) {
2756 path.moveTo(rect.centerX(), rect.centerY());
2757 }
2758 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2759 if (useCenter) {
2760 path.close();
2761 }
Chris Craik65cd6122012-12-10 17:56:27 -08002762 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002763}
2764
Romain Guycf8675e2012-10-02 12:32:25 -07002765// See SkPaintDefaults.h
2766#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2767
Chet Haase48659092012-05-31 15:21:51 -07002768status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002769 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2770 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002771 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002772 }
2773
Chris Craik710f46d2012-09-17 17:25:49 -07002774 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002775 // only fill style is supported by drawConvexPath, since others have to handle joins
2776 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2777 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2778 mCaches.activeTexture(0);
2779 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002780 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002781 return drawShape(left, top, texture, p);
2782 }
2783
2784 SkPath path;
2785 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2786 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2787 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2788 }
2789 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002790 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002791 }
2792
Romain Guy3b753822013-03-05 10:27:35 -08002793 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002794 SkPath path;
2795 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002796 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002797 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002798 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002799 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002800 }
Romain Guyc7d53492010-06-25 13:41:57 -07002801}
Romain Guy9d5316e2010-06-24 19:30:36 -07002802
Raph Levien416a8472012-07-19 22:48:17 -07002803void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2804 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2805 float x, float y) {
2806 mCaches.activeTexture(0);
2807
2808 // NOTE: The drop shadow will not perform gamma correction
2809 // if shader-based correction is enabled
2810 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2811 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002812 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002813 // If the drop shadow exceeds the max texture size or couldn't be
2814 // allocated, skip drawing
2815 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002816 const AutoTexture autoCleanup(shadow);
2817
Chris Craikc3566d02013-02-04 16:16:33 -08002818 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2819 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002820
Chris Craikc3566d02013-02-04 16:16:33 -08002821 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2822 int shadowColor = mDrawModifiers.mShadowColor;
2823 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002824 shadowColor = 0xffffffff;
2825 }
2826
2827 setupDraw();
2828 setupDrawWithTexture(true);
2829 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2830 setupDrawColorFilter();
2831 setupDrawShader();
2832 setupDrawBlending(true, mode);
2833 setupDrawProgram();
2834 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2835 setupDrawTexture(shadow->id);
2836 setupDrawPureColorUniforms();
2837 setupDrawColorFilterUniforms();
2838 setupDrawShaderUniforms();
2839 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2840
2841 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2842}
2843
Romain Guy768bffc2013-02-27 13:50:45 -08002844bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2845 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2846 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2847}
2848
Chet Haase48659092012-05-31 15:21:51 -07002849status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002850 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002851 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002852 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002853 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002854
Romain Guy671d6cf2012-01-18 12:39:17 -08002855 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002856 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002857 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002858 }
2859
Chris Craik39a908c2013-06-13 14:39:01 -07002860 mCaches.enableScissor();
2861
Romain Guy671d6cf2012-01-18 12:39:17 -08002862 float x = 0.0f;
2863 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002864 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002865 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002866 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2867 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002868 }
2869
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002870 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002871 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002872
2873 int alpha;
2874 SkXfermode::Mode mode;
2875 getAlphaAndMode(paint, &alpha, &mode);
2876
Chris Craikc3566d02013-02-04 16:16:33 -08002877 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002878 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2879 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002880 }
2881
Romain Guy671d6cf2012-01-18 12:39:17 -08002882 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002883 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002884 if (pureTranslate && !linearFilter) {
2885 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2886 }
Romain Guy257ae352013-03-20 16:31:12 -07002887 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002888
2889 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2890 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2891
Romain Guy211370f2012-02-01 16:10:55 -08002892 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002893
Victoria Lease1e546812013-06-25 14:25:17 -07002894 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002895 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002896 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002897 if (hasActiveLayer) {
2898 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002899 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002900 }
2901 dirtyLayerUnchecked(bounds, getRegion());
2902 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 }
Chet Haase48659092012-05-31 15:21:51 -07002904
2905 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002906}
2907
Romain Guy624234f2013-03-05 16:43:31 -08002908mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2909 mat4 fontTransform;
2910 if (CC_LIKELY(transform.isPureTranslate())) {
2911 fontTransform = mat4::identity();
2912 } else {
2913 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002914 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002915 } else {
2916 float sx, sy;
2917 currentTransform().decomposeScale(sx, sy);
2918 fontTransform.loadScale(sx, sy, 1.0f);
2919 }
2920 }
2921 return fontTransform;
2922}
2923
Chris Craik41541822013-05-03 16:35:54 -07002924status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2925 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002926 DrawOpMode drawOpMode) {
2927
Chris Craik527a3aa2013-03-04 10:19:31 -08002928 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002929 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2930 // drawing as ops from DeferredDisplayList are already filtered for these
2931 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2932 quickReject(bounds)) {
2933 return DrawGlInfo::kStatusDone;
2934 }
Romain Guycac5fd32011-12-01 20:08:50 -08002935 }
2936
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002937 const float oldX = x;
2938 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002939
2940 const mat4& transform = currentTransform();
2941 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002942
Romain Guy211370f2012-02-01 16:10:55 -08002943 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002944 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2945 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002946 }
2947
Romain Guy86568192010-12-14 15:55:39 -08002948 int alpha;
2949 SkXfermode::Mode mode;
2950 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002951
Romain Guyc74f45a2013-02-26 19:10:14 -08002952 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2953
Chris Craikc3566d02013-02-04 16:16:33 -08002954 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002955 fontRenderer.setFont(paint, mat4::identity());
2956 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2957 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002958 }
2959
Romain Guy19d4dd82013-03-04 11:14:26 -08002960 const bool hasActiveLayer = hasLayer();
2961
Romain Guy624234f2013-03-05 16:43:31 -08002962 // We only pass a partial transform to the font renderer. That partial
2963 // matrix defines how glyphs are rasterized. Typically we want glyphs
2964 // to be rasterized at their final size on screen, which means the partial
2965 // matrix needs to take the scale factor into account.
2966 // When a partial matrix is used to transform glyphs during rasterization,
2967 // the mesh is generated with the inverse transform (in the case of scale,
2968 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2969 // apply the full transform matrix at draw time in the vertex shader.
2970 // Applying the full matrix in the shader is the easiest way to handle
2971 // rotation and perspective and allows us to always generated quads in the
2972 // font renderer which greatly simplifies the code, clipping in particular.
2973 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002974 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002975
Romain Guy6620c6d2010-12-06 18:07:02 -08002976 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002977 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002978 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002979
Romain Guy624234f2013-03-05 16:43:31 -08002980 // TODO: Implement better clipping for scaled/rotated text
2981 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002982 Rect layerBounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07002983
Raph Levien996e57c2012-07-23 15:22:52 -07002984 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002985 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002986
2987 // don't call issuedrawcommand, do it at end of batch
2988 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002989 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002990 SkPaint paintCopy(*paint);
2991 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2992 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002993 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002994 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002995 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002996 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002997 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002998
Chris Craik527a3aa2013-03-04 10:19:31 -08002999 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003000 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003001 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003002 }
Chris Craik41541822013-05-03 16:35:54 -07003003 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003004 }
Romain Guy694b5192010-07-21 21:33:20 -07003005
Chris Craik41541822013-05-03 16:35:54 -07003006 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003007
3008 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003009}
3010
Chet Haase48659092012-05-31 15:21:51 -07003011status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003012 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003013 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003014 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003015 }
3016
Chris Craik39a908c2013-06-13 14:39:01 -07003017 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3018 mCaches.enableScissor();
3019
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003020 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003021 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003022 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003023
3024 int alpha;
3025 SkXfermode::Mode mode;
3026 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003027 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003028
Romain Guy97771732012-02-28 18:17:02 -08003029 const Rect* clip = &mSnapshot->getLocalClip();
3030 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 -08003031
Romain Guy97771732012-02-28 18:17:02 -08003032 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003033
3034 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003035 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003036 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003037 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003038 dirtyLayerUnchecked(bounds, getRegion());
3039 }
Romain Guy97771732012-02-28 18:17:02 -08003040 }
Chet Haase48659092012-05-31 15:21:51 -07003041
3042 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003043}
3044
Chet Haase48659092012-05-31 15:21:51 -07003045status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3046 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003047
Romain Guya1d3c912011-12-13 14:55:06 -08003048 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003049
Romain Guyfb8b7632010-08-23 21:05:08 -07003050 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003051 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003052 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003053
Romain Guy8b55f372010-08-18 17:10:07 -07003054 const float x = texture->left - texture->offset;
3055 const float y = texture->top - texture->offset;
3056
Romain Guy01d58e42011-01-19 21:54:02 -08003057 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003058
3059 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003060}
3061
Chris Craika08f95c2013-03-15 17:24:33 -07003062status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003063 if (!layer) {
3064 return DrawGlInfo::kStatusDone;
3065 }
3066
Romain Guyb2e2f242012-10-17 18:18:35 -07003067 mat4* transform = NULL;
3068 if (layer->isTextureLayer()) {
3069 transform = &layer->getTransform();
3070 if (!transform->isIdentity()) {
3071 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003072 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003073 }
3074 }
3075
Chris Craik39a908c2013-06-13 14:39:01 -07003076 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003077 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003078 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003079
3080 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003081 if (transform && !transform->isIdentity()) {
3082 restore();
3083 }
Chet Haase48659092012-05-31 15:21:51 -07003084 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003085 }
3086
Romain Guy5bb3c732012-11-29 17:52:58 -08003087 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003088
Chris Craik39a908c2013-06-13 14:39:01 -07003089 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003090 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003091
Romain Guy211370f2012-02-01 16:10:55 -08003092 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003093 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3094 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003095
Romain Guyc88e3572011-01-22 00:32:12 -08003096 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003097 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3098 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003099 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003100 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003101 setupDraw();
3102 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003103 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003104 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003105 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003106 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003107 setupDrawPureColorUniforms();
3108 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003109 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003110 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3111 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3112 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003113
Romain Guyd21b6e12011-11-30 20:21:23 -08003114 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003115 setupDrawModelViewTranslate(tx, ty,
3116 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003117 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003118 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003119 setupDrawModelViewTranslate(x, y,
3120 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3121 }
Romain Guyf219da52011-01-16 12:54:25 -08003122
Romain Guy448455f2013-07-22 13:57:50 -07003123 TextureVertex* mesh = &layer->mesh[0];
3124 GLsizei elementsCount = layer->meshElementCount;
3125
3126 while (elementsCount > 0) {
3127 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3128
3129 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3130 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3131 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3132
3133 elementsCount -= drawCount;
3134 // Though there are 4 vertices in a quad, we use 6 indices per
3135 // quad to draw with GL_TRIANGLES
3136 mesh += (drawCount / 6) * 4;
3137 }
Romain Guyf219da52011-01-16 12:54:25 -08003138
Romain Guy3a3133d2011-02-01 22:59:58 -08003139#if DEBUG_LAYERS_AS_REGIONS
3140 drawRegionRects(layer->region);
3141#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003142 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003143
Chris Craikc3566d02013-02-04 16:16:33 -08003144 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003145
Romain Guy5bb3c732012-11-29 17:52:58 -08003146 if (layer->debugDrawUpdate) {
3147 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003148 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3149 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3150 }
Romain Guyf219da52011-01-16 12:54:25 -08003151 }
Chris Craik34416ea2013-04-15 16:08:28 -07003152 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003153
Romain Guyb2e2f242012-10-17 18:18:35 -07003154 if (transform && !transform->isIdentity()) {
3155 restore();
3156 }
3157
Chet Haase48659092012-05-31 15:21:51 -07003158 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003159}
3160
Romain Guy6926c722010-07-12 20:20:03 -07003161///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003162// Shaders
3163///////////////////////////////////////////////////////////////////////////////
3164
3165void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003166 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003167}
3168
Romain Guy06f96e22010-07-30 19:18:16 -07003169void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003170 mDrawModifiers.mShader = shader;
3171 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003172 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003173 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003174}
3175
Romain Guyd27977d2010-07-14 19:18:51 -07003176///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003177// Color filters
3178///////////////////////////////////////////////////////////////////////////////
3179
3180void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003181 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003182}
3183
3184void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003185 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003186}
3187
3188///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003189// Drop shadow
3190///////////////////////////////////////////////////////////////////////////////
3191
3192void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003193 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003194}
3195
3196void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003197 mDrawModifiers.mHasShadow = true;
3198 mDrawModifiers.mShadowRadius = radius;
3199 mDrawModifiers.mShadowDx = dx;
3200 mDrawModifiers.mShadowDy = dy;
3201 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003202}
3203
3204///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003205// Draw filters
3206///////////////////////////////////////////////////////////////////////////////
3207
3208void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003209 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3210 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003211 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003212 mDrawModifiers.mPaintFilterClearBits = 0;
3213 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003214}
3215
3216void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003217 mDrawModifiers.mHasDrawFilter = true;
3218 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3219 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003220}
3221
Chris Craika08f95c2013-03-15 17:24:33 -07003222SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003223 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003224 return paint;
3225 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003226
3227 uint32_t flags = paint->getFlags();
3228
3229 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003230 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3231 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003232
3233 return &mFilteredPaint;
3234}
3235
3236///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003237// Drawing implementation
3238///////////////////////////////////////////////////////////////////////////////
3239
Romain Guy3b748a42013-04-17 18:54:38 -07003240Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3241 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3242 if (!texture) {
3243 return mCaches.textureCache.get(bitmap);
3244 }
3245 return texture;
3246}
3247
Romain Guy01d58e42011-01-19 21:54:02 -08003248void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3249 float x, float y, SkPaint* paint) {
3250 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3251 return;
3252 }
3253
3254 int alpha;
3255 SkXfermode::Mode mode;
3256 getAlphaAndMode(paint, &alpha, &mode);
3257
3258 setupDraw();
3259 setupDrawWithTexture(true);
3260 setupDrawAlpha8Color(paint->getColor(), alpha);
3261 setupDrawColorFilter();
3262 setupDrawShader();
3263 setupDrawBlending(true, mode);
3264 setupDrawProgram();
3265 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3266 setupDrawTexture(texture->id);
3267 setupDrawPureColorUniforms();
3268 setupDrawColorFilterUniforms();
3269 setupDrawShaderUniforms();
3270 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3271
3272 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003273}
3274
Romain Guyf607bdc2010-09-10 19:20:06 -07003275// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003276#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3277#define kStdUnderline_Offset (1.0f / 9.0f)
3278#define kStdUnderline_Thickness (1.0f / 18.0f)
3279
Chris Craik41541822013-05-03 16:35:54 -07003280void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003281 float x, float y, SkPaint* paint) {
3282 // Handle underline and strike-through
3283 uint32_t flags = paint->getFlags();
3284 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003285 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003286
Romain Guy211370f2012-02-01 16:10:55 -08003287 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003288 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003289 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003290
Raph Levien8b4072d2012-07-30 15:50:00 -07003291 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003292 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003293
Romain Guyf6834472011-01-23 13:32:12 -08003294 int linesCount = 0;
3295 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3296 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3297
3298 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003299 float points[pointsCount];
3300 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003301
3302 if (flags & SkPaint::kUnderlineText_Flag) {
3303 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003304 points[currentPoint++] = left;
3305 points[currentPoint++] = top;
3306 points[currentPoint++] = left + underlineWidth;
3307 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003308 }
3309
3310 if (flags & SkPaint::kStrikeThruText_Flag) {
3311 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003312 points[currentPoint++] = left;
3313 points[currentPoint++] = top;
3314 points[currentPoint++] = left + underlineWidth;
3315 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003316 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003317
Romain Guy726aeba2011-06-01 14:52:00 -07003318 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003319
Romain Guy726aeba2011-06-01 14:52:00 -07003320 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003321 }
3322 }
3323}
3324
Romain Guy672433d2013-01-04 19:05:13 -08003325status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3326 if (mSnapshot->isIgnored()) {
3327 return DrawGlInfo::kStatusDone;
3328 }
3329
Romain Guy735738c2012-12-03 12:34:51 -08003330 int color = paint->getColor();
3331 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003332 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003333 color |= 0x00ffffff;
3334 }
3335 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3336
3337 return drawColorRects(rects, count, color, mode);
3338}
3339
3340status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003341 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003342 if (count == 0) {
3343 return DrawGlInfo::kStatusDone;
3344 }
Romain Guy735738c2012-12-03 12:34:51 -08003345
Romain Guy672433d2013-01-04 19:05:13 -08003346 float left = FLT_MAX;
3347 float top = FLT_MAX;
3348 float right = FLT_MIN;
3349 float bottom = FLT_MIN;
3350
Romain Guy448455f2013-07-22 13:57:50 -07003351 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003352 Vertex* vertex = mesh;
3353
Chris Craik2af46352012-11-26 18:30:17 -08003354 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003355 float l = rects[index + 0];
3356 float t = rects[index + 1];
3357 float r = rects[index + 2];
3358 float b = rects[index + 3];
3359
Romain Guy3b753822013-03-05 10:27:35 -08003360 Vertex::set(vertex++, l, t);
3361 Vertex::set(vertex++, r, t);
3362 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003363 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003364
Romain Guy3b753822013-03-05 10:27:35 -08003365 left = fminf(left, l);
3366 top = fminf(top, t);
3367 right = fmaxf(right, r);
3368 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003369 }
3370
Romain Guy3b753822013-03-05 10:27:35 -08003371 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003372 return DrawGlInfo::kStatusDone;
3373 }
Romain Guy672433d2013-01-04 19:05:13 -08003374
Romain Guy672433d2013-01-04 19:05:13 -08003375 setupDraw();
3376 setupDrawNoTexture();
3377 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3378 setupDrawShader();
3379 setupDrawColorFilter();
3380 setupDrawBlending(mode);
3381 setupDrawProgram();
3382 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003383 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003384 setupDrawColorUniforms();
3385 setupDrawShaderUniforms();
3386 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003387
Romain Guy8ce00302013-01-15 18:51:42 -08003388 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003389 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003390 }
3391
Romain Guy448455f2013-07-22 13:57:50 -07003392 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003393
3394 return DrawGlInfo::kStatusDrew;
3395}
3396
Romain Guy026c5e162010-06-28 17:12:22 -07003397void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003398 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003399 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003400 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003401 color |= 0x00ffffff;
3402 }
3403
Romain Guy70ca14e2010-12-13 18:24:33 -08003404 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003405 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003406 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003407 setupDrawShader();
3408 setupDrawColorFilter();
3409 setupDrawBlending(mode);
3410 setupDrawProgram();
3411 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3412 setupDrawColorUniforms();
3413 setupDrawShaderUniforms(ignoreTransform);
3414 setupDrawColorFilterUniforms();
3415 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003416
Romain Guyc95c8d62010-09-17 15:31:32 -07003417 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3418}
3419
Romain Guy82ba8142010-07-09 13:25:56 -07003420void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003421 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003422 int alpha;
3423 SkXfermode::Mode mode;
3424 getAlphaAndMode(paint, &alpha, &mode);
3425
Romain Guyd21b6e12011-11-30 20:21:23 -08003426 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003427
Romain Guy3b748a42013-04-17 18:54:38 -07003428 GLvoid* vertices = (GLvoid*) NULL;
3429 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3430
3431 if (texture->uvMapper) {
3432 vertices = &mMeshVertices[0].position[0];
3433 texCoords = &mMeshVertices[0].texture[0];
3434
3435 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3436 texture->uvMapper->map(uvs);
3437
3438 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3439 }
3440
Romain Guy3b753822013-03-05 10:27:35 -08003441 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3442 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3443 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003444
Romain Guyd21b6e12011-11-30 20:21:23 -08003445 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003446 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003447 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3448 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003449 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003450 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003451 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003452 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3453 }
3454
3455 if (texture->uvMapper) {
3456 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003457 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003458}
3459
Romain Guybd6b79b2010-06-26 00:13:53 -07003460void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003461 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3462 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003463 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003464}
3465
3466void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003467 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003468 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003469 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003470
Romain Guy746b7402010-10-26 16:27:31 -07003471 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003472 setupDrawWithTexture();
3473 setupDrawColor(alpha, alpha, alpha, alpha);
3474 setupDrawColorFilter();
3475 setupDrawBlending(blend, mode, swapSrcDst);
3476 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003477 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003478 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003479 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003480 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003481 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003482 }
Romain Guy886b2752013-01-04 12:26:18 -08003483 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003484 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003485 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003486 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003487
Romain Guy6820ac82010-09-15 18:11:50 -07003488 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003489}
3490
Romain Guy3b748a42013-04-17 18:54:38 -07003491void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3492 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3493 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3494 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3495
3496 setupDraw();
3497 setupDrawWithTexture();
3498 setupDrawColor(alpha, alpha, alpha, alpha);
3499 setupDrawColorFilter();
3500 setupDrawBlending(blend, mode, swapSrcDst);
3501 setupDrawProgram();
3502 if (!dirty) setupDrawDirtyRegionsDisabled();
3503 if (!ignoreScale) {
3504 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3505 } else {
3506 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3507 }
3508 setupDrawTexture(texture);
3509 setupDrawPureColorUniforms();
3510 setupDrawColorFilterUniforms();
3511 setupDrawMeshIndices(vertices, texCoords, vbo);
3512
3513 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003514}
3515
Romain Guy886b2752013-01-04 12:26:18 -08003516void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3517 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3518 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003519 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003520
3521 setupDraw();
3522 setupDrawWithTexture(true);
3523 if (hasColor) {
3524 setupDrawAlpha8Color(color, alpha);
3525 }
3526 setupDrawColorFilter();
3527 setupDrawShader();
3528 setupDrawBlending(true, mode);
3529 setupDrawProgram();
3530 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003531 if (!ignoreScale) {
3532 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3533 } else {
3534 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3535 }
Romain Guy886b2752013-01-04 12:26:18 -08003536 setupDrawTexture(texture);
3537 setupDrawPureColorUniforms();
3538 setupDrawColorFilterUniforms();
3539 setupDrawShaderUniforms();
3540 setupDrawMesh(vertices, texCoords);
3541
3542 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003543}
3544
Romain Guya5aed0d2010-09-09 14:42:43 -07003545void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003546 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003547 if (mCountOverdraw) {
3548 if (!mCaches.blend) glEnable(GL_BLEND);
3549 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3550 glBlendFunc(GL_ONE, GL_ONE);
3551 }
3552
3553 mCaches.blend = true;
3554 mCaches.lastSrcMode = GL_ONE;
3555 mCaches.lastDstMode = GL_ONE;
3556
3557 return;
3558 }
3559
Romain Guy82ba8142010-07-09 13:25:56 -07003560 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003561
Romain Guy82ba8142010-07-09 13:25:56 -07003562 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003563 // These blend modes are not supported by OpenGL directly and have
3564 // to be implemented using shaders. Since the shader will perform
3565 // the blending, turn blending off here
3566 // If the blend mode cannot be implemented using shaders, fall
3567 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003568 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003569 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003570 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003571 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003572
Romain Guy82bc7a72012-01-03 14:13:39 -08003573 if (mCaches.blend) {
3574 glDisable(GL_BLEND);
3575 mCaches.blend = false;
3576 }
3577
3578 return;
3579 } else {
3580 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003581 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003582 }
3583
3584 if (!mCaches.blend) {
3585 glEnable(GL_BLEND);
3586 }
3587
3588 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3589 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3590
3591 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3592 glBlendFunc(sourceMode, destMode);
3593 mCaches.lastSrcMode = sourceMode;
3594 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003595 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003596 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003597 glDisable(GL_BLEND);
3598 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003599 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003600}
3601
Romain Guy889f8d12010-07-29 14:37:42 -07003602bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003603 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003604 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003605 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003606 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003607 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003608 }
Romain Guy6926c722010-07-12 20:20:03 -07003609 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003610}
3611
Romain Guy026c5e162010-06-28 17:12:22 -07003612void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003613 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003614 TextureVertex::setUV(v++, u1, v1);
3615 TextureVertex::setUV(v++, u2, v1);
3616 TextureVertex::setUV(v++, u1, v2);
3617 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003618}
3619
Chris Craik16ecda52013-03-29 10:59:59 -07003620void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003621 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003622 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3623 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003624 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003625 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003626 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003627}
3628
Chris Craik16ecda52013-03-29 10:59:59 -07003629float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3630 float alpha;
3631 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3632 alpha = mDrawModifiers.mOverrideLayerAlpha;
3633 } else {
3634 alpha = layer->getAlpha() / 255.0f;
3635 }
3636 return alpha * mSnapshot->alpha;
3637}
3638
Romain Guy9d5316e2010-06-24 19:30:36 -07003639}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003640}; // namespace android