blob: f2352bbfc794a58ed920a2f9dff5ecbc1471b225 [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
Chet Haasedaf98e92011-01-10 14:10:36 -0800452 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700453 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700454
Romain Guy8a4ac612012-07-17 17:32:48 -0700455 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800456 if (mDirtyClip) {
457 setScissorFromClip();
Chris Craikecca6da2013-07-16 13:27:18 -0700458 setStencilFromClip();
Romain Guyf90f8172011-01-25 22:53:24 -0800459 }
Romain Guyd643bb52011-03-01 14:55:21 -0800460
Romain Guy80911b82011-03-16 15:30:12 -0700461 Rect clip(*mSnapshot->clipRect);
462 clip.snapToPixelBoundaries();
463
Romain Guyd643bb52011-03-01 14:55:21 -0800464 // Since we don't know what the functor will draw, let's dirty
465 // tne entire clip region
466 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800467 dirtyLayerUnchecked(clip, getRegion());
468 }
Romain Guyd643bb52011-03-01 14:55:21 -0800469
Romain Guy08aa2cb2011-03-17 11:06:57 -0700470 DrawGlInfo info;
471 info.clipLeft = clip.left;
472 info.clipTop = clip.top;
473 info.clipRight = clip.right;
474 info.clipBottom = clip.bottom;
475 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700476 info.width = getSnapshot()->viewport.getWidth();
477 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700478 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700479
Chris Craik4a2bff72013-04-16 13:50:16 -0700480 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800481
Romain Guy8f3b8e32012-03-27 16:33:45 -0700482 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700483 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800484 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700485
Chris Craik65924a32012-04-05 17:52:11 -0700486 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700487 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700488 }
Romain Guycabfcc12011-03-07 18:06:46 -0800489 }
490
Chet Haasedaf98e92011-01-10 14:10:36 -0800491 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700492 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800493}
494
Romain Guyf6a11b82010-06-23 17:47:49 -0700495///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700496// Debug
497///////////////////////////////////////////////////////////////////////////////
498
Romain Guy0f667532013-03-01 14:31:04 -0800499void OpenGLRenderer::eventMark(const char* name) const {
500 mCaches.eventMark(0, name);
501}
502
Romain Guy87e2f7572012-09-24 11:37:12 -0700503void OpenGLRenderer::startMark(const char* name) const {
504 mCaches.startMark(0, name);
505}
506
507void OpenGLRenderer::endMark() const {
508 mCaches.endMark();
509}
510
511void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
512 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
513 if (clear) {
514 mCaches.disableScissor();
515 mCaches.stencil.clear();
516 }
517 if (enable) {
518 mCaches.stencil.enableDebugWrite();
519 } else {
520 mCaches.stencil.disable();
521 }
522 }
523}
524
525void OpenGLRenderer::renderOverdraw() {
526 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700527 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700528
529 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700530 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700531 clip->right - clip->left, clip->bottom - clip->top);
532
Romain Guy627c6fd2013-08-21 11:53:18 -0700533 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700534 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700535 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
536
537 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700538 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700539 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
540
541 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700542 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700543 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
544
545 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700546 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700547 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
548
Romain Guy87e2f7572012-09-24 11:37:12 -0700549 mCaches.stencil.disable();
550 }
551}
552
Romain Guy78dd96d2013-05-03 14:24:16 -0700553void OpenGLRenderer::countOverdraw() {
554 size_t count = mWidth * mHeight;
555 uint32_t* buffer = new uint32_t[count];
556 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
557
558 size_t total = 0;
559 for (size_t i = 0; i < count; i++) {
560 total += buffer[i] & 0xff;
561 }
562
563 mOverdraw = total / float(count);
564
565 delete[] buffer;
566}
567
Romain Guy87e2f7572012-09-24 11:37:12 -0700568///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700569// Layers
570///////////////////////////////////////////////////////////////////////////////
571
572bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700573 if (layer->deferredUpdateScheduled && layer->renderer &&
574 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700575 ATRACE_CALL();
576
Romain Guy11cb6422012-09-21 00:39:43 -0700577 Rect& dirty = layer->dirtyRect;
578
Romain Guy7c450aa2012-09-21 19:15:00 -0700579 if (inFrame) {
580 endTiling();
581 debugOverdraw(false, false);
582 }
Romain Guy11cb6422012-09-21 00:39:43 -0700583
Romain Guy96885eb2013-03-26 15:05:58 -0700584 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700585 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700586 } else {
587 layer->defer();
588 }
Romain Guy11cb6422012-09-21 00:39:43 -0700589
590 if (inFrame) {
591 resumeAfterLayer();
592 startTiling(mSnapshot);
593 }
594
Romain Guy5bb3c732012-11-29 17:52:58 -0800595 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700596 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700597
598 return true;
599 }
600
601 return false;
602}
603
604void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700605 // If draw deferring is enabled this method will simply defer
606 // the display list of each individual layer. The layers remain
607 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700608 int count = mLayerUpdates.size();
609 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700610 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
611 startMark("Layer Updates");
612 } else {
613 startMark("Defer Layer Updates");
614 }
Romain Guy11cb6422012-09-21 00:39:43 -0700615
Chris Craik1206b9b2013-04-04 14:46:24 -0700616 // Note: it is very important to update the layers in order
617 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700618 Layer* layer = mLayerUpdates.itemAt(i);
619 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700620 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
621 mCaches.resourceCache.decrementRefcount(layer);
622 }
Romain Guy11cb6422012-09-21 00:39:43 -0700623 }
Romain Guy11cb6422012-09-21 00:39:43 -0700624
Romain Guy96885eb2013-03-26 15:05:58 -0700625 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
626 mLayerUpdates.clear();
627 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
628 }
629 endMark();
630 }
631}
632
633void OpenGLRenderer::flushLayers() {
634 int count = mLayerUpdates.size();
635 if (count > 0) {
636 startMark("Apply Layer Updates");
637 char layerName[12];
638
Chris Craik1206b9b2013-04-04 14:46:24 -0700639 // Note: it is very important to update the layers in order
640 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700641 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700642 startMark(layerName);
643
Romain Guy40543602013-06-12 15:31:28 -0700644 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700645 Layer* layer = mLayerUpdates.itemAt(i);
646 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700647 ATRACE_END();
648
Romain Guy02b49b72013-03-29 12:37:16 -0700649 mCaches.resourceCache.decrementRefcount(layer);
650
Romain Guy96885eb2013-03-26 15:05:58 -0700651 endMark();
652 }
653
654 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700655 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700656
Romain Guy11cb6422012-09-21 00:39:43 -0700657 endMark();
658 }
659}
660
661void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
662 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700663 // Make sure we don't introduce duplicates.
664 // SortedVector would do this automatically but we need to respect
665 // the insertion order. The linear search is not an issue since
666 // this list is usually very short (typically one item, at most a few)
667 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
668 if (mLayerUpdates.itemAt(i) == layer) {
669 return;
670 }
671 }
Romain Guy11cb6422012-09-21 00:39:43 -0700672 mLayerUpdates.push_back(layer);
673 mCaches.resourceCache.incrementRefcount(layer);
674 }
675}
676
Romain Guye93482f2013-06-17 13:14:51 -0700677void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
678 if (layer) {
679 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
680 if (mLayerUpdates.itemAt(i) == layer) {
681 mLayerUpdates.removeAt(i);
682 mCaches.resourceCache.decrementRefcount(layer);
683 break;
684 }
685 }
686 }
687}
688
Romain Guy11cb6422012-09-21 00:39:43 -0700689void OpenGLRenderer::clearLayerUpdates() {
690 size_t count = mLayerUpdates.size();
691 if (count > 0) {
692 mCaches.resourceCache.lock();
693 for (size_t i = 0; i < count; i++) {
694 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
695 }
696 mCaches.resourceCache.unlock();
697 mLayerUpdates.clear();
698 }
699}
700
Romain Guy40543602013-06-12 15:31:28 -0700701void OpenGLRenderer::flushLayerUpdates() {
702 syncState();
703 updateLayers();
704 flushLayers();
705 // Wait for all the layer updates to be executed
706 AutoFence fence;
707}
708
Romain Guy11cb6422012-09-21 00:39:43 -0700709///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700710// State management
711///////////////////////////////////////////////////////////////////////////////
712
Romain Guybb9524b2010-06-22 18:56:38 -0700713int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700714 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700715}
716
717int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700718 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700719}
720
721void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700722 if (mSaveCount > 1) {
723 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700724 }
Romain Guybb9524b2010-06-22 18:56:38 -0700725}
726
727void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700728 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700729
Romain Guy8fb95422010-08-17 18:38:51 -0700730 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700731 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700732 }
Romain Guybb9524b2010-06-22 18:56:38 -0700733}
734
Romain Guy8aef54f2010-09-01 15:13:49 -0700735int OpenGLRenderer::saveSnapshot(int flags) {
736 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700737 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
740bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700741 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700742 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700743 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700744
Romain Guybd6b79b2010-06-26 00:13:53 -0700745 sp<Snapshot> current = mSnapshot;
746 sp<Snapshot> previous = mSnapshot->previous;
747
Romain Guyeb993562010-10-05 18:14:38 -0700748 if (restoreOrtho) {
749 Rect& r = previous->viewport;
750 glViewport(r.left, r.top, r.right, r.bottom);
751 mOrthoMatrix.load(current->orthoMatrix);
752 }
753
Romain Guy8b55f372010-08-18 17:10:07 -0700754 mSaveCount--;
755 mSnapshot = previous;
756
Romain Guy2542d192010-08-18 11:47:12 -0700757 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700758 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700759 }
Romain Guy2542d192010-08-18 11:47:12 -0700760
Romain Guy5ec99242010-11-03 16:19:08 -0700761 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700762 endMark(); // Savelayer
763 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700764 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700765 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700766 }
767
Romain Guy2542d192010-08-18 11:47:12 -0700768 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700769}
770
Romain Guyf6a11b82010-06-23 17:47:49 -0700771///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700772// Layers
773///////////////////////////////////////////////////////////////////////////////
774
775int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800776 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700777 const GLuint previousFbo = mSnapshot->fbo;
778 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700779
Romain Guyaf636eb2010-12-09 17:47:21 -0800780 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700781 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700782 }
Romain Guyd55a8612010-06-28 17:42:46 -0700783
784 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700785}
786
Chris Craikd90144d2013-03-19 15:03:48 -0700787void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
788 const Rect untransformedBounds(bounds);
789
790 currentTransform().mapRect(bounds);
791
792 // Layers only make sense if they are in the framebuffer's bounds
793 if (bounds.intersect(*mSnapshot->clipRect)) {
794 // We cannot work with sub-pixels in this case
795 bounds.snapToPixelBoundaries();
796
797 // When the layer is not an FBO, we may use glCopyTexImage so we
798 // need to make sure the layer does not extend outside the bounds
799 // of the framebuffer
800 if (!bounds.intersect(mSnapshot->previous->viewport)) {
801 bounds.setEmpty();
802 } else if (fboLayer) {
803 clip.set(bounds);
804 mat4 inverse;
805 inverse.loadInverse(currentTransform());
806 inverse.mapRect(clip);
807 clip.snapToPixelBoundaries();
808 if (clip.intersect(untransformedBounds)) {
809 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
810 bounds.set(untransformedBounds);
811 } else {
812 clip.setEmpty();
813 }
814 }
815 } else {
816 bounds.setEmpty();
817 }
818}
819
Chris Craik408eb122013-03-26 18:55:15 -0700820void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
821 bool fboLayer, int alpha) {
822 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
823 bounds.getHeight() > mCaches.maxTextureSize ||
824 (fboLayer && clip.isEmpty())) {
825 mSnapshot->empty = fboLayer;
826 } else {
827 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
828 }
829}
830
Chris Craikd90144d2013-03-19 15:03:48 -0700831int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
832 int alpha, SkXfermode::Mode mode, int flags) {
833 const GLuint previousFbo = mSnapshot->fbo;
834 const int count = saveSnapshot(flags);
835
836 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
837 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
838 // operations will be able to store and restore the current clip and transform info, and
839 // quick rejection will be correct (for display lists)
840
841 Rect bounds(left, top, right, bottom);
842 Rect clip;
843 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700844 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700845
Chris Craik408eb122013-03-26 18:55:15 -0700846 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700847 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
848 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700849 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700850 }
851 }
852
853 return count;
854}
855
856
Romain Guy1c740bc2010-09-13 18:00:09 -0700857/**
858 * Layers are viewed by Skia are slightly different than layers in image editing
859 * programs (for instance.) When a layer is created, previously created layers
860 * and the frame buffer still receive every drawing command. For instance, if a
861 * layer is created and a shape intersecting the bounds of the layers and the
862 * framebuffer is draw, the shape will be drawn on both (unless the layer was
863 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
864 *
865 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
866 * texture. Unfortunately, this is inefficient as it requires every primitive to
867 * be drawn n + 1 times, where n is the number of active layers. In practice this
868 * means, for every primitive:
869 * - Switch active frame buffer
870 * - Change viewport, clip and projection matrix
871 * - Issue the drawing
872 *
873 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700874 * To avoid this, layers are implemented in a different way here, at least in the
875 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
876 * is set. When this flag is set we can redirect all drawing operations into a
877 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700878 *
879 * This implementation relies on the frame buffer being at least RGBA 8888. When
880 * a layer is created, only a texture is created, not an FBO. The content of the
881 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700882 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700883 * buffer and drawing continues as normal. This technique therefore treats the
884 * frame buffer as a scratch buffer for the layers.
885 *
886 * To compose the layers back onto the frame buffer, each layer texture
887 * (containing the original frame buffer data) is drawn as a simple quad over
888 * the frame buffer. The trick is that the quad is set as the composition
889 * destination in the blending equation, and the frame buffer becomes the source
890 * of the composition.
891 *
892 * Drawing layers with an alpha value requires an extra step before composition.
893 * An empty quad is drawn over the layer's region in the frame buffer. This quad
894 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
895 * quad is used to multiply the colors in the frame buffer. This is achieved by
896 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
897 * GL_ZERO, GL_SRC_ALPHA.
898 *
899 * Because glCopyTexImage2D() can be slow, an alternative implementation might
900 * be use to draw a single clipped layer. The implementation described above
901 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700902 *
903 * (1) The frame buffer is actually not cleared right away. To allow the GPU
904 * to potentially optimize series of calls to glCopyTexImage2D, the frame
905 * buffer is left untouched until the first drawing operation. Only when
906 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700907 */
Chet Haased48885a2012-08-28 17:43:28 -0700908bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
909 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700910 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700911 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700912
Romain Guyeb993562010-10-05 18:14:38 -0700913 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
914
Romain Guyf607bdc2010-09-10 19:20:06 -0700915 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700916 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700917 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700918 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700919 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700920
921 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700922 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700923 return false;
924 }
Romain Guyf18fd992010-07-08 11:45:51 -0700925
Romain Guya1d3c912011-12-13 14:55:06 -0800926 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700927 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700928 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700929 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700930 }
931
Romain Guy9ace8f52011-07-07 20:50:11 -0700932 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700933 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700934 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
935 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800936 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700937 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700938 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700939
Romain Guy8fb95422010-08-17 18:38:51 -0700940 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700941 mSnapshot->flags |= Snapshot::kFlagIsLayer;
942 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700943
Chris Craik7273daa2013-03-28 11:25:24 -0700944 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700945 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700946 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700947 } else {
948 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800950 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700952 // Workaround for some GL drivers. When reading pixels lying outside
953 // of the window we should get undefined values for those pixels.
954 // Unfortunately some drivers will turn the entire target texture black
955 // when reading outside of the window.
956 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
957 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700958 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800959 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700960
Romain Guyb254c242013-06-27 17:15:24 -0700961 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
962 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
963
Romain Guy54be1cd2011-06-13 19:04:27 -0700964 // Enqueue the buffer coordinates to clear the corresponding region later
965 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700966 }
Romain Guyeb993562010-10-05 18:14:38 -0700967 }
Romain Guyf86ef572010-07-01 11:05:42 -0700968
Romain Guyd55a8612010-06-28 17:42:46 -0700969 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700970}
971
Chet Haased48885a2012-08-28 17:43:28 -0700972bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800973 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700974 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700975
Chet Haased48885a2012-08-28 17:43:28 -0700976 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800977 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
978 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700979 mSnapshot->fbo = layer->getFbo();
980 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
981 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
982 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
983 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700984 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700985
Romain Guy2b7028e2012-09-19 17:25:38 -0700986 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700987 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700989 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
990 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700991
992 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700994 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700995 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700996 }
997
998 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700999 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001000
Romain Guyf735c8e2013-01-31 17:45:55 -08001001 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002
1003 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001004 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001005 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001006 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 glClear(GL_COLOR_BUFFER_BIT);
1008
1009 dirtyClip();
1010
1011 // Change the ortho projection
1012 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1013 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1014
1015 return true;
1016}
1017
Romain Guy1c740bc2010-09-13 18:00:09 -07001018/**
1019 * Read the documentation of createLayer() before doing anything in this method.
1020 */
Romain Guy1d83e192010-08-17 11:37:00 -07001021void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1022 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001023 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001024 return;
1025 }
1026
Romain Guy8ce00302013-01-15 18:51:42 -08001027 Layer* layer = current->layer;
1028 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001029 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001030
Chris Craik39a908c2013-06-13 14:39:01 -07001031 bool clipRequired = false;
1032 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1033 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1034
Romain Guyeb993562010-10-05 18:14:38 -07001035 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001036 endTiling();
1037
Romain Guye0aa84b2012-04-03 19:30:26 -07001038 // Detach the texture from the FBO
1039 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001040
1041 layer->removeFbo(false);
1042
Romain Guyeb993562010-10-05 18:14:38 -07001043 // Unbind current FBO and restore previous one
1044 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001045 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001046
1047 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001048 }
1049
Romain Guy9ace8f52011-07-07 20:50:11 -07001050 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001051 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001052 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001053 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001054 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001055 }
1056
Romain Guy03750a02010-10-18 14:06:08 -07001057 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001058
Romain Guya1d3c912011-12-13 14:55:06 -08001059 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001060
Romain Guy5b3b3522010-10-27 18:57:51 -07001061 // When the layer is stored in an FBO, we can save a bit of fillrate by
1062 // drawing only the dirty region
1063 if (fboLayer) {
1064 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001065 if (layer->getColorFilter()) {
1066 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001067 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001068 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001069 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001070 resetColorFilter();
1071 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 } else if (!rect.isEmpty()) {
1073 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1074 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001075 }
Romain Guy8b55f372010-08-18 17:10:07 -07001076
Romain Guy746b7402010-10-26 16:27:31 -07001077 dirtyClip();
1078
Romain Guyeb993562010-10-05 18:14:38 -07001079 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001080 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001081 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001082 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001083 }
1084}
1085
Romain Guyaa6c24c2011-04-28 18:40:04 -07001086void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001087 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001088
1089 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001090 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001091 setupDrawWithTexture();
1092 } else {
1093 setupDrawWithExternalTexture();
1094 }
1095 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001096 setupDrawColor(alpha, alpha, alpha, alpha);
1097 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001098 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001099 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001100 setupDrawPureColorUniforms();
1101 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001102 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1103 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001104 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001105 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001106 }
Romain Guy3b753822013-03-05 10:27:35 -08001107 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001108 layer->getWidth() == (uint32_t) rect.getWidth() &&
1109 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001110 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1111 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001112
Romain Guyd21b6e12011-11-30 20:21:23 -08001113 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1115 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001116 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001117 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1118 }
1119 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001120 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1121
1122 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001123}
1124
Romain Guy5b3b3522010-10-27 18:57:51 -07001125void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001126 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001127 const Rect& texCoords = layer->texCoords;
1128 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1129 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001130
Romain Guy9ace8f52011-07-07 20:50:11 -07001131 float x = rect.left;
1132 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001133 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001134 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001135 layer->getHeight() == (uint32_t) rect.getHeight();
1136
1137 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001138 // When we're swapping, the layer is already in screen coordinates
1139 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001140 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1141 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001142 }
1143
Romain Guyd21b6e12011-11-30 20:21:23 -08001144 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001145 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001146 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001147 }
1148
Chris Craik16ecda52013-03-29 10:59:59 -07001149 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001150 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001151 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001152 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001153 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1154 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001155
Romain Guyaa6c24c2011-04-28 18:40:04 -07001156 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1157 } else {
1158 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1159 drawTextureLayer(layer, rect);
1160 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1161 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001162}
1163
Chris Craik34416ea2013-04-15 16:08:28 -07001164/**
1165 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1166 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1167 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1168 * by saveLayer's restore
1169 */
1170#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1171 DRAW_COMMAND; \
1172 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1173 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1174 DRAW_COMMAND; \
1175 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1176 } \
1177 }
1178
1179#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1180
Romain Guy5b3b3522010-10-27 18:57:51 -07001181void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001182 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001183 layer->setRegionAsRect();
1184
Chris Craik34416ea2013-04-15 16:08:28 -07001185 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001186
Romain Guy5b3b3522010-10-27 18:57:51 -07001187 layer->region.clear();
1188 return;
1189 }
1190
Romain Guy211370f2012-02-01 16:10:55 -08001191 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001192 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001193 const android::Rect* rects;
1194 Region safeRegion;
1195 if (CC_LIKELY(hasRectToRectTransform())) {
1196 rects = layer->region.getArray(&count);
1197 } else {
1198 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1199 rects = safeRegion.getArray(&count);
1200 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001201
Chris Craik16ecda52013-03-29 10:59:59 -07001202 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001203 const float texX = 1.0f / float(layer->getWidth());
1204 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001205 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001206
Romain Guy8ce00302013-01-15 18:51:42 -08001207 setupDraw();
1208
1209 // We must get (and therefore bind) the region mesh buffer
1210 // after we setup drawing in case we need to mess with the
1211 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001212 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001213 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001214
Romain Guy7230a742011-01-10 22:26:16 -08001215 setupDrawWithTexture();
1216 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001217 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001218 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001219 setupDrawProgram();
1220 setupDrawDirtyRegionsDisabled();
1221 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001222 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001223 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001224 if (currentTransform().isPureTranslate()) {
1225 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1226 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001227
Romain Guyd21b6e12011-11-30 20:21:23 -08001228 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001229 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1230 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001231 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001232 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1233 }
Romain Guy15bc6432011-12-13 13:11:32 -08001234 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001235
1236 for (size_t i = 0; i < count; i++) {
1237 const android::Rect* r = &rects[i];
1238
1239 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001240 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001241 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001242 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001243
1244 // TODO: Reject quads outside of the clip
1245 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1246 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1247 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1248 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1249
1250 numQuads++;
1251
Romain Guy31e08e92013-06-18 15:53:53 -07001252 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001253 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1254 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001255 numQuads = 0;
1256 mesh = mCaches.getRegionMesh();
1257 }
1258 }
1259
1260 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001261 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1262 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001263 }
1264
Romain Guy5b3b3522010-10-27 18:57:51 -07001265#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001266 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001267#endif
1268
1269 layer->region.clear();
1270 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001271}
1272
Romain Guy3a3133d2011-02-01 22:59:58 -08001273void OpenGLRenderer::drawRegionRects(const Region& region) {
1274#if DEBUG_LAYERS_AS_REGIONS
1275 size_t count;
1276 const android::Rect* rects = region.getArray(&count);
1277
1278 uint32_t colors[] = {
1279 0x7fff0000, 0x7f00ff00,
1280 0x7f0000ff, 0x7fff00ff,
1281 };
1282
1283 int offset = 0;
1284 int32_t top = rects[0].top;
1285
1286 for (size_t i = 0; i < count; i++) {
1287 if (top != rects[i].top) {
1288 offset ^= 0x2;
1289 top = rects[i].top;
1290 }
1291
1292 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1293 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1294 SkXfermode::kSrcOver_Mode);
1295 }
1296#endif
1297}
1298
Romain Guy8ce00302013-01-15 18:51:42 -08001299void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1300 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001301 Vector<float> rects;
1302
1303 SkRegion::Iterator it(region);
1304 while (!it.done()) {
1305 const SkIRect& r = it.rect();
1306 rects.push(r.fLeft);
1307 rects.push(r.fTop);
1308 rects.push(r.fRight);
1309 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001310 it.next();
1311 }
1312
Romain Guy448455f2013-07-22 13:57:50 -07001313 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001314}
1315
Romain Guy5b3b3522010-10-27 18:57:51 -07001316void OpenGLRenderer::dirtyLayer(const float left, const float top,
1317 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001318 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001319 Rect bounds(left, top, right, bottom);
1320 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001321 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001322 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001323}
1324
1325void OpenGLRenderer::dirtyLayer(const float left, const float top,
1326 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001327 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001328 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001329 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001330 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001331}
1332
1333void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001334 if (bounds.intersect(*mSnapshot->clipRect)) {
1335 bounds.snapToPixelBoundaries();
1336 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1337 if (!dirty.isEmpty()) {
1338 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001339 }
1340 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001341}
1342
Romain Guy448455f2013-07-22 13:57:50 -07001343void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1344 GLsizei elementsCount = quadsCount * 6;
1345 while (elementsCount > 0) {
1346 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1347
1348 setupDrawIndexedVertices(&mesh[0].position[0]);
1349 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1350
1351 elementsCount -= drawCount;
1352 // Though there are 4 vertices in a quad, we use 6 indices per
1353 // quad to draw with GL_TRIANGLES
1354 mesh += (drawCount / 6) * 4;
1355 }
1356}
1357
Romain Guy54be1cd2011-06-13 19:04:27 -07001358void OpenGLRenderer::clearLayerRegions() {
1359 const size_t count = mLayers.size();
1360 if (count == 0) return;
1361
1362 if (!mSnapshot->isIgnored()) {
1363 // Doing several glScissor/glClear here can negatively impact
1364 // GPUs with a tiler architecture, instead we draw quads with
1365 // the Clear blending mode
1366
1367 // The list contains bounds that have already been clipped
1368 // against their initial clip rect, and the current clip
1369 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001370 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001371
Romain Guy448455f2013-07-22 13:57:50 -07001372 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001373 Vertex* vertex = mesh;
1374
1375 for (uint32_t i = 0; i < count; i++) {
1376 Rect* bounds = mLayers.itemAt(i);
1377
Romain Guy54be1cd2011-06-13 19:04:27 -07001378 Vertex::set(vertex++, bounds->left, bounds->top);
1379 Vertex::set(vertex++, bounds->right, bounds->top);
1380 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001381 Vertex::set(vertex++, bounds->right, bounds->bottom);
1382
1383 delete bounds;
1384 }
Romain Guye67307c2013-02-11 18:01:20 -08001385 // We must clear the list of dirty rects before we
1386 // call setupDraw() to prevent stencil setup to do
1387 // the same thing again
1388 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001389
1390 setupDraw(false);
1391 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1392 setupDrawBlending(true, SkXfermode::kClear_Mode);
1393 setupDrawProgram();
1394 setupDrawPureColorUniforms();
1395 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1396
Romain Guy448455f2013-07-22 13:57:50 -07001397 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001398
1399 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001400 } else {
1401 for (uint32_t i = 0; i < count; i++) {
1402 delete mLayers.itemAt(i);
1403 }
Romain Guye67307c2013-02-11 18:01:20 -08001404 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001405 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001406}
1407
Romain Guybd6b79b2010-06-26 00:13:53 -07001408///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001409// State Deferral
1410///////////////////////////////////////////////////////////////////////////////
1411
Chris Craikff785832013-03-08 13:12:16 -08001412bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001413 const Rect& currentClip = *(mSnapshot->clipRect);
1414 const mat4& currentMatrix = *(mSnapshot->transform);
1415
Chris Craikff785832013-03-08 13:12:16 -08001416 if (stateDeferFlags & kStateDeferFlag_Draw) {
1417 // state has bounds initialized in local coordinates
1418 if (!state.mBounds.isEmpty()) {
1419 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001420 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001421 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1422 // is used, it should more closely duplicate the quickReject logic (in how it uses
1423 // snapToPixelBoundaries)
1424
Chris Craik28ce94a2013-05-31 11:38:03 -07001425 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001426 // quick rejected
1427 return true;
1428 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001429
Chris Craika02c4ed2013-06-14 13:43:58 -07001430 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001431 if (!currentClip.contains(state.mBounds)) {
1432 int& flags = state.mClipSideFlags;
1433 // op partially clipped, so record which sides are clipped for clip-aware merging
1434 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1435 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1436 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1437 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1438 }
1439 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001440 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001441 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1442 // overdraw avoidance (since we don't know what it overlaps)
1443 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001444 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001445 }
1446 }
1447
Chris Craik527a3aa2013-03-04 10:19:31 -08001448 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1449 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001450 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001451 }
1452
Chris Craik7273daa2013-03-28 11:25:24 -07001453 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1454 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001455 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001456 state.mDrawModifiers = mDrawModifiers;
1457 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001458 return false;
1459}
1460
Chris Craik527a3aa2013-03-04 10:19:31 -08001461void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001462 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001463 mDrawModifiers = state.mDrawModifiers;
1464 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001465
Chris Craik527a3aa2013-03-04 10:19:31 -08001466 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001467 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1468 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001469 dirtyClip();
1470 }
Chris Craikc3566d02013-02-04 16:16:33 -08001471}
1472
Chris Craik28ce94a2013-05-31 11:38:03 -07001473/**
1474 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1475 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1476 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1477 *
1478 * This method should be called when restoreDisplayState() won't be restoring the clip
1479 */
1480void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1481 if (clipRect != NULL) {
1482 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1483 } else {
1484 mSnapshot->setClip(0, 0, mWidth, mHeight);
1485 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001486 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001487 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001488}
1489
Chris Craikc3566d02013-02-04 16:16:33 -08001490///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001491// Transforms
1492///////////////////////////////////////////////////////////////////////////////
1493
1494void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001495 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001496}
1497
1498void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001499 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001500}
1501
1502void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001503 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001504}
1505
Romain Guy807daf72011-01-18 11:19:19 -08001506void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001507 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001508}
1509
Romain Guyf6a11b82010-06-23 17:47:49 -07001510void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001511 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001512 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001513 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001514 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001515 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001516}
1517
Chris Craikb98a0162013-02-21 11:30:22 -08001518bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001519 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001520}
1521
Romain Guyf6a11b82010-06-23 17:47:49 -07001522void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001523 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001524}
1525
1526void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001527 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001528 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001529 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001530 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001531}
1532
1533///////////////////////////////////////////////////////////////////////////////
1534// Clipping
1535///////////////////////////////////////////////////////////////////////////////
1536
Romain Guybb9524b2010-06-22 18:56:38 -07001537void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001538 Rect clip(*mSnapshot->clipRect);
1539 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001540
Romain Guy8a4ac612012-07-17 17:32:48 -07001541 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1542 clip.getWidth(), clip.getHeight())) {
1543 mDirtyClip = false;
1544 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001545}
1546
Romain Guy8ce00302013-01-15 18:51:42 -08001547void OpenGLRenderer::ensureStencilBuffer() {
1548 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1549 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1550 // just hope we have one when hasLayer() returns false.
1551 if (hasLayer()) {
1552 attachStencilBufferToLayer(mSnapshot->layer);
1553 }
1554}
1555
1556void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1557 // The layer's FBO is already bound when we reach this stage
1558 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001559 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1560 // is attached after we initiated tiling. We must turn it off,
1561 // attach the new render buffer then turn tiling back on
1562 endTiling();
1563
Romain Guy8d4aeb72013-02-12 16:08:55 -08001564 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001565 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001566 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001567
Romain Guyf735c8e2013-01-31 17:45:55 -08001568 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001569 }
1570}
1571
1572void OpenGLRenderer::setStencilFromClip() {
1573 if (!mCaches.debugOverdraw) {
1574 if (!mSnapshot->clipRegion->isEmpty()) {
1575 // NOTE: The order here is important, we must set dirtyClip to false
1576 // before any draw call to avoid calling back into this method
1577 mDirtyClip = false;
1578
1579 ensureStencilBuffer();
1580
1581 mCaches.stencil.enableWrite();
1582
1583 // Clear the stencil but first make sure we restrict drawing
1584 // to the region's bounds
1585 bool resetScissor = mCaches.enableScissor();
1586 if (resetScissor) {
1587 // The scissor was not set so we now need to update it
1588 setScissorFromClip();
1589 }
1590 mCaches.stencil.clear();
1591 if (resetScissor) mCaches.disableScissor();
1592
1593 // NOTE: We could use the region contour path to generate a smaller mesh
1594 // Since we are using the stencil we could use the red book path
1595 // drawing technique. It might increase bandwidth usage though.
1596
1597 // The last parameter is important: we are not drawing in the color buffer
1598 // so we don't want to dirty the current layer, if any
1599 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1600
1601 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001602
1603 // Draw the region used to generate the stencil if the appropriate debug
1604 // mode is enabled
1605 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1606 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1607 }
Romain Guy8ce00302013-01-15 18:51:42 -08001608 } else {
1609 mCaches.stencil.disable();
1610 }
1611 }
1612}
1613
Romain Guy9d5316e2010-06-24 19:30:36 -07001614const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001615 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001616}
1617
Chris Craik39a908c2013-06-13 14:39:01 -07001618bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001619 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001620 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001621 return true;
1622 }
1623
1624 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001625 currentTransform().mapRect(r);
Chris Craik5e49b302013-07-30 19:05:20 -07001626
1627 if (snapOut) {
1628 // snapOut is generally used to account for 1 pixel ramp (in window coordinates)
1629 // outside of the provided rect boundaries in tessellated AA geometry
1630 r.snapOutToPixelBoundaries();
1631 } else {
1632 r.snapToPixelBoundaries();
1633 }
Romain Guy8a4ac612012-07-17 17:32:48 -07001634
1635 Rect clipRect(*mSnapshot->clipRect);
1636 clipRect.snapToPixelBoundaries();
1637
Chris Craik39a908c2013-06-13 14:39:01 -07001638 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001639
Chris Craik39a908c2013-06-13 14:39:01 -07001640 if (clipRequired) *clipRequired = !clipRect.contains(r);
1641 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001642}
1643
Romain Guy672433d2013-01-04 19:05:13 -08001644bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1645 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001646 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1647 // the final mapped rect to ensure correct clipping behavior for the ramp.
1648 bool snapOut = paint->isAntiAlias();
1649
Chris Craikcb4d6002012-09-25 12:00:29 -07001650 if (paint->getStyle() != SkPaint::kFill_Style) {
1651 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001652 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001653 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001654 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001655 }
1656}
1657
Chris Craik5e49b302013-07-30 19:05:20 -07001658bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001659 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001660 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001661 return true;
1662 }
1663
Chris Craik39a908c2013-06-13 14:39:01 -07001664 if (!isDeferred()) {
1665 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001666 }
Chris Craik39a908c2013-06-13 14:39:01 -07001667 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001668}
1669
Romain Guy8ce00302013-01-15 18:51:42 -08001670void OpenGLRenderer::debugClip() {
1671#if DEBUG_CLIP_REGIONS
1672 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1673 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1674 }
1675#endif
1676}
1677
Romain Guy079ba2c2010-07-16 14:12:24 -07001678bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001679 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001680 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1681 if (clipped) {
1682 dirtyClip();
1683 }
1684 return !mSnapshot->clipRect->isEmpty();
1685 }
1686
1687 SkPath path;
1688 path.addRect(left, top, right, bottom);
1689
Romain Guyb7b93e02013-08-01 15:29:25 -07001690 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001691}
1692
1693bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1694 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001695 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001696
1697 SkPath transformed;
1698 path->transform(transform, &transformed);
1699
1700 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001701 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1702 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001703 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001704 if (mSnapshot->previous == mFirstSnapshot) {
1705 clip.setRect(0, 0, mWidth, mHeight);
1706 } else {
1707 Rect* bounds = mSnapshot->previous->clipRect;
1708 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1709 }
Romain Guy8ce00302013-01-15 18:51:42 -08001710 }
1711
1712 SkRegion region;
1713 region.setPath(transformed, clip);
1714
1715 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001716 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001717 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001718 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001719 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001720}
1721
Romain Guy735738c2012-12-03 12:34:51 -08001722bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001723 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1724 if (clipped) {
1725 dirtyClip();
1726 }
1727 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001728}
1729
Chet Haasea23eed82012-04-12 15:19:04 -07001730Rect* OpenGLRenderer::getClipRect() {
1731 return mSnapshot->clipRect;
1732}
1733
Romain Guyf6a11b82010-06-23 17:47:49 -07001734///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001735// Drawing commands
1736///////////////////////////////////////////////////////////////////////////////
1737
Romain Guy54be1cd2011-06-13 19:04:27 -07001738void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001739 // TODO: It would be best if we could do this before quickReject()
1740 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001741 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001742 // Make sure setScissor & setStencil happen at the beginning of
1743 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001744 if (mDirtyClip) {
1745 if (mCaches.scissorEnabled) {
1746 setScissorFromClip();
1747 }
Romain Guy8ce00302013-01-15 18:51:42 -08001748 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001749 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001750
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001752
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 mSetShaderColor = false;
1754 mColorSet = false;
1755 mColorA = mColorR = mColorG = mColorB = 0.0f;
1756 mTextureUnit = 0;
1757 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001758
1759 // Enable debug highlight when what we're about to draw is tested against
1760 // the stencil buffer and if stencil highlight debugging is on
1761 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1762 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1763 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001764
1765 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001766}
1767
1768void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1769 mDescription.hasTexture = true;
1770 mDescription.hasAlpha8Texture = isAlpha8;
1771}
1772
Romain Guyff316ec2013-02-13 18:39:43 -08001773void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1774 mDescription.hasTexture = true;
1775 mDescription.hasColors = true;
1776 mDescription.hasAlpha8Texture = isAlpha8;
1777}
1778
Romain Guyaa6c24c2011-04-28 18:40:04 -07001779void OpenGLRenderer::setupDrawWithExternalTexture() {
1780 mDescription.hasExternalTexture = true;
1781}
1782
Romain Guy15bc6432011-12-13 13:11:32 -08001783void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001784 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001785}
1786
Chris Craik710f46d2012-09-17 17:25:49 -07001787void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001788 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001789}
1790
Romain Guy8d0d4782010-12-14 20:13:35 -08001791void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1792 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001793 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1794 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1795 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001796 mColorSet = true;
1797 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1798}
1799
Romain Guy86568192010-12-14 15:55:39 -08001800void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1801 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001802 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1803 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1804 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001805 mColorSet = true;
1806 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1807}
1808
Romain Guy41210632012-07-16 17:04:24 -07001809void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1810 mCaches.fontRenderer->describe(mDescription, paint);
1811}
1812
Romain Guy70ca14e2010-12-13 18:24:33 -08001813void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1814 mColorA = a;
1815 mColorR = r;
1816 mColorG = g;
1817 mColorB = b;
1818 mColorSet = true;
1819 mSetShaderColor = mDescription.setColor(r, g, b, a);
1820}
1821
1822void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001823 if (mDrawModifiers.mShader) {
1824 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001825 }
1826}
1827
1828void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001829 if (mDrawModifiers.mColorFilter) {
1830 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001831 }
1832}
1833
Romain Guyf09ef512011-05-27 11:43:46 -07001834void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1835 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1836 mColorA = 1.0f;
1837 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001838 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001839 }
1840}
1841
Romain Guy70ca14e2010-12-13 18:24:33 -08001842void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001843 // When the blending mode is kClear_Mode, we need to use a modulate color
1844 // argb=1,0,0,0
1845 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001846 bool blend = (mColorSet && mColorA < 1.0f) ||
1847 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1848 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001849}
1850
1851void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001852 // When the blending mode is kClear_Mode, we need to use a modulate color
1853 // argb=1,0,0,0
1854 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001855 blend |= (mColorSet && mColorA < 1.0f) ||
1856 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1857 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1858 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001859}
1860
1861void OpenGLRenderer::setupDrawProgram() {
1862 useProgram(mCaches.programCache.get(mDescription));
1863}
1864
1865void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1866 mTrackDirtyRegions = false;
1867}
1868
1869void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1870 bool ignoreTransform) {
1871 mModelView.loadTranslate(left, top, 0.0f);
1872 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001873 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1874 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001875 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001876 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001877 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1878 }
1879}
1880
Chet Haase8a5cc922011-04-26 07:28:09 -07001881void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001882 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001883}
1884
Romain Guy70ca14e2010-12-13 18:24:33 -08001885void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1886 bool ignoreTransform, bool ignoreModelView) {
1887 if (!ignoreModelView) {
1888 mModelView.loadTranslate(left, top, 0.0f);
1889 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001890 } else {
1891 mModelView.loadIdentity();
1892 }
Romain Guy86568192010-12-14 15:55:39 -08001893 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1894 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001895 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001896 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001897 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001898 }
1899 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001900 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001901 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1902 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001903}
1904
1905void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001906 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001907 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1908 }
1909}
1910
Romain Guy86568192010-12-14 15:55:39 -08001911void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001912 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001913 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001914 }
1915}
1916
Romain Guy70ca14e2010-12-13 18:24:33 -08001917void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001918 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001919 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001920 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001921 }
Chris Craikc3566d02013-02-04 16:16:33 -08001922 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1923 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001924 }
1925}
1926
Romain Guy8d0d4782010-12-14 20:13:35 -08001927void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001928 if (mDrawModifiers.mShader) {
1929 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001930 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001931 }
1932}
1933
Romain Guy70ca14e2010-12-13 18:24:33 -08001934void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001935 if (mDrawModifiers.mColorFilter) {
1936 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001937 }
1938}
1939
Romain Guy41210632012-07-16 17:04:24 -07001940void OpenGLRenderer::setupDrawTextGammaUniforms() {
1941 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1942}
1943
Romain Guy70ca14e2010-12-13 18:24:33 -08001944void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001945 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001946 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001947 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001948}
1949
1950void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001951 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001952 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001953 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001954}
1955
Romain Guyaa6c24c2011-04-28 18:40:04 -07001956void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1957 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001958 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001959 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001960}
1961
Romain Guy8f0095c2011-05-02 17:24:22 -07001962void OpenGLRenderer::setupDrawTextureTransform() {
1963 mDescription.hasTextureTransform = true;
1964}
1965
1966void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001967 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1968 GL_FALSE, &transform.data[0]);
1969}
1970
Romain Guy70ca14e2010-12-13 18:24:33 -08001971void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001972 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001973 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001974 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001975 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001976 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001977 }
Romain Guyd71dd362011-12-12 19:03:35 -08001978
Chris Craikcb4d6002012-09-25 12:00:29 -07001979 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001980 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001981 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001982 }
1983
1984 mCaches.unbindIndicesBuffer();
1985}
1986
Romain Guyff316ec2013-02-13 18:39:43 -08001987void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1988 bool force = mCaches.unbindMeshBuffer();
1989 GLsizei stride = sizeof(ColorTextureVertex);
1990
1991 mCaches.bindPositionVertexPointer(force, vertices, stride);
1992 if (mCaches.currentProgram->texCoords >= 0) {
1993 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1994 }
1995 int slot = mCaches.currentProgram->getAttrib("colors");
1996 if (slot >= 0) {
1997 glEnableVertexAttribArray(slot);
1998 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1999 }
2000
2001 mCaches.unbindIndicesBuffer();
2002}
2003
Romain Guy3b748a42013-04-17 18:54:38 -07002004void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2005 bool force = false;
2006 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2007 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2008 // use the default VBO found in Caches
2009 if (!vertices || vbo) {
2010 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2011 } else {
2012 force = mCaches.unbindMeshBuffer();
2013 }
2014 mCaches.bindIndicesBuffer();
2015
Chris Craikcb4d6002012-09-25 12:00:29 -07002016 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002017 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002018 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002019 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002020}
2021
Romain Guy448455f2013-07-22 13:57:50 -07002022void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002023 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002024 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002025 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002026}
2027
Romain Guy70ca14e2010-12-13 18:24:33 -08002028///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002029// Drawing
2030///////////////////////////////////////////////////////////////////////////////
2031
Chris Craikff785832013-03-08 13:12:16 -08002032status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2033 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002034 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002035 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2036 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002037 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002038 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002039 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002040 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2041 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002042 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002043 }
2044
Chris Craik28ce94a2013-05-31 11:38:03 -07002045 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2046 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002047 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2048 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002049
2050 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002051 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002052
Chet Haase58d110a2013-04-10 07:43:29 -07002053 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002054 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002055
Romain Guy65549432012-03-26 16:45:05 -07002056 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002057}
2058
Chris Craikc3566d02013-02-04 16:16:33 -08002059void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002060 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002061 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002062 }
2063}
2064
Romain Guya168d732011-03-18 16:50:13 -07002065void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2066 int alpha;
2067 SkXfermode::Mode mode;
2068 getAlphaAndMode(paint, &alpha, &mode);
2069
Romain Guy886b2752013-01-04 12:26:18 -08002070 int color = paint != NULL ? paint->getColor() : 0;
2071
Romain Guya168d732011-03-18 16:50:13 -07002072 float x = left;
2073 float y = top;
2074
Romain Guy886b2752013-01-04 12:26:18 -08002075 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2076
Romain Guya168d732011-03-18 16:50:13 -07002077 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002078 if (currentTransform().isPureTranslate()) {
2079 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2080 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002081 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002082
2083 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002084 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002085 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002086 }
2087
Romain Guy3b748a42013-04-17 18:54:38 -07002088 // No need to check for a UV mapper on the texture object, only ARGB_8888
2089 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002090 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002091 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2092 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002093}
2094
Romain Guy03c00b52013-06-20 18:30:28 -07002095/**
2096 * Important note: this method is intended to draw batches of bitmaps and
2097 * will not set the scissor enable or dirty the current layer, if any.
2098 * The caller is responsible for properly dirtying the current layer.
2099 */
Romain Guy55b6f952013-06-27 15:27:09 -07002100status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2101 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002102 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002103 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002104 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002105
Chris Craik527a3aa2013-03-04 10:19:31 -08002106 const AutoTexture autoCleanup(texture);
2107
2108 int alpha;
2109 SkXfermode::Mode mode;
2110 getAlphaAndMode(paint, &alpha, &mode);
2111
2112 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002113 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002114
2115 const float x = (int) floorf(bounds.left + 0.5f);
2116 const float y = (int) floorf(bounds.top + 0.5f);
2117 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2118 int color = paint != NULL ? paint->getColor() : 0;
2119 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2120 texture->id, paint != NULL, color, alpha, mode,
2121 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002122 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002123 } else {
2124 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2125 texture->id, alpha / 255.0f, mode, texture->blend,
2126 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002127 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002128 }
2129
2130 return DrawGlInfo::kStatusDrew;
2131}
2132
Chet Haase48659092012-05-31 15:21:51 -07002133status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002134 const float right = left + bitmap->width();
2135 const float bottom = top + bitmap->height();
2136
2137 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002138 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002139 }
2140
Romain Guya1d3c912011-12-13 14:55:06 -08002141 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002142 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002143 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002144 const AutoTexture autoCleanup(texture);
2145
Romain Guy211370f2012-02-01 16:10:55 -08002146 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002147 drawAlphaBitmap(texture, left, top, paint);
2148 } else {
2149 drawTextureRect(left, top, right, bottom, texture, paint);
2150 }
Chet Haase48659092012-05-31 15:21:51 -07002151
2152 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002153}
2154
Chet Haase48659092012-05-31 15:21:51 -07002155status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002156 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2157 const mat4 transform(*matrix);
2158 transform.mapRect(r);
2159
Romain Guy6926c722010-07-12 20:20:03 -07002160 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002161 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002162 }
2163
Romain Guya1d3c912011-12-13 14:55:06 -08002164 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002165 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002166 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002167 const AutoTexture autoCleanup(texture);
2168
Romain Guy5b3b3522010-10-27 18:57:51 -07002169 // This could be done in a cheaper way, all we need is pass the matrix
2170 // to the vertex shader. The save/restore is a bit overkill.
2171 save(SkCanvas::kMatrix_SaveFlag);
2172 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002173 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2174 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2175 } else {
2176 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2177 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002178 restore();
Chet Haase48659092012-05-31 15:21:51 -07002179
2180 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002181}
2182
Chet Haase48659092012-05-31 15:21:51 -07002183status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002184 const float right = left + bitmap->width();
2185 const float bottom = top + bitmap->height();
2186
2187 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002188 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002189 }
2190
2191 mCaches.activeTexture(0);
2192 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2193 const AutoTexture autoCleanup(texture);
2194
Romain Guy886b2752013-01-04 12:26:18 -08002195 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2196 drawAlphaBitmap(texture, left, top, paint);
2197 } else {
2198 drawTextureRect(left, top, right, bottom, texture, paint);
2199 }
Chet Haase48659092012-05-31 15:21:51 -07002200
2201 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002202}
2203
Chet Haase48659092012-05-31 15:21:51 -07002204status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002205 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002206 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002207 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002208 }
2209
Chris Craik39a908c2013-06-13 14:39:01 -07002210 // TODO: use quickReject on bounds from vertices
2211 mCaches.enableScissor();
2212
Romain Guyb18d2d02011-02-10 15:52:54 -08002213 float left = FLT_MAX;
2214 float top = FLT_MAX;
2215 float right = FLT_MIN;
2216 float bottom = FLT_MIN;
2217
Romain Guya92bb4d2012-10-16 11:08:44 -07002218 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002219
Romain Guyff316ec2013-02-13 18:39:43 -08002220 ColorTextureVertex mesh[count];
2221 ColorTextureVertex* vertex = mesh;
2222
2223 bool cleanupColors = false;
2224 if (!colors) {
2225 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2226 colors = new int[colorsCount];
2227 memset(colors, 0xff, colorsCount * sizeof(int));
2228 cleanupColors = true;
2229 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002230
Romain Guy3b748a42013-04-17 18:54:38 -07002231 mCaches.activeTexture(0);
2232 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2233 const UvMapper& mapper(getMapper(texture));
2234
Romain Guy5a7b4662011-01-20 19:09:30 -08002235 for (int32_t y = 0; y < meshHeight; y++) {
2236 for (int32_t x = 0; x < meshWidth; x++) {
2237 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2238
2239 float u1 = float(x) / meshWidth;
2240 float u2 = float(x + 1) / meshWidth;
2241 float v1 = float(y) / meshHeight;
2242 float v2 = float(y + 1) / meshHeight;
2243
Romain Guy3b748a42013-04-17 18:54:38 -07002244 mapper.map(u1, v1, u2, v2);
2245
Romain Guy5a7b4662011-01-20 19:09:30 -08002246 int ax = i + (meshWidth + 1) * 2;
2247 int ay = ax + 1;
2248 int bx = i;
2249 int by = bx + 1;
2250 int cx = i + 2;
2251 int cy = cx + 1;
2252 int dx = i + (meshWidth + 1) * 2 + 2;
2253 int dy = dx + 1;
2254
Romain Guyff316ec2013-02-13 18:39:43 -08002255 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2256 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2257 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002258
Romain Guyff316ec2013-02-13 18:39:43 -08002259 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2260 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2261 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002262
Romain Guya92bb4d2012-10-16 11:08:44 -07002263 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2264 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2265 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2266 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002267 }
2268 }
2269
Romain Guya92bb4d2012-10-16 11:08:44 -07002270 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002271 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002272 return DrawGlInfo::kStatusDone;
2273 }
2274
Romain Guyff316ec2013-02-13 18:39:43 -08002275 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002276 texture = mCaches.textureCache.get(bitmap);
2277 if (!texture) {
2278 if (cleanupColors) delete[] colors;
2279 return DrawGlInfo::kStatusDone;
2280 }
Romain Guyff316ec2013-02-13 18:39:43 -08002281 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002282 const AutoTexture autoCleanup(texture);
2283
2284 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2285 texture->setFilter(FILTER(paint), true);
2286
2287 int alpha;
2288 SkXfermode::Mode mode;
2289 getAlphaAndMode(paint, &alpha, &mode);
2290
Romain Guyff316ec2013-02-13 18:39:43 -08002291 float a = alpha / 255.0f;
2292
Romain Guya92bb4d2012-10-16 11:08:44 -07002293 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002294 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002295 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002296
Romain Guyff316ec2013-02-13 18:39:43 -08002297 setupDraw();
2298 setupDrawWithTextureAndColor();
2299 setupDrawColor(a, a, a, a);
2300 setupDrawColorFilter();
2301 setupDrawBlending(true, mode, false);
2302 setupDrawProgram();
2303 setupDrawDirtyRegionsDisabled();
2304 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2305 setupDrawTexture(texture->id);
2306 setupDrawPureColorUniforms();
2307 setupDrawColorFilterUniforms();
2308 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2309
2310 glDrawArrays(GL_TRIANGLES, 0, count);
2311
Romain Guyff316ec2013-02-13 18:39:43 -08002312 int slot = mCaches.currentProgram->getAttrib("colors");
2313 if (slot >= 0) {
2314 glDisableVertexAttribArray(slot);
2315 }
2316
2317 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002318
2319 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002320}
2321
Chet Haase48659092012-05-31 15:21:51 -07002322status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002323 float srcLeft, float srcTop, float srcRight, float srcBottom,
2324 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002325 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002326 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002327 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002328 }
2329
Romain Guya1d3c912011-12-13 14:55:06 -08002330 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002331 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002332 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002333 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002334
Romain Guy8ba548f2010-06-30 19:21:21 -07002335 const float width = texture->width;
2336 const float height = texture->height;
2337
Romain Guy3b748a42013-04-17 18:54:38 -07002338 float u1 = fmax(0.0f, srcLeft / width);
2339 float v1 = fmax(0.0f, srcTop / height);
2340 float u2 = fmin(1.0f, srcRight / width);
2341 float v2 = fmin(1.0f, srcBottom / height);
2342
2343 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002344
Romain Guy03750a02010-10-18 14:06:08 -07002345 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002346 resetDrawTextureTexCoords(u1, v1, u2, v2);
2347
Romain Guy03750a02010-10-18 14:06:08 -07002348 int alpha;
2349 SkXfermode::Mode mode;
2350 getAlphaAndMode(paint, &alpha, &mode);
2351
Romain Guyd21b6e12011-11-30 20:21:23 -08002352 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2353
Romain Guy886b2752013-01-04 12:26:18 -08002354 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2355 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002356
Romain Guy886b2752013-01-04 12:26:18 -08002357 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2358 // Apply a scale transform on the canvas only when a shader is in use
2359 // Skia handles the ratio between the dst and src rects as a scale factor
2360 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002361 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002362 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002363
Romain Guy3b753822013-03-05 10:27:35 -08002364 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2365 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2366 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002367
2368 dstRight = x + (dstRight - dstLeft);
2369 dstBottom = y + (dstBottom - dstTop);
2370
2371 dstLeft = x;
2372 dstTop = y;
2373
2374 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2375 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002376 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002377 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002378 }
2379
2380 if (CC_UNLIKELY(useScaleTransform)) {
2381 save(SkCanvas::kMatrix_SaveFlag);
2382 translate(dstLeft, dstTop);
2383 scale(scaleX, scaleY);
2384
2385 dstLeft = 0.0f;
2386 dstTop = 0.0f;
2387
2388 dstRight = srcRight - srcLeft;
2389 dstBottom = srcBottom - srcTop;
2390 }
2391
2392 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2393 int color = paint ? paint->getColor() : 0;
2394 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2395 texture->id, paint != NULL, color, alpha, mode,
2396 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2397 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2398 } else {
2399 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2400 texture->id, alpha / 255.0f, mode, texture->blend,
2401 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2402 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2403 }
2404
2405 if (CC_UNLIKELY(useScaleTransform)) {
2406 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002407 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002408
2409 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002410
2411 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002412}
2413
Romain Guy3b748a42013-04-17 18:54:38 -07002414status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002415 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002416 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002417 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002418 }
2419
Romain Guy4c2547f2013-06-11 16:19:24 -07002420 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002421 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2422 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002423
Romain Guy03c00b52013-06-20 18:30:28 -07002424 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002425}
2426
Romain Guy03c00b52013-06-20 18:30:28 -07002427status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2428 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002429 if (quickReject(left, top, right, bottom)) {
2430 return DrawGlInfo::kStatusDone;
2431 }
2432
Romain Guy211370f2012-02-01 16:10:55 -08002433 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002434 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002435 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002436 if (!texture) return DrawGlInfo::kStatusDone;
2437 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002438
Romain Guya4adcf02013-02-28 12:15:35 -08002439 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2440 texture->setFilter(GL_LINEAR, true);
2441
Romain Guy03c00b52013-06-20 18:30:28 -07002442 int alpha;
2443 SkXfermode::Mode mode;
2444 getAlphaAndMode(paint, &alpha, &mode);
2445
Romain Guy3b753822013-03-05 10:27:35 -08002446 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002447 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002448 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002449 const float offsetX = left + currentTransform().getTranslateX();
2450 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002451 const size_t count = mesh->quads.size();
2452 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002453 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002454 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002455 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2456 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2457 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002458 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002459 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002460 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002461 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002462 }
2463 }
2464
Romain Guy211370f2012-02-01 16:10:55 -08002465 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002466 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2467 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002468
Romain Guy3b748a42013-04-17 18:54:38 -07002469 right = x + right - left;
2470 bottom = y + bottom - top;
2471 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2472 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2473 GL_TRIANGLES, mesh->indexCount, false, true,
2474 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002475 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002476 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2477 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2478 GL_TRIANGLES, mesh->indexCount, false, false,
2479 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002480 }
Romain Guy054dc182010-10-15 17:55:25 -07002481 }
Chet Haase48659092012-05-31 15:21:51 -07002482
2483 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002484}
2485
Romain Guy03c00b52013-06-20 18:30:28 -07002486/**
2487 * Important note: this method is intended to draw batches of 9-patch objects and
2488 * will not set the scissor enable or dirty the current layer, if any.
2489 * The caller is responsible for properly dirtying the current layer.
2490 */
2491status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2492 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2493 mCaches.activeTexture(0);
2494 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2495 if (!texture) return DrawGlInfo::kStatusDone;
2496 const AutoTexture autoCleanup(texture);
2497
2498 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2499 texture->setFilter(GL_LINEAR, true);
2500
2501 int alpha;
2502 SkXfermode::Mode mode;
2503 getAlphaAndMode(paint, &alpha, &mode);
2504
2505 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2506 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2507 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2508
2509 return DrawGlInfo::kStatusDrew;
2510}
2511
Chris Craik65cd6122012-12-10 17:56:27 -08002512status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2513 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002514 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002515 // no vertices to draw
2516 return DrawGlInfo::kStatusDone;
2517 }
2518
Chris Craikcb4d6002012-09-25 12:00:29 -07002519 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002520 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2521 bool isAA = paint->isAntiAlias();
2522
Chris Craik710f46d2012-09-17 17:25:49 -07002523 setupDraw();
2524 setupDrawNoTexture();
2525 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002526 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2527 setupDrawColorFilter();
2528 setupDrawShader();
2529 setupDrawBlending(isAA, mode);
2530 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002531 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002532 setupDrawColorUniforms();
2533 setupDrawColorFilterUniforms();
2534 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002535
Chris Craik710f46d2012-09-17 17:25:49 -07002536 void* vertices = vertexBuffer.getBuffer();
2537 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002538 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002539 mCaches.resetTexCoordsVertexPointer();
2540 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002541
Chris Craik710f46d2012-09-17 17:25:49 -07002542 int alphaSlot = -1;
2543 if (isAA) {
2544 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2545 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002546
Chris Craik710f46d2012-09-17 17:25:49 -07002547 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002548 glEnableVertexAttribArray(alphaSlot);
2549 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002550 }
Romain Guy04299382012-07-18 17:15:41 -07002551
Chris Craik6d29c8d2013-05-08 18:35:44 -07002552 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002553
Chris Craik710f46d2012-09-17 17:25:49 -07002554 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002555 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002556 }
Chris Craik65cd6122012-12-10 17:56:27 -08002557
2558 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002559}
2560
2561/**
Chris Craik65cd6122012-12-10 17:56:27 -08002562 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2563 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2564 * screen space in all directions. However, instead of using a fragment shader to compute the
2565 * translucency of the color from its position, we simply use a varying parameter to define how far
2566 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2567 *
2568 * Doesn't yet support joins, caps, or path effects.
2569 */
2570status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2571 VertexBuffer vertexBuffer;
2572 // TODO: try clipping large paths to viewport
2573 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2574
Romain Guyc46d07a2013-03-15 19:06:39 -07002575 if (hasLayer()) {
2576 SkRect bounds = path.getBounds();
2577 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2578 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2579 }
Chris Craik65cd6122012-12-10 17:56:27 -08002580
2581 return drawVertexBuffer(vertexBuffer, paint);
2582}
2583
2584/**
2585 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2586 * and additional geometry for defining an alpha slope perimeter.
2587 *
2588 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2589 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2590 * in-shader alpha region, but found it to be taxing on some GPUs.
2591 *
2592 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2593 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002594 */
Chet Haase48659092012-05-31 15:21:51 -07002595status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002596 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002597
Chris Craik65cd6122012-12-10 17:56:27 -08002598 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002599
Chris Craik65cd6122012-12-10 17:56:27 -08002600 VertexBuffer buffer;
2601 SkRect bounds;
2602 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002603
2604 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2605 return DrawGlInfo::kStatusDone;
2606 }
2607
Romain Guy3b753822013-03-05 10:27:35 -08002608 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002609
Chris Craik65cd6122012-12-10 17:56:27 -08002610 bool useOffset = !paint->isAntiAlias();
2611 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002612}
2613
Chet Haase48659092012-05-31 15:21:51 -07002614status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002615 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002616
Chris Craik6d29c8d2013-05-08 18:35:44 -07002617 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002618
Chris Craik6d29c8d2013-05-08 18:35:44 -07002619 VertexBuffer buffer;
2620 SkRect bounds;
2621 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002622
Chris Craik6d29c8d2013-05-08 18:35:44 -07002623 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2624 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002625 }
2626
Chris Craik6d29c8d2013-05-08 18:35:44 -07002627 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002628
Chris Craik6d29c8d2013-05-08 18:35:44 -07002629 bool useOffset = !paint->isAntiAlias();
2630 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002631}
2632
Chet Haase48659092012-05-31 15:21:51 -07002633status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002634 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002635 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002636
Romain Guyae88e5e2010-10-22 17:49:18 -07002637 Rect& clip(*mSnapshot->clipRect);
2638 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002639
Romain Guy3d58c032010-07-14 16:34:53 -07002640 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002641
2642 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002643}
Romain Guy9d5316e2010-06-24 19:30:36 -07002644
Chet Haase48659092012-05-31 15:21:51 -07002645status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2646 SkPaint* paint) {
2647 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002648 const AutoTexture autoCleanup(texture);
2649
2650 const float x = left + texture->left - texture->offset;
2651 const float y = top + texture->top - texture->offset;
2652
2653 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002654
2655 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002656}
2657
Chet Haase48659092012-05-31 15:21:51 -07002658status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002659 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002660 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2661 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002662 return DrawGlInfo::kStatusDone;
2663 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002664
Chris Craikcb4d6002012-09-25 12:00:29 -07002665 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002666 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002667 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002668 right - left, bottom - top, rx, ry, p);
2669 return drawShape(left, top, texture, p);
2670 }
2671
2672 SkPath path;
2673 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002674 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2675 float outset = p->getStrokeWidth() / 2;
2676 rect.outset(outset, outset);
2677 rx += outset;
2678 ry += outset;
2679 }
Chris Craik710f46d2012-09-17 17:25:49 -07002680 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002681 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002682}
2683
Chris Craik710f46d2012-09-17 17:25:49 -07002684status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002685 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002686 x + radius, y + radius, p) ||
2687 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002688 return DrawGlInfo::kStatusDone;
2689 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002690 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002691 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002692 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002693 return drawShape(x - radius, y - radius, texture, p);
2694 }
2695
2696 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002697 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2698 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2699 } else {
2700 path.addCircle(x, y, radius);
2701 }
Chris Craik65cd6122012-12-10 17:56:27 -08002702 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002703}
Romain Guy01d58e42011-01-19 21:54:02 -08002704
Chet Haase48659092012-05-31 15:21:51 -07002705status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002706 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002707 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2708 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002709 return DrawGlInfo::kStatusDone;
2710 }
Romain Guy01d58e42011-01-19 21:54:02 -08002711
Chris Craikcb4d6002012-09-25 12:00:29 -07002712 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002713 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002714 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002715 return drawShape(left, top, texture, p);
2716 }
2717
2718 SkPath path;
2719 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002720 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2721 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2722 }
Chris Craik710f46d2012-09-17 17:25:49 -07002723 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002724 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002725}
2726
Chet Haase48659092012-05-31 15:21:51 -07002727status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002728 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002729 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2730 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002731 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002732 }
2733
Chris Craik780c1282012-10-04 14:10:49 -07002734 if (fabs(sweepAngle) >= 360.0f) {
2735 return drawOval(left, top, right, bottom, p);
2736 }
2737
2738 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002739 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002740 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002741 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002742 startAngle, sweepAngle, useCenter, p);
2743 return drawShape(left, top, texture, p);
2744 }
2745
2746 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2747 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2748 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2749 }
2750
2751 SkPath path;
2752 if (useCenter) {
2753 path.moveTo(rect.centerX(), rect.centerY());
2754 }
2755 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2756 if (useCenter) {
2757 path.close();
2758 }
Chris Craik65cd6122012-12-10 17:56:27 -08002759 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002760}
2761
Romain Guycf8675e2012-10-02 12:32:25 -07002762// See SkPaintDefaults.h
2763#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2764
Chet Haase48659092012-05-31 15:21:51 -07002765status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002766 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2767 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002768 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002769 }
2770
Chris Craik710f46d2012-09-17 17:25:49 -07002771 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002772 // only fill style is supported by drawConvexPath, since others have to handle joins
2773 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2774 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2775 mCaches.activeTexture(0);
2776 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002777 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002778 return drawShape(left, top, texture, p);
2779 }
2780
2781 SkPath path;
2782 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2783 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2784 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2785 }
2786 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002787 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002788 }
2789
Romain Guy3b753822013-03-05 10:27:35 -08002790 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002791 SkPath path;
2792 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002793 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002794 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002795 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002796 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002797 }
Romain Guyc7d53492010-06-25 13:41:57 -07002798}
Romain Guy9d5316e2010-06-24 19:30:36 -07002799
Raph Levien416a8472012-07-19 22:48:17 -07002800void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2801 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2802 float x, float y) {
2803 mCaches.activeTexture(0);
2804
2805 // NOTE: The drop shadow will not perform gamma correction
2806 // if shader-based correction is enabled
2807 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2808 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002809 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002810 // If the drop shadow exceeds the max texture size or couldn't be
2811 // allocated, skip drawing
2812 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002813 const AutoTexture autoCleanup(shadow);
2814
Chris Craikc3566d02013-02-04 16:16:33 -08002815 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2816 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002817
Chris Craikc3566d02013-02-04 16:16:33 -08002818 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2819 int shadowColor = mDrawModifiers.mShadowColor;
2820 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002821 shadowColor = 0xffffffff;
2822 }
2823
2824 setupDraw();
2825 setupDrawWithTexture(true);
2826 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2827 setupDrawColorFilter();
2828 setupDrawShader();
2829 setupDrawBlending(true, mode);
2830 setupDrawProgram();
2831 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2832 setupDrawTexture(shadow->id);
2833 setupDrawPureColorUniforms();
2834 setupDrawColorFilterUniforms();
2835 setupDrawShaderUniforms();
2836 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2837
2838 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2839}
2840
Romain Guy768bffc2013-02-27 13:50:45 -08002841bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2842 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2843 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2844}
2845
Chet Haase48659092012-05-31 15:21:51 -07002846status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002847 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002848 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002849 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002850 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002851
Romain Guy671d6cf2012-01-18 12:39:17 -08002852 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002853 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002854 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002855 }
2856
Chris Craik39a908c2013-06-13 14:39:01 -07002857 mCaches.enableScissor();
2858
Romain Guy671d6cf2012-01-18 12:39:17 -08002859 float x = 0.0f;
2860 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002861 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002862 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002863 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2864 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002865 }
2866
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002867 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002868 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002869
2870 int alpha;
2871 SkXfermode::Mode mode;
2872 getAlphaAndMode(paint, &alpha, &mode);
2873
Chris Craikc3566d02013-02-04 16:16:33 -08002874 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002875 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2876 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002877 }
2878
Romain Guy671d6cf2012-01-18 12:39:17 -08002879 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002880 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002881 if (pureTranslate && !linearFilter) {
2882 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2883 }
Romain Guy257ae352013-03-20 16:31:12 -07002884 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002885
2886 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2887 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2888
Romain Guy211370f2012-02-01 16:10:55 -08002889 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002890
Victoria Lease1e546812013-06-25 14:25:17 -07002891 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002892 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002893 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002894 if (hasActiveLayer) {
2895 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002896 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002897 }
2898 dirtyLayerUnchecked(bounds, getRegion());
2899 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002900 }
Chet Haase48659092012-05-31 15:21:51 -07002901
2902 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002903}
2904
Romain Guy624234f2013-03-05 16:43:31 -08002905mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2906 mat4 fontTransform;
2907 if (CC_LIKELY(transform.isPureTranslate())) {
2908 fontTransform = mat4::identity();
2909 } else {
2910 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002911 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002912 } else {
2913 float sx, sy;
2914 currentTransform().decomposeScale(sx, sy);
2915 fontTransform.loadScale(sx, sy, 1.0f);
2916 }
2917 }
2918 return fontTransform;
2919}
2920
Chris Craik41541822013-05-03 16:35:54 -07002921status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2922 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002923 DrawOpMode drawOpMode) {
2924
Chris Craik527a3aa2013-03-04 10:19:31 -08002925 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002926 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2927 // drawing as ops from DeferredDisplayList are already filtered for these
2928 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2929 quickReject(bounds)) {
2930 return DrawGlInfo::kStatusDone;
2931 }
Romain Guycac5fd32011-12-01 20:08:50 -08002932 }
2933
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002934 const float oldX = x;
2935 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002936
2937 const mat4& transform = currentTransform();
2938 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002939
Romain Guy211370f2012-02-01 16:10:55 -08002940 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002941 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2942 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002943 }
2944
Romain Guy86568192010-12-14 15:55:39 -08002945 int alpha;
2946 SkXfermode::Mode mode;
2947 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002948
Romain Guyc74f45a2013-02-26 19:10:14 -08002949 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2950
Chris Craikc3566d02013-02-04 16:16:33 -08002951 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002952 fontRenderer.setFont(paint, mat4::identity());
2953 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2954 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002955 }
2956
Romain Guy19d4dd82013-03-04 11:14:26 -08002957 const bool hasActiveLayer = hasLayer();
2958
Romain Guy624234f2013-03-05 16:43:31 -08002959 // We only pass a partial transform to the font renderer. That partial
2960 // matrix defines how glyphs are rasterized. Typically we want glyphs
2961 // to be rasterized at their final size on screen, which means the partial
2962 // matrix needs to take the scale factor into account.
2963 // When a partial matrix is used to transform glyphs during rasterization,
2964 // the mesh is generated with the inverse transform (in the case of scale,
2965 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2966 // apply the full transform matrix at draw time in the vertex shader.
2967 // Applying the full matrix in the shader is the easiest way to handle
2968 // rotation and perspective and allows us to always generated quads in the
2969 // font renderer which greatly simplifies the code, clipping in particular.
2970 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002971 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002972
Romain Guy6620c6d2010-12-06 18:07:02 -08002973 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002974 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002975 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002976
Romain Guy624234f2013-03-05 16:43:31 -08002977 // TODO: Implement better clipping for scaled/rotated text
2978 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002979 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 -07002980
Raph Levien996e57c2012-07-23 15:22:52 -07002981 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002982 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002983
2984 // don't call issuedrawcommand, do it at end of batch
2985 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002986 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002987 SkPaint paintCopy(*paint);
2988 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2989 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002990 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002991 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002992 status = fontRenderer.renderPosText(paint, 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 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002995
Chris Craik527a3aa2013-03-04 10:19:31 -08002996 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002997 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002998 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002999 }
Chris Craik41541822013-05-03 16:35:54 -07003000 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003001 }
Romain Guy694b5192010-07-21 21:33:20 -07003002
Chris Craik41541822013-05-03 16:35:54 -07003003 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003004
3005 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003006}
3007
Chet Haase48659092012-05-31 15:21:51 -07003008status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003009 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003010 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003011 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003012 }
3013
Chris Craik39a908c2013-06-13 14:39:01 -07003014 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3015 mCaches.enableScissor();
3016
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003017 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003018 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003019 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003020
3021 int alpha;
3022 SkXfermode::Mode mode;
3023 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003024 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003025
Romain Guy97771732012-02-28 18:17:02 -08003026 const Rect* clip = &mSnapshot->getLocalClip();
3027 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 -08003028
Romain Guy97771732012-02-28 18:17:02 -08003029 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003030
3031 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003032 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003033 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003034 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003035 dirtyLayerUnchecked(bounds, getRegion());
3036 }
Romain Guy97771732012-02-28 18:17:02 -08003037 }
Chet Haase48659092012-05-31 15:21:51 -07003038
3039 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003040}
3041
Chet Haase48659092012-05-31 15:21:51 -07003042status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3043 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003044
Romain Guya1d3c912011-12-13 14:55:06 -08003045 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003046
Romain Guyfb8b7632010-08-23 21:05:08 -07003047 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003048 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003049 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003050
Romain Guy8b55f372010-08-18 17:10:07 -07003051 const float x = texture->left - texture->offset;
3052 const float y = texture->top - texture->offset;
3053
Romain Guy01d58e42011-01-19 21:54:02 -08003054 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003055
3056 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003057}
3058
Chris Craika08f95c2013-03-15 17:24:33 -07003059status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003060 if (!layer) {
3061 return DrawGlInfo::kStatusDone;
3062 }
3063
Romain Guyb2e2f242012-10-17 18:18:35 -07003064 mat4* transform = NULL;
3065 if (layer->isTextureLayer()) {
3066 transform = &layer->getTransform();
3067 if (!transform->isIdentity()) {
3068 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003069 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003070 }
3071 }
3072
Chris Craik39a908c2013-06-13 14:39:01 -07003073 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003074 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003075 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003076
3077 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003078 if (transform && !transform->isIdentity()) {
3079 restore();
3080 }
Chet Haase48659092012-05-31 15:21:51 -07003081 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003082 }
3083
Romain Guy5bb3c732012-11-29 17:52:58 -08003084 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003085
Chris Craik39a908c2013-06-13 14:39:01 -07003086 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003087 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003088
Romain Guy211370f2012-02-01 16:10:55 -08003089 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003090 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3091 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003092
Romain Guyc88e3572011-01-22 00:32:12 -08003093 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003094 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3095 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003096 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003097 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003098 setupDraw();
3099 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003100 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003101 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003102 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003103 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003104 setupDrawPureColorUniforms();
3105 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003106 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003107 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3108 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3109 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003110
Romain Guyd21b6e12011-11-30 20:21:23 -08003111 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003112 setupDrawModelViewTranslate(tx, ty,
3113 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003114 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003115 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003116 setupDrawModelViewTranslate(x, y,
3117 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3118 }
Romain Guyf219da52011-01-16 12:54:25 -08003119
Romain Guy448455f2013-07-22 13:57:50 -07003120 TextureVertex* mesh = &layer->mesh[0];
3121 GLsizei elementsCount = layer->meshElementCount;
3122
3123 while (elementsCount > 0) {
3124 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3125
3126 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3127 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3128 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3129
3130 elementsCount -= drawCount;
3131 // Though there are 4 vertices in a quad, we use 6 indices per
3132 // quad to draw with GL_TRIANGLES
3133 mesh += (drawCount / 6) * 4;
3134 }
Romain Guyf219da52011-01-16 12:54:25 -08003135
Romain Guy3a3133d2011-02-01 22:59:58 -08003136#if DEBUG_LAYERS_AS_REGIONS
3137 drawRegionRects(layer->region);
3138#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003139 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003140
Chris Craikc3566d02013-02-04 16:16:33 -08003141 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003142
Romain Guy5bb3c732012-11-29 17:52:58 -08003143 if (layer->debugDrawUpdate) {
3144 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003145 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3146 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3147 }
Romain Guyf219da52011-01-16 12:54:25 -08003148 }
Chris Craik34416ea2013-04-15 16:08:28 -07003149 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003150
Romain Guyb2e2f242012-10-17 18:18:35 -07003151 if (transform && !transform->isIdentity()) {
3152 restore();
3153 }
3154
Chet Haase48659092012-05-31 15:21:51 -07003155 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003156}
3157
Romain Guy6926c722010-07-12 20:20:03 -07003158///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003159// Shaders
3160///////////////////////////////////////////////////////////////////////////////
3161
3162void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003163 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003164}
3165
Romain Guy06f96e22010-07-30 19:18:16 -07003166void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003167 mDrawModifiers.mShader = shader;
3168 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003169 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003170 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003171}
3172
Romain Guyd27977d2010-07-14 19:18:51 -07003173///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003174// Color filters
3175///////////////////////////////////////////////////////////////////////////////
3176
3177void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003178 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003179}
3180
3181void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003182 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003183}
3184
3185///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003186// Drop shadow
3187///////////////////////////////////////////////////////////////////////////////
3188
3189void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003190 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003191}
3192
3193void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003194 mDrawModifiers.mHasShadow = true;
3195 mDrawModifiers.mShadowRadius = radius;
3196 mDrawModifiers.mShadowDx = dx;
3197 mDrawModifiers.mShadowDy = dy;
3198 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003199}
3200
3201///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003202// Draw filters
3203///////////////////////////////////////////////////////////////////////////////
3204
3205void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003206 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3207 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003208 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003209 mDrawModifiers.mPaintFilterClearBits = 0;
3210 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003211}
3212
3213void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003214 mDrawModifiers.mHasDrawFilter = true;
3215 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3216 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003217}
3218
Chris Craika08f95c2013-03-15 17:24:33 -07003219SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003220 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003221 return paint;
3222 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003223
3224 uint32_t flags = paint->getFlags();
3225
3226 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003227 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3228 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003229
3230 return &mFilteredPaint;
3231}
3232
3233///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003234// Drawing implementation
3235///////////////////////////////////////////////////////////////////////////////
3236
Romain Guy3b748a42013-04-17 18:54:38 -07003237Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3238 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3239 if (!texture) {
3240 return mCaches.textureCache.get(bitmap);
3241 }
3242 return texture;
3243}
3244
Romain Guy01d58e42011-01-19 21:54:02 -08003245void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3246 float x, float y, SkPaint* paint) {
3247 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3248 return;
3249 }
3250
3251 int alpha;
3252 SkXfermode::Mode mode;
3253 getAlphaAndMode(paint, &alpha, &mode);
3254
3255 setupDraw();
3256 setupDrawWithTexture(true);
3257 setupDrawAlpha8Color(paint->getColor(), alpha);
3258 setupDrawColorFilter();
3259 setupDrawShader();
3260 setupDrawBlending(true, mode);
3261 setupDrawProgram();
3262 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3263 setupDrawTexture(texture->id);
3264 setupDrawPureColorUniforms();
3265 setupDrawColorFilterUniforms();
3266 setupDrawShaderUniforms();
3267 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3268
3269 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003270}
3271
Romain Guyf607bdc2010-09-10 19:20:06 -07003272// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003273#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3274#define kStdUnderline_Offset (1.0f / 9.0f)
3275#define kStdUnderline_Thickness (1.0f / 18.0f)
3276
Chris Craik41541822013-05-03 16:35:54 -07003277void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003278 float x, float y, SkPaint* paint) {
3279 // Handle underline and strike-through
3280 uint32_t flags = paint->getFlags();
3281 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003282 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003283
Romain Guy211370f2012-02-01 16:10:55 -08003284 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003285 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003286 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003287
Raph Levien8b4072d2012-07-30 15:50:00 -07003288 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003289 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003290
Romain Guyf6834472011-01-23 13:32:12 -08003291 int linesCount = 0;
3292 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3293 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3294
3295 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003296 float points[pointsCount];
3297 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003298
3299 if (flags & SkPaint::kUnderlineText_Flag) {
3300 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003301 points[currentPoint++] = left;
3302 points[currentPoint++] = top;
3303 points[currentPoint++] = left + underlineWidth;
3304 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003305 }
3306
3307 if (flags & SkPaint::kStrikeThruText_Flag) {
3308 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003309 points[currentPoint++] = left;
3310 points[currentPoint++] = top;
3311 points[currentPoint++] = left + underlineWidth;
3312 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003313 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003314
Romain Guy726aeba2011-06-01 14:52:00 -07003315 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003316
Romain Guy726aeba2011-06-01 14:52:00 -07003317 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003318 }
3319 }
3320}
3321
Romain Guy672433d2013-01-04 19:05:13 -08003322status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3323 if (mSnapshot->isIgnored()) {
3324 return DrawGlInfo::kStatusDone;
3325 }
3326
Romain Guy735738c2012-12-03 12:34:51 -08003327 int color = paint->getColor();
3328 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003329 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003330 color |= 0x00ffffff;
3331 }
3332 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3333
3334 return drawColorRects(rects, count, color, mode);
3335}
3336
3337status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003338 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003339 if (count == 0) {
3340 return DrawGlInfo::kStatusDone;
3341 }
Romain Guy735738c2012-12-03 12:34:51 -08003342
Romain Guy672433d2013-01-04 19:05:13 -08003343 float left = FLT_MAX;
3344 float top = FLT_MAX;
3345 float right = FLT_MIN;
3346 float bottom = FLT_MIN;
3347
Romain Guy448455f2013-07-22 13:57:50 -07003348 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003349 Vertex* vertex = mesh;
3350
Chris Craik2af46352012-11-26 18:30:17 -08003351 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003352 float l = rects[index + 0];
3353 float t = rects[index + 1];
3354 float r = rects[index + 2];
3355 float b = rects[index + 3];
3356
Romain Guy3b753822013-03-05 10:27:35 -08003357 Vertex::set(vertex++, l, t);
3358 Vertex::set(vertex++, r, t);
3359 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003360 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003361
Romain Guy3b753822013-03-05 10:27:35 -08003362 left = fminf(left, l);
3363 top = fminf(top, t);
3364 right = fmaxf(right, r);
3365 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003366 }
3367
Romain Guy3b753822013-03-05 10:27:35 -08003368 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003369 return DrawGlInfo::kStatusDone;
3370 }
Romain Guy672433d2013-01-04 19:05:13 -08003371
Romain Guy672433d2013-01-04 19:05:13 -08003372 setupDraw();
3373 setupDrawNoTexture();
3374 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3375 setupDrawShader();
3376 setupDrawColorFilter();
3377 setupDrawBlending(mode);
3378 setupDrawProgram();
3379 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003380 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003381 setupDrawColorUniforms();
3382 setupDrawShaderUniforms();
3383 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003384
Romain Guy8ce00302013-01-15 18:51:42 -08003385 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003386 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003387 }
3388
Romain Guy448455f2013-07-22 13:57:50 -07003389 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003390
3391 return DrawGlInfo::kStatusDrew;
3392}
3393
Romain Guy026c5e162010-06-28 17:12:22 -07003394void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003395 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003396 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003397 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003398 color |= 0x00ffffff;
3399 }
3400
Romain Guy70ca14e2010-12-13 18:24:33 -08003401 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003402 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003403 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003404 setupDrawShader();
3405 setupDrawColorFilter();
3406 setupDrawBlending(mode);
3407 setupDrawProgram();
3408 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3409 setupDrawColorUniforms();
3410 setupDrawShaderUniforms(ignoreTransform);
3411 setupDrawColorFilterUniforms();
3412 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003413
Romain Guyc95c8d62010-09-17 15:31:32 -07003414 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3415}
3416
Romain Guy82ba8142010-07-09 13:25:56 -07003417void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003418 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003419 int alpha;
3420 SkXfermode::Mode mode;
3421 getAlphaAndMode(paint, &alpha, &mode);
3422
Romain Guyd21b6e12011-11-30 20:21:23 -08003423 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003424
Romain Guy3b748a42013-04-17 18:54:38 -07003425 GLvoid* vertices = (GLvoid*) NULL;
3426 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3427
3428 if (texture->uvMapper) {
3429 vertices = &mMeshVertices[0].position[0];
3430 texCoords = &mMeshVertices[0].texture[0];
3431
3432 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3433 texture->uvMapper->map(uvs);
3434
3435 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3436 }
3437
Romain Guy3b753822013-03-05 10:27:35 -08003438 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3439 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3440 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003441
Romain Guyd21b6e12011-11-30 20:21:23 -08003442 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003443 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003444 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3445 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003446 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003447 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003448 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003449 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3450 }
3451
3452 if (texture->uvMapper) {
3453 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003454 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003455}
3456
Romain Guybd6b79b2010-06-26 00:13:53 -07003457void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003458 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3459 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003460 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003461}
3462
3463void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003464 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003465 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003466 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003467
Romain Guy746b7402010-10-26 16:27:31 -07003468 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003469 setupDrawWithTexture();
3470 setupDrawColor(alpha, alpha, alpha, alpha);
3471 setupDrawColorFilter();
3472 setupDrawBlending(blend, mode, swapSrcDst);
3473 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003474 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003475 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003476 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003477 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003478 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003479 }
Romain Guy886b2752013-01-04 12:26:18 -08003480 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003481 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003482 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003483 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003484
Romain Guy6820ac82010-09-15 18:11:50 -07003485 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003486}
3487
Romain Guy3b748a42013-04-17 18:54:38 -07003488void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3489 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3490 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3491 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3492
3493 setupDraw();
3494 setupDrawWithTexture();
3495 setupDrawColor(alpha, alpha, alpha, alpha);
3496 setupDrawColorFilter();
3497 setupDrawBlending(blend, mode, swapSrcDst);
3498 setupDrawProgram();
3499 if (!dirty) setupDrawDirtyRegionsDisabled();
3500 if (!ignoreScale) {
3501 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3502 } else {
3503 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3504 }
3505 setupDrawTexture(texture);
3506 setupDrawPureColorUniforms();
3507 setupDrawColorFilterUniforms();
3508 setupDrawMeshIndices(vertices, texCoords, vbo);
3509
3510 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003511}
3512
Romain Guy886b2752013-01-04 12:26:18 -08003513void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3514 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3515 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003516 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003517
3518 setupDraw();
3519 setupDrawWithTexture(true);
3520 if (hasColor) {
3521 setupDrawAlpha8Color(color, alpha);
3522 }
3523 setupDrawColorFilter();
3524 setupDrawShader();
3525 setupDrawBlending(true, mode);
3526 setupDrawProgram();
3527 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003528 if (!ignoreScale) {
3529 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3530 } else {
3531 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3532 }
Romain Guy886b2752013-01-04 12:26:18 -08003533 setupDrawTexture(texture);
3534 setupDrawPureColorUniforms();
3535 setupDrawColorFilterUniforms();
3536 setupDrawShaderUniforms();
3537 setupDrawMesh(vertices, texCoords);
3538
3539 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003540}
3541
Romain Guya5aed0d2010-09-09 14:42:43 -07003542void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003543 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003544 if (mCountOverdraw) {
3545 if (!mCaches.blend) glEnable(GL_BLEND);
3546 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3547 glBlendFunc(GL_ONE, GL_ONE);
3548 }
3549
3550 mCaches.blend = true;
3551 mCaches.lastSrcMode = GL_ONE;
3552 mCaches.lastDstMode = GL_ONE;
3553
3554 return;
3555 }
3556
Romain Guy82ba8142010-07-09 13:25:56 -07003557 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003558
Romain Guy82ba8142010-07-09 13:25:56 -07003559 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003560 // These blend modes are not supported by OpenGL directly and have
3561 // to be implemented using shaders. Since the shader will perform
3562 // the blending, turn blending off here
3563 // If the blend mode cannot be implemented using shaders, fall
3564 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003565 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003566 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003567 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003568 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003569
Romain Guy82bc7a72012-01-03 14:13:39 -08003570 if (mCaches.blend) {
3571 glDisable(GL_BLEND);
3572 mCaches.blend = false;
3573 }
3574
3575 return;
3576 } else {
3577 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003578 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003579 }
3580
3581 if (!mCaches.blend) {
3582 glEnable(GL_BLEND);
3583 }
3584
3585 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3586 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3587
3588 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3589 glBlendFunc(sourceMode, destMode);
3590 mCaches.lastSrcMode = sourceMode;
3591 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003592 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003593 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003594 glDisable(GL_BLEND);
3595 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003596 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003597}
3598
Romain Guy889f8d12010-07-29 14:37:42 -07003599bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003600 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003601 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003602 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003603 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003604 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003605 }
Romain Guy6926c722010-07-12 20:20:03 -07003606 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003607}
3608
Romain Guy026c5e162010-06-28 17:12:22 -07003609void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003610 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003611 TextureVertex::setUV(v++, u1, v1);
3612 TextureVertex::setUV(v++, u2, v1);
3613 TextureVertex::setUV(v++, u1, v2);
3614 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003615}
3616
Chris Craik16ecda52013-03-29 10:59:59 -07003617void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003618 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003619 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3620 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003621 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003622 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003623 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003624}
3625
Chris Craik16ecda52013-03-29 10:59:59 -07003626float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3627 float alpha;
3628 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3629 alpha = mDrawModifiers.mOverrideLayerAlpha;
3630 } else {
3631 alpha = layer->getAlpha() / 255.0f;
3632 }
3633 return alpha * mSnapshot->alpha;
3634}
3635
Romain Guy9d5316e2010-06-24 19:30:36 -07003636}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003637}; // namespace android