blob: 24cd5cb6b1636d8715a0a27bd42c0e852d64ccaa [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700183 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chet Haasedaf98e92011-01-10 14:10:36 -0800452 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700453 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700454
Romain Guy8a4ac612012-07-17 17:32:48 -0700455 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800456 if (mDirtyClip) {
457 setScissorFromClip();
Chris Craikecca6da2013-07-16 13:27:18 -0700458 setStencilFromClip();
Romain Guyf90f8172011-01-25 22:53:24 -0800459 }
Romain Guyd643bb52011-03-01 14:55:21 -0800460
Romain Guy80911b82011-03-16 15:30:12 -0700461 Rect clip(*mSnapshot->clipRect);
462 clip.snapToPixelBoundaries();
463
Romain Guyd643bb52011-03-01 14:55:21 -0800464 // Since we don't know what the functor will draw, let's dirty
465 // tne entire clip region
466 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800467 dirtyLayerUnchecked(clip, getRegion());
468 }
Romain Guyd643bb52011-03-01 14:55:21 -0800469
Romain Guy08aa2cb2011-03-17 11:06:57 -0700470 DrawGlInfo info;
471 info.clipLeft = clip.left;
472 info.clipTop = clip.top;
473 info.clipRight = clip.right;
474 info.clipBottom = clip.bottom;
475 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700476 info.width = getSnapshot()->viewport.getWidth();
477 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700478 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700479
Chris Craik4a2bff72013-04-16 13:50:16 -0700480 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800481
Romain Guy8f3b8e32012-03-27 16:33:45 -0700482 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700483 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800484 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700485
Chris Craik65924a32012-04-05 17:52:11 -0700486 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700487 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700488 }
Romain Guycabfcc12011-03-07 18:06:46 -0800489 }
490
Chet Haasedaf98e92011-01-10 14:10:36 -0800491 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700492 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800493}
494
Romain Guyf6a11b82010-06-23 17:47:49 -0700495///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700496// Debug
497///////////////////////////////////////////////////////////////////////////////
498
Romain Guy0f667532013-03-01 14:31:04 -0800499void OpenGLRenderer::eventMark(const char* name) const {
500 mCaches.eventMark(0, name);
501}
502
Romain Guy87e2f7572012-09-24 11:37:12 -0700503void OpenGLRenderer::startMark(const char* name) const {
504 mCaches.startMark(0, name);
505}
506
507void OpenGLRenderer::endMark() const {
508 mCaches.endMark();
509}
510
511void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
512 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
513 if (clear) {
514 mCaches.disableScissor();
515 mCaches.stencil.clear();
516 }
517 if (enable) {
518 mCaches.stencil.enableDebugWrite();
519 } else {
520 mCaches.stencil.disable();
521 }
522 }
523}
524
525void OpenGLRenderer::renderOverdraw() {
526 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700527 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700528
529 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700530 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700531 clip->right - clip->left, clip->bottom - clip->top);
532
533 mCaches.stencil.enableDebugTest(2);
534 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
535 mCaches.stencil.enableDebugTest(3);
536 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
537 mCaches.stencil.enableDebugTest(4);
538 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
539 mCaches.stencil.enableDebugTest(4, true);
540 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
541 mCaches.stencil.disable();
542 }
543}
544
Romain Guy78dd96d2013-05-03 14:24:16 -0700545void OpenGLRenderer::countOverdraw() {
546 size_t count = mWidth * mHeight;
547 uint32_t* buffer = new uint32_t[count];
548 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
549
550 size_t total = 0;
551 for (size_t i = 0; i < count; i++) {
552 total += buffer[i] & 0xff;
553 }
554
555 mOverdraw = total / float(count);
556
557 delete[] buffer;
558}
559
Romain Guy87e2f7572012-09-24 11:37:12 -0700560///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700561// Layers
562///////////////////////////////////////////////////////////////////////////////
563
564bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700565 if (layer->deferredUpdateScheduled && layer->renderer &&
566 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700567 ATRACE_CALL();
568
Romain Guy11cb6422012-09-21 00:39:43 -0700569 Rect& dirty = layer->dirtyRect;
570
Romain Guy7c450aa2012-09-21 19:15:00 -0700571 if (inFrame) {
572 endTiling();
573 debugOverdraw(false, false);
574 }
Romain Guy11cb6422012-09-21 00:39:43 -0700575
Romain Guy96885eb2013-03-26 15:05:58 -0700576 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700577 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700578 } else {
579 layer->defer();
580 }
Romain Guy11cb6422012-09-21 00:39:43 -0700581
582 if (inFrame) {
583 resumeAfterLayer();
584 startTiling(mSnapshot);
585 }
586
Romain Guy5bb3c732012-11-29 17:52:58 -0800587 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700588 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700589
590 return true;
591 }
592
593 return false;
594}
595
596void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700597 // If draw deferring is enabled this method will simply defer
598 // the display list of each individual layer. The layers remain
599 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700600 int count = mLayerUpdates.size();
601 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700602 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
603 startMark("Layer Updates");
604 } else {
605 startMark("Defer Layer Updates");
606 }
Romain Guy11cb6422012-09-21 00:39:43 -0700607
Chris Craik1206b9b2013-04-04 14:46:24 -0700608 // Note: it is very important to update the layers in order
609 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700610 Layer* layer = mLayerUpdates.itemAt(i);
611 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700612 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
613 mCaches.resourceCache.decrementRefcount(layer);
614 }
Romain Guy11cb6422012-09-21 00:39:43 -0700615 }
Romain Guy11cb6422012-09-21 00:39:43 -0700616
Romain Guy96885eb2013-03-26 15:05:58 -0700617 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
618 mLayerUpdates.clear();
619 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
620 }
621 endMark();
622 }
623}
624
625void OpenGLRenderer::flushLayers() {
626 int count = mLayerUpdates.size();
627 if (count > 0) {
628 startMark("Apply Layer Updates");
629 char layerName[12];
630
Chris Craik1206b9b2013-04-04 14:46:24 -0700631 // Note: it is very important to update the layers in order
632 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700633 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700634 startMark(layerName);
635
Romain Guy40543602013-06-12 15:31:28 -0700636 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700637 Layer* layer = mLayerUpdates.itemAt(i);
638 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700639 ATRACE_END();
640
Romain Guy02b49b72013-03-29 12:37:16 -0700641 mCaches.resourceCache.decrementRefcount(layer);
642
Romain Guy96885eb2013-03-26 15:05:58 -0700643 endMark();
644 }
645
646 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700647 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700648
Romain Guy11cb6422012-09-21 00:39:43 -0700649 endMark();
650 }
651}
652
653void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
654 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700655 // Make sure we don't introduce duplicates.
656 // SortedVector would do this automatically but we need to respect
657 // the insertion order. The linear search is not an issue since
658 // this list is usually very short (typically one item, at most a few)
659 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
660 if (mLayerUpdates.itemAt(i) == layer) {
661 return;
662 }
663 }
Romain Guy11cb6422012-09-21 00:39:43 -0700664 mLayerUpdates.push_back(layer);
665 mCaches.resourceCache.incrementRefcount(layer);
666 }
667}
668
Romain Guye93482f2013-06-17 13:14:51 -0700669void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
670 if (layer) {
671 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
672 if (mLayerUpdates.itemAt(i) == layer) {
673 mLayerUpdates.removeAt(i);
674 mCaches.resourceCache.decrementRefcount(layer);
675 break;
676 }
677 }
678 }
679}
680
Romain Guy11cb6422012-09-21 00:39:43 -0700681void OpenGLRenderer::clearLayerUpdates() {
682 size_t count = mLayerUpdates.size();
683 if (count > 0) {
684 mCaches.resourceCache.lock();
685 for (size_t i = 0; i < count; i++) {
686 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
687 }
688 mCaches.resourceCache.unlock();
689 mLayerUpdates.clear();
690 }
691}
692
Romain Guy40543602013-06-12 15:31:28 -0700693void OpenGLRenderer::flushLayerUpdates() {
694 syncState();
695 updateLayers();
696 flushLayers();
697 // Wait for all the layer updates to be executed
698 AutoFence fence;
699}
700
Romain Guy11cb6422012-09-21 00:39:43 -0700701///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700702// State management
703///////////////////////////////////////////////////////////////////////////////
704
Romain Guybb9524b2010-06-22 18:56:38 -0700705int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700706 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700707}
708
709int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700710 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700711}
712
713void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700714 if (mSaveCount > 1) {
715 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700716 }
Romain Guybb9524b2010-06-22 18:56:38 -0700717}
718
719void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700720 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700721
Romain Guy8fb95422010-08-17 18:38:51 -0700722 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700723 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700724 }
Romain Guybb9524b2010-06-22 18:56:38 -0700725}
726
Romain Guy8aef54f2010-09-01 15:13:49 -0700727int OpenGLRenderer::saveSnapshot(int flags) {
728 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700729 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700730}
731
732bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700733 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700734 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700735 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700736
Romain Guybd6b79b2010-06-26 00:13:53 -0700737 sp<Snapshot> current = mSnapshot;
738 sp<Snapshot> previous = mSnapshot->previous;
739
Romain Guyeb993562010-10-05 18:14:38 -0700740 if (restoreOrtho) {
741 Rect& r = previous->viewport;
742 glViewport(r.left, r.top, r.right, r.bottom);
743 mOrthoMatrix.load(current->orthoMatrix);
744 }
745
Romain Guy8b55f372010-08-18 17:10:07 -0700746 mSaveCount--;
747 mSnapshot = previous;
748
Romain Guy2542d192010-08-18 11:47:12 -0700749 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700750 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700751 }
Romain Guy2542d192010-08-18 11:47:12 -0700752
Romain Guy5ec99242010-11-03 16:19:08 -0700753 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700754 endMark(); // Savelayer
755 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700756 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700757 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700758 }
759
Romain Guy2542d192010-08-18 11:47:12 -0700760 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700761}
762
Romain Guyf6a11b82010-06-23 17:47:49 -0700763///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700764// Layers
765///////////////////////////////////////////////////////////////////////////////
766
767int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800768 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700769 const GLuint previousFbo = mSnapshot->fbo;
770 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700771
Romain Guyaf636eb2010-12-09 17:47:21 -0800772 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700773 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700774 }
Romain Guyd55a8612010-06-28 17:42:46 -0700775
776 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700777}
778
Chris Craikd90144d2013-03-19 15:03:48 -0700779void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
780 const Rect untransformedBounds(bounds);
781
782 currentTransform().mapRect(bounds);
783
784 // Layers only make sense if they are in the framebuffer's bounds
785 if (bounds.intersect(*mSnapshot->clipRect)) {
786 // We cannot work with sub-pixels in this case
787 bounds.snapToPixelBoundaries();
788
789 // When the layer is not an FBO, we may use glCopyTexImage so we
790 // need to make sure the layer does not extend outside the bounds
791 // of the framebuffer
792 if (!bounds.intersect(mSnapshot->previous->viewport)) {
793 bounds.setEmpty();
794 } else if (fboLayer) {
795 clip.set(bounds);
796 mat4 inverse;
797 inverse.loadInverse(currentTransform());
798 inverse.mapRect(clip);
799 clip.snapToPixelBoundaries();
800 if (clip.intersect(untransformedBounds)) {
801 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
802 bounds.set(untransformedBounds);
803 } else {
804 clip.setEmpty();
805 }
806 }
807 } else {
808 bounds.setEmpty();
809 }
810}
811
Chris Craik408eb122013-03-26 18:55:15 -0700812void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
813 bool fboLayer, int alpha) {
814 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
815 bounds.getHeight() > mCaches.maxTextureSize ||
816 (fboLayer && clip.isEmpty())) {
817 mSnapshot->empty = fboLayer;
818 } else {
819 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
820 }
821}
822
Chris Craikd90144d2013-03-19 15:03:48 -0700823int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
824 int alpha, SkXfermode::Mode mode, int flags) {
825 const GLuint previousFbo = mSnapshot->fbo;
826 const int count = saveSnapshot(flags);
827
828 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
829 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
830 // operations will be able to store and restore the current clip and transform info, and
831 // quick rejection will be correct (for display lists)
832
833 Rect bounds(left, top, right, bottom);
834 Rect clip;
835 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700836 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700837
Chris Craik408eb122013-03-26 18:55:15 -0700838 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700839 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
840 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700841 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700842 }
843 }
844
845 return count;
846}
847
848
Romain Guy1c740bc2010-09-13 18:00:09 -0700849/**
850 * Layers are viewed by Skia are slightly different than layers in image editing
851 * programs (for instance.) When a layer is created, previously created layers
852 * and the frame buffer still receive every drawing command. For instance, if a
853 * layer is created and a shape intersecting the bounds of the layers and the
854 * framebuffer is draw, the shape will be drawn on both (unless the layer was
855 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
856 *
857 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
858 * texture. Unfortunately, this is inefficient as it requires every primitive to
859 * be drawn n + 1 times, where n is the number of active layers. In practice this
860 * means, for every primitive:
861 * - Switch active frame buffer
862 * - Change viewport, clip and projection matrix
863 * - Issue the drawing
864 *
865 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700866 * To avoid this, layers are implemented in a different way here, at least in the
867 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
868 * is set. When this flag is set we can redirect all drawing operations into a
869 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700870 *
871 * This implementation relies on the frame buffer being at least RGBA 8888. When
872 * a layer is created, only a texture is created, not an FBO. The content of the
873 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700874 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700875 * buffer and drawing continues as normal. This technique therefore treats the
876 * frame buffer as a scratch buffer for the layers.
877 *
878 * To compose the layers back onto the frame buffer, each layer texture
879 * (containing the original frame buffer data) is drawn as a simple quad over
880 * the frame buffer. The trick is that the quad is set as the composition
881 * destination in the blending equation, and the frame buffer becomes the source
882 * of the composition.
883 *
884 * Drawing layers with an alpha value requires an extra step before composition.
885 * An empty quad is drawn over the layer's region in the frame buffer. This quad
886 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
887 * quad is used to multiply the colors in the frame buffer. This is achieved by
888 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
889 * GL_ZERO, GL_SRC_ALPHA.
890 *
891 * Because glCopyTexImage2D() can be slow, an alternative implementation might
892 * be use to draw a single clipped layer. The implementation described above
893 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700894 *
895 * (1) The frame buffer is actually not cleared right away. To allow the GPU
896 * to potentially optimize series of calls to glCopyTexImage2D, the frame
897 * buffer is left untouched until the first drawing operation. Only when
898 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700899 */
Chet Haased48885a2012-08-28 17:43:28 -0700900bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
901 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700902 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700903 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700904
Romain Guyeb993562010-10-05 18:14:38 -0700905 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
906
Romain Guyf607bdc2010-09-10 19:20:06 -0700907 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700908 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700909 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700910 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700911 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700912
913 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700914 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700915 return false;
916 }
Romain Guyf18fd992010-07-08 11:45:51 -0700917
Romain Guya1d3c912011-12-13 14:55:06 -0800918 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700919 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700920 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700921 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700922 }
923
Romain Guy9ace8f52011-07-07 20:50:11 -0700924 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700925 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700926 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
927 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800928 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700929 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700930 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700931
Romain Guy8fb95422010-08-17 18:38:51 -0700932 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700933 mSnapshot->flags |= Snapshot::kFlagIsLayer;
934 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700935
Chris Craik7273daa2013-03-28 11:25:24 -0700936 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700937 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700938 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700939 } else {
940 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700941 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800942 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700944 // Workaround for some GL drivers. When reading pixels lying outside
945 // of the window we should get undefined values for those pixels.
946 // Unfortunately some drivers will turn the entire target texture black
947 // when reading outside of the window.
948 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
949 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700950 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800951 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700952
Romain Guyb254c242013-06-27 17:15:24 -0700953 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
954 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
955
Romain Guy54be1cd2011-06-13 19:04:27 -0700956 // Enqueue the buffer coordinates to clear the corresponding region later
957 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700958 }
Romain Guyeb993562010-10-05 18:14:38 -0700959 }
Romain Guyf86ef572010-07-01 11:05:42 -0700960
Romain Guyd55a8612010-06-28 17:42:46 -0700961 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700962}
963
Chet Haased48885a2012-08-28 17:43:28 -0700964bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800965 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700966 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700967
Chet Haased48885a2012-08-28 17:43:28 -0700968 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800969 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
970 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700971 mSnapshot->fbo = layer->getFbo();
972 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
973 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
974 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
975 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700976 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700977
Romain Guy2b7028e2012-09-19 17:25:38 -0700978 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700979 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700980 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
982 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700983
984 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700985 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700986 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700987 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988 }
989
990 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700992
Romain Guyf735c8e2013-01-31 17:45:55 -0800993 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
995 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700996 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800997 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700998 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 glClear(GL_COLOR_BUFFER_BIT);
1000
1001 dirtyClip();
1002
1003 // Change the ortho projection
1004 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1005 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1006
1007 return true;
1008}
1009
Romain Guy1c740bc2010-09-13 18:00:09 -07001010/**
1011 * Read the documentation of createLayer() before doing anything in this method.
1012 */
Romain Guy1d83e192010-08-17 11:37:00 -07001013void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1014 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001015 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001016 return;
1017 }
1018
Romain Guy8ce00302013-01-15 18:51:42 -08001019 Layer* layer = current->layer;
1020 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001021 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001022
Chris Craik39a908c2013-06-13 14:39:01 -07001023 bool clipRequired = false;
1024 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1025 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1026
Romain Guyeb993562010-10-05 18:14:38 -07001027 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001028 endTiling();
1029
Romain Guye0aa84b2012-04-03 19:30:26 -07001030 // Detach the texture from the FBO
1031 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001032
1033 layer->removeFbo(false);
1034
Romain Guyeb993562010-10-05 18:14:38 -07001035 // Unbind current FBO and restore previous one
1036 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001037 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001038
1039 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001040 }
1041
Romain Guy9ace8f52011-07-07 20:50:11 -07001042 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001043 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001044 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001045 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001046 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001047 }
1048
Romain Guy03750a02010-10-18 14:06:08 -07001049 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001050
Romain Guya1d3c912011-12-13 14:55:06 -08001051 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001052
Romain Guy5b3b3522010-10-27 18:57:51 -07001053 // When the layer is stored in an FBO, we can save a bit of fillrate by
1054 // drawing only the dirty region
1055 if (fboLayer) {
1056 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 if (layer->getColorFilter()) {
1058 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001059 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001060 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001061 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001062 resetColorFilter();
1063 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001064 } else if (!rect.isEmpty()) {
1065 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1066 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001067 }
Romain Guy8b55f372010-08-18 17:10:07 -07001068
Romain Guy746b7402010-10-26 16:27:31 -07001069 dirtyClip();
1070
Romain Guyeb993562010-10-05 18:14:38 -07001071 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001072 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001073 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001074 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001075 }
1076}
1077
Romain Guyaa6c24c2011-04-28 18:40:04 -07001078void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001079 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001080
1081 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001083 setupDrawWithTexture();
1084 } else {
1085 setupDrawWithExternalTexture();
1086 }
1087 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001088 setupDrawColor(alpha, alpha, alpha, alpha);
1089 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001090 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001091 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001092 setupDrawPureColorUniforms();
1093 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001094 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1095 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001096 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001097 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001098 }
Romain Guy3b753822013-03-05 10:27:35 -08001099 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001100 layer->getWidth() == (uint32_t) rect.getWidth() &&
1101 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001102 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1103 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001104
Romain Guyd21b6e12011-11-30 20:21:23 -08001105 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001106 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1107 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001108 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001109 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1110 }
1111 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001112 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1113
1114 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1115
1116 finishDrawTexture();
1117}
1118
Romain Guy5b3b3522010-10-27 18:57:51 -07001119void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001120 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001121 const Rect& texCoords = layer->texCoords;
1122 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1123 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001124
Romain Guy9ace8f52011-07-07 20:50:11 -07001125 float x = rect.left;
1126 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001127 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001128 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001129 layer->getHeight() == (uint32_t) rect.getHeight();
1130
1131 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001132 // When we're swapping, the layer is already in screen coordinates
1133 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001134 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1135 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001136 }
1137
Romain Guyd21b6e12011-11-30 20:21:23 -08001138 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001139 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001140 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001141 }
1142
Chris Craik16ecda52013-03-29 10:59:59 -07001143 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001144 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001145 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001146 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001147 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1148 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001149
Romain Guyaa6c24c2011-04-28 18:40:04 -07001150 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1151 } else {
1152 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1153 drawTextureLayer(layer, rect);
1154 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1155 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001156}
1157
Chris Craik34416ea2013-04-15 16:08:28 -07001158/**
1159 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1160 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1161 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1162 * by saveLayer's restore
1163 */
1164#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1165 DRAW_COMMAND; \
1166 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1167 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1168 DRAW_COMMAND; \
1169 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1170 } \
1171 }
1172
1173#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1174
Romain Guy5b3b3522010-10-27 18:57:51 -07001175void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001176 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001177 layer->setRegionAsRect();
1178
Chris Craik34416ea2013-04-15 16:08:28 -07001179 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001180
Romain Guy5b3b3522010-10-27 18:57:51 -07001181 layer->region.clear();
1182 return;
1183 }
1184
Romain Guy211370f2012-02-01 16:10:55 -08001185 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001186 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001187 const android::Rect* rects;
1188 Region safeRegion;
1189 if (CC_LIKELY(hasRectToRectTransform())) {
1190 rects = layer->region.getArray(&count);
1191 } else {
1192 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1193 rects = safeRegion.getArray(&count);
1194 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001195
Chris Craik16ecda52013-03-29 10:59:59 -07001196 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001197 const float texX = 1.0f / float(layer->getWidth());
1198 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001199 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001200
Romain Guy8ce00302013-01-15 18:51:42 -08001201 setupDraw();
1202
1203 // We must get (and therefore bind) the region mesh buffer
1204 // after we setup drawing in case we need to mess with the
1205 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001206 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001207 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001208
Romain Guy7230a742011-01-10 22:26:16 -08001209 setupDrawWithTexture();
1210 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001211 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001212 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001213 setupDrawProgram();
1214 setupDrawDirtyRegionsDisabled();
1215 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001216 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001217 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001218 if (currentTransform().isPureTranslate()) {
1219 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1220 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001221
Romain Guyd21b6e12011-11-30 20:21:23 -08001222 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001223 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1224 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001225 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001226 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1227 }
Romain Guy15bc6432011-12-13 13:11:32 -08001228 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001229
1230 for (size_t i = 0; i < count; i++) {
1231 const android::Rect* r = &rects[i];
1232
1233 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001234 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001235 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001236 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001237
1238 // TODO: Reject quads outside of the clip
1239 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1240 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1241 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1242 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1243
1244 numQuads++;
1245
Romain Guy31e08e92013-06-18 15:53:53 -07001246 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001247 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1248 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001249 numQuads = 0;
1250 mesh = mCaches.getRegionMesh();
1251 }
1252 }
1253
1254 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001255 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1256 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 }
1258
Romain Guy7230a742011-01-10 22:26:16 -08001259 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001260
1261#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001262 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001263#endif
1264
1265 layer->region.clear();
1266 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001267}
1268
Romain Guy3a3133d2011-02-01 22:59:58 -08001269void OpenGLRenderer::drawRegionRects(const Region& region) {
1270#if DEBUG_LAYERS_AS_REGIONS
1271 size_t count;
1272 const android::Rect* rects = region.getArray(&count);
1273
1274 uint32_t colors[] = {
1275 0x7fff0000, 0x7f00ff00,
1276 0x7f0000ff, 0x7fff00ff,
1277 };
1278
1279 int offset = 0;
1280 int32_t top = rects[0].top;
1281
1282 for (size_t i = 0; i < count; i++) {
1283 if (top != rects[i].top) {
1284 offset ^= 0x2;
1285 top = rects[i].top;
1286 }
1287
1288 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1289 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1290 SkXfermode::kSrcOver_Mode);
1291 }
1292#endif
1293}
1294
Romain Guy8ce00302013-01-15 18:51:42 -08001295void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1296 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001297 Vector<float> rects;
1298
1299 SkRegion::Iterator it(region);
1300 while (!it.done()) {
1301 const SkIRect& r = it.rect();
1302 rects.push(r.fLeft);
1303 rects.push(r.fTop);
1304 rects.push(r.fRight);
1305 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001306 it.next();
1307 }
1308
Romain Guy448455f2013-07-22 13:57:50 -07001309 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001310}
1311
Romain Guy5b3b3522010-10-27 18:57:51 -07001312void OpenGLRenderer::dirtyLayer(const float left, const float top,
1313 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001314 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001315 Rect bounds(left, top, right, bottom);
1316 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001317 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001318 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001319}
1320
1321void OpenGLRenderer::dirtyLayer(const float left, const float top,
1322 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001323 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001324 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001325 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001326 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001327}
1328
1329void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001330 if (bounds.intersect(*mSnapshot->clipRect)) {
1331 bounds.snapToPixelBoundaries();
1332 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1333 if (!dirty.isEmpty()) {
1334 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001335 }
1336 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001337}
1338
Romain Guy448455f2013-07-22 13:57:50 -07001339void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1340 GLsizei elementsCount = quadsCount * 6;
1341 while (elementsCount > 0) {
1342 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1343
1344 setupDrawIndexedVertices(&mesh[0].position[0]);
1345 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1346
1347 elementsCount -= drawCount;
1348 // Though there are 4 vertices in a quad, we use 6 indices per
1349 // quad to draw with GL_TRIANGLES
1350 mesh += (drawCount / 6) * 4;
1351 }
1352}
1353
Romain Guy54be1cd2011-06-13 19:04:27 -07001354void OpenGLRenderer::clearLayerRegions() {
1355 const size_t count = mLayers.size();
1356 if (count == 0) return;
1357
1358 if (!mSnapshot->isIgnored()) {
1359 // Doing several glScissor/glClear here can negatively impact
1360 // GPUs with a tiler architecture, instead we draw quads with
1361 // the Clear blending mode
1362
1363 // The list contains bounds that have already been clipped
1364 // against their initial clip rect, and the current clip
1365 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001366 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001367
Romain Guy448455f2013-07-22 13:57:50 -07001368 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001369 Vertex* vertex = mesh;
1370
1371 for (uint32_t i = 0; i < count; i++) {
1372 Rect* bounds = mLayers.itemAt(i);
1373
Romain Guy54be1cd2011-06-13 19:04:27 -07001374 Vertex::set(vertex++, bounds->left, bounds->top);
1375 Vertex::set(vertex++, bounds->right, bounds->top);
1376 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001377 Vertex::set(vertex++, bounds->right, bounds->bottom);
1378
1379 delete bounds;
1380 }
Romain Guye67307c2013-02-11 18:01:20 -08001381 // We must clear the list of dirty rects before we
1382 // call setupDraw() to prevent stencil setup to do
1383 // the same thing again
1384 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001385
1386 setupDraw(false);
1387 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1388 setupDrawBlending(true, SkXfermode::kClear_Mode);
1389 setupDrawProgram();
1390 setupDrawPureColorUniforms();
1391 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1392
Romain Guy448455f2013-07-22 13:57:50 -07001393 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001394
1395 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001396 } else {
1397 for (uint32_t i = 0; i < count; i++) {
1398 delete mLayers.itemAt(i);
1399 }
Romain Guye67307c2013-02-11 18:01:20 -08001400 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001401 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001402}
1403
Romain Guybd6b79b2010-06-26 00:13:53 -07001404///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001405// State Deferral
1406///////////////////////////////////////////////////////////////////////////////
1407
Chris Craikff785832013-03-08 13:12:16 -08001408bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001409 const Rect& currentClip = *(mSnapshot->clipRect);
1410 const mat4& currentMatrix = *(mSnapshot->transform);
1411
Chris Craikff785832013-03-08 13:12:16 -08001412 if (stateDeferFlags & kStateDeferFlag_Draw) {
1413 // state has bounds initialized in local coordinates
1414 if (!state.mBounds.isEmpty()) {
1415 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001416 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001417 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1418 // is used, it should more closely duplicate the quickReject logic (in how it uses
1419 // snapToPixelBoundaries)
1420
Chris Craik28ce94a2013-05-31 11:38:03 -07001421 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001422 // quick rejected
1423 return true;
1424 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001425
Chris Craika02c4ed2013-06-14 13:43:58 -07001426 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001427 if (!currentClip.contains(state.mBounds)) {
1428 int& flags = state.mClipSideFlags;
1429 // op partially clipped, so record which sides are clipped for clip-aware merging
1430 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1431 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1432 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1433 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1434 }
1435 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001436 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001437 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1438 // overdraw avoidance (since we don't know what it overlaps)
1439 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001440 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001441 }
1442 }
1443
Chris Craik527a3aa2013-03-04 10:19:31 -08001444 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1445 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001446 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001447 }
1448
Chris Craik7273daa2013-03-28 11:25:24 -07001449 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1450 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001451 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001452 state.mDrawModifiers = mDrawModifiers;
1453 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001454 return false;
1455}
1456
Chris Craik527a3aa2013-03-04 10:19:31 -08001457void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001458 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001459 mDrawModifiers = state.mDrawModifiers;
1460 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001461
Chris Craik527a3aa2013-03-04 10:19:31 -08001462 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001463 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1464 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001465 dirtyClip();
1466 }
Chris Craikc3566d02013-02-04 16:16:33 -08001467}
1468
Chris Craik28ce94a2013-05-31 11:38:03 -07001469/**
1470 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1471 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1472 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1473 *
1474 * This method should be called when restoreDisplayState() won't be restoring the clip
1475 */
1476void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1477 if (clipRect != NULL) {
1478 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1479 } else {
1480 mSnapshot->setClip(0, 0, mWidth, mHeight);
1481 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001482 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001483 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001484}
1485
Chris Craikc3566d02013-02-04 16:16:33 -08001486///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001487// Transforms
1488///////////////////////////////////////////////////////////////////////////////
1489
1490void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001491 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001492}
1493
1494void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001495 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001496}
1497
1498void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001499 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001500}
1501
Romain Guy807daf72011-01-18 11:19:19 -08001502void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001503 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001504}
1505
Romain Guyf6a11b82010-06-23 17:47:49 -07001506void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001507 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001508 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001509 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001510 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001511 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001512}
1513
Chris Craikb98a0162013-02-21 11:30:22 -08001514bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001515 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001516}
1517
Romain Guyf6a11b82010-06-23 17:47:49 -07001518void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001519 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001520}
1521
1522void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001523 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001524 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001525 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001526 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001527}
1528
1529///////////////////////////////////////////////////////////////////////////////
1530// Clipping
1531///////////////////////////////////////////////////////////////////////////////
1532
Romain Guybb9524b2010-06-22 18:56:38 -07001533void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001534 Rect clip(*mSnapshot->clipRect);
1535 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001536
Romain Guy8a4ac612012-07-17 17:32:48 -07001537 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1538 clip.getWidth(), clip.getHeight())) {
1539 mDirtyClip = false;
1540 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001541}
1542
Romain Guy8ce00302013-01-15 18:51:42 -08001543void OpenGLRenderer::ensureStencilBuffer() {
1544 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1545 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1546 // just hope we have one when hasLayer() returns false.
1547 if (hasLayer()) {
1548 attachStencilBufferToLayer(mSnapshot->layer);
1549 }
1550}
1551
1552void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1553 // The layer's FBO is already bound when we reach this stage
1554 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001555 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1556 // is attached after we initiated tiling. We must turn it off,
1557 // attach the new render buffer then turn tiling back on
1558 endTiling();
1559
Romain Guy8d4aeb72013-02-12 16:08:55 -08001560 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001561 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001562 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001563
Romain Guyf735c8e2013-01-31 17:45:55 -08001564 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001565 }
1566}
1567
1568void OpenGLRenderer::setStencilFromClip() {
1569 if (!mCaches.debugOverdraw) {
1570 if (!mSnapshot->clipRegion->isEmpty()) {
1571 // NOTE: The order here is important, we must set dirtyClip to false
1572 // before any draw call to avoid calling back into this method
1573 mDirtyClip = false;
1574
1575 ensureStencilBuffer();
1576
1577 mCaches.stencil.enableWrite();
1578
1579 // Clear the stencil but first make sure we restrict drawing
1580 // to the region's bounds
1581 bool resetScissor = mCaches.enableScissor();
1582 if (resetScissor) {
1583 // The scissor was not set so we now need to update it
1584 setScissorFromClip();
1585 }
1586 mCaches.stencil.clear();
1587 if (resetScissor) mCaches.disableScissor();
1588
1589 // NOTE: We could use the region contour path to generate a smaller mesh
1590 // Since we are using the stencil we could use the red book path
1591 // drawing technique. It might increase bandwidth usage though.
1592
1593 // The last parameter is important: we are not drawing in the color buffer
1594 // so we don't want to dirty the current layer, if any
1595 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1596
1597 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001598
1599 // Draw the region used to generate the stencil if the appropriate debug
1600 // mode is enabled
1601 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1602 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1603 }
Romain Guy8ce00302013-01-15 18:51:42 -08001604 } else {
1605 mCaches.stencil.disable();
1606 }
1607 }
1608}
1609
Romain Guy9d5316e2010-06-24 19:30:36 -07001610const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001611 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001612}
1613
Chris Craik39a908c2013-06-13 14:39:01 -07001614bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001615 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001616 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001617 return true;
1618 }
1619
1620 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001621 currentTransform().mapRect(r);
Chris Craik5e49b302013-07-30 19:05:20 -07001622
1623 if (snapOut) {
1624 // snapOut is generally used to account for 1 pixel ramp (in window coordinates)
1625 // outside of the provided rect boundaries in tessellated AA geometry
1626 r.snapOutToPixelBoundaries();
1627 } else {
1628 r.snapToPixelBoundaries();
1629 }
Romain Guy8a4ac612012-07-17 17:32:48 -07001630
1631 Rect clipRect(*mSnapshot->clipRect);
1632 clipRect.snapToPixelBoundaries();
1633
Chris Craik39a908c2013-06-13 14:39:01 -07001634 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001635
Chris Craik39a908c2013-06-13 14:39:01 -07001636 if (clipRequired) *clipRequired = !clipRect.contains(r);
1637 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001638}
1639
Romain Guy672433d2013-01-04 19:05:13 -08001640bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1641 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001642 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1643 // the final mapped rect to ensure correct clipping behavior for the ramp.
1644 bool snapOut = paint->isAntiAlias();
1645
Chris Craikcb4d6002012-09-25 12:00:29 -07001646 if (paint->getStyle() != SkPaint::kFill_Style) {
1647 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001648 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001649 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001650 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001651 }
1652}
1653
Chris Craik5e49b302013-07-30 19:05:20 -07001654bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001655 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001656 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001657 return true;
1658 }
1659
Chris Craik39a908c2013-06-13 14:39:01 -07001660 if (!isDeferred()) {
1661 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001662 }
Chris Craik39a908c2013-06-13 14:39:01 -07001663 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001664}
1665
Romain Guy8ce00302013-01-15 18:51:42 -08001666void OpenGLRenderer::debugClip() {
1667#if DEBUG_CLIP_REGIONS
1668 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1669 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1670 }
1671#endif
1672}
1673
Romain Guy079ba2c2010-07-16 14:12:24 -07001674bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001675 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001676 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1677 if (clipped) {
1678 dirtyClip();
1679 }
1680 return !mSnapshot->clipRect->isEmpty();
1681 }
1682
1683 SkPath path;
1684 path.addRect(left, top, right, bottom);
1685
1686 return clipPath(&path, op);
1687}
1688
1689bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1690 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001691 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001692
1693 SkPath transformed;
1694 path->transform(transform, &transformed);
1695
1696 SkRegion clip;
1697 if (!mSnapshot->clipRegion->isEmpty()) {
1698 clip.setRegion(*mSnapshot->clipRegion);
1699 } else {
1700 Rect* bounds = mSnapshot->clipRect;
1701 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1702 }
1703
1704 SkRegion region;
1705 region.setPath(transformed, clip);
1706
1707 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001708 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001709 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001710 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001711 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001712}
1713
Romain Guy735738c2012-12-03 12:34:51 -08001714bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001715 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1716 if (clipped) {
1717 dirtyClip();
1718 }
1719 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001720}
1721
Chet Haasea23eed82012-04-12 15:19:04 -07001722Rect* OpenGLRenderer::getClipRect() {
1723 return mSnapshot->clipRect;
1724}
1725
Romain Guyf6a11b82010-06-23 17:47:49 -07001726///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001727// Drawing commands
1728///////////////////////////////////////////////////////////////////////////////
1729
Romain Guy54be1cd2011-06-13 19:04:27 -07001730void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001731 // TODO: It would be best if we could do this before quickReject()
1732 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001733 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001734 // Make sure setScissor & setStencil happen at the beginning of
1735 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001736 if (mDirtyClip) {
1737 if (mCaches.scissorEnabled) {
1738 setScissorFromClip();
1739 }
Romain Guy8ce00302013-01-15 18:51:42 -08001740 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001741 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001742
Romain Guy70ca14e2010-12-13 18:24:33 -08001743 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001744
Romain Guy70ca14e2010-12-13 18:24:33 -08001745 mSetShaderColor = false;
1746 mColorSet = false;
1747 mColorA = mColorR = mColorG = mColorB = 0.0f;
1748 mTextureUnit = 0;
1749 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001750
1751 // Enable debug highlight when what we're about to draw is tested against
1752 // the stencil buffer and if stencil highlight debugging is on
1753 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1754 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1755 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001756
1757 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001758}
1759
1760void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1761 mDescription.hasTexture = true;
1762 mDescription.hasAlpha8Texture = isAlpha8;
1763}
1764
Romain Guyff316ec2013-02-13 18:39:43 -08001765void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1766 mDescription.hasTexture = true;
1767 mDescription.hasColors = true;
1768 mDescription.hasAlpha8Texture = isAlpha8;
1769}
1770
Romain Guyaa6c24c2011-04-28 18:40:04 -07001771void OpenGLRenderer::setupDrawWithExternalTexture() {
1772 mDescription.hasExternalTexture = true;
1773}
1774
Romain Guy15bc6432011-12-13 13:11:32 -08001775void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001776 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001777}
1778
Chris Craik710f46d2012-09-17 17:25:49 -07001779void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001780 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001781}
1782
Romain Guy8d0d4782010-12-14 20:13:35 -08001783void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1784 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001785 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1786 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1787 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001788 mColorSet = true;
1789 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1790}
1791
Romain Guy86568192010-12-14 15:55:39 -08001792void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1793 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001794 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1795 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1796 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001797 mColorSet = true;
1798 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1799}
1800
Romain Guy41210632012-07-16 17:04:24 -07001801void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1802 mCaches.fontRenderer->describe(mDescription, paint);
1803}
1804
Romain Guy70ca14e2010-12-13 18:24:33 -08001805void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1806 mColorA = a;
1807 mColorR = r;
1808 mColorG = g;
1809 mColorB = b;
1810 mColorSet = true;
1811 mSetShaderColor = mDescription.setColor(r, g, b, a);
1812}
1813
1814void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001815 if (mDrawModifiers.mShader) {
1816 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001817 }
1818}
1819
1820void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001821 if (mDrawModifiers.mColorFilter) {
1822 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001823 }
1824}
1825
Romain Guyf09ef512011-05-27 11:43:46 -07001826void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1827 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1828 mColorA = 1.0f;
1829 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001830 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001831 }
1832}
1833
Romain Guy70ca14e2010-12-13 18:24:33 -08001834void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001835 // When the blending mode is kClear_Mode, we need to use a modulate color
1836 // argb=1,0,0,0
1837 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001838 bool blend = (mColorSet && mColorA < 1.0f) ||
1839 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1840 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001841}
1842
1843void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001844 // When the blending mode is kClear_Mode, we need to use a modulate color
1845 // argb=1,0,0,0
1846 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001847 blend |= (mColorSet && mColorA < 1.0f) ||
1848 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1849 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1850 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001851}
1852
1853void OpenGLRenderer::setupDrawProgram() {
1854 useProgram(mCaches.programCache.get(mDescription));
1855}
1856
1857void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1858 mTrackDirtyRegions = false;
1859}
1860
1861void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1862 bool ignoreTransform) {
1863 mModelView.loadTranslate(left, top, 0.0f);
1864 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001865 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1866 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001867 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001868 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001869 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1870 }
1871}
1872
Chet Haase8a5cc922011-04-26 07:28:09 -07001873void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001874 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001875}
1876
Romain Guy70ca14e2010-12-13 18:24:33 -08001877void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1878 bool ignoreTransform, bool ignoreModelView) {
1879 if (!ignoreModelView) {
1880 mModelView.loadTranslate(left, top, 0.0f);
1881 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001882 } else {
1883 mModelView.loadIdentity();
1884 }
Romain Guy86568192010-12-14 15:55:39 -08001885 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1886 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001887 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001888 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001889 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001890 }
1891 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001892 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001893 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1894 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001895}
1896
1897void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001898 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001899 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1900 }
1901}
1902
Romain Guy86568192010-12-14 15:55:39 -08001903void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001904 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001905 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001906 }
1907}
1908
Romain Guy70ca14e2010-12-13 18:24:33 -08001909void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001910 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001911 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001912 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001913 }
Chris Craikc3566d02013-02-04 16:16:33 -08001914 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1915 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001916 }
1917}
1918
Romain Guy8d0d4782010-12-14 20:13:35 -08001919void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001920 if (mDrawModifiers.mShader) {
1921 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001922 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001923 }
1924}
1925
Romain Guy70ca14e2010-12-13 18:24:33 -08001926void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001927 if (mDrawModifiers.mColorFilter) {
1928 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001929 }
1930}
1931
Romain Guy41210632012-07-16 17:04:24 -07001932void OpenGLRenderer::setupDrawTextGammaUniforms() {
1933 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1934}
1935
Romain Guy70ca14e2010-12-13 18:24:33 -08001936void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001937 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001938 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001939 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001940}
1941
1942void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001943 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001944 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001945 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001946}
1947
Romain Guyaa6c24c2011-04-28 18:40:04 -07001948void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1949 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001950 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001951 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001952}
1953
Romain Guy8f0095c2011-05-02 17:24:22 -07001954void OpenGLRenderer::setupDrawTextureTransform() {
1955 mDescription.hasTextureTransform = true;
1956}
1957
1958void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001959 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1960 GL_FALSE, &transform.data[0]);
1961}
1962
Romain Guy70ca14e2010-12-13 18:24:33 -08001963void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001964 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001965 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001966 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001967 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001968 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001969 }
Romain Guyd71dd362011-12-12 19:03:35 -08001970
Chris Craikcb4d6002012-09-25 12:00:29 -07001971 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001972 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001973 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001974 }
1975
1976 mCaches.unbindIndicesBuffer();
1977}
1978
Romain Guyff316ec2013-02-13 18:39:43 -08001979void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1980 bool force = mCaches.unbindMeshBuffer();
1981 GLsizei stride = sizeof(ColorTextureVertex);
1982
1983 mCaches.bindPositionVertexPointer(force, vertices, stride);
1984 if (mCaches.currentProgram->texCoords >= 0) {
1985 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1986 }
1987 int slot = mCaches.currentProgram->getAttrib("colors");
1988 if (slot >= 0) {
1989 glEnableVertexAttribArray(slot);
1990 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1991 }
1992
1993 mCaches.unbindIndicesBuffer();
1994}
1995
Romain Guy3b748a42013-04-17 18:54:38 -07001996void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1997 bool force = false;
1998 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1999 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2000 // use the default VBO found in Caches
2001 if (!vertices || vbo) {
2002 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2003 } else {
2004 force = mCaches.unbindMeshBuffer();
2005 }
2006 mCaches.bindIndicesBuffer();
2007
Chris Craikcb4d6002012-09-25 12:00:29 -07002008 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002009 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002010 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002011 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002012}
2013
Romain Guy448455f2013-07-22 13:57:50 -07002014void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002015 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002016 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002017 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002018}
2019
Romain Guy70ca14e2010-12-13 18:24:33 -08002020void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08002021}
2022
2023///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002024// Drawing
2025///////////////////////////////////////////////////////////////////////////////
2026
Chris Craikff785832013-03-08 13:12:16 -08002027status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2028 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002029 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002030 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2031 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002032 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002033 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002034 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002035 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2036 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002037 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002038 }
2039
Chris Craik28ce94a2013-05-31 11:38:03 -07002040 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2041 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002042 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2043 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002044
2045 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002046 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002047
Chet Haase58d110a2013-04-10 07:43:29 -07002048 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002049 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002050
Romain Guy65549432012-03-26 16:45:05 -07002051 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002052}
2053
Chris Craikc3566d02013-02-04 16:16:33 -08002054void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002055 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002056 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002057 }
2058}
2059
Romain Guya168d732011-03-18 16:50:13 -07002060void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2061 int alpha;
2062 SkXfermode::Mode mode;
2063 getAlphaAndMode(paint, &alpha, &mode);
2064
Romain Guy886b2752013-01-04 12:26:18 -08002065 int color = paint != NULL ? paint->getColor() : 0;
2066
Romain Guya168d732011-03-18 16:50:13 -07002067 float x = left;
2068 float y = top;
2069
Romain Guy886b2752013-01-04 12:26:18 -08002070 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2071
Romain Guya168d732011-03-18 16:50:13 -07002072 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002073 if (currentTransform().isPureTranslate()) {
2074 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2075 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002076 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002077
2078 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002079 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002080 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002081 }
2082
Romain Guy3b748a42013-04-17 18:54:38 -07002083 // No need to check for a UV mapper on the texture object, only ARGB_8888
2084 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002085 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002086 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2087 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002088}
2089
Romain Guy03c00b52013-06-20 18:30:28 -07002090/**
2091 * Important note: this method is intended to draw batches of bitmaps and
2092 * will not set the scissor enable or dirty the current layer, if any.
2093 * The caller is responsible for properly dirtying the current layer.
2094 */
Romain Guy55b6f952013-06-27 15:27:09 -07002095status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2096 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002097 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002098 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002099 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002100
Chris Craik527a3aa2013-03-04 10:19:31 -08002101 const AutoTexture autoCleanup(texture);
2102
2103 int alpha;
2104 SkXfermode::Mode mode;
2105 getAlphaAndMode(paint, &alpha, &mode);
2106
2107 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002108 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002109
2110 const float x = (int) floorf(bounds.left + 0.5f);
2111 const float y = (int) floorf(bounds.top + 0.5f);
2112 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2113 int color = paint != NULL ? paint->getColor() : 0;
2114 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2115 texture->id, paint != NULL, color, alpha, mode,
2116 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002117 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002118 } else {
2119 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2120 texture->id, alpha / 255.0f, mode, texture->blend,
2121 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002122 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002123 }
2124
2125 return DrawGlInfo::kStatusDrew;
2126}
2127
Chet Haase48659092012-05-31 15:21:51 -07002128status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002129 const float right = left + bitmap->width();
2130 const float bottom = top + bitmap->height();
2131
2132 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002133 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002134 }
2135
Romain Guya1d3c912011-12-13 14:55:06 -08002136 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002137 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002138 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002139 const AutoTexture autoCleanup(texture);
2140
Romain Guy211370f2012-02-01 16:10:55 -08002141 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002142 drawAlphaBitmap(texture, left, top, paint);
2143 } else {
2144 drawTextureRect(left, top, right, bottom, texture, paint);
2145 }
Chet Haase48659092012-05-31 15:21:51 -07002146
2147 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002148}
2149
Chet Haase48659092012-05-31 15:21:51 -07002150status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002151 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2152 const mat4 transform(*matrix);
2153 transform.mapRect(r);
2154
Romain Guy6926c722010-07-12 20:20:03 -07002155 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002156 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002157 }
2158
Romain Guya1d3c912011-12-13 14:55:06 -08002159 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002160 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002161 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002162 const AutoTexture autoCleanup(texture);
2163
Romain Guy5b3b3522010-10-27 18:57:51 -07002164 // This could be done in a cheaper way, all we need is pass the matrix
2165 // to the vertex shader. The save/restore is a bit overkill.
2166 save(SkCanvas::kMatrix_SaveFlag);
2167 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002168 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2169 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2170 } else {
2171 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2172 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002173 restore();
Chet Haase48659092012-05-31 15:21:51 -07002174
2175 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002176}
2177
Chet Haase48659092012-05-31 15:21:51 -07002178status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002179 const float right = left + bitmap->width();
2180 const float bottom = top + bitmap->height();
2181
2182 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002183 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002184 }
2185
2186 mCaches.activeTexture(0);
2187 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2188 const AutoTexture autoCleanup(texture);
2189
Romain Guy886b2752013-01-04 12:26:18 -08002190 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2191 drawAlphaBitmap(texture, left, top, paint);
2192 } else {
2193 drawTextureRect(left, top, right, bottom, texture, paint);
2194 }
Chet Haase48659092012-05-31 15:21:51 -07002195
2196 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002197}
2198
Chet Haase48659092012-05-31 15:21:51 -07002199status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002200 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002201 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002202 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002203 }
2204
Chris Craik39a908c2013-06-13 14:39:01 -07002205 // TODO: use quickReject on bounds from vertices
2206 mCaches.enableScissor();
2207
Romain Guyb18d2d02011-02-10 15:52:54 -08002208 float left = FLT_MAX;
2209 float top = FLT_MAX;
2210 float right = FLT_MIN;
2211 float bottom = FLT_MIN;
2212
Romain Guya92bb4d2012-10-16 11:08:44 -07002213 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002214
Romain Guyff316ec2013-02-13 18:39:43 -08002215 ColorTextureVertex mesh[count];
2216 ColorTextureVertex* vertex = mesh;
2217
2218 bool cleanupColors = false;
2219 if (!colors) {
2220 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2221 colors = new int[colorsCount];
2222 memset(colors, 0xff, colorsCount * sizeof(int));
2223 cleanupColors = true;
2224 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002225
Romain Guy3b748a42013-04-17 18:54:38 -07002226 mCaches.activeTexture(0);
2227 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2228 const UvMapper& mapper(getMapper(texture));
2229
Romain Guy5a7b4662011-01-20 19:09:30 -08002230 for (int32_t y = 0; y < meshHeight; y++) {
2231 for (int32_t x = 0; x < meshWidth; x++) {
2232 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2233
2234 float u1 = float(x) / meshWidth;
2235 float u2 = float(x + 1) / meshWidth;
2236 float v1 = float(y) / meshHeight;
2237 float v2 = float(y + 1) / meshHeight;
2238
Romain Guy3b748a42013-04-17 18:54:38 -07002239 mapper.map(u1, v1, u2, v2);
2240
Romain Guy5a7b4662011-01-20 19:09:30 -08002241 int ax = i + (meshWidth + 1) * 2;
2242 int ay = ax + 1;
2243 int bx = i;
2244 int by = bx + 1;
2245 int cx = i + 2;
2246 int cy = cx + 1;
2247 int dx = i + (meshWidth + 1) * 2 + 2;
2248 int dy = dx + 1;
2249
Romain Guyff316ec2013-02-13 18:39:43 -08002250 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2251 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2252 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002253
Romain Guyff316ec2013-02-13 18:39:43 -08002254 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2255 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2256 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002257
Romain Guya92bb4d2012-10-16 11:08:44 -07002258 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2259 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2260 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2261 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002262 }
2263 }
2264
Romain Guya92bb4d2012-10-16 11:08:44 -07002265 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002266 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002267 return DrawGlInfo::kStatusDone;
2268 }
2269
Romain Guyff316ec2013-02-13 18:39:43 -08002270 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002271 texture = mCaches.textureCache.get(bitmap);
2272 if (!texture) {
2273 if (cleanupColors) delete[] colors;
2274 return DrawGlInfo::kStatusDone;
2275 }
Romain Guyff316ec2013-02-13 18:39:43 -08002276 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002277 const AutoTexture autoCleanup(texture);
2278
2279 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2280 texture->setFilter(FILTER(paint), true);
2281
2282 int alpha;
2283 SkXfermode::Mode mode;
2284 getAlphaAndMode(paint, &alpha, &mode);
2285
Romain Guyff316ec2013-02-13 18:39:43 -08002286 float a = alpha / 255.0f;
2287
Romain Guya92bb4d2012-10-16 11:08:44 -07002288 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002289 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002290 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002291
Romain Guyff316ec2013-02-13 18:39:43 -08002292 setupDraw();
2293 setupDrawWithTextureAndColor();
2294 setupDrawColor(a, a, a, a);
2295 setupDrawColorFilter();
2296 setupDrawBlending(true, mode, false);
2297 setupDrawProgram();
2298 setupDrawDirtyRegionsDisabled();
2299 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2300 setupDrawTexture(texture->id);
2301 setupDrawPureColorUniforms();
2302 setupDrawColorFilterUniforms();
2303 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2304
2305 glDrawArrays(GL_TRIANGLES, 0, count);
2306
2307 finishDrawTexture();
2308
2309 int slot = mCaches.currentProgram->getAttrib("colors");
2310 if (slot >= 0) {
2311 glDisableVertexAttribArray(slot);
2312 }
2313
2314 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002315
2316 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002317}
2318
Chet Haase48659092012-05-31 15:21:51 -07002319status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002320 float srcLeft, float srcTop, float srcRight, float srcBottom,
2321 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002322 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002323 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002324 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002325 }
2326
Romain Guya1d3c912011-12-13 14:55:06 -08002327 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002328 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002329 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002330 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002331
Romain Guy8ba548f2010-06-30 19:21:21 -07002332 const float width = texture->width;
2333 const float height = texture->height;
2334
Romain Guy3b748a42013-04-17 18:54:38 -07002335 float u1 = fmax(0.0f, srcLeft / width);
2336 float v1 = fmax(0.0f, srcTop / height);
2337 float u2 = fmin(1.0f, srcRight / width);
2338 float v2 = fmin(1.0f, srcBottom / height);
2339
2340 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002341
Romain Guy03750a02010-10-18 14:06:08 -07002342 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002343 resetDrawTextureTexCoords(u1, v1, u2, v2);
2344
Romain Guy03750a02010-10-18 14:06:08 -07002345 int alpha;
2346 SkXfermode::Mode mode;
2347 getAlphaAndMode(paint, &alpha, &mode);
2348
Romain Guyd21b6e12011-11-30 20:21:23 -08002349 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2350
Romain Guy886b2752013-01-04 12:26:18 -08002351 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2352 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002353
Romain Guy886b2752013-01-04 12:26:18 -08002354 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2355 // Apply a scale transform on the canvas only when a shader is in use
2356 // Skia handles the ratio between the dst and src rects as a scale factor
2357 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002358 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002359 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002360
Romain Guy3b753822013-03-05 10:27:35 -08002361 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2362 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2363 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002364
2365 dstRight = x + (dstRight - dstLeft);
2366 dstBottom = y + (dstBottom - dstTop);
2367
2368 dstLeft = x;
2369 dstTop = y;
2370
2371 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2372 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002373 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002374 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002375 }
2376
2377 if (CC_UNLIKELY(useScaleTransform)) {
2378 save(SkCanvas::kMatrix_SaveFlag);
2379 translate(dstLeft, dstTop);
2380 scale(scaleX, scaleY);
2381
2382 dstLeft = 0.0f;
2383 dstTop = 0.0f;
2384
2385 dstRight = srcRight - srcLeft;
2386 dstBottom = srcBottom - srcTop;
2387 }
2388
2389 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2390 int color = paint ? paint->getColor() : 0;
2391 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2392 texture->id, paint != NULL, color, alpha, mode,
2393 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2394 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2395 } else {
2396 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2397 texture->id, alpha / 255.0f, mode, texture->blend,
2398 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2399 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2400 }
2401
2402 if (CC_UNLIKELY(useScaleTransform)) {
2403 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002404 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002405
2406 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002407
2408 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002409}
2410
Romain Guy3b748a42013-04-17 18:54:38 -07002411status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002412 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002413 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002414 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002415 }
2416
Romain Guy4c2547f2013-06-11 16:19:24 -07002417 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002418 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2419 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002420
Romain Guy03c00b52013-06-20 18:30:28 -07002421 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002422}
2423
Romain Guy03c00b52013-06-20 18:30:28 -07002424status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2425 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002426 if (quickReject(left, top, right, bottom)) {
2427 return DrawGlInfo::kStatusDone;
2428 }
2429
Romain Guy211370f2012-02-01 16:10:55 -08002430 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002431 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002432 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002433 if (!texture) return DrawGlInfo::kStatusDone;
2434 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002435
Romain Guya4adcf02013-02-28 12:15:35 -08002436 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2437 texture->setFilter(GL_LINEAR, true);
2438
Romain Guy03c00b52013-06-20 18:30:28 -07002439 int alpha;
2440 SkXfermode::Mode mode;
2441 getAlphaAndMode(paint, &alpha, &mode);
2442
Romain Guy3b753822013-03-05 10:27:35 -08002443 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002444 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002445 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002446 const float offsetX = left + currentTransform().getTranslateX();
2447 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002448 const size_t count = mesh->quads.size();
2449 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002450 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002451 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002452 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2453 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2454 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002455 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002456 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002457 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002458 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002459 }
2460 }
2461
Romain Guy211370f2012-02-01 16:10:55 -08002462 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002463 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2464 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002465
Romain Guy3b748a42013-04-17 18:54:38 -07002466 right = x + right - left;
2467 bottom = y + bottom - top;
2468 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2469 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2470 GL_TRIANGLES, mesh->indexCount, false, true,
2471 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002472 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002473 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2474 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2475 GL_TRIANGLES, mesh->indexCount, false, false,
2476 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002477 }
Romain Guy054dc182010-10-15 17:55:25 -07002478 }
Chet Haase48659092012-05-31 15:21:51 -07002479
2480 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002481}
2482
Romain Guy03c00b52013-06-20 18:30:28 -07002483/**
2484 * Important note: this method is intended to draw batches of 9-patch objects and
2485 * will not set the scissor enable or dirty the current layer, if any.
2486 * The caller is responsible for properly dirtying the current layer.
2487 */
2488status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2489 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2490 mCaches.activeTexture(0);
2491 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2492 if (!texture) return DrawGlInfo::kStatusDone;
2493 const AutoTexture autoCleanup(texture);
2494
2495 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2496 texture->setFilter(GL_LINEAR, true);
2497
2498 int alpha;
2499 SkXfermode::Mode mode;
2500 getAlphaAndMode(paint, &alpha, &mode);
2501
2502 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2503 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2504 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2505
2506 return DrawGlInfo::kStatusDrew;
2507}
2508
Chris Craik65cd6122012-12-10 17:56:27 -08002509status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2510 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002511 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002512 // no vertices to draw
2513 return DrawGlInfo::kStatusDone;
2514 }
2515
Chris Craikcb4d6002012-09-25 12:00:29 -07002516 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002517 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2518 bool isAA = paint->isAntiAlias();
2519
Chris Craik710f46d2012-09-17 17:25:49 -07002520 setupDraw();
2521 setupDrawNoTexture();
2522 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002523 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2524 setupDrawColorFilter();
2525 setupDrawShader();
2526 setupDrawBlending(isAA, mode);
2527 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002528 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002529 setupDrawColorUniforms();
2530 setupDrawColorFilterUniforms();
2531 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002532
Chris Craik710f46d2012-09-17 17:25:49 -07002533 void* vertices = vertexBuffer.getBuffer();
2534 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002535 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002536 mCaches.resetTexCoordsVertexPointer();
2537 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002538
Chris Craik710f46d2012-09-17 17:25:49 -07002539 int alphaSlot = -1;
2540 if (isAA) {
2541 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2542 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002543
Chris Craik710f46d2012-09-17 17:25:49 -07002544 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002545 glEnableVertexAttribArray(alphaSlot);
2546 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002547 }
Romain Guy04299382012-07-18 17:15:41 -07002548
Chris Craik6d29c8d2013-05-08 18:35:44 -07002549 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002550
Chris Craik710f46d2012-09-17 17:25:49 -07002551 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002552 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002553 }
Chris Craik65cd6122012-12-10 17:56:27 -08002554
2555 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002556}
2557
2558/**
Chris Craik65cd6122012-12-10 17:56:27 -08002559 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2560 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2561 * screen space in all directions. However, instead of using a fragment shader to compute the
2562 * translucency of the color from its position, we simply use a varying parameter to define how far
2563 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2564 *
2565 * Doesn't yet support joins, caps, or path effects.
2566 */
2567status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2568 VertexBuffer vertexBuffer;
2569 // TODO: try clipping large paths to viewport
2570 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2571
Romain Guyc46d07a2013-03-15 19:06:39 -07002572 if (hasLayer()) {
2573 SkRect bounds = path.getBounds();
2574 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2575 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2576 }
Chris Craik65cd6122012-12-10 17:56:27 -08002577
2578 return drawVertexBuffer(vertexBuffer, paint);
2579}
2580
2581/**
2582 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2583 * and additional geometry for defining an alpha slope perimeter.
2584 *
2585 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2586 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2587 * in-shader alpha region, but found it to be taxing on some GPUs.
2588 *
2589 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2590 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002591 */
Chet Haase48659092012-05-31 15:21:51 -07002592status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002593 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002594
Chris Craik65cd6122012-12-10 17:56:27 -08002595 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002596
Chris Craik65cd6122012-12-10 17:56:27 -08002597 VertexBuffer buffer;
2598 SkRect bounds;
2599 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002600
2601 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2602 return DrawGlInfo::kStatusDone;
2603 }
2604
Romain Guy3b753822013-03-05 10:27:35 -08002605 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002606
Chris Craik65cd6122012-12-10 17:56:27 -08002607 bool useOffset = !paint->isAntiAlias();
2608 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002609}
2610
Chet Haase48659092012-05-31 15:21:51 -07002611status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002612 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002613
Chris Craik6d29c8d2013-05-08 18:35:44 -07002614 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002615
Chris Craik6d29c8d2013-05-08 18:35:44 -07002616 VertexBuffer buffer;
2617 SkRect bounds;
2618 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002619
Chris Craik6d29c8d2013-05-08 18:35:44 -07002620 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2621 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002622 }
2623
Chris Craik6d29c8d2013-05-08 18:35:44 -07002624 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002625
Chris Craik6d29c8d2013-05-08 18:35:44 -07002626 bool useOffset = !paint->isAntiAlias();
2627 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002628}
2629
Chet Haase48659092012-05-31 15:21:51 -07002630status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002631 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002632 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002633
Romain Guyae88e5e2010-10-22 17:49:18 -07002634 Rect& clip(*mSnapshot->clipRect);
2635 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002636
Romain Guy3d58c032010-07-14 16:34:53 -07002637 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002638
2639 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002640}
Romain Guy9d5316e2010-06-24 19:30:36 -07002641
Chet Haase48659092012-05-31 15:21:51 -07002642status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2643 SkPaint* paint) {
2644 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002645 const AutoTexture autoCleanup(texture);
2646
2647 const float x = left + texture->left - texture->offset;
2648 const float y = top + texture->top - texture->offset;
2649
2650 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002651
2652 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002653}
2654
Chet Haase48659092012-05-31 15:21:51 -07002655status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002656 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002657 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2658 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002659 return DrawGlInfo::kStatusDone;
2660 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002661
Chris Craikcb4d6002012-09-25 12:00:29 -07002662 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002663 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002664 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002665 right - left, bottom - top, rx, ry, p);
2666 return drawShape(left, top, texture, p);
2667 }
2668
2669 SkPath path;
2670 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002671 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2672 float outset = p->getStrokeWidth() / 2;
2673 rect.outset(outset, outset);
2674 rx += outset;
2675 ry += outset;
2676 }
Chris Craik710f46d2012-09-17 17:25:49 -07002677 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002678 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002679}
2680
Chris Craik710f46d2012-09-17 17:25:49 -07002681status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002682 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002683 x + radius, y + radius, p) ||
2684 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002685 return DrawGlInfo::kStatusDone;
2686 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002687 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002688 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002689 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002690 return drawShape(x - radius, y - radius, texture, p);
2691 }
2692
2693 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002694 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2695 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2696 } else {
2697 path.addCircle(x, y, radius);
2698 }
Chris Craik65cd6122012-12-10 17:56:27 -08002699 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002700}
Romain Guy01d58e42011-01-19 21:54:02 -08002701
Chet Haase48659092012-05-31 15:21:51 -07002702status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002703 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002704 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2705 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002706 return DrawGlInfo::kStatusDone;
2707 }
Romain Guy01d58e42011-01-19 21:54:02 -08002708
Chris Craikcb4d6002012-09-25 12:00:29 -07002709 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002710 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002711 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002712 return drawShape(left, top, texture, p);
2713 }
2714
2715 SkPath path;
2716 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002717 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2718 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2719 }
Chris Craik710f46d2012-09-17 17:25:49 -07002720 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002721 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002722}
2723
Chet Haase48659092012-05-31 15:21:51 -07002724status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002725 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002726 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2727 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002728 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002729 }
2730
Chris Craik780c1282012-10-04 14:10:49 -07002731 if (fabs(sweepAngle) >= 360.0f) {
2732 return drawOval(left, top, right, bottom, p);
2733 }
2734
2735 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002736 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002737 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002738 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002739 startAngle, sweepAngle, useCenter, p);
2740 return drawShape(left, top, texture, p);
2741 }
2742
2743 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2744 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2745 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2746 }
2747
2748 SkPath path;
2749 if (useCenter) {
2750 path.moveTo(rect.centerX(), rect.centerY());
2751 }
2752 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2753 if (useCenter) {
2754 path.close();
2755 }
Chris Craik65cd6122012-12-10 17:56:27 -08002756 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002757}
2758
Romain Guycf8675e2012-10-02 12:32:25 -07002759// See SkPaintDefaults.h
2760#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2761
Chet Haase48659092012-05-31 15:21:51 -07002762status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002763 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2764 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002765 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002766 }
2767
Chris Craik710f46d2012-09-17 17:25:49 -07002768 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002769 // only fill style is supported by drawConvexPath, since others have to handle joins
2770 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2771 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2772 mCaches.activeTexture(0);
2773 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002774 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002775 return drawShape(left, top, texture, p);
2776 }
2777
2778 SkPath path;
2779 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2780 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2781 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2782 }
2783 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002784 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002785 }
2786
Romain Guy3b753822013-03-05 10:27:35 -08002787 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002788 SkPath path;
2789 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002790 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002791 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002792 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002793 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002794 }
Romain Guyc7d53492010-06-25 13:41:57 -07002795}
Romain Guy9d5316e2010-06-24 19:30:36 -07002796
Raph Levien416a8472012-07-19 22:48:17 -07002797void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2798 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2799 float x, float y) {
2800 mCaches.activeTexture(0);
2801
2802 // NOTE: The drop shadow will not perform gamma correction
2803 // if shader-based correction is enabled
2804 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2805 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002806 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002807 // If the drop shadow exceeds the max texture size or couldn't be
2808 // allocated, skip drawing
2809 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002810 const AutoTexture autoCleanup(shadow);
2811
Chris Craikc3566d02013-02-04 16:16:33 -08002812 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2813 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002814
Chris Craikc3566d02013-02-04 16:16:33 -08002815 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2816 int shadowColor = mDrawModifiers.mShadowColor;
2817 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002818 shadowColor = 0xffffffff;
2819 }
2820
2821 setupDraw();
2822 setupDrawWithTexture(true);
2823 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2824 setupDrawColorFilter();
2825 setupDrawShader();
2826 setupDrawBlending(true, mode);
2827 setupDrawProgram();
2828 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2829 setupDrawTexture(shadow->id);
2830 setupDrawPureColorUniforms();
2831 setupDrawColorFilterUniforms();
2832 setupDrawShaderUniforms();
2833 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2834
2835 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2836}
2837
Romain Guy768bffc2013-02-27 13:50:45 -08002838bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2839 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2840 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2841}
2842
Romain Guy257ae352013-03-20 16:31:12 -07002843class TextSetupFunctor: public Functor {
2844public:
2845 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2846 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2847 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2848 alpha(alpha), mode(mode), paint(paint) {
2849 }
2850 ~TextSetupFunctor() { }
2851
2852 status_t operator ()(int what, void* data) {
2853 renderer.setupDraw();
2854 renderer.setupDrawTextGamma(paint);
2855 renderer.setupDrawDirtyRegionsDisabled();
2856 renderer.setupDrawWithTexture(true);
2857 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2858 renderer.setupDrawColorFilter();
2859 renderer.setupDrawShader();
2860 renderer.setupDrawBlending(true, mode);
2861 renderer.setupDrawProgram();
2862 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2863 // Calling setupDrawTexture with the name 0 will enable the
2864 // uv attributes and increase the texture unit count
2865 // texture binding will be performed by the font renderer as
2866 // needed
2867 renderer.setupDrawTexture(0);
2868 renderer.setupDrawPureColorUniforms();
2869 renderer.setupDrawColorFilterUniforms();
2870 renderer.setupDrawShaderUniforms(pureTranslate);
2871 renderer.setupDrawTextGammaUniforms();
2872
2873 return NO_ERROR;
2874 }
2875
2876 OpenGLRenderer& renderer;
2877 float x;
2878 float y;
2879 bool pureTranslate;
2880 int alpha;
2881 SkXfermode::Mode mode;
2882 SkPaint* paint;
2883};
2884
Chet Haase48659092012-05-31 15:21:51 -07002885status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002886 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002887 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002888 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002889 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002890
Romain Guy671d6cf2012-01-18 12:39:17 -08002891 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002892 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002893 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002894 }
2895
Chris Craik39a908c2013-06-13 14:39:01 -07002896 mCaches.enableScissor();
2897
Romain Guy671d6cf2012-01-18 12:39:17 -08002898 float x = 0.0f;
2899 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002900 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002901 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002902 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2903 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002904 }
2905
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002906 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002907 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002908
2909 int alpha;
2910 SkXfermode::Mode mode;
2911 getAlphaAndMode(paint, &alpha, &mode);
2912
Chris Craikc3566d02013-02-04 16:16:33 -08002913 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002914 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2915 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002916 }
2917
Romain Guy671d6cf2012-01-18 12:39:17 -08002918 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002919 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002920 if (pureTranslate && !linearFilter) {
2921 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2922 }
Romain Guy257ae352013-03-20 16:31:12 -07002923 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002924
2925 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2926 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2927
Romain Guy211370f2012-02-01 16:10:55 -08002928 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002929
Romain Guy257ae352013-03-20 16:31:12 -07002930 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002931 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002932 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002933 if (hasActiveLayer) {
2934 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002935 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002936 }
2937 dirtyLayerUnchecked(bounds, getRegion());
2938 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002939 }
Chet Haase48659092012-05-31 15:21:51 -07002940
2941 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002942}
2943
Romain Guy624234f2013-03-05 16:43:31 -08002944mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2945 mat4 fontTransform;
2946 if (CC_LIKELY(transform.isPureTranslate())) {
2947 fontTransform = mat4::identity();
2948 } else {
2949 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002950 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002951 } else {
2952 float sx, sy;
2953 currentTransform().decomposeScale(sx, sy);
2954 fontTransform.loadScale(sx, sy, 1.0f);
2955 }
2956 }
2957 return fontTransform;
2958}
2959
Chris Craik41541822013-05-03 16:35:54 -07002960status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2961 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002962 DrawOpMode drawOpMode) {
2963
Chris Craik527a3aa2013-03-04 10:19:31 -08002964 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002965 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2966 // drawing as ops from DeferredDisplayList are already filtered for these
2967 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2968 quickReject(bounds)) {
2969 return DrawGlInfo::kStatusDone;
2970 }
Romain Guycac5fd32011-12-01 20:08:50 -08002971 }
2972
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002973 const float oldX = x;
2974 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002975
2976 const mat4& transform = currentTransform();
2977 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002978
Romain Guy211370f2012-02-01 16:10:55 -08002979 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002980 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2981 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002982 }
2983
Romain Guy86568192010-12-14 15:55:39 -08002984 int alpha;
2985 SkXfermode::Mode mode;
2986 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002987
Romain Guyc74f45a2013-02-26 19:10:14 -08002988 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2989
Chris Craikc3566d02013-02-04 16:16:33 -08002990 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002991 fontRenderer.setFont(paint, mat4::identity());
2992 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2993 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002994 }
2995
Romain Guy19d4dd82013-03-04 11:14:26 -08002996 const bool hasActiveLayer = hasLayer();
2997
Romain Guy624234f2013-03-05 16:43:31 -08002998 // We only pass a partial transform to the font renderer. That partial
2999 // matrix defines how glyphs are rasterized. Typically we want glyphs
3000 // to be rasterized at their final size on screen, which means the partial
3001 // matrix needs to take the scale factor into account.
3002 // When a partial matrix is used to transform glyphs during rasterization,
3003 // the mesh is generated with the inverse transform (in the case of scale,
3004 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3005 // apply the full transform matrix at draw time in the vertex shader.
3006 // Applying the full matrix in the shader is the easiest way to handle
3007 // rotation and perspective and allows us to always generated quads in the
3008 // font renderer which greatly simplifies the code, clipping in particular.
3009 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003010 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003011
Romain Guy6620c6d2010-12-06 18:07:02 -08003012 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003013 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003014 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003015
Romain Guy624234f2013-03-05 16:43:31 -08003016 // TODO: Implement better clipping for scaled/rotated text
3017 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003018 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 -07003019
Raph Levien996e57c2012-07-23 15:22:52 -07003020 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07003021 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003022
3023 // don't call issuedrawcommand, do it at end of batch
3024 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003025 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003026 SkPaint paintCopy(*paint);
3027 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3028 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003029 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003030 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003031 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003032 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003033 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003034
Chris Craik527a3aa2013-03-04 10:19:31 -08003035 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003036 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003037 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003038 }
Chris Craik41541822013-05-03 16:35:54 -07003039 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003040 }
Romain Guy694b5192010-07-21 21:33:20 -07003041
Chris Craik41541822013-05-03 16:35:54 -07003042 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003043
3044 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003045}
3046
Chet Haase48659092012-05-31 15:21:51 -07003047status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003048 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003049 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003050 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003051 }
3052
Chris Craik39a908c2013-06-13 14:39:01 -07003053 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3054 mCaches.enableScissor();
3055
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003056 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003057 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003058 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003059
3060 int alpha;
3061 SkXfermode::Mode mode;
3062 getAlphaAndMode(paint, &alpha, &mode);
3063
Romain Guy03d58522012-02-24 17:54:07 -08003064 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07003065 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08003066 setupDrawDirtyRegionsDisabled();
3067 setupDrawWithTexture(true);
3068 setupDrawAlpha8Color(paint->getColor(), alpha);
3069 setupDrawColorFilter();
3070 setupDrawShader();
3071 setupDrawBlending(true, mode);
3072 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08003073 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07003074 // Calling setupDrawTexture with the name 0 will enable the
3075 // uv attributes and increase the texture unit count
3076 // texture binding will be performed by the font renderer as
3077 // needed
3078 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003079 setupDrawPureColorUniforms();
3080 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003081 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003082 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003083
Romain Guy97771732012-02-28 18:17:02 -08003084 const Rect* clip = &mSnapshot->getLocalClip();
3085 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 -08003086
Romain Guy97771732012-02-28 18:17:02 -08003087 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003088
3089 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3090 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003091 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003092 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003093 dirtyLayerUnchecked(bounds, getRegion());
3094 }
Romain Guy97771732012-02-28 18:17:02 -08003095 }
Chet Haase48659092012-05-31 15:21:51 -07003096
3097 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003098}
3099
Chet Haase48659092012-05-31 15:21:51 -07003100status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3101 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003102
Romain Guya1d3c912011-12-13 14:55:06 -08003103 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003104
Romain Guyfb8b7632010-08-23 21:05:08 -07003105 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003106 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003107 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003108
Romain Guy8b55f372010-08-18 17:10:07 -07003109 const float x = texture->left - texture->offset;
3110 const float y = texture->top - texture->offset;
3111
Romain Guy01d58e42011-01-19 21:54:02 -08003112 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003113
3114 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003115}
3116
Chris Craika08f95c2013-03-15 17:24:33 -07003117status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003118 if (!layer) {
3119 return DrawGlInfo::kStatusDone;
3120 }
3121
Romain Guyb2e2f242012-10-17 18:18:35 -07003122 mat4* transform = NULL;
3123 if (layer->isTextureLayer()) {
3124 transform = &layer->getTransform();
3125 if (!transform->isIdentity()) {
3126 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003127 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003128 }
3129 }
3130
Chris Craik39a908c2013-06-13 14:39:01 -07003131 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003132 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003133 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003134
3135 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003136 if (transform && !transform->isIdentity()) {
3137 restore();
3138 }
Chet Haase48659092012-05-31 15:21:51 -07003139 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003140 }
3141
Romain Guy5bb3c732012-11-29 17:52:58 -08003142 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003143
Chris Craik39a908c2013-06-13 14:39:01 -07003144 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003145 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003146
Romain Guy211370f2012-02-01 16:10:55 -08003147 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003148 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3149 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003150
Romain Guyc88e3572011-01-22 00:32:12 -08003151 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003152 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3153 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003154 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003155 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003156 setupDraw();
3157 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003158 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003159 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003160 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003161 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003162 setupDrawPureColorUniforms();
3163 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003164 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003165 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3166 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3167 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003168
Romain Guyd21b6e12011-11-30 20:21:23 -08003169 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003170 setupDrawModelViewTranslate(tx, ty,
3171 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003172 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003173 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003174 setupDrawModelViewTranslate(x, y,
3175 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3176 }
Romain Guyf219da52011-01-16 12:54:25 -08003177
Romain Guy448455f2013-07-22 13:57:50 -07003178 TextureVertex* mesh = &layer->mesh[0];
3179 GLsizei elementsCount = layer->meshElementCount;
3180
3181 while (elementsCount > 0) {
3182 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3183
3184 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3185 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3186 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3187
3188 elementsCount -= drawCount;
3189 // Though there are 4 vertices in a quad, we use 6 indices per
3190 // quad to draw with GL_TRIANGLES
3191 mesh += (drawCount / 6) * 4;
3192 }
Romain Guyf219da52011-01-16 12:54:25 -08003193
Romain Guyc88e3572011-01-22 00:32:12 -08003194 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003195
3196#if DEBUG_LAYERS_AS_REGIONS
3197 drawRegionRects(layer->region);
3198#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003199 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003200
Chris Craikc3566d02013-02-04 16:16:33 -08003201 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003202
Romain Guy5bb3c732012-11-29 17:52:58 -08003203 if (layer->debugDrawUpdate) {
3204 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003205 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3206 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3207 }
Romain Guyf219da52011-01-16 12:54:25 -08003208 }
Chris Craik34416ea2013-04-15 16:08:28 -07003209 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003210
Romain Guyb2e2f242012-10-17 18:18:35 -07003211 if (transform && !transform->isIdentity()) {
3212 restore();
3213 }
3214
Chet Haase48659092012-05-31 15:21:51 -07003215 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003216}
3217
Romain Guy6926c722010-07-12 20:20:03 -07003218///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003219// Shaders
3220///////////////////////////////////////////////////////////////////////////////
3221
3222void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003223 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003224}
3225
Romain Guy06f96e22010-07-30 19:18:16 -07003226void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003227 mDrawModifiers.mShader = shader;
3228 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003229 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003230 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003231}
3232
Romain Guyd27977d2010-07-14 19:18:51 -07003233///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003234// Color filters
3235///////////////////////////////////////////////////////////////////////////////
3236
3237void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003238 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003239}
3240
3241void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003242 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003243}
3244
3245///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003246// Drop shadow
3247///////////////////////////////////////////////////////////////////////////////
3248
3249void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003250 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003251}
3252
3253void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003254 mDrawModifiers.mHasShadow = true;
3255 mDrawModifiers.mShadowRadius = radius;
3256 mDrawModifiers.mShadowDx = dx;
3257 mDrawModifiers.mShadowDy = dy;
3258 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003259}
3260
3261///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003262// Draw filters
3263///////////////////////////////////////////////////////////////////////////////
3264
3265void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003266 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3267 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003268 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003269 mDrawModifiers.mPaintFilterClearBits = 0;
3270 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003271}
3272
3273void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003274 mDrawModifiers.mHasDrawFilter = true;
3275 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3276 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003277}
3278
Chris Craika08f95c2013-03-15 17:24:33 -07003279SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003280 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003281 return paint;
3282 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003283
3284 uint32_t flags = paint->getFlags();
3285
3286 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003287 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3288 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003289
3290 return &mFilteredPaint;
3291}
3292
3293///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003294// Drawing implementation
3295///////////////////////////////////////////////////////////////////////////////
3296
Romain Guy3b748a42013-04-17 18:54:38 -07003297Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3298 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3299 if (!texture) {
3300 return mCaches.textureCache.get(bitmap);
3301 }
3302 return texture;
3303}
3304
Romain Guy01d58e42011-01-19 21:54:02 -08003305void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3306 float x, float y, SkPaint* paint) {
3307 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3308 return;
3309 }
3310
3311 int alpha;
3312 SkXfermode::Mode mode;
3313 getAlphaAndMode(paint, &alpha, &mode);
3314
3315 setupDraw();
3316 setupDrawWithTexture(true);
3317 setupDrawAlpha8Color(paint->getColor(), alpha);
3318 setupDrawColorFilter();
3319 setupDrawShader();
3320 setupDrawBlending(true, mode);
3321 setupDrawProgram();
3322 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3323 setupDrawTexture(texture->id);
3324 setupDrawPureColorUniforms();
3325 setupDrawColorFilterUniforms();
3326 setupDrawShaderUniforms();
3327 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3328
3329 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3330
3331 finishDrawTexture();
3332}
3333
Romain Guyf607bdc2010-09-10 19:20:06 -07003334// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003335#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3336#define kStdUnderline_Offset (1.0f / 9.0f)
3337#define kStdUnderline_Thickness (1.0f / 18.0f)
3338
Chris Craik41541822013-05-03 16:35:54 -07003339void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003340 float x, float y, SkPaint* paint) {
3341 // Handle underline and strike-through
3342 uint32_t flags = paint->getFlags();
3343 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003344 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003345
Romain Guy211370f2012-02-01 16:10:55 -08003346 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003347 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003348 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003349
Raph Levien8b4072d2012-07-30 15:50:00 -07003350 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003351 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003352
Romain Guyf6834472011-01-23 13:32:12 -08003353 int linesCount = 0;
3354 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3355 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3356
3357 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003358 float points[pointsCount];
3359 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003360
3361 if (flags & SkPaint::kUnderlineText_Flag) {
3362 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003363 points[currentPoint++] = left;
3364 points[currentPoint++] = top;
3365 points[currentPoint++] = left + underlineWidth;
3366 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003367 }
3368
3369 if (flags & SkPaint::kStrikeThruText_Flag) {
3370 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003371 points[currentPoint++] = left;
3372 points[currentPoint++] = top;
3373 points[currentPoint++] = left + underlineWidth;
3374 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003375 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003376
Romain Guy726aeba2011-06-01 14:52:00 -07003377 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003378
Romain Guy726aeba2011-06-01 14:52:00 -07003379 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003380 }
3381 }
3382}
3383
Romain Guy672433d2013-01-04 19:05:13 -08003384status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3385 if (mSnapshot->isIgnored()) {
3386 return DrawGlInfo::kStatusDone;
3387 }
3388
Romain Guy735738c2012-12-03 12:34:51 -08003389 int color = paint->getColor();
3390 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003391 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003392 color |= 0x00ffffff;
3393 }
3394 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3395
3396 return drawColorRects(rects, count, color, mode);
3397}
3398
3399status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003400 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003401 if (count == 0) {
3402 return DrawGlInfo::kStatusDone;
3403 }
Romain Guy735738c2012-12-03 12:34:51 -08003404
Romain Guy672433d2013-01-04 19:05:13 -08003405 float left = FLT_MAX;
3406 float top = FLT_MAX;
3407 float right = FLT_MIN;
3408 float bottom = FLT_MIN;
3409
Romain Guy448455f2013-07-22 13:57:50 -07003410 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003411 Vertex* vertex = mesh;
3412
Chris Craik2af46352012-11-26 18:30:17 -08003413 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003414 float l = rects[index + 0];
3415 float t = rects[index + 1];
3416 float r = rects[index + 2];
3417 float b = rects[index + 3];
3418
Romain Guy3b753822013-03-05 10:27:35 -08003419 Vertex::set(vertex++, l, t);
3420 Vertex::set(vertex++, r, t);
3421 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003422 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003423
Romain Guy3b753822013-03-05 10:27:35 -08003424 left = fminf(left, l);
3425 top = fminf(top, t);
3426 right = fmaxf(right, r);
3427 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003428 }
3429
Romain Guy3b753822013-03-05 10:27:35 -08003430 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003431 return DrawGlInfo::kStatusDone;
3432 }
Romain Guy672433d2013-01-04 19:05:13 -08003433
Romain Guy672433d2013-01-04 19:05:13 -08003434 setupDraw();
3435 setupDrawNoTexture();
3436 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3437 setupDrawShader();
3438 setupDrawColorFilter();
3439 setupDrawBlending(mode);
3440 setupDrawProgram();
3441 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003442 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003443 setupDrawColorUniforms();
3444 setupDrawShaderUniforms();
3445 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003446
Romain Guy8ce00302013-01-15 18:51:42 -08003447 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003448 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003449 }
3450
Romain Guy448455f2013-07-22 13:57:50 -07003451 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003452
3453 return DrawGlInfo::kStatusDrew;
3454}
3455
Romain Guy026c5e162010-06-28 17:12:22 -07003456void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003457 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003458 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003459 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003460 color |= 0x00ffffff;
3461 }
3462
Romain Guy70ca14e2010-12-13 18:24:33 -08003463 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003464 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003465 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003466 setupDrawShader();
3467 setupDrawColorFilter();
3468 setupDrawBlending(mode);
3469 setupDrawProgram();
3470 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3471 setupDrawColorUniforms();
3472 setupDrawShaderUniforms(ignoreTransform);
3473 setupDrawColorFilterUniforms();
3474 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003475
Romain Guyc95c8d62010-09-17 15:31:32 -07003476 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3477}
3478
Romain Guy82ba8142010-07-09 13:25:56 -07003479void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003480 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003481 int alpha;
3482 SkXfermode::Mode mode;
3483 getAlphaAndMode(paint, &alpha, &mode);
3484
Romain Guyd21b6e12011-11-30 20:21:23 -08003485 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003486
Romain Guy3b748a42013-04-17 18:54:38 -07003487 GLvoid* vertices = (GLvoid*) NULL;
3488 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3489
3490 if (texture->uvMapper) {
3491 vertices = &mMeshVertices[0].position[0];
3492 texCoords = &mMeshVertices[0].texture[0];
3493
3494 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3495 texture->uvMapper->map(uvs);
3496
3497 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3498 }
3499
Romain Guy3b753822013-03-05 10:27:35 -08003500 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3501 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3502 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003503
Romain Guyd21b6e12011-11-30 20:21:23 -08003504 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003505 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003506 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3507 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003508 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003509 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003510 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003511 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3512 }
3513
3514 if (texture->uvMapper) {
3515 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003516 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003517}
3518
Romain Guybd6b79b2010-06-26 00:13:53 -07003519void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003520 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3521 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003522 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003523}
3524
3525void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003526 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003527 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003528 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003529
Romain Guy746b7402010-10-26 16:27:31 -07003530 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003531 setupDrawWithTexture();
3532 setupDrawColor(alpha, alpha, alpha, alpha);
3533 setupDrawColorFilter();
3534 setupDrawBlending(blend, mode, swapSrcDst);
3535 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003536 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003537 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003538 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003539 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003540 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003541 }
Romain Guy886b2752013-01-04 12:26:18 -08003542 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003543 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003544 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003545 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003546
Romain Guy6820ac82010-09-15 18:11:50 -07003547 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003548
3549 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003550}
3551
Romain Guy3b748a42013-04-17 18:54:38 -07003552void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3553 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3554 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3555 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3556
3557 setupDraw();
3558 setupDrawWithTexture();
3559 setupDrawColor(alpha, alpha, alpha, alpha);
3560 setupDrawColorFilter();
3561 setupDrawBlending(blend, mode, swapSrcDst);
3562 setupDrawProgram();
3563 if (!dirty) setupDrawDirtyRegionsDisabled();
3564 if (!ignoreScale) {
3565 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3566 } else {
3567 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3568 }
3569 setupDrawTexture(texture);
3570 setupDrawPureColorUniforms();
3571 setupDrawColorFilterUniforms();
3572 setupDrawMeshIndices(vertices, texCoords, vbo);
3573
3574 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3575
3576 finishDrawTexture();
3577}
3578
Romain Guy886b2752013-01-04 12:26:18 -08003579void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3580 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3581 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003582 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003583
3584 setupDraw();
3585 setupDrawWithTexture(true);
3586 if (hasColor) {
3587 setupDrawAlpha8Color(color, alpha);
3588 }
3589 setupDrawColorFilter();
3590 setupDrawShader();
3591 setupDrawBlending(true, mode);
3592 setupDrawProgram();
3593 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003594 if (!ignoreScale) {
3595 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3596 } else {
3597 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3598 }
Romain Guy886b2752013-01-04 12:26:18 -08003599 setupDrawTexture(texture);
3600 setupDrawPureColorUniforms();
3601 setupDrawColorFilterUniforms();
3602 setupDrawShaderUniforms();
3603 setupDrawMesh(vertices, texCoords);
3604
3605 glDrawArrays(drawMode, 0, elementsCount);
3606
3607 finishDrawTexture();
3608}
3609
Romain Guya5aed0d2010-09-09 14:42:43 -07003610void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003611 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003612 if (mCountOverdraw) {
3613 if (!mCaches.blend) glEnable(GL_BLEND);
3614 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3615 glBlendFunc(GL_ONE, GL_ONE);
3616 }
3617
3618 mCaches.blend = true;
3619 mCaches.lastSrcMode = GL_ONE;
3620 mCaches.lastDstMode = GL_ONE;
3621
3622 return;
3623 }
3624
Romain Guy82ba8142010-07-09 13:25:56 -07003625 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003626
Romain Guy82ba8142010-07-09 13:25:56 -07003627 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003628 // These blend modes are not supported by OpenGL directly and have
3629 // to be implemented using shaders. Since the shader will perform
3630 // the blending, turn blending off here
3631 // If the blend mode cannot be implemented using shaders, fall
3632 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003633 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003634 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003635 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003636 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003637
Romain Guy82bc7a72012-01-03 14:13:39 -08003638 if (mCaches.blend) {
3639 glDisable(GL_BLEND);
3640 mCaches.blend = false;
3641 }
3642
3643 return;
3644 } else {
3645 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003646 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003647 }
3648
3649 if (!mCaches.blend) {
3650 glEnable(GL_BLEND);
3651 }
3652
3653 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3654 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3655
3656 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3657 glBlendFunc(sourceMode, destMode);
3658 mCaches.lastSrcMode = sourceMode;
3659 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003660 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003661 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003662 glDisable(GL_BLEND);
3663 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003664 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003665}
3666
Romain Guy889f8d12010-07-29 14:37:42 -07003667bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003668 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003669 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003670 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003671 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003672 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003673 }
Romain Guy6926c722010-07-12 20:20:03 -07003674 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003675}
3676
Romain Guy026c5e162010-06-28 17:12:22 -07003677void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003678 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003679 TextureVertex::setUV(v++, u1, v1);
3680 TextureVertex::setUV(v++, u2, v1);
3681 TextureVertex::setUV(v++, u1, v2);
3682 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003683}
3684
Chris Craik16ecda52013-03-29 10:59:59 -07003685void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003686 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003687 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3688 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003689 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003690 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003691 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003692}
3693
Chris Craik16ecda52013-03-29 10:59:59 -07003694float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3695 float alpha;
3696 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3697 alpha = mDrawModifiers.mOverrideLayerAlpha;
3698 } else {
3699 alpha = layer->getAlpha() / 255.0f;
3700 }
3701 return alpha * mSnapshot->alpha;
3702}
3703
Romain Guy9d5316e2010-06-24 19:30:36 -07003704}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003705}; // namespace android