blob: 35fc804594b5d13eb32da002ffd0b18723d73316 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700183 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guyd643bb52011-03-01 14:55:21 -0800454
Romain Guy80911b82011-03-16 15:30:12 -0700455 Rect clip(*mSnapshot->clipRect);
456 clip.snapToPixelBoundaries();
457
Romain Guyd643bb52011-03-01 14:55:21 -0800458 // Since we don't know what the functor will draw, let's dirty
459 // tne entire clip region
460 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800461 dirtyLayerUnchecked(clip, getRegion());
462 }
Romain Guyd643bb52011-03-01 14:55:21 -0800463
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 DrawGlInfo info;
465 info.clipLeft = clip.left;
466 info.clipTop = clip.top;
467 info.clipRight = clip.right;
468 info.clipBottom = clip.bottom;
469 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700470 info.width = getSnapshot()->viewport.getWidth();
471 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700473
John Reck25d2f7b2013-09-10 13:12:09 -0700474 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700475 // setup GL state for functor
476 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700477 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
478 }
John Reck25d2f7b2013-09-10 13:12:09 -0700479 if (mCaches.enableScissor() || dirtyClip) {
480 setScissorFromClip();
481 }
Chris Craik54f574a2013-08-26 11:23:46 -0700482 interrupt();
483
484 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700485 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800486
Romain Guy8f3b8e32012-03-27 16:33:45 -0700487 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700488 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800489 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700490
Chris Craik65924a32012-04-05 17:52:11 -0700491 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700492 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700493 }
Romain Guycabfcc12011-03-07 18:06:46 -0800494 }
495
Chet Haasedaf98e92011-01-10 14:10:36 -0800496 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700497 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800498}
499
Romain Guyf6a11b82010-06-23 17:47:49 -0700500///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700501// Debug
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guy0f667532013-03-01 14:31:04 -0800504void OpenGLRenderer::eventMark(const char* name) const {
505 mCaches.eventMark(0, name);
506}
507
Romain Guy87e2f7572012-09-24 11:37:12 -0700508void OpenGLRenderer::startMark(const char* name) const {
509 mCaches.startMark(0, name);
510}
511
512void OpenGLRenderer::endMark() const {
513 mCaches.endMark();
514}
515
516void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
517 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
518 if (clear) {
519 mCaches.disableScissor();
520 mCaches.stencil.clear();
521 }
522 if (enable) {
523 mCaches.stencil.enableDebugWrite();
524 } else {
525 mCaches.stencil.disable();
526 }
527 }
528}
529
530void OpenGLRenderer::renderOverdraw() {
531 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700532 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700533
534 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700535 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700536 clip->right - clip->left, clip->bottom - clip->top);
537
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700539 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700540 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
541
542 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700543 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700544 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
545
546 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700547 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700548 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
549
550 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700551 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700552 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
553
Romain Guy87e2f7572012-09-24 11:37:12 -0700554 mCaches.stencil.disable();
555 }
556}
557
Romain Guy78dd96d2013-05-03 14:24:16 -0700558void OpenGLRenderer::countOverdraw() {
559 size_t count = mWidth * mHeight;
560 uint32_t* buffer = new uint32_t[count];
561 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
562
563 size_t total = 0;
564 for (size_t i = 0; i < count; i++) {
565 total += buffer[i] & 0xff;
566 }
567
568 mOverdraw = total / float(count);
569
570 delete[] buffer;
571}
572
Romain Guy87e2f7572012-09-24 11:37:12 -0700573///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700574// Layers
575///////////////////////////////////////////////////////////////////////////////
576
577bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (layer->deferredUpdateScheduled && layer->renderer &&
579 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700580 ATRACE_CALL();
581
Romain Guy11cb6422012-09-21 00:39:43 -0700582 Rect& dirty = layer->dirtyRect;
583
Romain Guy7c450aa2012-09-21 19:15:00 -0700584 if (inFrame) {
585 endTiling();
586 debugOverdraw(false, false);
587 }
Romain Guy11cb6422012-09-21 00:39:43 -0700588
Romain Guy96885eb2013-03-26 15:05:58 -0700589 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700590 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700591 } else {
592 layer->defer();
593 }
Romain Guy11cb6422012-09-21 00:39:43 -0700594
595 if (inFrame) {
596 resumeAfterLayer();
597 startTiling(mSnapshot);
598 }
599
Romain Guy5bb3c732012-11-29 17:52:58 -0800600 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700601 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700602
603 return true;
604 }
605
606 return false;
607}
608
609void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700610 // If draw deferring is enabled this method will simply defer
611 // the display list of each individual layer. The layers remain
612 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700613 int count = mLayerUpdates.size();
614 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700615 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
616 startMark("Layer Updates");
617 } else {
618 startMark("Defer Layer Updates");
619 }
Romain Guy11cb6422012-09-21 00:39:43 -0700620
Chris Craik1206b9b2013-04-04 14:46:24 -0700621 // Note: it is very important to update the layers in order
622 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700623 Layer* layer = mLayerUpdates.itemAt(i);
624 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700625 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
626 mCaches.resourceCache.decrementRefcount(layer);
627 }
Romain Guy11cb6422012-09-21 00:39:43 -0700628 }
Romain Guy11cb6422012-09-21 00:39:43 -0700629
Romain Guy96885eb2013-03-26 15:05:58 -0700630 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
631 mLayerUpdates.clear();
632 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
633 }
634 endMark();
635 }
636}
637
638void OpenGLRenderer::flushLayers() {
639 int count = mLayerUpdates.size();
640 if (count > 0) {
641 startMark("Apply Layer Updates");
642 char layerName[12];
643
Chris Craik1206b9b2013-04-04 14:46:24 -0700644 // Note: it is very important to update the layers in order
645 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700646 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700647 startMark(layerName);
648
Romain Guy40543602013-06-12 15:31:28 -0700649 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700650 Layer* layer = mLayerUpdates.itemAt(i);
651 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700652 ATRACE_END();
653
Romain Guy02b49b72013-03-29 12:37:16 -0700654 mCaches.resourceCache.decrementRefcount(layer);
655
Romain Guy96885eb2013-03-26 15:05:58 -0700656 endMark();
657 }
658
659 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700660 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700661
Romain Guy11cb6422012-09-21 00:39:43 -0700662 endMark();
663 }
664}
665
666void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
667 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700668 // Make sure we don't introduce duplicates.
669 // SortedVector would do this automatically but we need to respect
670 // the insertion order. The linear search is not an issue since
671 // this list is usually very short (typically one item, at most a few)
672 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
673 if (mLayerUpdates.itemAt(i) == layer) {
674 return;
675 }
676 }
Romain Guy11cb6422012-09-21 00:39:43 -0700677 mLayerUpdates.push_back(layer);
678 mCaches.resourceCache.incrementRefcount(layer);
679 }
680}
681
Romain Guye93482f2013-06-17 13:14:51 -0700682void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
683 if (layer) {
684 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
685 if (mLayerUpdates.itemAt(i) == layer) {
686 mLayerUpdates.removeAt(i);
687 mCaches.resourceCache.decrementRefcount(layer);
688 break;
689 }
690 }
691 }
692}
693
Romain Guy11cb6422012-09-21 00:39:43 -0700694void OpenGLRenderer::clearLayerUpdates() {
695 size_t count = mLayerUpdates.size();
696 if (count > 0) {
697 mCaches.resourceCache.lock();
698 for (size_t i = 0; i < count; i++) {
699 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
700 }
701 mCaches.resourceCache.unlock();
702 mLayerUpdates.clear();
703 }
704}
705
Romain Guy40543602013-06-12 15:31:28 -0700706void OpenGLRenderer::flushLayerUpdates() {
707 syncState();
708 updateLayers();
709 flushLayers();
710 // Wait for all the layer updates to be executed
711 AutoFence fence;
712}
713
Romain Guy11cb6422012-09-21 00:39:43 -0700714///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700715// State management
716///////////////////////////////////////////////////////////////////////////////
717
Romain Guybb9524b2010-06-22 18:56:38 -0700718int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700719 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700720}
721
722int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700723 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
726void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700727 if (mSaveCount > 1) {
728 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700729 }
Romain Guybb9524b2010-06-22 18:56:38 -0700730}
731
732void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700733 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700734
Romain Guy8fb95422010-08-17 18:38:51 -0700735 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700736 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700737 }
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
Romain Guy8aef54f2010-09-01 15:13:49 -0700740int OpenGLRenderer::saveSnapshot(int flags) {
741 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700742 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700743}
744
745bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700746 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700747 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700748 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700749
Romain Guybd6b79b2010-06-26 00:13:53 -0700750 sp<Snapshot> current = mSnapshot;
751 sp<Snapshot> previous = mSnapshot->previous;
752
Romain Guyeb993562010-10-05 18:14:38 -0700753 if (restoreOrtho) {
754 Rect& r = previous->viewport;
755 glViewport(r.left, r.top, r.right, r.bottom);
756 mOrthoMatrix.load(current->orthoMatrix);
757 }
758
Romain Guy8b55f372010-08-18 17:10:07 -0700759 mSaveCount--;
760 mSnapshot = previous;
761
Romain Guy2542d192010-08-18 11:47:12 -0700762 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700763 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700764 }
Romain Guy2542d192010-08-18 11:47:12 -0700765
Romain Guy5ec99242010-11-03 16:19:08 -0700766 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700767 endMark(); // Savelayer
768 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700769 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700770 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700771 }
772
Romain Guy2542d192010-08-18 11:47:12 -0700773 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700774}
775
Romain Guyf6a11b82010-06-23 17:47:49 -0700776///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700777// Layers
778///////////////////////////////////////////////////////////////////////////////
779
780int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800781 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700782 const GLuint previousFbo = mSnapshot->fbo;
783 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700784
Romain Guyaf636eb2010-12-09 17:47:21 -0800785 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700786 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700787 }
Romain Guyd55a8612010-06-28 17:42:46 -0700788
789 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700790}
791
Chris Craikd90144d2013-03-19 15:03:48 -0700792void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
793 const Rect untransformedBounds(bounds);
794
795 currentTransform().mapRect(bounds);
796
797 // Layers only make sense if they are in the framebuffer's bounds
798 if (bounds.intersect(*mSnapshot->clipRect)) {
799 // We cannot work with sub-pixels in this case
800 bounds.snapToPixelBoundaries();
801
802 // When the layer is not an FBO, we may use glCopyTexImage so we
803 // need to make sure the layer does not extend outside the bounds
804 // of the framebuffer
805 if (!bounds.intersect(mSnapshot->previous->viewport)) {
806 bounds.setEmpty();
807 } else if (fboLayer) {
808 clip.set(bounds);
809 mat4 inverse;
810 inverse.loadInverse(currentTransform());
811 inverse.mapRect(clip);
812 clip.snapToPixelBoundaries();
813 if (clip.intersect(untransformedBounds)) {
814 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
815 bounds.set(untransformedBounds);
816 } else {
817 clip.setEmpty();
818 }
819 }
820 } else {
821 bounds.setEmpty();
822 }
823}
824
Chris Craik408eb122013-03-26 18:55:15 -0700825void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
826 bool fboLayer, int alpha) {
827 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
828 bounds.getHeight() > mCaches.maxTextureSize ||
829 (fboLayer && clip.isEmpty())) {
830 mSnapshot->empty = fboLayer;
831 } else {
832 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
833 }
834}
835
Chris Craikd90144d2013-03-19 15:03:48 -0700836int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
837 int alpha, SkXfermode::Mode mode, int flags) {
838 const GLuint previousFbo = mSnapshot->fbo;
839 const int count = saveSnapshot(flags);
840
841 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
842 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
843 // operations will be able to store and restore the current clip and transform info, and
844 // quick rejection will be correct (for display lists)
845
846 Rect bounds(left, top, right, bottom);
847 Rect clip;
848 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700849 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700850
Chris Craik408eb122013-03-26 18:55:15 -0700851 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700852 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
853 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700854 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700855 }
856 }
857
858 return count;
859}
860
861
Romain Guy1c740bc2010-09-13 18:00:09 -0700862/**
863 * Layers are viewed by Skia are slightly different than layers in image editing
864 * programs (for instance.) When a layer is created, previously created layers
865 * and the frame buffer still receive every drawing command. For instance, if a
866 * layer is created and a shape intersecting the bounds of the layers and the
867 * framebuffer is draw, the shape will be drawn on both (unless the layer was
868 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
869 *
870 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
871 * texture. Unfortunately, this is inefficient as it requires every primitive to
872 * be drawn n + 1 times, where n is the number of active layers. In practice this
873 * means, for every primitive:
874 * - Switch active frame buffer
875 * - Change viewport, clip and projection matrix
876 * - Issue the drawing
877 *
878 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700879 * To avoid this, layers are implemented in a different way here, at least in the
880 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
881 * is set. When this flag is set we can redirect all drawing operations into a
882 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700883 *
884 * This implementation relies on the frame buffer being at least RGBA 8888. When
885 * a layer is created, only a texture is created, not an FBO. The content of the
886 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700887 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700888 * buffer and drawing continues as normal. This technique therefore treats the
889 * frame buffer as a scratch buffer for the layers.
890 *
891 * To compose the layers back onto the frame buffer, each layer texture
892 * (containing the original frame buffer data) is drawn as a simple quad over
893 * the frame buffer. The trick is that the quad is set as the composition
894 * destination in the blending equation, and the frame buffer becomes the source
895 * of the composition.
896 *
897 * Drawing layers with an alpha value requires an extra step before composition.
898 * An empty quad is drawn over the layer's region in the frame buffer. This quad
899 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
900 * quad is used to multiply the colors in the frame buffer. This is achieved by
901 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
902 * GL_ZERO, GL_SRC_ALPHA.
903 *
904 * Because glCopyTexImage2D() can be slow, an alternative implementation might
905 * be use to draw a single clipped layer. The implementation described above
906 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700907 *
908 * (1) The frame buffer is actually not cleared right away. To allow the GPU
909 * to potentially optimize series of calls to glCopyTexImage2D, the frame
910 * buffer is left untouched until the first drawing operation. Only when
911 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700912 */
Chet Haased48885a2012-08-28 17:43:28 -0700913bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
914 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700915 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700916 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700917
Romain Guyeb993562010-10-05 18:14:38 -0700918 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
919
Romain Guyf607bdc2010-09-10 19:20:06 -0700920 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700921 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700922 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700923 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700924 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700925
926 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700927 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700928 return false;
929 }
Romain Guyf18fd992010-07-08 11:45:51 -0700930
Romain Guya1d3c912011-12-13 14:55:06 -0800931 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700932 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700933 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700934 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700935 }
936
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700938 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700939 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
940 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800941 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700942 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700943 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700944
Romain Guy8fb95422010-08-17 18:38:51 -0700945 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700946 mSnapshot->flags |= Snapshot::kFlagIsLayer;
947 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700948
Chris Craik7273daa2013-03-28 11:25:24 -0700949 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700950 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700951 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700952 } else {
953 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800955 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700956 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700957 // Workaround for some GL drivers. When reading pixels lying outside
958 // of the window we should get undefined values for those pixels.
959 // Unfortunately some drivers will turn the entire target texture black
960 // when reading outside of the window.
961 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
962 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700963 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800964 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700965
Romain Guyb254c242013-06-27 17:15:24 -0700966 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
967 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
968
Romain Guy54be1cd2011-06-13 19:04:27 -0700969 // Enqueue the buffer coordinates to clear the corresponding region later
970 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700971 }
Romain Guyeb993562010-10-05 18:14:38 -0700972 }
Romain Guyf86ef572010-07-01 11:05:42 -0700973
Romain Guyd55a8612010-06-28 17:42:46 -0700974 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700975}
976
Chet Haased48885a2012-08-28 17:43:28 -0700977bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800978 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700979 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700980
Chet Haased48885a2012-08-28 17:43:28 -0700981 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800982 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
983 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700984 mSnapshot->fbo = layer->getFbo();
985 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
986 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
987 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
988 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700989 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700990
Romain Guy2b7028e2012-09-19 17:25:38 -0700991 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700992 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700993 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
995 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700996
997 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700999 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -07001000 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001001 }
1002
1003 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001004 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
Romain Guyf735c8e2013-01-31 17:45:55 -08001006 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001007
1008 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001009 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001010 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001011 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001012 glClear(GL_COLOR_BUFFER_BIT);
1013
1014 dirtyClip();
1015
1016 // Change the ortho projection
1017 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1018 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1019
1020 return true;
1021}
1022
Romain Guy1c740bc2010-09-13 18:00:09 -07001023/**
1024 * Read the documentation of createLayer() before doing anything in this method.
1025 */
Romain Guy1d83e192010-08-17 11:37:00 -07001026void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1027 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001028 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001029 return;
1030 }
1031
Romain Guy8ce00302013-01-15 18:51:42 -08001032 Layer* layer = current->layer;
1033 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001035
Chris Craik39a908c2013-06-13 14:39:01 -07001036 bool clipRequired = false;
1037 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1038 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1039
Romain Guyeb993562010-10-05 18:14:38 -07001040 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001041 endTiling();
1042
Romain Guye0aa84b2012-04-03 19:30:26 -07001043 // Detach the texture from the FBO
1044 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001045
1046 layer->removeFbo(false);
1047
Romain Guyeb993562010-10-05 18:14:38 -07001048 // Unbind current FBO and restore previous one
1049 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001050 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001051
1052 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001053 }
1054
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001056 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001058 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001059 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001060 }
1061
Romain Guy03750a02010-10-18 14:06:08 -07001062 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001063
Romain Guya1d3c912011-12-13 14:55:06 -08001064 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001065
Romain Guy5b3b3522010-10-27 18:57:51 -07001066 // When the layer is stored in an FBO, we can save a bit of fillrate by
1067 // drawing only the dirty region
1068 if (fboLayer) {
1069 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001070 if (layer->getColorFilter()) {
1071 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001072 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001073 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001075 resetColorFilter();
1076 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001077 } else if (!rect.isEmpty()) {
1078 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1079 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001080 }
Romain Guy8b55f372010-08-18 17:10:07 -07001081
Romain Guy746b7402010-10-26 16:27:31 -07001082 dirtyClip();
1083
Romain Guyeb993562010-10-05 18:14:38 -07001084 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001085 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001086 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001087 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001088 }
1089}
1090
Romain Guyaa6c24c2011-04-28 18:40:04 -07001091void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001092 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001093
1094 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001095 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001096 setupDrawWithTexture();
1097 } else {
1098 setupDrawWithExternalTexture();
1099 }
1100 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001101 setupDrawColor(alpha, alpha, alpha, alpha);
1102 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001103 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001104 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001105 setupDrawPureColorUniforms();
1106 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001107 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1108 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001109 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001110 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001111 }
Romain Guy3b753822013-03-05 10:27:35 -08001112 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001113 layer->getWidth() == (uint32_t) rect.getWidth() &&
1114 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001115 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1116 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001117
Romain Guyd21b6e12011-11-30 20:21:23 -08001118 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001119 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1120 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001121 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001122 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1123 }
1124 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001125 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1126
1127 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001128}
1129
Romain Guy5b3b3522010-10-27 18:57:51 -07001130void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001131 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001132 const Rect& texCoords = layer->texCoords;
1133 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1134 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001135
Romain Guy9ace8f52011-07-07 20:50:11 -07001136 float x = rect.left;
1137 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001138 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001139 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001140 layer->getHeight() == (uint32_t) rect.getHeight();
1141
1142 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001143 // When we're swapping, the layer is already in screen coordinates
1144 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001145 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1146 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001147 }
1148
Romain Guyd21b6e12011-11-30 20:21:23 -08001149 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001150 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001151 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001152 }
1153
Chris Craik16ecda52013-03-29 10:59:59 -07001154 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001155 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001157 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001158 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1159 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Romain Guyaa6c24c2011-04-28 18:40:04 -07001161 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1162 } else {
1163 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1164 drawTextureLayer(layer, rect);
1165 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1166 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001167}
1168
Chris Craik34416ea2013-04-15 16:08:28 -07001169/**
1170 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1171 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1172 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1173 * by saveLayer's restore
1174 */
1175#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1176 DRAW_COMMAND; \
1177 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1178 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1179 DRAW_COMMAND; \
1180 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1181 } \
1182 }
1183
1184#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1185
Romain Guy5b3b3522010-10-27 18:57:51 -07001186void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001187 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001188 layer->setRegionAsRect();
1189
Chris Craik34416ea2013-04-15 16:08:28 -07001190 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001191
Romain Guy5b3b3522010-10-27 18:57:51 -07001192 layer->region.clear();
1193 return;
1194 }
1195
Romain Guy211370f2012-02-01 16:10:55 -08001196 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001197 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001198 const android::Rect* rects;
1199 Region safeRegion;
1200 if (CC_LIKELY(hasRectToRectTransform())) {
1201 rects = layer->region.getArray(&count);
1202 } else {
1203 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1204 rects = safeRegion.getArray(&count);
1205 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001206
Chris Craik16ecda52013-03-29 10:59:59 -07001207 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001208 const float texX = 1.0f / float(layer->getWidth());
1209 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001210 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001211
Romain Guy8ce00302013-01-15 18:51:42 -08001212 setupDraw();
1213
1214 // We must get (and therefore bind) the region mesh buffer
1215 // after we setup drawing in case we need to mess with the
1216 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001217 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001218 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001219
Romain Guy7230a742011-01-10 22:26:16 -08001220 setupDrawWithTexture();
1221 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001222 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001223 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001224 setupDrawProgram();
1225 setupDrawDirtyRegionsDisabled();
1226 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001227 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001228 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001229 if (currentTransform().isPureTranslate()) {
1230 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1231 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001232
Romain Guyd21b6e12011-11-30 20:21:23 -08001233 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001234 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1235 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001236 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001237 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1238 }
Romain Guy15bc6432011-12-13 13:11:32 -08001239 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001240
1241 for (size_t i = 0; i < count; i++) {
1242 const android::Rect* r = &rects[i];
1243
1244 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001245 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001246 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001247 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001248
1249 // TODO: Reject quads outside of the clip
1250 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1251 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1252 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1253 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1254
1255 numQuads++;
1256
Romain Guy31e08e92013-06-18 15:53:53 -07001257 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001258 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1259 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001260 numQuads = 0;
1261 mesh = mCaches.getRegionMesh();
1262 }
1263 }
1264
1265 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001266 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1267 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001268 }
1269
Romain Guy5b3b3522010-10-27 18:57:51 -07001270#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001271 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001272#endif
1273
1274 layer->region.clear();
1275 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001276}
1277
Romain Guy3a3133d2011-02-01 22:59:58 -08001278void OpenGLRenderer::drawRegionRects(const Region& region) {
1279#if DEBUG_LAYERS_AS_REGIONS
1280 size_t count;
1281 const android::Rect* rects = region.getArray(&count);
1282
1283 uint32_t colors[] = {
1284 0x7fff0000, 0x7f00ff00,
1285 0x7f0000ff, 0x7fff00ff,
1286 };
1287
1288 int offset = 0;
1289 int32_t top = rects[0].top;
1290
1291 for (size_t i = 0; i < count; i++) {
1292 if (top != rects[i].top) {
1293 offset ^= 0x2;
1294 top = rects[i].top;
1295 }
1296
1297 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1298 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1299 SkXfermode::kSrcOver_Mode);
1300 }
1301#endif
1302}
1303
Romain Guy8ce00302013-01-15 18:51:42 -08001304void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1305 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001306 Vector<float> rects;
1307
1308 SkRegion::Iterator it(region);
1309 while (!it.done()) {
1310 const SkIRect& r = it.rect();
1311 rects.push(r.fLeft);
1312 rects.push(r.fTop);
1313 rects.push(r.fRight);
1314 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001315 it.next();
1316 }
1317
Romain Guy448455f2013-07-22 13:57:50 -07001318 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001319}
1320
Romain Guy5b3b3522010-10-27 18:57:51 -07001321void OpenGLRenderer::dirtyLayer(const float left, const float top,
1322 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001323 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001324 Rect bounds(left, top, right, bottom);
1325 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001326 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001327 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001328}
1329
1330void OpenGLRenderer::dirtyLayer(const float left, const float top,
1331 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001332 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001333 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001334 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001335 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001336}
1337
1338void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001339 if (bounds.intersect(*mSnapshot->clipRect)) {
1340 bounds.snapToPixelBoundaries();
1341 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1342 if (!dirty.isEmpty()) {
1343 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001344 }
1345 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001346}
1347
Romain Guy448455f2013-07-22 13:57:50 -07001348void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1349 GLsizei elementsCount = quadsCount * 6;
1350 while (elementsCount > 0) {
1351 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1352
1353 setupDrawIndexedVertices(&mesh[0].position[0]);
1354 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1355
1356 elementsCount -= drawCount;
1357 // Though there are 4 vertices in a quad, we use 6 indices per
1358 // quad to draw with GL_TRIANGLES
1359 mesh += (drawCount / 6) * 4;
1360 }
1361}
1362
Romain Guy54be1cd2011-06-13 19:04:27 -07001363void OpenGLRenderer::clearLayerRegions() {
1364 const size_t count = mLayers.size();
1365 if (count == 0) return;
1366
1367 if (!mSnapshot->isIgnored()) {
1368 // Doing several glScissor/glClear here can negatively impact
1369 // GPUs with a tiler architecture, instead we draw quads with
1370 // the Clear blending mode
1371
1372 // The list contains bounds that have already been clipped
1373 // against their initial clip rect, and the current clip
1374 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001375 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001376
Romain Guy448455f2013-07-22 13:57:50 -07001377 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001378 Vertex* vertex = mesh;
1379
1380 for (uint32_t i = 0; i < count; i++) {
1381 Rect* bounds = mLayers.itemAt(i);
1382
Romain Guy54be1cd2011-06-13 19:04:27 -07001383 Vertex::set(vertex++, bounds->left, bounds->top);
1384 Vertex::set(vertex++, bounds->right, bounds->top);
1385 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001386 Vertex::set(vertex++, bounds->right, bounds->bottom);
1387
1388 delete bounds;
1389 }
Romain Guye67307c2013-02-11 18:01:20 -08001390 // We must clear the list of dirty rects before we
1391 // call setupDraw() to prevent stencil setup to do
1392 // the same thing again
1393 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001394
1395 setupDraw(false);
1396 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1397 setupDrawBlending(true, SkXfermode::kClear_Mode);
1398 setupDrawProgram();
1399 setupDrawPureColorUniforms();
1400 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1401
Romain Guy448455f2013-07-22 13:57:50 -07001402 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001403
1404 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001405 } else {
1406 for (uint32_t i = 0; i < count; i++) {
1407 delete mLayers.itemAt(i);
1408 }
Romain Guye67307c2013-02-11 18:01:20 -08001409 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001410 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001411}
1412
Romain Guybd6b79b2010-06-26 00:13:53 -07001413///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001414// State Deferral
1415///////////////////////////////////////////////////////////////////////////////
1416
Chris Craikff785832013-03-08 13:12:16 -08001417bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001418 const Rect& currentClip = *(mSnapshot->clipRect);
1419 const mat4& currentMatrix = *(mSnapshot->transform);
1420
Chris Craikff785832013-03-08 13:12:16 -08001421 if (stateDeferFlags & kStateDeferFlag_Draw) {
1422 // state has bounds initialized in local coordinates
1423 if (!state.mBounds.isEmpty()) {
1424 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001425 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001426 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1427 // is used, it should more closely duplicate the quickReject logic (in how it uses
1428 // snapToPixelBoundaries)
1429
Chris Craik28ce94a2013-05-31 11:38:03 -07001430 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001431 // quick rejected
1432 return true;
1433 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001434
Chris Craika02c4ed2013-06-14 13:43:58 -07001435 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001436 if (!currentClip.contains(state.mBounds)) {
1437 int& flags = state.mClipSideFlags;
1438 // op partially clipped, so record which sides are clipped for clip-aware merging
1439 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1440 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1441 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1442 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1443 }
1444 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001445 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001446 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1447 // overdraw avoidance (since we don't know what it overlaps)
1448 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001449 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001450 }
1451 }
1452
Chris Craik527a3aa2013-03-04 10:19:31 -08001453 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1454 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001455 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001456 }
1457
Chris Craik7273daa2013-03-28 11:25:24 -07001458 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1459 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001460 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001461 state.mDrawModifiers = mDrawModifiers;
1462 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001463 return false;
1464}
1465
Chris Craik527a3aa2013-03-04 10:19:31 -08001466void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001467 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001468 mDrawModifiers = state.mDrawModifiers;
1469 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001470
Chris Craik527a3aa2013-03-04 10:19:31 -08001471 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001472 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1473 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001474 dirtyClip();
1475 }
Chris Craikc3566d02013-02-04 16:16:33 -08001476}
1477
Chris Craik28ce94a2013-05-31 11:38:03 -07001478/**
1479 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1480 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1481 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1482 *
1483 * This method should be called when restoreDisplayState() won't be restoring the clip
1484 */
1485void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1486 if (clipRect != NULL) {
1487 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1488 } else {
1489 mSnapshot->setClip(0, 0, mWidth, mHeight);
1490 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001491 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001492 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001493}
1494
Chris Craikc3566d02013-02-04 16:16:33 -08001495///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001496// Transforms
1497///////////////////////////////////////////////////////////////////////////////
1498
1499void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001500 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001501}
1502
1503void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001504 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001505}
1506
1507void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001508 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001509}
1510
Romain Guy807daf72011-01-18 11:19:19 -08001511void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001512 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001513}
1514
Romain Guyf6a11b82010-06-23 17:47:49 -07001515void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001516 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001517 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001518 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001519 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001520 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001521}
1522
Chris Craikb98a0162013-02-21 11:30:22 -08001523bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001524 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001525}
1526
Romain Guyf6a11b82010-06-23 17:47:49 -07001527void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001528 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001529}
1530
1531void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001532 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001533 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001534 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001535 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001536}
1537
1538///////////////////////////////////////////////////////////////////////////////
1539// Clipping
1540///////////////////////////////////////////////////////////////////////////////
1541
Romain Guybb9524b2010-06-22 18:56:38 -07001542void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001543 Rect clip(*mSnapshot->clipRect);
1544 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001545
Romain Guy8a4ac612012-07-17 17:32:48 -07001546 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1547 clip.getWidth(), clip.getHeight())) {
1548 mDirtyClip = false;
1549 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001550}
1551
Romain Guy8ce00302013-01-15 18:51:42 -08001552void OpenGLRenderer::ensureStencilBuffer() {
1553 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1554 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1555 // just hope we have one when hasLayer() returns false.
1556 if (hasLayer()) {
1557 attachStencilBufferToLayer(mSnapshot->layer);
1558 }
1559}
1560
1561void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1562 // The layer's FBO is already bound when we reach this stage
1563 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001564 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1565 // is attached after we initiated tiling. We must turn it off,
1566 // attach the new render buffer then turn tiling back on
1567 endTiling();
1568
Romain Guy8d4aeb72013-02-12 16:08:55 -08001569 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001570 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001571 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001572
Romain Guyf735c8e2013-01-31 17:45:55 -08001573 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001574 }
1575}
1576
1577void OpenGLRenderer::setStencilFromClip() {
1578 if (!mCaches.debugOverdraw) {
1579 if (!mSnapshot->clipRegion->isEmpty()) {
1580 // NOTE: The order here is important, we must set dirtyClip to false
1581 // before any draw call to avoid calling back into this method
1582 mDirtyClip = false;
1583
1584 ensureStencilBuffer();
1585
1586 mCaches.stencil.enableWrite();
1587
1588 // Clear the stencil but first make sure we restrict drawing
1589 // to the region's bounds
1590 bool resetScissor = mCaches.enableScissor();
1591 if (resetScissor) {
1592 // The scissor was not set so we now need to update it
1593 setScissorFromClip();
1594 }
1595 mCaches.stencil.clear();
1596 if (resetScissor) mCaches.disableScissor();
1597
1598 // NOTE: We could use the region contour path to generate a smaller mesh
1599 // Since we are using the stencil we could use the red book path
1600 // drawing technique. It might increase bandwidth usage though.
1601
1602 // The last parameter is important: we are not drawing in the color buffer
1603 // so we don't want to dirty the current layer, if any
1604 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1605
1606 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001607
1608 // Draw the region used to generate the stencil if the appropriate debug
1609 // mode is enabled
1610 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1611 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1612 }
Romain Guy8ce00302013-01-15 18:51:42 -08001613 } else {
1614 mCaches.stencil.disable();
1615 }
1616 }
1617}
1618
Romain Guy9d5316e2010-06-24 19:30:36 -07001619const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001620 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001621}
1622
Chris Craik39a908c2013-06-13 14:39:01 -07001623bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001624 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001625 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001626 return true;
1627 }
1628
1629 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001630 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001631 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001632
1633 Rect clipRect(*mSnapshot->clipRect);
1634 clipRect.snapToPixelBoundaries();
1635
Chris Craik39a908c2013-06-13 14:39:01 -07001636 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001637
Chris Craik39a908c2013-06-13 14:39:01 -07001638 if (clipRequired) *clipRequired = !clipRect.contains(r);
1639 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001640}
1641
Romain Guy672433d2013-01-04 19:05:13 -08001642bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1643 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001644 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1645 // the final mapped rect to ensure correct clipping behavior for the ramp.
1646 bool snapOut = paint->isAntiAlias();
1647
Chris Craikcb4d6002012-09-25 12:00:29 -07001648 if (paint->getStyle() != SkPaint::kFill_Style) {
1649 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001650 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001651 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001652 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001653 }
1654}
1655
Chris Craik5e49b302013-07-30 19:05:20 -07001656bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001657 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001658 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001659 return true;
1660 }
1661
Chris Craik39a908c2013-06-13 14:39:01 -07001662 if (!isDeferred()) {
1663 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001664 }
Chris Craik39a908c2013-06-13 14:39:01 -07001665 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001666}
1667
Romain Guy8ce00302013-01-15 18:51:42 -08001668void OpenGLRenderer::debugClip() {
1669#if DEBUG_CLIP_REGIONS
1670 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1671 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1672 }
1673#endif
1674}
1675
Romain Guy079ba2c2010-07-16 14:12:24 -07001676bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001677 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001678 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1679 if (clipped) {
1680 dirtyClip();
1681 }
1682 return !mSnapshot->clipRect->isEmpty();
1683 }
1684
1685 SkPath path;
1686 path.addRect(left, top, right, bottom);
1687
Romain Guyb7b93e02013-08-01 15:29:25 -07001688 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001689}
1690
1691bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1692 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001693 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001694
1695 SkPath transformed;
1696 path->transform(transform, &transformed);
1697
1698 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001699 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1700 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001701 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001702 if (mSnapshot->previous == mFirstSnapshot) {
1703 clip.setRect(0, 0, mWidth, mHeight);
1704 } else {
1705 Rect* bounds = mSnapshot->previous->clipRect;
1706 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1707 }
Romain Guy8ce00302013-01-15 18:51:42 -08001708 }
1709
1710 SkRegion region;
1711 region.setPath(transformed, clip);
1712
1713 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001714 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001715 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001716 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001717 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001718}
1719
Romain Guy735738c2012-12-03 12:34:51 -08001720bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001721 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1722 if (clipped) {
1723 dirtyClip();
1724 }
1725 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001726}
1727
Chet Haasea23eed82012-04-12 15:19:04 -07001728Rect* OpenGLRenderer::getClipRect() {
1729 return mSnapshot->clipRect;
1730}
1731
Romain Guyf6a11b82010-06-23 17:47:49 -07001732///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001733// Drawing commands
1734///////////////////////////////////////////////////////////////////////////////
1735
Romain Guy54be1cd2011-06-13 19:04:27 -07001736void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001737 // TODO: It would be best if we could do this before quickReject()
1738 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001739 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001740 // Make sure setScissor & setStencil happen at the beginning of
1741 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001742 if (mDirtyClip) {
1743 if (mCaches.scissorEnabled) {
1744 setScissorFromClip();
1745 }
Romain Guy8ce00302013-01-15 18:51:42 -08001746 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001747 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001748
Romain Guy70ca14e2010-12-13 18:24:33 -08001749 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001750
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 mSetShaderColor = false;
1752 mColorSet = false;
1753 mColorA = mColorR = mColorG = mColorB = 0.0f;
1754 mTextureUnit = 0;
1755 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001756
1757 // Enable debug highlight when what we're about to draw is tested against
1758 // the stencil buffer and if stencil highlight debugging is on
1759 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1760 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1761 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001762
1763 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001764}
1765
1766void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1767 mDescription.hasTexture = true;
1768 mDescription.hasAlpha8Texture = isAlpha8;
1769}
1770
Romain Guyff316ec2013-02-13 18:39:43 -08001771void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1772 mDescription.hasTexture = true;
1773 mDescription.hasColors = true;
1774 mDescription.hasAlpha8Texture = isAlpha8;
1775}
1776
Romain Guyaa6c24c2011-04-28 18:40:04 -07001777void OpenGLRenderer::setupDrawWithExternalTexture() {
1778 mDescription.hasExternalTexture = true;
1779}
1780
Romain Guy15bc6432011-12-13 13:11:32 -08001781void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001782 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001783}
1784
Chris Craik710f46d2012-09-17 17:25:49 -07001785void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001786 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001787}
1788
Romain Guy8d0d4782010-12-14 20:13:35 -08001789void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1790 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001791 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1792 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1793 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001794 mColorSet = true;
1795 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1796}
1797
Romain Guy86568192010-12-14 15:55:39 -08001798void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1799 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001800 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1801 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1802 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001803 mColorSet = true;
1804 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1805}
1806
Romain Guy41210632012-07-16 17:04:24 -07001807void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1808 mCaches.fontRenderer->describe(mDescription, paint);
1809}
1810
Romain Guy70ca14e2010-12-13 18:24:33 -08001811void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1812 mColorA = a;
1813 mColorR = r;
1814 mColorG = g;
1815 mColorB = b;
1816 mColorSet = true;
1817 mSetShaderColor = mDescription.setColor(r, g, b, a);
1818}
1819
1820void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001821 if (mDrawModifiers.mShader) {
1822 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001823 }
1824}
1825
1826void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001827 if (mDrawModifiers.mColorFilter) {
1828 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001829 }
1830}
1831
Romain Guyf09ef512011-05-27 11:43:46 -07001832void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1833 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1834 mColorA = 1.0f;
1835 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001836 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001837 }
1838}
1839
Romain Guy70ca14e2010-12-13 18:24:33 -08001840void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001841 // When the blending mode is kClear_Mode, we need to use a modulate color
1842 // argb=1,0,0,0
1843 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001844 bool blend = (mColorSet && mColorA < 1.0f) ||
1845 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1846 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001847}
1848
1849void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001850 // When the blending mode is kClear_Mode, we need to use a modulate color
1851 // argb=1,0,0,0
1852 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001853 blend |= (mColorSet && mColorA < 1.0f) ||
1854 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1855 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1856 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001857}
1858
1859void OpenGLRenderer::setupDrawProgram() {
1860 useProgram(mCaches.programCache.get(mDescription));
1861}
1862
1863void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1864 mTrackDirtyRegions = false;
1865}
1866
1867void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1868 bool ignoreTransform) {
1869 mModelView.loadTranslate(left, top, 0.0f);
1870 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001871 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1872 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001873 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001874 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001875 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1876 }
1877}
1878
Chet Haase8a5cc922011-04-26 07:28:09 -07001879void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001880 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001881}
1882
Romain Guy70ca14e2010-12-13 18:24:33 -08001883void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1884 bool ignoreTransform, bool ignoreModelView) {
1885 if (!ignoreModelView) {
1886 mModelView.loadTranslate(left, top, 0.0f);
1887 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001888 } else {
1889 mModelView.loadIdentity();
1890 }
Romain Guy86568192010-12-14 15:55:39 -08001891 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1892 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001893 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001894 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001895 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001896 }
1897 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001898 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001899 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1900 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001901}
1902
1903void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001904 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001905 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1906 }
1907}
1908
Romain Guy86568192010-12-14 15:55:39 -08001909void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001910 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001911 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001912 }
1913}
1914
Romain Guy70ca14e2010-12-13 18:24:33 -08001915void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001916 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001917 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001918 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001919 }
Chris Craikc3566d02013-02-04 16:16:33 -08001920 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1921 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001922 }
1923}
1924
Romain Guy8d0d4782010-12-14 20:13:35 -08001925void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001926 if (mDrawModifiers.mShader) {
1927 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001928 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001929 }
1930}
1931
Romain Guy70ca14e2010-12-13 18:24:33 -08001932void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001933 if (mDrawModifiers.mColorFilter) {
1934 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001935 }
1936}
1937
Romain Guy41210632012-07-16 17:04:24 -07001938void OpenGLRenderer::setupDrawTextGammaUniforms() {
1939 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1940}
1941
Romain Guy70ca14e2010-12-13 18:24:33 -08001942void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001943 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001944 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001945 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001946}
1947
1948void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001949 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001950 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001951 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001952}
1953
Romain Guyaa6c24c2011-04-28 18:40:04 -07001954void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1955 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001956 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001957 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001958}
1959
Romain Guy8f0095c2011-05-02 17:24:22 -07001960void OpenGLRenderer::setupDrawTextureTransform() {
1961 mDescription.hasTextureTransform = true;
1962}
1963
1964void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001965 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1966 GL_FALSE, &transform.data[0]);
1967}
1968
Romain Guy70ca14e2010-12-13 18:24:33 -08001969void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001970 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001971 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001972 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001973 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001974 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001975 }
Romain Guyd71dd362011-12-12 19:03:35 -08001976
Chris Craikcb4d6002012-09-25 12:00:29 -07001977 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001978 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001979 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001980 }
1981
1982 mCaches.unbindIndicesBuffer();
1983}
1984
Romain Guyff316ec2013-02-13 18:39:43 -08001985void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1986 bool force = mCaches.unbindMeshBuffer();
1987 GLsizei stride = sizeof(ColorTextureVertex);
1988
1989 mCaches.bindPositionVertexPointer(force, vertices, stride);
1990 if (mCaches.currentProgram->texCoords >= 0) {
1991 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1992 }
1993 int slot = mCaches.currentProgram->getAttrib("colors");
1994 if (slot >= 0) {
1995 glEnableVertexAttribArray(slot);
1996 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1997 }
1998
1999 mCaches.unbindIndicesBuffer();
2000}
2001
Romain Guy3b748a42013-04-17 18:54:38 -07002002void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2003 bool force = false;
2004 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2005 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2006 // use the default VBO found in Caches
2007 if (!vertices || vbo) {
2008 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2009 } else {
2010 force = mCaches.unbindMeshBuffer();
2011 }
2012 mCaches.bindIndicesBuffer();
2013
Chris Craikcb4d6002012-09-25 12:00:29 -07002014 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002015 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002016 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002017 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002018}
2019
Romain Guy448455f2013-07-22 13:57:50 -07002020void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002021 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002022 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002023 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002024}
2025
Romain Guy70ca14e2010-12-13 18:24:33 -08002026///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002027// Drawing
2028///////////////////////////////////////////////////////////////////////////////
2029
Chris Craikff785832013-03-08 13:12:16 -08002030status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2031 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002032 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002033 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2034 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002035 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002036 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002037 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002038 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2039 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002040 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002041 }
2042
Chris Craik28ce94a2013-05-31 11:38:03 -07002043 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2044 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002045 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2046 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002047
2048 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002049 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002050
Chet Haase58d110a2013-04-10 07:43:29 -07002051 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002052 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002053
Romain Guy65549432012-03-26 16:45:05 -07002054 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002055}
2056
Chris Craikc3566d02013-02-04 16:16:33 -08002057void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002058 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002059 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002060 }
2061}
2062
Romain Guya168d732011-03-18 16:50:13 -07002063void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2064 int alpha;
2065 SkXfermode::Mode mode;
2066 getAlphaAndMode(paint, &alpha, &mode);
2067
Romain Guy886b2752013-01-04 12:26:18 -08002068 int color = paint != NULL ? paint->getColor() : 0;
2069
Romain Guya168d732011-03-18 16:50:13 -07002070 float x = left;
2071 float y = top;
2072
Romain Guy886b2752013-01-04 12:26:18 -08002073 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2074
Romain Guya168d732011-03-18 16:50:13 -07002075 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002076 if (currentTransform().isPureTranslate()) {
2077 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2078 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002079 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002080
2081 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002082 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002083 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002084 }
2085
Romain Guy3b748a42013-04-17 18:54:38 -07002086 // No need to check for a UV mapper on the texture object, only ARGB_8888
2087 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002088 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002089 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2090 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002091}
2092
Romain Guy03c00b52013-06-20 18:30:28 -07002093/**
2094 * Important note: this method is intended to draw batches of bitmaps and
2095 * will not set the scissor enable or dirty the current layer, if any.
2096 * The caller is responsible for properly dirtying the current layer.
2097 */
Romain Guy55b6f952013-06-27 15:27:09 -07002098status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002099 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002100 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002101 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002102 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002103
Chris Craik527a3aa2013-03-04 10:19:31 -08002104 const AutoTexture autoCleanup(texture);
2105
2106 int alpha;
2107 SkXfermode::Mode mode;
2108 getAlphaAndMode(paint, &alpha, &mode);
2109
2110 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002111 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002112
2113 const float x = (int) floorf(bounds.left + 0.5f);
2114 const float y = (int) floorf(bounds.top + 0.5f);
2115 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2116 int color = paint != NULL ? paint->getColor() : 0;
2117 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2118 texture->id, paint != NULL, color, alpha, mode,
2119 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002120 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002121 } else {
2122 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2123 texture->id, alpha / 255.0f, mode, texture->blend,
2124 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002125 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002126 }
2127
2128 return DrawGlInfo::kStatusDrew;
2129}
2130
Chet Haase48659092012-05-31 15:21:51 -07002131status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002132 const float right = left + bitmap->width();
2133 const float bottom = top + bitmap->height();
2134
2135 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002136 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002137 }
2138
Romain Guya1d3c912011-12-13 14:55:06 -08002139 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002140 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002141 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002142 const AutoTexture autoCleanup(texture);
2143
Romain Guy211370f2012-02-01 16:10:55 -08002144 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002145 drawAlphaBitmap(texture, left, top, paint);
2146 } else {
2147 drawTextureRect(left, top, right, bottom, texture, paint);
2148 }
Chet Haase48659092012-05-31 15:21:51 -07002149
2150 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002151}
2152
Chet Haase48659092012-05-31 15:21:51 -07002153status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002154 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2155 const mat4 transform(*matrix);
2156 transform.mapRect(r);
2157
Romain Guy6926c722010-07-12 20:20:03 -07002158 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002159 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002160 }
2161
Romain Guya1d3c912011-12-13 14:55:06 -08002162 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002163 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002164 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002165 const AutoTexture autoCleanup(texture);
2166
Romain Guy5b3b3522010-10-27 18:57:51 -07002167 // This could be done in a cheaper way, all we need is pass the matrix
2168 // to the vertex shader. The save/restore is a bit overkill.
2169 save(SkCanvas::kMatrix_SaveFlag);
2170 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002171 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2172 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2173 } else {
2174 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2175 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002176 restore();
Chet Haase48659092012-05-31 15:21:51 -07002177
2178 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002179}
2180
Chet Haase48659092012-05-31 15:21:51 -07002181status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002182 const float right = left + bitmap->width();
2183 const float bottom = top + bitmap->height();
2184
2185 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002186 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002187 }
2188
2189 mCaches.activeTexture(0);
2190 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2191 const AutoTexture autoCleanup(texture);
2192
Romain Guy886b2752013-01-04 12:26:18 -08002193 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2194 drawAlphaBitmap(texture, left, top, paint);
2195 } else {
2196 drawTextureRect(left, top, right, bottom, texture, paint);
2197 }
Chet Haase48659092012-05-31 15:21:51 -07002198
2199 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002200}
2201
Chet Haase48659092012-05-31 15:21:51 -07002202status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002203 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002204 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002205 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002206 }
2207
Chris Craik39a908c2013-06-13 14:39:01 -07002208 // TODO: use quickReject on bounds from vertices
2209 mCaches.enableScissor();
2210
Romain Guyb18d2d02011-02-10 15:52:54 -08002211 float left = FLT_MAX;
2212 float top = FLT_MAX;
2213 float right = FLT_MIN;
2214 float bottom = FLT_MIN;
2215
Romain Guya92bb4d2012-10-16 11:08:44 -07002216 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002217
Romain Guyff316ec2013-02-13 18:39:43 -08002218 ColorTextureVertex mesh[count];
2219 ColorTextureVertex* vertex = mesh;
2220
2221 bool cleanupColors = false;
2222 if (!colors) {
2223 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2224 colors = new int[colorsCount];
2225 memset(colors, 0xff, colorsCount * sizeof(int));
2226 cleanupColors = true;
2227 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002228
Romain Guy3b748a42013-04-17 18:54:38 -07002229 mCaches.activeTexture(0);
2230 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2231 const UvMapper& mapper(getMapper(texture));
2232
Romain Guy5a7b4662011-01-20 19:09:30 -08002233 for (int32_t y = 0; y < meshHeight; y++) {
2234 for (int32_t x = 0; x < meshWidth; x++) {
2235 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2236
2237 float u1 = float(x) / meshWidth;
2238 float u2 = float(x + 1) / meshWidth;
2239 float v1 = float(y) / meshHeight;
2240 float v2 = float(y + 1) / meshHeight;
2241
Romain Guy3b748a42013-04-17 18:54:38 -07002242 mapper.map(u1, v1, u2, v2);
2243
Romain Guy5a7b4662011-01-20 19:09:30 -08002244 int ax = i + (meshWidth + 1) * 2;
2245 int ay = ax + 1;
2246 int bx = i;
2247 int by = bx + 1;
2248 int cx = i + 2;
2249 int cy = cx + 1;
2250 int dx = i + (meshWidth + 1) * 2 + 2;
2251 int dy = dx + 1;
2252
Romain Guyff316ec2013-02-13 18:39:43 -08002253 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2254 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2255 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002256
Romain Guyff316ec2013-02-13 18:39:43 -08002257 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2258 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2259 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002260
Romain Guya92bb4d2012-10-16 11:08:44 -07002261 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2262 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2263 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2264 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002265 }
2266 }
2267
Romain Guya92bb4d2012-10-16 11:08:44 -07002268 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002269 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002270 return DrawGlInfo::kStatusDone;
2271 }
2272
Romain Guyff316ec2013-02-13 18:39:43 -08002273 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002274 texture = mCaches.textureCache.get(bitmap);
2275 if (!texture) {
2276 if (cleanupColors) delete[] colors;
2277 return DrawGlInfo::kStatusDone;
2278 }
Romain Guyff316ec2013-02-13 18:39:43 -08002279 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002280 const AutoTexture autoCleanup(texture);
2281
2282 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2283 texture->setFilter(FILTER(paint), true);
2284
2285 int alpha;
2286 SkXfermode::Mode mode;
2287 getAlphaAndMode(paint, &alpha, &mode);
2288
Romain Guyff316ec2013-02-13 18:39:43 -08002289 float a = alpha / 255.0f;
2290
Romain Guya92bb4d2012-10-16 11:08:44 -07002291 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002292 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002293 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002294
Romain Guyff316ec2013-02-13 18:39:43 -08002295 setupDraw();
2296 setupDrawWithTextureAndColor();
2297 setupDrawColor(a, a, a, a);
2298 setupDrawColorFilter();
2299 setupDrawBlending(true, mode, false);
2300 setupDrawProgram();
2301 setupDrawDirtyRegionsDisabled();
2302 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2303 setupDrawTexture(texture->id);
2304 setupDrawPureColorUniforms();
2305 setupDrawColorFilterUniforms();
2306 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2307
2308 glDrawArrays(GL_TRIANGLES, 0, count);
2309
Romain Guyff316ec2013-02-13 18:39:43 -08002310 int slot = mCaches.currentProgram->getAttrib("colors");
2311 if (slot >= 0) {
2312 glDisableVertexAttribArray(slot);
2313 }
2314
2315 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002316
2317 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002318}
2319
Chet Haase48659092012-05-31 15:21:51 -07002320status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002321 float srcLeft, float srcTop, float srcRight, float srcBottom,
2322 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002323 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002324 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002325 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002326 }
2327
Romain Guya1d3c912011-12-13 14:55:06 -08002328 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002329 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002330 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002331 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002332
Romain Guy8ba548f2010-06-30 19:21:21 -07002333 const float width = texture->width;
2334 const float height = texture->height;
2335
Romain Guy3b748a42013-04-17 18:54:38 -07002336 float u1 = fmax(0.0f, srcLeft / width);
2337 float v1 = fmax(0.0f, srcTop / height);
2338 float u2 = fmin(1.0f, srcRight / width);
2339 float v2 = fmin(1.0f, srcBottom / height);
2340
2341 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002342
Romain Guy03750a02010-10-18 14:06:08 -07002343 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002344 resetDrawTextureTexCoords(u1, v1, u2, v2);
2345
Romain Guy03750a02010-10-18 14:06:08 -07002346 int alpha;
2347 SkXfermode::Mode mode;
2348 getAlphaAndMode(paint, &alpha, &mode);
2349
Romain Guyd21b6e12011-11-30 20:21:23 -08002350 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2351
Romain Guy886b2752013-01-04 12:26:18 -08002352 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2353 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002354
Romain Guy886b2752013-01-04 12:26:18 -08002355 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2356 // Apply a scale transform on the canvas only when a shader is in use
2357 // Skia handles the ratio between the dst and src rects as a scale factor
2358 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002359 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002360 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002361
Romain Guy3b753822013-03-05 10:27:35 -08002362 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2363 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2364 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002365
2366 dstRight = x + (dstRight - dstLeft);
2367 dstBottom = y + (dstBottom - dstTop);
2368
2369 dstLeft = x;
2370 dstTop = y;
2371
2372 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2373 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002374 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002375 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002376 }
2377
2378 if (CC_UNLIKELY(useScaleTransform)) {
2379 save(SkCanvas::kMatrix_SaveFlag);
2380 translate(dstLeft, dstTop);
2381 scale(scaleX, scaleY);
2382
2383 dstLeft = 0.0f;
2384 dstTop = 0.0f;
2385
2386 dstRight = srcRight - srcLeft;
2387 dstBottom = srcBottom - srcTop;
2388 }
2389
2390 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2391 int color = paint ? paint->getColor() : 0;
2392 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2393 texture->id, paint != NULL, color, alpha, mode,
2394 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2395 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2396 } else {
2397 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2398 texture->id, alpha / 255.0f, mode, texture->blend,
2399 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2400 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2401 }
2402
2403 if (CC_UNLIKELY(useScaleTransform)) {
2404 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002405 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002406
2407 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002408
2409 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002410}
2411
Romain Guy3b748a42013-04-17 18:54:38 -07002412status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002413 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002414 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002415 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002416 }
2417
Romain Guy4c2547f2013-06-11 16:19:24 -07002418 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002419 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2420 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002421
Romain Guy03c00b52013-06-20 18:30:28 -07002422 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002423}
2424
Romain Guy03c00b52013-06-20 18:30:28 -07002425status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2426 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002427 if (quickReject(left, top, right, bottom)) {
2428 return DrawGlInfo::kStatusDone;
2429 }
2430
Romain Guy211370f2012-02-01 16:10:55 -08002431 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002432 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002433 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002434 if (!texture) return DrawGlInfo::kStatusDone;
2435 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002436
Romain Guya4adcf02013-02-28 12:15:35 -08002437 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2438 texture->setFilter(GL_LINEAR, true);
2439
Romain Guy03c00b52013-06-20 18:30:28 -07002440 int alpha;
2441 SkXfermode::Mode mode;
2442 getAlphaAndMode(paint, &alpha, &mode);
2443
Romain Guy3b753822013-03-05 10:27:35 -08002444 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002445 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002446 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002447 const float offsetX = left + currentTransform().getTranslateX();
2448 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002449 const size_t count = mesh->quads.size();
2450 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002451 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002452 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002453 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2454 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2455 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002456 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002457 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002458 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002459 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002460 }
2461 }
2462
Romain Guy211370f2012-02-01 16:10:55 -08002463 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002464 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2465 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002466
Romain Guy3b748a42013-04-17 18:54:38 -07002467 right = x + right - left;
2468 bottom = y + bottom - top;
2469 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2470 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2471 GL_TRIANGLES, mesh->indexCount, false, true,
2472 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002473 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002474 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2475 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2476 GL_TRIANGLES, mesh->indexCount, false, false,
2477 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002478 }
Romain Guy054dc182010-10-15 17:55:25 -07002479 }
Chet Haase48659092012-05-31 15:21:51 -07002480
2481 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002482}
2483
Romain Guy03c00b52013-06-20 18:30:28 -07002484/**
2485 * Important note: this method is intended to draw batches of 9-patch objects and
2486 * will not set the scissor enable or dirty the current layer, if any.
2487 * The caller is responsible for properly dirtying the current layer.
2488 */
2489status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2490 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2491 mCaches.activeTexture(0);
2492 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2493 if (!texture) return DrawGlInfo::kStatusDone;
2494 const AutoTexture autoCleanup(texture);
2495
2496 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2497 texture->setFilter(GL_LINEAR, true);
2498
2499 int alpha;
2500 SkXfermode::Mode mode;
2501 getAlphaAndMode(paint, &alpha, &mode);
2502
2503 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2504 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2505 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2506
2507 return DrawGlInfo::kStatusDrew;
2508}
2509
Chris Craik65cd6122012-12-10 17:56:27 -08002510status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2511 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002512 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002513 // no vertices to draw
2514 return DrawGlInfo::kStatusDone;
2515 }
2516
Chris Craikcb4d6002012-09-25 12:00:29 -07002517 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002518 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2519 bool isAA = paint->isAntiAlias();
2520
Chris Craik710f46d2012-09-17 17:25:49 -07002521 setupDraw();
2522 setupDrawNoTexture();
2523 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002524 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2525 setupDrawColorFilter();
2526 setupDrawShader();
2527 setupDrawBlending(isAA, mode);
2528 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002529 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002530 setupDrawColorUniforms();
2531 setupDrawColorFilterUniforms();
2532 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002533
Chris Craik710f46d2012-09-17 17:25:49 -07002534 void* vertices = vertexBuffer.getBuffer();
2535 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002536 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002537 mCaches.resetTexCoordsVertexPointer();
2538 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002539
Chris Craik710f46d2012-09-17 17:25:49 -07002540 int alphaSlot = -1;
2541 if (isAA) {
2542 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2543 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002544
Chris Craik710f46d2012-09-17 17:25:49 -07002545 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002546 glEnableVertexAttribArray(alphaSlot);
2547 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002548 }
Romain Guy04299382012-07-18 17:15:41 -07002549
Chris Craik6d29c8d2013-05-08 18:35:44 -07002550 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002551
Chris Craik710f46d2012-09-17 17:25:49 -07002552 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002553 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002554 }
Chris Craik65cd6122012-12-10 17:56:27 -08002555
2556 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002557}
2558
2559/**
Chris Craik65cd6122012-12-10 17:56:27 -08002560 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2561 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2562 * screen space in all directions. However, instead of using a fragment shader to compute the
2563 * translucency of the color from its position, we simply use a varying parameter to define how far
2564 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2565 *
2566 * Doesn't yet support joins, caps, or path effects.
2567 */
2568status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2569 VertexBuffer vertexBuffer;
2570 // TODO: try clipping large paths to viewport
2571 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2572
Romain Guyc46d07a2013-03-15 19:06:39 -07002573 if (hasLayer()) {
2574 SkRect bounds = path.getBounds();
2575 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2576 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2577 }
Chris Craik65cd6122012-12-10 17:56:27 -08002578
2579 return drawVertexBuffer(vertexBuffer, paint);
2580}
2581
2582/**
2583 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2584 * and additional geometry for defining an alpha slope perimeter.
2585 *
2586 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2587 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2588 * in-shader alpha region, but found it to be taxing on some GPUs.
2589 *
2590 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2591 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002592 */
Chet Haase48659092012-05-31 15:21:51 -07002593status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002594 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002595
Chris Craik65cd6122012-12-10 17:56:27 -08002596 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002597
Chris Craik65cd6122012-12-10 17:56:27 -08002598 VertexBuffer buffer;
2599 SkRect bounds;
2600 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002601
2602 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2603 return DrawGlInfo::kStatusDone;
2604 }
2605
Romain Guy3b753822013-03-05 10:27:35 -08002606 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002607
Chris Craik65cd6122012-12-10 17:56:27 -08002608 bool useOffset = !paint->isAntiAlias();
2609 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002610}
2611
Chet Haase48659092012-05-31 15:21:51 -07002612status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002613 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002614
Chris Craik6d29c8d2013-05-08 18:35:44 -07002615 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002616
Chris Craik6d29c8d2013-05-08 18:35:44 -07002617 VertexBuffer buffer;
2618 SkRect bounds;
2619 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002620
Chris Craik6d29c8d2013-05-08 18:35:44 -07002621 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2622 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002623 }
2624
Chris Craik6d29c8d2013-05-08 18:35:44 -07002625 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002626
Chris Craik6d29c8d2013-05-08 18:35:44 -07002627 bool useOffset = !paint->isAntiAlias();
2628 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002629}
2630
Chet Haase48659092012-05-31 15:21:51 -07002631status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002632 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002633 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002634
Romain Guyae88e5e2010-10-22 17:49:18 -07002635 Rect& clip(*mSnapshot->clipRect);
2636 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002637
Romain Guy3d58c032010-07-14 16:34:53 -07002638 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002639
2640 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002641}
Romain Guy9d5316e2010-06-24 19:30:36 -07002642
Chet Haase48659092012-05-31 15:21:51 -07002643status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2644 SkPaint* paint) {
2645 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002646 const AutoTexture autoCleanup(texture);
2647
2648 const float x = left + texture->left - texture->offset;
2649 const float y = top + texture->top - texture->offset;
2650
2651 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002652
2653 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002654}
2655
Chet Haase48659092012-05-31 15:21:51 -07002656status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002657 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002658 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2659 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002660 return DrawGlInfo::kStatusDone;
2661 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002662
Chris Craikcb4d6002012-09-25 12:00:29 -07002663 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002664 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002665 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002666 right - left, bottom - top, rx, ry, p);
2667 return drawShape(left, top, texture, p);
2668 }
2669
2670 SkPath path;
2671 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002672 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2673 float outset = p->getStrokeWidth() / 2;
2674 rect.outset(outset, outset);
2675 rx += outset;
2676 ry += outset;
2677 }
Chris Craik710f46d2012-09-17 17:25:49 -07002678 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002679 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002680}
2681
Chris Craik710f46d2012-09-17 17:25:49 -07002682status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002683 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002684 x + radius, y + radius, p) ||
2685 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002686 return DrawGlInfo::kStatusDone;
2687 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002688 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002689 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002690 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002691 return drawShape(x - radius, y - radius, texture, p);
2692 }
2693
2694 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002695 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2696 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2697 } else {
2698 path.addCircle(x, y, radius);
2699 }
Chris Craik65cd6122012-12-10 17:56:27 -08002700 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002701}
Romain Guy01d58e42011-01-19 21:54:02 -08002702
Chet Haase48659092012-05-31 15:21:51 -07002703status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002704 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002705 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2706 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002707 return DrawGlInfo::kStatusDone;
2708 }
Romain Guy01d58e42011-01-19 21:54:02 -08002709
Chris Craikcb4d6002012-09-25 12:00:29 -07002710 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002711 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002712 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002713 return drawShape(left, top, texture, p);
2714 }
2715
2716 SkPath path;
2717 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002718 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2719 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2720 }
Chris Craik710f46d2012-09-17 17:25:49 -07002721 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002722 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002723}
2724
Chet Haase48659092012-05-31 15:21:51 -07002725status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002726 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002727 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2728 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002729 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002730 }
2731
Chris Craik780c1282012-10-04 14:10:49 -07002732 if (fabs(sweepAngle) >= 360.0f) {
2733 return drawOval(left, top, right, bottom, p);
2734 }
2735
2736 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002737 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002738 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002739 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002740 startAngle, sweepAngle, useCenter, p);
2741 return drawShape(left, top, texture, p);
2742 }
2743
2744 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2745 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2746 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2747 }
2748
2749 SkPath path;
2750 if (useCenter) {
2751 path.moveTo(rect.centerX(), rect.centerY());
2752 }
2753 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2754 if (useCenter) {
2755 path.close();
2756 }
Chris Craik65cd6122012-12-10 17:56:27 -08002757 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002758}
2759
Romain Guycf8675e2012-10-02 12:32:25 -07002760// See SkPaintDefaults.h
2761#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2762
Chet Haase48659092012-05-31 15:21:51 -07002763status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002764 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2765 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002766 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002767 }
2768
Chris Craik710f46d2012-09-17 17:25:49 -07002769 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002770 // only fill style is supported by drawConvexPath, since others have to handle joins
2771 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2772 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2773 mCaches.activeTexture(0);
2774 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002775 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002776 return drawShape(left, top, texture, p);
2777 }
2778
2779 SkPath path;
2780 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2781 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2782 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2783 }
2784 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002785 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002786 }
2787
Romain Guy3b753822013-03-05 10:27:35 -08002788 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002789 SkPath path;
2790 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002791 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002792 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002793 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002794 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002795 }
Romain Guyc7d53492010-06-25 13:41:57 -07002796}
Romain Guy9d5316e2010-06-24 19:30:36 -07002797
Raph Levien416a8472012-07-19 22:48:17 -07002798void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2799 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2800 float x, float y) {
2801 mCaches.activeTexture(0);
2802
2803 // NOTE: The drop shadow will not perform gamma correction
2804 // if shader-based correction is enabled
2805 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2806 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002807 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002808 // If the drop shadow exceeds the max texture size or couldn't be
2809 // allocated, skip drawing
2810 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002811 const AutoTexture autoCleanup(shadow);
2812
Chris Craikc3566d02013-02-04 16:16:33 -08002813 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2814 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002815
Chris Craikc3566d02013-02-04 16:16:33 -08002816 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2817 int shadowColor = mDrawModifiers.mShadowColor;
2818 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002819 shadowColor = 0xffffffff;
2820 }
2821
2822 setupDraw();
2823 setupDrawWithTexture(true);
2824 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2825 setupDrawColorFilter();
2826 setupDrawShader();
2827 setupDrawBlending(true, mode);
2828 setupDrawProgram();
2829 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2830 setupDrawTexture(shadow->id);
2831 setupDrawPureColorUniforms();
2832 setupDrawColorFilterUniforms();
2833 setupDrawShaderUniforms();
2834 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2835
2836 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2837}
2838
Romain Guy768bffc2013-02-27 13:50:45 -08002839bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2840 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2841 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2842}
2843
Chet Haase48659092012-05-31 15:21:51 -07002844status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002845 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002846 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002847 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002848 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002849
Romain Guy671d6cf2012-01-18 12:39:17 -08002850 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002851 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002852 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002853 }
2854
Chris Craik39a908c2013-06-13 14:39:01 -07002855 mCaches.enableScissor();
2856
Romain Guy671d6cf2012-01-18 12:39:17 -08002857 float x = 0.0f;
2858 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002859 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002860 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002861 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2862 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002863 }
2864
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002865 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002866 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002867
2868 int alpha;
2869 SkXfermode::Mode mode;
2870 getAlphaAndMode(paint, &alpha, &mode);
2871
Chris Craikc3566d02013-02-04 16:16:33 -08002872 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002873 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2874 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002875 }
2876
Romain Guy671d6cf2012-01-18 12:39:17 -08002877 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002878 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002879 if (pureTranslate && !linearFilter) {
2880 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2881 }
Romain Guy257ae352013-03-20 16:31:12 -07002882 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002883
2884 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2885 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2886
Romain Guy211370f2012-02-01 16:10:55 -08002887 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002888
Victoria Lease1e546812013-06-25 14:25:17 -07002889 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002890 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002891 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002892 if (hasActiveLayer) {
2893 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002894 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002895 }
2896 dirtyLayerUnchecked(bounds, getRegion());
2897 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002898 }
Chet Haase48659092012-05-31 15:21:51 -07002899
2900 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002901}
2902
Romain Guy624234f2013-03-05 16:43:31 -08002903mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2904 mat4 fontTransform;
2905 if (CC_LIKELY(transform.isPureTranslate())) {
2906 fontTransform = mat4::identity();
2907 } else {
2908 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002909 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002910 } else {
2911 float sx, sy;
2912 currentTransform().decomposeScale(sx, sy);
2913 fontTransform.loadScale(sx, sy, 1.0f);
2914 }
2915 }
2916 return fontTransform;
2917}
2918
Chris Craik41541822013-05-03 16:35:54 -07002919status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2920 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002921 DrawOpMode drawOpMode) {
2922
Chris Craik527a3aa2013-03-04 10:19:31 -08002923 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002924 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2925 // drawing as ops from DeferredDisplayList are already filtered for these
2926 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2927 quickReject(bounds)) {
2928 return DrawGlInfo::kStatusDone;
2929 }
Romain Guycac5fd32011-12-01 20:08:50 -08002930 }
2931
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002932 const float oldX = x;
2933 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002934
2935 const mat4& transform = currentTransform();
2936 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002937
Romain Guy211370f2012-02-01 16:10:55 -08002938 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002939 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2940 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002941 }
2942
Romain Guy86568192010-12-14 15:55:39 -08002943 int alpha;
2944 SkXfermode::Mode mode;
2945 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002946
Romain Guyc74f45a2013-02-26 19:10:14 -08002947 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2948
Chris Craikc3566d02013-02-04 16:16:33 -08002949 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002950 fontRenderer.setFont(paint, mat4::identity());
2951 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2952 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002953 }
2954
Romain Guy19d4dd82013-03-04 11:14:26 -08002955 const bool hasActiveLayer = hasLayer();
2956
Romain Guy624234f2013-03-05 16:43:31 -08002957 // We only pass a partial transform to the font renderer. That partial
2958 // matrix defines how glyphs are rasterized. Typically we want glyphs
2959 // to be rasterized at their final size on screen, which means the partial
2960 // matrix needs to take the scale factor into account.
2961 // When a partial matrix is used to transform glyphs during rasterization,
2962 // the mesh is generated with the inverse transform (in the case of scale,
2963 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2964 // apply the full transform matrix at draw time in the vertex shader.
2965 // Applying the full matrix in the shader is the easiest way to handle
2966 // rotation and perspective and allows us to always generated quads in the
2967 // font renderer which greatly simplifies the code, clipping in particular.
2968 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002969 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002970
Romain Guy6620c6d2010-12-06 18:07:02 -08002971 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002972 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002973 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002974
Romain Guy624234f2013-03-05 16:43:31 -08002975 // TODO: Implement better clipping for scaled/rotated text
2976 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002977 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 -07002978
Raph Levien996e57c2012-07-23 15:22:52 -07002979 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002980 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002981
2982 // don't call issuedrawcommand, do it at end of batch
2983 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002984 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002985 SkPaint paintCopy(*paint);
2986 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2987 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002988 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002989 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002990 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002991 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002992 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002993
Chris Craik527a3aa2013-03-04 10:19:31 -08002994 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002995 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002996 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002997 }
Chris Craik41541822013-05-03 16:35:54 -07002998 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002999 }
Romain Guy694b5192010-07-21 21:33:20 -07003000
Chris Craik41541822013-05-03 16:35:54 -07003001 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003002
3003 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003004}
3005
Chet Haase48659092012-05-31 15:21:51 -07003006status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003007 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003008 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003009 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003010 }
3011
Chris Craik39a908c2013-06-13 14:39:01 -07003012 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3013 mCaches.enableScissor();
3014
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003015 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003016 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003017 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003018
3019 int alpha;
3020 SkXfermode::Mode mode;
3021 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003022 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003023
Romain Guy97771732012-02-28 18:17:02 -08003024 const Rect* clip = &mSnapshot->getLocalClip();
3025 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 -08003026
Romain Guy97771732012-02-28 18:17:02 -08003027 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003028
3029 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003030 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003031 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003032 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003033 dirtyLayerUnchecked(bounds, getRegion());
3034 }
Romain Guy97771732012-02-28 18:17:02 -08003035 }
Chet Haase48659092012-05-31 15:21:51 -07003036
3037 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003038}
3039
Chet Haase48659092012-05-31 15:21:51 -07003040status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3041 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003042
Romain Guya1d3c912011-12-13 14:55:06 -08003043 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003044
Romain Guyfb8b7632010-08-23 21:05:08 -07003045 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003046 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003047 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003048
Romain Guy8b55f372010-08-18 17:10:07 -07003049 const float x = texture->left - texture->offset;
3050 const float y = texture->top - texture->offset;
3051
Romain Guy01d58e42011-01-19 21:54:02 -08003052 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003053
3054 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003055}
3056
Chris Craika08f95c2013-03-15 17:24:33 -07003057status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003058 if (!layer) {
3059 return DrawGlInfo::kStatusDone;
3060 }
3061
Romain Guyb2e2f242012-10-17 18:18:35 -07003062 mat4* transform = NULL;
3063 if (layer->isTextureLayer()) {
3064 transform = &layer->getTransform();
3065 if (!transform->isIdentity()) {
3066 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003067 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003068 }
3069 }
3070
Chris Craik39a908c2013-06-13 14:39:01 -07003071 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003072 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003073 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003074
3075 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003076 if (transform && !transform->isIdentity()) {
3077 restore();
3078 }
Chet Haase48659092012-05-31 15:21:51 -07003079 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003080 }
3081
Romain Guy5bb3c732012-11-29 17:52:58 -08003082 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003083
Chris Craik39a908c2013-06-13 14:39:01 -07003084 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003085 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003086
Romain Guy211370f2012-02-01 16:10:55 -08003087 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003088 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3089 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003090
Romain Guyc88e3572011-01-22 00:32:12 -08003091 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003092 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3093 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003094 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003095 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003096 setupDraw();
3097 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003098 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003099 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003100 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003101 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003102 setupDrawPureColorUniforms();
3103 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003104 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003105 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3106 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3107 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003108
Romain Guyd21b6e12011-11-30 20:21:23 -08003109 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003110 setupDrawModelViewTranslate(tx, ty,
3111 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003112 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003113 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003114 setupDrawModelViewTranslate(x, y,
3115 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3116 }
Romain Guyf219da52011-01-16 12:54:25 -08003117
Romain Guy448455f2013-07-22 13:57:50 -07003118 TextureVertex* mesh = &layer->mesh[0];
3119 GLsizei elementsCount = layer->meshElementCount;
3120
3121 while (elementsCount > 0) {
3122 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3123
3124 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3125 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3126 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3127
3128 elementsCount -= drawCount;
3129 // Though there are 4 vertices in a quad, we use 6 indices per
3130 // quad to draw with GL_TRIANGLES
3131 mesh += (drawCount / 6) * 4;
3132 }
Romain Guyf219da52011-01-16 12:54:25 -08003133
Romain Guy3a3133d2011-02-01 22:59:58 -08003134#if DEBUG_LAYERS_AS_REGIONS
3135 drawRegionRects(layer->region);
3136#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003137 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003138
Chris Craikc3566d02013-02-04 16:16:33 -08003139 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003140
Romain Guy5bb3c732012-11-29 17:52:58 -08003141 if (layer->debugDrawUpdate) {
3142 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003143 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3144 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3145 }
Romain Guyf219da52011-01-16 12:54:25 -08003146 }
Chris Craik34416ea2013-04-15 16:08:28 -07003147 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003148
Romain Guyb2e2f242012-10-17 18:18:35 -07003149 if (transform && !transform->isIdentity()) {
3150 restore();
3151 }
3152
Chet Haase48659092012-05-31 15:21:51 -07003153 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003154}
3155
Romain Guy6926c722010-07-12 20:20:03 -07003156///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003157// Shaders
3158///////////////////////////////////////////////////////////////////////////////
3159
3160void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003161 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003162}
3163
Romain Guy06f96e22010-07-30 19:18:16 -07003164void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003165 mDrawModifiers.mShader = shader;
3166 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003167 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003168 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003169}
3170
Romain Guyd27977d2010-07-14 19:18:51 -07003171///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003172// Color filters
3173///////////////////////////////////////////////////////////////////////////////
3174
3175void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003176 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003177}
3178
3179void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003180 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003181}
3182
3183///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003184// Drop shadow
3185///////////////////////////////////////////////////////////////////////////////
3186
3187void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003188 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003189}
3190
3191void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003192 mDrawModifiers.mHasShadow = true;
3193 mDrawModifiers.mShadowRadius = radius;
3194 mDrawModifiers.mShadowDx = dx;
3195 mDrawModifiers.mShadowDy = dy;
3196 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003197}
3198
3199///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003200// Draw filters
3201///////////////////////////////////////////////////////////////////////////////
3202
3203void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003204 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3205 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003206 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003207 mDrawModifiers.mPaintFilterClearBits = 0;
3208 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003209}
3210
3211void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003212 mDrawModifiers.mHasDrawFilter = true;
3213 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3214 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003215}
3216
Chris Craika08f95c2013-03-15 17:24:33 -07003217SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003218 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003219 return paint;
3220 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003221
3222 uint32_t flags = paint->getFlags();
3223
3224 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003225 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3226 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003227
3228 return &mFilteredPaint;
3229}
3230
3231///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003232// Drawing implementation
3233///////////////////////////////////////////////////////////////////////////////
3234
Romain Guy3b748a42013-04-17 18:54:38 -07003235Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3236 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3237 if (!texture) {
3238 return mCaches.textureCache.get(bitmap);
3239 }
3240 return texture;
3241}
3242
Romain Guy01d58e42011-01-19 21:54:02 -08003243void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3244 float x, float y, SkPaint* paint) {
3245 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3246 return;
3247 }
3248
3249 int alpha;
3250 SkXfermode::Mode mode;
3251 getAlphaAndMode(paint, &alpha, &mode);
3252
3253 setupDraw();
3254 setupDrawWithTexture(true);
3255 setupDrawAlpha8Color(paint->getColor(), alpha);
3256 setupDrawColorFilter();
3257 setupDrawShader();
3258 setupDrawBlending(true, mode);
3259 setupDrawProgram();
3260 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3261 setupDrawTexture(texture->id);
3262 setupDrawPureColorUniforms();
3263 setupDrawColorFilterUniforms();
3264 setupDrawShaderUniforms();
3265 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3266
3267 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003268}
3269
Romain Guyf607bdc2010-09-10 19:20:06 -07003270// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003271#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3272#define kStdUnderline_Offset (1.0f / 9.0f)
3273#define kStdUnderline_Thickness (1.0f / 18.0f)
3274
Chris Craik41541822013-05-03 16:35:54 -07003275void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003276 float x, float y, SkPaint* paint) {
3277 // Handle underline and strike-through
3278 uint32_t flags = paint->getFlags();
3279 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003280 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003281
Romain Guy211370f2012-02-01 16:10:55 -08003282 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003283 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003284 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003285
Raph Levien8b4072d2012-07-30 15:50:00 -07003286 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003287 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003288
Romain Guyf6834472011-01-23 13:32:12 -08003289 int linesCount = 0;
3290 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3291 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3292
3293 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003294 float points[pointsCount];
3295 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003296
3297 if (flags & SkPaint::kUnderlineText_Flag) {
3298 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003299 points[currentPoint++] = left;
3300 points[currentPoint++] = top;
3301 points[currentPoint++] = left + underlineWidth;
3302 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003303 }
3304
3305 if (flags & SkPaint::kStrikeThruText_Flag) {
3306 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003307 points[currentPoint++] = left;
3308 points[currentPoint++] = top;
3309 points[currentPoint++] = left + underlineWidth;
3310 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003311 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003312
Romain Guy726aeba2011-06-01 14:52:00 -07003313 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003314
Romain Guy726aeba2011-06-01 14:52:00 -07003315 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003316 }
3317 }
3318}
3319
Romain Guy672433d2013-01-04 19:05:13 -08003320status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3321 if (mSnapshot->isIgnored()) {
3322 return DrawGlInfo::kStatusDone;
3323 }
3324
Romain Guy735738c2012-12-03 12:34:51 -08003325 int color = paint->getColor();
3326 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003327 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003328 color |= 0x00ffffff;
3329 }
3330 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3331
3332 return drawColorRects(rects, count, color, mode);
3333}
3334
3335status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003336 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003337 if (count == 0) {
3338 return DrawGlInfo::kStatusDone;
3339 }
Romain Guy735738c2012-12-03 12:34:51 -08003340
Romain Guy672433d2013-01-04 19:05:13 -08003341 float left = FLT_MAX;
3342 float top = FLT_MAX;
3343 float right = FLT_MIN;
3344 float bottom = FLT_MIN;
3345
Romain Guy448455f2013-07-22 13:57:50 -07003346 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003347 Vertex* vertex = mesh;
3348
Chris Craik2af46352012-11-26 18:30:17 -08003349 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003350 float l = rects[index + 0];
3351 float t = rects[index + 1];
3352 float r = rects[index + 2];
3353 float b = rects[index + 3];
3354
Romain Guy3b753822013-03-05 10:27:35 -08003355 Vertex::set(vertex++, l, t);
3356 Vertex::set(vertex++, r, t);
3357 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003358 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003359
Romain Guy3b753822013-03-05 10:27:35 -08003360 left = fminf(left, l);
3361 top = fminf(top, t);
3362 right = fmaxf(right, r);
3363 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003364 }
3365
Romain Guy3b753822013-03-05 10:27:35 -08003366 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003367 return DrawGlInfo::kStatusDone;
3368 }
Romain Guy672433d2013-01-04 19:05:13 -08003369
Romain Guy672433d2013-01-04 19:05:13 -08003370 setupDraw();
3371 setupDrawNoTexture();
3372 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3373 setupDrawShader();
3374 setupDrawColorFilter();
3375 setupDrawBlending(mode);
3376 setupDrawProgram();
3377 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003378 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003379 setupDrawColorUniforms();
3380 setupDrawShaderUniforms();
3381 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003382
Romain Guy8ce00302013-01-15 18:51:42 -08003383 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003384 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003385 }
3386
Romain Guy448455f2013-07-22 13:57:50 -07003387 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003388
3389 return DrawGlInfo::kStatusDrew;
3390}
3391
Romain Guy026c5e162010-06-28 17:12:22 -07003392void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003393 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003394 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003395 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003396 color |= 0x00ffffff;
3397 }
3398
Romain Guy70ca14e2010-12-13 18:24:33 -08003399 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003400 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003401 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003402 setupDrawShader();
3403 setupDrawColorFilter();
3404 setupDrawBlending(mode);
3405 setupDrawProgram();
3406 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3407 setupDrawColorUniforms();
3408 setupDrawShaderUniforms(ignoreTransform);
3409 setupDrawColorFilterUniforms();
3410 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003411
Romain Guyc95c8d62010-09-17 15:31:32 -07003412 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3413}
3414
Romain Guy82ba8142010-07-09 13:25:56 -07003415void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003416 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003417 int alpha;
3418 SkXfermode::Mode mode;
3419 getAlphaAndMode(paint, &alpha, &mode);
3420
Romain Guyd21b6e12011-11-30 20:21:23 -08003421 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003422
Romain Guy3b748a42013-04-17 18:54:38 -07003423 GLvoid* vertices = (GLvoid*) NULL;
3424 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3425
3426 if (texture->uvMapper) {
3427 vertices = &mMeshVertices[0].position[0];
3428 texCoords = &mMeshVertices[0].texture[0];
3429
3430 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3431 texture->uvMapper->map(uvs);
3432
3433 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3434 }
3435
Romain Guy3b753822013-03-05 10:27:35 -08003436 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3437 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3438 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003439
Romain Guyd21b6e12011-11-30 20:21:23 -08003440 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003441 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003442 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3443 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003444 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003445 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003446 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003447 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3448 }
3449
3450 if (texture->uvMapper) {
3451 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003452 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003453}
3454
Romain Guybd6b79b2010-06-26 00:13:53 -07003455void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003456 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3457 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003458 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003459}
3460
3461void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003462 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003463 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003464 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003465
Romain Guy746b7402010-10-26 16:27:31 -07003466 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003467 setupDrawWithTexture();
3468 setupDrawColor(alpha, alpha, alpha, alpha);
3469 setupDrawColorFilter();
3470 setupDrawBlending(blend, mode, swapSrcDst);
3471 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003472 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003473 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003474 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003475 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003476 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003477 }
Romain Guy886b2752013-01-04 12:26:18 -08003478 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003479 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003480 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003481 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003482
Romain Guy6820ac82010-09-15 18:11:50 -07003483 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003484}
3485
Romain Guy3b748a42013-04-17 18:54:38 -07003486void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3487 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3488 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3489 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3490
3491 setupDraw();
3492 setupDrawWithTexture();
3493 setupDrawColor(alpha, alpha, alpha, alpha);
3494 setupDrawColorFilter();
3495 setupDrawBlending(blend, mode, swapSrcDst);
3496 setupDrawProgram();
3497 if (!dirty) setupDrawDirtyRegionsDisabled();
3498 if (!ignoreScale) {
3499 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3500 } else {
3501 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3502 }
3503 setupDrawTexture(texture);
3504 setupDrawPureColorUniforms();
3505 setupDrawColorFilterUniforms();
3506 setupDrawMeshIndices(vertices, texCoords, vbo);
3507
3508 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003509}
3510
Romain Guy886b2752013-01-04 12:26:18 -08003511void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3512 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3513 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003514 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003515
3516 setupDraw();
3517 setupDrawWithTexture(true);
3518 if (hasColor) {
3519 setupDrawAlpha8Color(color, alpha);
3520 }
3521 setupDrawColorFilter();
3522 setupDrawShader();
3523 setupDrawBlending(true, mode);
3524 setupDrawProgram();
3525 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003526 if (!ignoreScale) {
3527 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3528 } else {
3529 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3530 }
Romain Guy886b2752013-01-04 12:26:18 -08003531 setupDrawTexture(texture);
3532 setupDrawPureColorUniforms();
3533 setupDrawColorFilterUniforms();
3534 setupDrawShaderUniforms();
3535 setupDrawMesh(vertices, texCoords);
3536
3537 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003538}
3539
Romain Guya5aed0d2010-09-09 14:42:43 -07003540void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003541 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003542 if (mCountOverdraw) {
3543 if (!mCaches.blend) glEnable(GL_BLEND);
3544 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3545 glBlendFunc(GL_ONE, GL_ONE);
3546 }
3547
3548 mCaches.blend = true;
3549 mCaches.lastSrcMode = GL_ONE;
3550 mCaches.lastDstMode = GL_ONE;
3551
3552 return;
3553 }
3554
Romain Guy82ba8142010-07-09 13:25:56 -07003555 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003556
Romain Guy82ba8142010-07-09 13:25:56 -07003557 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003558 // These blend modes are not supported by OpenGL directly and have
3559 // to be implemented using shaders. Since the shader will perform
3560 // the blending, turn blending off here
3561 // If the blend mode cannot be implemented using shaders, fall
3562 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003563 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003564 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003565 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003566 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003567
Romain Guy82bc7a72012-01-03 14:13:39 -08003568 if (mCaches.blend) {
3569 glDisable(GL_BLEND);
3570 mCaches.blend = false;
3571 }
3572
3573 return;
3574 } else {
3575 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003576 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003577 }
3578
3579 if (!mCaches.blend) {
3580 glEnable(GL_BLEND);
3581 }
3582
3583 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3584 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3585
3586 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3587 glBlendFunc(sourceMode, destMode);
3588 mCaches.lastSrcMode = sourceMode;
3589 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003590 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003591 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003592 glDisable(GL_BLEND);
3593 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003594 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003595}
3596
Romain Guy889f8d12010-07-29 14:37:42 -07003597bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003598 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003599 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003600 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003601 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003602 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003603 }
Romain Guy6926c722010-07-12 20:20:03 -07003604 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003605}
3606
Romain Guy026c5e162010-06-28 17:12:22 -07003607void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003608 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003609 TextureVertex::setUV(v++, u1, v1);
3610 TextureVertex::setUV(v++, u2, v1);
3611 TextureVertex::setUV(v++, u1, v2);
3612 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003613}
3614
Chris Craik16ecda52013-03-29 10:59:59 -07003615void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003616 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003617 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3618 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003619 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003620 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003621 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003622}
3623
Chris Craik16ecda52013-03-29 10:59:59 -07003624float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3625 float alpha;
3626 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3627 alpha = mDrawModifiers.mOverrideLayerAlpha;
3628 } else {
3629 alpha = layer->getAlpha() / 255.0f;
3630 }
3631 return alpha * mSnapshot->alpha;
3632}
3633
Romain Guy9d5316e2010-06-24 19:30:36 -07003634}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003635}; // namespace android