blob: 06315baa2960992ebff34c1c67aaa647364facc2 [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 }
Romain Guy50c0f092010-10-19 11:42:22 -0700363 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800364 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800365 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800366 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700367 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700368}
369
Romain Guy6c319ca2011-01-11 14:29:25 -0800370void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800371 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800372 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700373 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700374 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700375
Romain Guy3e263fa2011-12-12 16:47:48 -0800376 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
377
Chet Haase80250612012-08-15 13:46:54 -0700378 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700379 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800380 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700381 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700382
Romain Guya1d3c912011-12-13 14:55:06 -0800383 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700384 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700385
Romain Guy50c0f092010-10-19 11:42:22 -0700386 mCaches.blend = true;
387 glEnable(GL_BLEND);
388 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
389 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700390}
391
Romain Guy35643dd2012-09-18 15:40:58 -0700392void OpenGLRenderer::resumeAfterLayer() {
393 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
394 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
395 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700396 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700397
398 mCaches.resetScissor();
399 dirtyClip();
400}
401
Romain Guyba6be8a2012-04-23 18:22:09 -0700402void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700403 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700404}
405
406void OpenGLRenderer::attachFunctor(Functor* functor) {
407 mFunctors.add(functor);
408}
409
Romain Guy8f3b8e32012-03-27 16:33:45 -0700410status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
411 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700412 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700413
Romain Guyba6be8a2012-04-23 18:22:09 -0700414 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800415 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700416 SortedVector<Functor*> functors(mFunctors);
417 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700418
Romain Guyba6be8a2012-04-23 18:22:09 -0700419 DrawGlInfo info;
420 info.clipLeft = 0;
421 info.clipTop = 0;
422 info.clipRight = 0;
423 info.clipBottom = 0;
424 info.isLayer = false;
425 info.width = 0;
426 info.height = 0;
427 memset(info.transform, 0, sizeof(float) * 16);
428
429 for (size_t i = 0; i < count; i++) {
430 Functor* f = functors.itemAt(i);
431 result |= (*f)(DrawGlInfo::kModeProcess, &info);
432
Chris Craikc2c95432012-04-25 15:13:52 -0700433 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700434 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
435 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700436 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700437
Chris Craikc2c95432012-04-25 15:13:52 -0700438 if (result & DrawGlInfo::kStatusInvoke) {
439 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700440 }
441 }
Chris Craikd15321b2012-11-28 14:45:04 -0800442 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700443 }
444
445 return result;
446}
447
448status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700449 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
450
Chet Haasedaf98e92011-01-10 14:10:36 -0800451 interrupt();
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guy8a4ac612012-07-17 17:32:48 -0700454 mCaches.enableScissor();
Romain Guyf90f8172011-01-25 22:53:24 -0800455 if (mDirtyClip) {
456 setScissorFromClip();
Chris Craikecca6da2013-07-16 13:27:18 -0700457 setStencilFromClip();
Romain Guyf90f8172011-01-25 22:53:24 -0800458 }
Romain Guyd643bb52011-03-01 14:55:21 -0800459
Romain Guy80911b82011-03-16 15:30:12 -0700460 Rect clip(*mSnapshot->clipRect);
461 clip.snapToPixelBoundaries();
462
Romain Guyd643bb52011-03-01 14:55:21 -0800463 // Since we don't know what the functor will draw, let's dirty
464 // tne entire clip region
465 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800466 dirtyLayerUnchecked(clip, getRegion());
467 }
Romain Guyd643bb52011-03-01 14:55:21 -0800468
Romain Guy08aa2cb2011-03-17 11:06:57 -0700469 DrawGlInfo info;
470 info.clipLeft = clip.left;
471 info.clipTop = clip.top;
472 info.clipRight = clip.right;
473 info.clipBottom = clip.bottom;
474 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700475 info.width = getSnapshot()->viewport.getWidth();
476 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700477 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700478
Chris Craik4a2bff72013-04-16 13:50:16 -0700479 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800480
Romain Guy8f3b8e32012-03-27 16:33:45 -0700481 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700482 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800483 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700484
Chris Craik65924a32012-04-05 17:52:11 -0700485 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700486 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700487 }
Romain Guycabfcc12011-03-07 18:06:46 -0800488 }
489
Chet Haasedaf98e92011-01-10 14:10:36 -0800490 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700491 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800492}
493
Romain Guyf6a11b82010-06-23 17:47:49 -0700494///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700495// Debug
496///////////////////////////////////////////////////////////////////////////////
497
Romain Guy0f667532013-03-01 14:31:04 -0800498void OpenGLRenderer::eventMark(const char* name) const {
499 mCaches.eventMark(0, name);
500}
501
Romain Guy87e2f7572012-09-24 11:37:12 -0700502void OpenGLRenderer::startMark(const char* name) const {
503 mCaches.startMark(0, name);
504}
505
506void OpenGLRenderer::endMark() const {
507 mCaches.endMark();
508}
509
510void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
511 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
512 if (clear) {
513 mCaches.disableScissor();
514 mCaches.stencil.clear();
515 }
516 if (enable) {
517 mCaches.stencil.enableDebugWrite();
518 } else {
519 mCaches.stencil.disable();
520 }
521 }
522}
523
524void OpenGLRenderer::renderOverdraw() {
525 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700526 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700527
528 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700529 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700530 clip->right - clip->left, clip->bottom - clip->top);
531
532 mCaches.stencil.enableDebugTest(2);
533 drawColor(0x2f0000ff, SkXfermode::kSrcOver_Mode);
534 mCaches.stencil.enableDebugTest(3);
535 drawColor(0x2f00ff00, SkXfermode::kSrcOver_Mode);
536 mCaches.stencil.enableDebugTest(4);
537 drawColor(0x3fff0000, SkXfermode::kSrcOver_Mode);
538 mCaches.stencil.enableDebugTest(4, true);
539 drawColor(0x7fff0000, SkXfermode::kSrcOver_Mode);
540 mCaches.stencil.disable();
541 }
542}
543
Romain Guy78dd96d2013-05-03 14:24:16 -0700544void OpenGLRenderer::countOverdraw() {
545 size_t count = mWidth * mHeight;
546 uint32_t* buffer = new uint32_t[count];
547 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
548
549 size_t total = 0;
550 for (size_t i = 0; i < count; i++) {
551 total += buffer[i] & 0xff;
552 }
553
554 mOverdraw = total / float(count);
555
556 delete[] buffer;
557}
558
Romain Guy87e2f7572012-09-24 11:37:12 -0700559///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700560// Layers
561///////////////////////////////////////////////////////////////////////////////
562
563bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700564 if (layer->deferredUpdateScheduled && layer->renderer &&
565 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700566 ATRACE_CALL();
567
Romain Guy11cb6422012-09-21 00:39:43 -0700568 Rect& dirty = layer->dirtyRect;
569
Romain Guy7c450aa2012-09-21 19:15:00 -0700570 if (inFrame) {
571 endTiling();
572 debugOverdraw(false, false);
573 }
Romain Guy11cb6422012-09-21 00:39:43 -0700574
Romain Guy96885eb2013-03-26 15:05:58 -0700575 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700576 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700577 } else {
578 layer->defer();
579 }
Romain Guy11cb6422012-09-21 00:39:43 -0700580
581 if (inFrame) {
582 resumeAfterLayer();
583 startTiling(mSnapshot);
584 }
585
Romain Guy5bb3c732012-11-29 17:52:58 -0800586 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700587 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700588
589 return true;
590 }
591
592 return false;
593}
594
595void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700596 // If draw deferring is enabled this method will simply defer
597 // the display list of each individual layer. The layers remain
598 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700599 int count = mLayerUpdates.size();
600 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700601 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
602 startMark("Layer Updates");
603 } else {
604 startMark("Defer Layer Updates");
605 }
Romain Guy11cb6422012-09-21 00:39:43 -0700606
Chris Craik1206b9b2013-04-04 14:46:24 -0700607 // Note: it is very important to update the layers in order
608 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700609 Layer* layer = mLayerUpdates.itemAt(i);
610 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700611 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
612 mCaches.resourceCache.decrementRefcount(layer);
613 }
Romain Guy11cb6422012-09-21 00:39:43 -0700614 }
Romain Guy11cb6422012-09-21 00:39:43 -0700615
Romain Guy96885eb2013-03-26 15:05:58 -0700616 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
617 mLayerUpdates.clear();
618 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
619 }
620 endMark();
621 }
622}
623
624void OpenGLRenderer::flushLayers() {
625 int count = mLayerUpdates.size();
626 if (count > 0) {
627 startMark("Apply Layer Updates");
628 char layerName[12];
629
Chris Craik1206b9b2013-04-04 14:46:24 -0700630 // Note: it is very important to update the layers in order
631 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700632 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700633 startMark(layerName);
634
Romain Guy40543602013-06-12 15:31:28 -0700635 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700636 Layer* layer = mLayerUpdates.itemAt(i);
637 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700638 ATRACE_END();
639
Romain Guy02b49b72013-03-29 12:37:16 -0700640 mCaches.resourceCache.decrementRefcount(layer);
641
Romain Guy96885eb2013-03-26 15:05:58 -0700642 endMark();
643 }
644
645 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700646 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700647
Romain Guy11cb6422012-09-21 00:39:43 -0700648 endMark();
649 }
650}
651
652void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
653 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700654 // Make sure we don't introduce duplicates.
655 // SortedVector would do this automatically but we need to respect
656 // the insertion order. The linear search is not an issue since
657 // this list is usually very short (typically one item, at most a few)
658 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
659 if (mLayerUpdates.itemAt(i) == layer) {
660 return;
661 }
662 }
Romain Guy11cb6422012-09-21 00:39:43 -0700663 mLayerUpdates.push_back(layer);
664 mCaches.resourceCache.incrementRefcount(layer);
665 }
666}
667
Romain Guye93482f2013-06-17 13:14:51 -0700668void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
669 if (layer) {
670 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
671 if (mLayerUpdates.itemAt(i) == layer) {
672 mLayerUpdates.removeAt(i);
673 mCaches.resourceCache.decrementRefcount(layer);
674 break;
675 }
676 }
677 }
678}
679
Romain Guy11cb6422012-09-21 00:39:43 -0700680void OpenGLRenderer::clearLayerUpdates() {
681 size_t count = mLayerUpdates.size();
682 if (count > 0) {
683 mCaches.resourceCache.lock();
684 for (size_t i = 0; i < count; i++) {
685 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
686 }
687 mCaches.resourceCache.unlock();
688 mLayerUpdates.clear();
689 }
690}
691
Romain Guy40543602013-06-12 15:31:28 -0700692void OpenGLRenderer::flushLayerUpdates() {
693 syncState();
694 updateLayers();
695 flushLayers();
696 // Wait for all the layer updates to be executed
697 AutoFence fence;
698}
699
Romain Guy11cb6422012-09-21 00:39:43 -0700700///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700701// State management
702///////////////////////////////////////////////////////////////////////////////
703
Romain Guybb9524b2010-06-22 18:56:38 -0700704int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700705 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700706}
707
708int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700709 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700710}
711
712void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700713 if (mSaveCount > 1) {
714 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700715 }
Romain Guybb9524b2010-06-22 18:56:38 -0700716}
717
718void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700719 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700720
Romain Guy8fb95422010-08-17 18:38:51 -0700721 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700722 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700723 }
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
Romain Guy8aef54f2010-09-01 15:13:49 -0700726int OpenGLRenderer::saveSnapshot(int flags) {
727 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700728 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700729}
730
731bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700732 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700733 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700734 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700735
Romain Guybd6b79b2010-06-26 00:13:53 -0700736 sp<Snapshot> current = mSnapshot;
737 sp<Snapshot> previous = mSnapshot->previous;
738
Romain Guyeb993562010-10-05 18:14:38 -0700739 if (restoreOrtho) {
740 Rect& r = previous->viewport;
741 glViewport(r.left, r.top, r.right, r.bottom);
742 mOrthoMatrix.load(current->orthoMatrix);
743 }
744
Romain Guy8b55f372010-08-18 17:10:07 -0700745 mSaveCount--;
746 mSnapshot = previous;
747
Romain Guy2542d192010-08-18 11:47:12 -0700748 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700749 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700750 }
Romain Guy2542d192010-08-18 11:47:12 -0700751
Romain Guy5ec99242010-11-03 16:19:08 -0700752 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700753 endMark(); // Savelayer
754 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700755 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700756 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700757 }
758
Romain Guy2542d192010-08-18 11:47:12 -0700759 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700760}
761
Romain Guyf6a11b82010-06-23 17:47:49 -0700762///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700763// Layers
764///////////////////////////////////////////////////////////////////////////////
765
766int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800767 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700768 const GLuint previousFbo = mSnapshot->fbo;
769 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700770
Romain Guyaf636eb2010-12-09 17:47:21 -0800771 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700772 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700773 }
Romain Guyd55a8612010-06-28 17:42:46 -0700774
775 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700776}
777
Chris Craikd90144d2013-03-19 15:03:48 -0700778void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
779 const Rect untransformedBounds(bounds);
780
781 currentTransform().mapRect(bounds);
782
783 // Layers only make sense if they are in the framebuffer's bounds
784 if (bounds.intersect(*mSnapshot->clipRect)) {
785 // We cannot work with sub-pixels in this case
786 bounds.snapToPixelBoundaries();
787
788 // When the layer is not an FBO, we may use glCopyTexImage so we
789 // need to make sure the layer does not extend outside the bounds
790 // of the framebuffer
791 if (!bounds.intersect(mSnapshot->previous->viewport)) {
792 bounds.setEmpty();
793 } else if (fboLayer) {
794 clip.set(bounds);
795 mat4 inverse;
796 inverse.loadInverse(currentTransform());
797 inverse.mapRect(clip);
798 clip.snapToPixelBoundaries();
799 if (clip.intersect(untransformedBounds)) {
800 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
801 bounds.set(untransformedBounds);
802 } else {
803 clip.setEmpty();
804 }
805 }
806 } else {
807 bounds.setEmpty();
808 }
809}
810
Chris Craik408eb122013-03-26 18:55:15 -0700811void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
812 bool fboLayer, int alpha) {
813 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
814 bounds.getHeight() > mCaches.maxTextureSize ||
815 (fboLayer && clip.isEmpty())) {
816 mSnapshot->empty = fboLayer;
817 } else {
818 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
819 }
820}
821
Chris Craikd90144d2013-03-19 15:03:48 -0700822int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
823 int alpha, SkXfermode::Mode mode, int flags) {
824 const GLuint previousFbo = mSnapshot->fbo;
825 const int count = saveSnapshot(flags);
826
827 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
828 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
829 // operations will be able to store and restore the current clip and transform info, and
830 // quick rejection will be correct (for display lists)
831
832 Rect bounds(left, top, right, bottom);
833 Rect clip;
834 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700835 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700836
Chris Craik408eb122013-03-26 18:55:15 -0700837 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700838 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
839 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700840 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700841 }
842 }
843
844 return count;
845}
846
847
Romain Guy1c740bc2010-09-13 18:00:09 -0700848/**
849 * Layers are viewed by Skia are slightly different than layers in image editing
850 * programs (for instance.) When a layer is created, previously created layers
851 * and the frame buffer still receive every drawing command. For instance, if a
852 * layer is created and a shape intersecting the bounds of the layers and the
853 * framebuffer is draw, the shape will be drawn on both (unless the layer was
854 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
855 *
856 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
857 * texture. Unfortunately, this is inefficient as it requires every primitive to
858 * be drawn n + 1 times, where n is the number of active layers. In practice this
859 * means, for every primitive:
860 * - Switch active frame buffer
861 * - Change viewport, clip and projection matrix
862 * - Issue the drawing
863 *
864 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700865 * To avoid this, layers are implemented in a different way here, at least in the
866 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
867 * is set. When this flag is set we can redirect all drawing operations into a
868 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700869 *
870 * This implementation relies on the frame buffer being at least RGBA 8888. When
871 * a layer is created, only a texture is created, not an FBO. The content of the
872 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700873 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700874 * buffer and drawing continues as normal. This technique therefore treats the
875 * frame buffer as a scratch buffer for the layers.
876 *
877 * To compose the layers back onto the frame buffer, each layer texture
878 * (containing the original frame buffer data) is drawn as a simple quad over
879 * the frame buffer. The trick is that the quad is set as the composition
880 * destination in the blending equation, and the frame buffer becomes the source
881 * of the composition.
882 *
883 * Drawing layers with an alpha value requires an extra step before composition.
884 * An empty quad is drawn over the layer's region in the frame buffer. This quad
885 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
886 * quad is used to multiply the colors in the frame buffer. This is achieved by
887 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
888 * GL_ZERO, GL_SRC_ALPHA.
889 *
890 * Because glCopyTexImage2D() can be slow, an alternative implementation might
891 * be use to draw a single clipped layer. The implementation described above
892 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700893 *
894 * (1) The frame buffer is actually not cleared right away. To allow the GPU
895 * to potentially optimize series of calls to glCopyTexImage2D, the frame
896 * buffer is left untouched until the first drawing operation. Only when
897 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700898 */
Chet Haased48885a2012-08-28 17:43:28 -0700899bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
900 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700901 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700902 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700903
Romain Guyeb993562010-10-05 18:14:38 -0700904 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
905
Romain Guyf607bdc2010-09-10 19:20:06 -0700906 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700907 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700908 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700909 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700910 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700911
912 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700913 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700914 return false;
915 }
Romain Guyf18fd992010-07-08 11:45:51 -0700916
Romain Guya1d3c912011-12-13 14:55:06 -0800917 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700918 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700919 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700920 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700921 }
922
Romain Guy9ace8f52011-07-07 20:50:11 -0700923 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700924 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700925 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
926 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800927 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700928 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700929 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700930
Romain Guy8fb95422010-08-17 18:38:51 -0700931 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700932 mSnapshot->flags |= Snapshot::kFlagIsLayer;
933 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700934
Chris Craik7273daa2013-03-28 11:25:24 -0700935 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700936 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700937 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700938 } else {
939 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700940 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800941 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700942 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700943 // Workaround for some GL drivers. When reading pixels lying outside
944 // of the window we should get undefined values for those pixels.
945 // Unfortunately some drivers will turn the entire target texture black
946 // when reading outside of the window.
947 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
948 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800950 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700951
Romain Guyb254c242013-06-27 17:15:24 -0700952 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
953 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
954
Romain Guy54be1cd2011-06-13 19:04:27 -0700955 // Enqueue the buffer coordinates to clear the corresponding region later
956 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700957 }
Romain Guyeb993562010-10-05 18:14:38 -0700958 }
Romain Guyf86ef572010-07-01 11:05:42 -0700959
Romain Guyd55a8612010-06-28 17:42:46 -0700960 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700961}
962
Chet Haased48885a2012-08-28 17:43:28 -0700963bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800964 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700965 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700966
Chet Haased48885a2012-08-28 17:43:28 -0700967 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800968 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
969 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700970 mSnapshot->fbo = layer->getFbo();
971 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
972 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
973 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
974 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700975 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700976
Romain Guy2b7028e2012-09-19 17:25:38 -0700977 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700978 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700979 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700980 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
981 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700982
983 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700984 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700985 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700986 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700987 }
988
989 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700990 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700991
Romain Guyf735c8e2013-01-31 17:45:55 -0800992 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700993
994 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700995 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800996 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700997 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700998 glClear(GL_COLOR_BUFFER_BIT);
999
1000 dirtyClip();
1001
1002 // Change the ortho projection
1003 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1004 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1005
1006 return true;
1007}
1008
Romain Guy1c740bc2010-09-13 18:00:09 -07001009/**
1010 * Read the documentation of createLayer() before doing anything in this method.
1011 */
Romain Guy1d83e192010-08-17 11:37:00 -07001012void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1013 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001014 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001015 return;
1016 }
1017
Romain Guy8ce00302013-01-15 18:51:42 -08001018 Layer* layer = current->layer;
1019 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001020 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001021
Chris Craik39a908c2013-06-13 14:39:01 -07001022 bool clipRequired = false;
1023 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1024 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1025
Romain Guyeb993562010-10-05 18:14:38 -07001026 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001027 endTiling();
1028
Romain Guye0aa84b2012-04-03 19:30:26 -07001029 // Detach the texture from the FBO
1030 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001031
1032 layer->removeFbo(false);
1033
Romain Guyeb993562010-10-05 18:14:38 -07001034 // Unbind current FBO and restore previous one
1035 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001036 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001037
1038 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001039 }
1040
Romain Guy9ace8f52011-07-07 20:50:11 -07001041 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001042 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001043 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001044 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001045 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001046 }
1047
Romain Guy03750a02010-10-18 14:06:08 -07001048 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001049
Romain Guya1d3c912011-12-13 14:55:06 -08001050 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001051
Romain Guy5b3b3522010-10-27 18:57:51 -07001052 // When the layer is stored in an FBO, we can save a bit of fillrate by
1053 // drawing only the dirty region
1054 if (fboLayer) {
1055 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001056 if (layer->getColorFilter()) {
1057 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001058 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001059 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001060 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001061 resetColorFilter();
1062 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001063 } else if (!rect.isEmpty()) {
1064 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
1065 composeLayerRect(layer, rect, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001066 }
Romain Guy8b55f372010-08-18 17:10:07 -07001067
Romain Guy746b7402010-10-26 16:27:31 -07001068 dirtyClip();
1069
Romain Guyeb993562010-10-05 18:14:38 -07001070 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001071 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001072 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001073 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001074 }
1075}
1076
Romain Guyaa6c24c2011-04-28 18:40:04 -07001077void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001078 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001079
1080 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001081 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001082 setupDrawWithTexture();
1083 } else {
1084 setupDrawWithExternalTexture();
1085 }
1086 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001087 setupDrawColor(alpha, alpha, alpha, alpha);
1088 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001089 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001090 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001091 setupDrawPureColorUniforms();
1092 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001093 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1094 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001095 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001096 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001097 }
Romain Guy3b753822013-03-05 10:27:35 -08001098 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001099 layer->getWidth() == (uint32_t) rect.getWidth() &&
1100 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001101 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1102 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001103
Romain Guyd21b6e12011-11-30 20:21:23 -08001104 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001105 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1106 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001107 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001108 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1109 }
1110 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001111 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1112
1113 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
1114
1115 finishDrawTexture();
1116}
1117
Romain Guy5b3b3522010-10-27 18:57:51 -07001118void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001119 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001120 const Rect& texCoords = layer->texCoords;
1121 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1122 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001123
Romain Guy9ace8f52011-07-07 20:50:11 -07001124 float x = rect.left;
1125 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001126 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001127 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001128 layer->getHeight() == (uint32_t) rect.getHeight();
1129
1130 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001131 // When we're swapping, the layer is already in screen coordinates
1132 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001133 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1134 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001135 }
1136
Romain Guyd21b6e12011-11-30 20:21:23 -08001137 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001138 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001139 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001140 }
1141
Chris Craik16ecda52013-03-29 10:59:59 -07001142 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001143 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001144 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001145 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001146 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1147 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001148
Romain Guyaa6c24c2011-04-28 18:40:04 -07001149 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1150 } else {
1151 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1152 drawTextureLayer(layer, rect);
1153 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1154 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001155}
1156
Chris Craik34416ea2013-04-15 16:08:28 -07001157/**
1158 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1159 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1160 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1161 * by saveLayer's restore
1162 */
1163#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1164 DRAW_COMMAND; \
1165 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1166 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1167 DRAW_COMMAND; \
1168 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1169 } \
1170 }
1171
1172#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1173
Romain Guy5b3b3522010-10-27 18:57:51 -07001174void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001175 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001176 layer->setRegionAsRect();
1177
Chris Craik34416ea2013-04-15 16:08:28 -07001178 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001179
Romain Guy5b3b3522010-10-27 18:57:51 -07001180 layer->region.clear();
1181 return;
1182 }
1183
Romain Guy211370f2012-02-01 16:10:55 -08001184 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001185 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001186 const android::Rect* rects;
1187 Region safeRegion;
1188 if (CC_LIKELY(hasRectToRectTransform())) {
1189 rects = layer->region.getArray(&count);
1190 } else {
1191 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1192 rects = safeRegion.getArray(&count);
1193 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001194
Chris Craik16ecda52013-03-29 10:59:59 -07001195 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001196 const float texX = 1.0f / float(layer->getWidth());
1197 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001198 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001199
Romain Guy8ce00302013-01-15 18:51:42 -08001200 setupDraw();
1201
1202 // We must get (and therefore bind) the region mesh buffer
1203 // after we setup drawing in case we need to mess with the
1204 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001205 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001206 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001207
Romain Guy7230a742011-01-10 22:26:16 -08001208 setupDrawWithTexture();
1209 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001210 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001211 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001212 setupDrawProgram();
1213 setupDrawDirtyRegionsDisabled();
1214 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001215 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001216 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001217 if (currentTransform().isPureTranslate()) {
1218 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1219 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001220
Romain Guyd21b6e12011-11-30 20:21:23 -08001221 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001222 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1223 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001224 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001225 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1226 }
Romain Guy15bc6432011-12-13 13:11:32 -08001227 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001228
1229 for (size_t i = 0; i < count; i++) {
1230 const android::Rect* r = &rects[i];
1231
1232 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001233 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001234 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001235 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001236
1237 // TODO: Reject quads outside of the clip
1238 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1239 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1240 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1241 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1242
1243 numQuads++;
1244
Romain Guy31e08e92013-06-18 15:53:53 -07001245 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001246 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1247 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001248 numQuads = 0;
1249 mesh = mCaches.getRegionMesh();
1250 }
1251 }
1252
1253 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001254 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1255 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001256 }
1257
Romain Guy7230a742011-01-10 22:26:16 -08001258 finishDrawTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001259
1260#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001261 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001262#endif
1263
1264 layer->region.clear();
1265 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001266}
1267
Romain Guy3a3133d2011-02-01 22:59:58 -08001268void OpenGLRenderer::drawRegionRects(const Region& region) {
1269#if DEBUG_LAYERS_AS_REGIONS
1270 size_t count;
1271 const android::Rect* rects = region.getArray(&count);
1272
1273 uint32_t colors[] = {
1274 0x7fff0000, 0x7f00ff00,
1275 0x7f0000ff, 0x7fff00ff,
1276 };
1277
1278 int offset = 0;
1279 int32_t top = rects[0].top;
1280
1281 for (size_t i = 0; i < count; i++) {
1282 if (top != rects[i].top) {
1283 offset ^= 0x2;
1284 top = rects[i].top;
1285 }
1286
1287 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1288 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1289 SkXfermode::kSrcOver_Mode);
1290 }
1291#endif
1292}
1293
Romain Guy8ce00302013-01-15 18:51:42 -08001294void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1295 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001296 Vector<float> rects;
1297
1298 SkRegion::Iterator it(region);
1299 while (!it.done()) {
1300 const SkIRect& r = it.rect();
1301 rects.push(r.fLeft);
1302 rects.push(r.fTop);
1303 rects.push(r.fRight);
1304 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001305 it.next();
1306 }
1307
Romain Guy448455f2013-07-22 13:57:50 -07001308 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001309}
1310
Romain Guy5b3b3522010-10-27 18:57:51 -07001311void OpenGLRenderer::dirtyLayer(const float left, const float top,
1312 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001313 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001314 Rect bounds(left, top, right, bottom);
1315 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001316 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001317 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001318}
1319
1320void OpenGLRenderer::dirtyLayer(const float left, const float top,
1321 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001322 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001323 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001324 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001325 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001326}
1327
1328void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001329 if (bounds.intersect(*mSnapshot->clipRect)) {
1330 bounds.snapToPixelBoundaries();
1331 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1332 if (!dirty.isEmpty()) {
1333 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001334 }
1335 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001336}
1337
Romain Guy448455f2013-07-22 13:57:50 -07001338void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1339 GLsizei elementsCount = quadsCount * 6;
1340 while (elementsCount > 0) {
1341 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1342
1343 setupDrawIndexedVertices(&mesh[0].position[0]);
1344 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1345
1346 elementsCount -= drawCount;
1347 // Though there are 4 vertices in a quad, we use 6 indices per
1348 // quad to draw with GL_TRIANGLES
1349 mesh += (drawCount / 6) * 4;
1350 }
1351}
1352
Romain Guy54be1cd2011-06-13 19:04:27 -07001353void OpenGLRenderer::clearLayerRegions() {
1354 const size_t count = mLayers.size();
1355 if (count == 0) return;
1356
1357 if (!mSnapshot->isIgnored()) {
1358 // Doing several glScissor/glClear here can negatively impact
1359 // GPUs with a tiler architecture, instead we draw quads with
1360 // the Clear blending mode
1361
1362 // The list contains bounds that have already been clipped
1363 // against their initial clip rect, and the current clip
1364 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001365 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001366
Romain Guy448455f2013-07-22 13:57:50 -07001367 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001368 Vertex* vertex = mesh;
1369
1370 for (uint32_t i = 0; i < count; i++) {
1371 Rect* bounds = mLayers.itemAt(i);
1372
Romain Guy54be1cd2011-06-13 19:04:27 -07001373 Vertex::set(vertex++, bounds->left, bounds->top);
1374 Vertex::set(vertex++, bounds->right, bounds->top);
1375 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001376 Vertex::set(vertex++, bounds->right, bounds->bottom);
1377
1378 delete bounds;
1379 }
Romain Guye67307c2013-02-11 18:01:20 -08001380 // We must clear the list of dirty rects before we
1381 // call setupDraw() to prevent stencil setup to do
1382 // the same thing again
1383 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001384
1385 setupDraw(false);
1386 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1387 setupDrawBlending(true, SkXfermode::kClear_Mode);
1388 setupDrawProgram();
1389 setupDrawPureColorUniforms();
1390 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1391
Romain Guy448455f2013-07-22 13:57:50 -07001392 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001393
1394 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001395 } else {
1396 for (uint32_t i = 0; i < count; i++) {
1397 delete mLayers.itemAt(i);
1398 }
Romain Guye67307c2013-02-11 18:01:20 -08001399 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001400 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001401}
1402
Romain Guybd6b79b2010-06-26 00:13:53 -07001403///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001404// State Deferral
1405///////////////////////////////////////////////////////////////////////////////
1406
Chris Craikff785832013-03-08 13:12:16 -08001407bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001408 const Rect& currentClip = *(mSnapshot->clipRect);
1409 const mat4& currentMatrix = *(mSnapshot->transform);
1410
Chris Craikff785832013-03-08 13:12:16 -08001411 if (stateDeferFlags & kStateDeferFlag_Draw) {
1412 // state has bounds initialized in local coordinates
1413 if (!state.mBounds.isEmpty()) {
1414 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001415 Rect clippedBounds(state.mBounds);
1416 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001417 // quick rejected
1418 return true;
1419 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001420
Chris Craika02c4ed2013-06-14 13:43:58 -07001421 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001422 if (!currentClip.contains(state.mBounds)) {
1423 int& flags = state.mClipSideFlags;
1424 // op partially clipped, so record which sides are clipped for clip-aware merging
1425 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1426 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1427 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1428 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1429 }
1430 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001431 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001432 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1433 // overdraw avoidance (since we don't know what it overlaps)
1434 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001435 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001436 }
1437 }
1438
Chris Craik527a3aa2013-03-04 10:19:31 -08001439 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1440 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001441 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001442 }
1443
Chris Craik7273daa2013-03-28 11:25:24 -07001444 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1445 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001446 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001447 state.mDrawModifiers = mDrawModifiers;
1448 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001449 return false;
1450}
1451
Chris Craik527a3aa2013-03-04 10:19:31 -08001452void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001453 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001454 mDrawModifiers = state.mDrawModifiers;
1455 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001456
Chris Craik527a3aa2013-03-04 10:19:31 -08001457 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001458 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1459 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001460 dirtyClip();
1461 }
Chris Craikc3566d02013-02-04 16:16:33 -08001462}
1463
Chris Craik28ce94a2013-05-31 11:38:03 -07001464/**
1465 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1466 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1467 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1468 *
1469 * This method should be called when restoreDisplayState() won't be restoring the clip
1470 */
1471void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1472 if (clipRect != NULL) {
1473 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1474 } else {
1475 mSnapshot->setClip(0, 0, mWidth, mHeight);
1476 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001477 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001478 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001479}
1480
Chris Craikc3566d02013-02-04 16:16:33 -08001481///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001482// Transforms
1483///////////////////////////////////////////////////////////////////////////////
1484
1485void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001486 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001487}
1488
1489void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001490 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001491}
1492
1493void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001494 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001495}
1496
Romain Guy807daf72011-01-18 11:19:19 -08001497void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001498 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001499}
1500
Romain Guyf6a11b82010-06-23 17:47:49 -07001501void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001502 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001503 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001504 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001505 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001506 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001507}
1508
Chris Craikb98a0162013-02-21 11:30:22 -08001509bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001510 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001511}
1512
Romain Guyf6a11b82010-06-23 17:47:49 -07001513void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001514 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001515}
1516
1517void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001518 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001519 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001520 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001521 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001522}
1523
1524///////////////////////////////////////////////////////////////////////////////
1525// Clipping
1526///////////////////////////////////////////////////////////////////////////////
1527
Romain Guybb9524b2010-06-22 18:56:38 -07001528void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001529 Rect clip(*mSnapshot->clipRect);
1530 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001531
Romain Guy8a4ac612012-07-17 17:32:48 -07001532 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1533 clip.getWidth(), clip.getHeight())) {
1534 mDirtyClip = false;
1535 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001536}
1537
Romain Guy8ce00302013-01-15 18:51:42 -08001538void OpenGLRenderer::ensureStencilBuffer() {
1539 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1540 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1541 // just hope we have one when hasLayer() returns false.
1542 if (hasLayer()) {
1543 attachStencilBufferToLayer(mSnapshot->layer);
1544 }
1545}
1546
1547void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1548 // The layer's FBO is already bound when we reach this stage
1549 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001550 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1551 // is attached after we initiated tiling. We must turn it off,
1552 // attach the new render buffer then turn tiling back on
1553 endTiling();
1554
Romain Guy8d4aeb72013-02-12 16:08:55 -08001555 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001556 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001557 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001558
Romain Guyf735c8e2013-01-31 17:45:55 -08001559 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001560 }
1561}
1562
1563void OpenGLRenderer::setStencilFromClip() {
1564 if (!mCaches.debugOverdraw) {
1565 if (!mSnapshot->clipRegion->isEmpty()) {
1566 // NOTE: The order here is important, we must set dirtyClip to false
1567 // before any draw call to avoid calling back into this method
1568 mDirtyClip = false;
1569
1570 ensureStencilBuffer();
1571
1572 mCaches.stencil.enableWrite();
1573
1574 // Clear the stencil but first make sure we restrict drawing
1575 // to the region's bounds
1576 bool resetScissor = mCaches.enableScissor();
1577 if (resetScissor) {
1578 // The scissor was not set so we now need to update it
1579 setScissorFromClip();
1580 }
1581 mCaches.stencil.clear();
1582 if (resetScissor) mCaches.disableScissor();
1583
1584 // NOTE: We could use the region contour path to generate a smaller mesh
1585 // Since we are using the stencil we could use the red book path
1586 // drawing technique. It might increase bandwidth usage though.
1587
1588 // The last parameter is important: we are not drawing in the color buffer
1589 // so we don't want to dirty the current layer, if any
1590 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1591
1592 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001593
1594 // Draw the region used to generate the stencil if the appropriate debug
1595 // mode is enabled
1596 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1597 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1598 }
Romain Guy8ce00302013-01-15 18:51:42 -08001599 } else {
1600 mCaches.stencil.disable();
1601 }
1602 }
1603}
1604
Romain Guy9d5316e2010-06-24 19:30:36 -07001605const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001606 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001607}
1608
Chris Craik39a908c2013-06-13 14:39:01 -07001609bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
1610 bool* clipRequired) {
1611 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001612 return true;
1613 }
1614
1615 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001616 currentTransform().mapRect(r);
Romain Guy8a4ac612012-07-17 17:32:48 -07001617 r.snapToPixelBoundaries();
1618
1619 Rect clipRect(*mSnapshot->clipRect);
1620 clipRect.snapToPixelBoundaries();
1621
Chris Craik39a908c2013-06-13 14:39:01 -07001622 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001623
Chris Craik39a908c2013-06-13 14:39:01 -07001624 if (clipRequired) *clipRequired = !clipRect.contains(r);
1625 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001626}
1627
Romain Guy672433d2013-01-04 19:05:13 -08001628bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1629 SkPaint* paint) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001630 if (paint->getStyle() != SkPaint::kFill_Style) {
1631 float outset = paint->getStrokeWidth() * 0.5f;
1632 return quickReject(left - outset, top - outset, right + outset, bottom + outset);
1633 } else {
1634 return quickReject(left, top, right, bottom);
1635 }
1636}
1637
Romain Guyc7d53492010-06-25 13:41:57 -07001638bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom) {
Chris Craik39a908c2013-06-13 14:39:01 -07001639 bool clipRequired = false;
1640 if (quickRejectNoScissor(left, top, right, bottom, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001641 return true;
1642 }
1643
Chris Craik39a908c2013-06-13 14:39:01 -07001644 if (!isDeferred()) {
1645 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001646 }
Chris Craik39a908c2013-06-13 14:39:01 -07001647 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001648}
1649
Romain Guy8ce00302013-01-15 18:51:42 -08001650void OpenGLRenderer::debugClip() {
1651#if DEBUG_CLIP_REGIONS
1652 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1653 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1654 }
1655#endif
1656}
1657
Romain Guy079ba2c2010-07-16 14:12:24 -07001658bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001659 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001660 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1661 if (clipped) {
1662 dirtyClip();
1663 }
1664 return !mSnapshot->clipRect->isEmpty();
1665 }
1666
1667 SkPath path;
1668 path.addRect(left, top, right, bottom);
1669
1670 return clipPath(&path, op);
1671}
1672
1673bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1674 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001675 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001676
1677 SkPath transformed;
1678 path->transform(transform, &transformed);
1679
1680 SkRegion clip;
1681 if (!mSnapshot->clipRegion->isEmpty()) {
1682 clip.setRegion(*mSnapshot->clipRegion);
1683 } else {
1684 Rect* bounds = mSnapshot->clipRect;
1685 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1686 }
1687
1688 SkRegion region;
1689 region.setPath(transformed, clip);
1690
1691 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001692 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001693 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001694 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001695 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001696}
1697
Romain Guy735738c2012-12-03 12:34:51 -08001698bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001699 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1700 if (clipped) {
1701 dirtyClip();
1702 }
1703 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001704}
1705
Chet Haasea23eed82012-04-12 15:19:04 -07001706Rect* OpenGLRenderer::getClipRect() {
1707 return mSnapshot->clipRect;
1708}
1709
Romain Guyf6a11b82010-06-23 17:47:49 -07001710///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001711// Drawing commands
1712///////////////////////////////////////////////////////////////////////////////
1713
Romain Guy54be1cd2011-06-13 19:04:27 -07001714void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001715 // TODO: It would be best if we could do this before quickReject()
1716 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001717 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001718 // Make sure setScissor & setStencil happen at the beginning of
1719 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001720 if (mDirtyClip) {
1721 if (mCaches.scissorEnabled) {
1722 setScissorFromClip();
1723 }
Romain Guy8ce00302013-01-15 18:51:42 -08001724 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001725 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001726
Romain Guy70ca14e2010-12-13 18:24:33 -08001727 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001728
Romain Guy70ca14e2010-12-13 18:24:33 -08001729 mSetShaderColor = false;
1730 mColorSet = false;
1731 mColorA = mColorR = mColorG = mColorB = 0.0f;
1732 mTextureUnit = 0;
1733 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001734
1735 // Enable debug highlight when what we're about to draw is tested against
1736 // the stencil buffer and if stencil highlight debugging is on
1737 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1738 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1739 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001740
1741 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001742}
1743
1744void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1745 mDescription.hasTexture = true;
1746 mDescription.hasAlpha8Texture = isAlpha8;
1747}
1748
Romain Guyff316ec2013-02-13 18:39:43 -08001749void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1750 mDescription.hasTexture = true;
1751 mDescription.hasColors = true;
1752 mDescription.hasAlpha8Texture = isAlpha8;
1753}
1754
Romain Guyaa6c24c2011-04-28 18:40:04 -07001755void OpenGLRenderer::setupDrawWithExternalTexture() {
1756 mDescription.hasExternalTexture = true;
1757}
1758
Romain Guy15bc6432011-12-13 13:11:32 -08001759void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001760 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001761}
1762
Chris Craik710f46d2012-09-17 17:25:49 -07001763void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001764 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001765}
1766
Romain Guy8d0d4782010-12-14 20:13:35 -08001767void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1768 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001769 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1770 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1771 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001772 mColorSet = true;
1773 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1774}
1775
Romain Guy86568192010-12-14 15:55:39 -08001776void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1777 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001778 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1779 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1780 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001781 mColorSet = true;
1782 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1783}
1784
Romain Guy41210632012-07-16 17:04:24 -07001785void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1786 mCaches.fontRenderer->describe(mDescription, paint);
1787}
1788
Romain Guy70ca14e2010-12-13 18:24:33 -08001789void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1790 mColorA = a;
1791 mColorR = r;
1792 mColorG = g;
1793 mColorB = b;
1794 mColorSet = true;
1795 mSetShaderColor = mDescription.setColor(r, g, b, a);
1796}
1797
1798void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001799 if (mDrawModifiers.mShader) {
1800 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001801 }
1802}
1803
1804void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001805 if (mDrawModifiers.mColorFilter) {
1806 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001807 }
1808}
1809
Romain Guyf09ef512011-05-27 11:43:46 -07001810void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1811 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1812 mColorA = 1.0f;
1813 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001814 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001815 }
1816}
1817
Romain Guy70ca14e2010-12-13 18:24:33 -08001818void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001819 // When the blending mode is kClear_Mode, we need to use a modulate color
1820 // argb=1,0,0,0
1821 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001822 bool blend = (mColorSet && mColorA < 1.0f) ||
1823 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1824 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001825}
1826
1827void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001828 // When the blending mode is kClear_Mode, we need to use a modulate color
1829 // argb=1,0,0,0
1830 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001831 blend |= (mColorSet && mColorA < 1.0f) ||
1832 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1833 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1834 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001835}
1836
1837void OpenGLRenderer::setupDrawProgram() {
1838 useProgram(mCaches.programCache.get(mDescription));
1839}
1840
1841void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1842 mTrackDirtyRegions = false;
1843}
1844
1845void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1846 bool ignoreTransform) {
1847 mModelView.loadTranslate(left, top, 0.0f);
1848 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001849 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1850 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001852 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001853 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1854 }
1855}
1856
Chet Haase8a5cc922011-04-26 07:28:09 -07001857void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001858 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001859}
1860
Romain Guy70ca14e2010-12-13 18:24:33 -08001861void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1862 bool ignoreTransform, bool ignoreModelView) {
1863 if (!ignoreModelView) {
1864 mModelView.loadTranslate(left, top, 0.0f);
1865 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001866 } else {
1867 mModelView.loadIdentity();
1868 }
Romain Guy86568192010-12-14 15:55:39 -08001869 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1870 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001871 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001872 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001873 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001874 }
1875 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001876 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001877 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1878 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001879}
1880
1881void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001882 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001883 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1884 }
1885}
1886
Romain Guy86568192010-12-14 15:55:39 -08001887void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001888 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001889 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001890 }
1891}
1892
Romain Guy70ca14e2010-12-13 18:24:33 -08001893void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001894 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001895 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001896 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001897 }
Chris Craikc3566d02013-02-04 16:16:33 -08001898 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1899 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001900 }
1901}
1902
Romain Guy8d0d4782010-12-14 20:13:35 -08001903void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001904 if (mDrawModifiers.mShader) {
1905 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001906 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001907 }
1908}
1909
Romain Guy70ca14e2010-12-13 18:24:33 -08001910void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001911 if (mDrawModifiers.mColorFilter) {
1912 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001913 }
1914}
1915
Romain Guy41210632012-07-16 17:04:24 -07001916void OpenGLRenderer::setupDrawTextGammaUniforms() {
1917 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1918}
1919
Romain Guy70ca14e2010-12-13 18:24:33 -08001920void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001921 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001922 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001923 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001924}
1925
1926void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001927 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001928 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001929 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001930}
1931
Romain Guyaa6c24c2011-04-28 18:40:04 -07001932void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1933 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001934 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001935 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001936}
1937
Romain Guy8f0095c2011-05-02 17:24:22 -07001938void OpenGLRenderer::setupDrawTextureTransform() {
1939 mDescription.hasTextureTransform = true;
1940}
1941
1942void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001943 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1944 GL_FALSE, &transform.data[0]);
1945}
1946
Romain Guy70ca14e2010-12-13 18:24:33 -08001947void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001948 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001949 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001950 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001951 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001952 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001953 }
Romain Guyd71dd362011-12-12 19:03:35 -08001954
Chris Craikcb4d6002012-09-25 12:00:29 -07001955 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001956 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001957 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001958 }
1959
1960 mCaches.unbindIndicesBuffer();
1961}
1962
Romain Guyff316ec2013-02-13 18:39:43 -08001963void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1964 bool force = mCaches.unbindMeshBuffer();
1965 GLsizei stride = sizeof(ColorTextureVertex);
1966
1967 mCaches.bindPositionVertexPointer(force, vertices, stride);
1968 if (mCaches.currentProgram->texCoords >= 0) {
1969 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1970 }
1971 int slot = mCaches.currentProgram->getAttrib("colors");
1972 if (slot >= 0) {
1973 glEnableVertexAttribArray(slot);
1974 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1975 }
1976
1977 mCaches.unbindIndicesBuffer();
1978}
1979
Romain Guy3b748a42013-04-17 18:54:38 -07001980void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1981 bool force = false;
1982 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1983 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1984 // use the default VBO found in Caches
1985 if (!vertices || vbo) {
1986 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1987 } else {
1988 force = mCaches.unbindMeshBuffer();
1989 }
1990 mCaches.bindIndicesBuffer();
1991
Chris Craikcb4d6002012-09-25 12:00:29 -07001992 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001993 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001994 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001995 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001996}
1997
Romain Guy448455f2013-07-22 13:57:50 -07001998void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001999 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002000 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002001 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002002}
2003
Romain Guy70ca14e2010-12-13 18:24:33 -08002004void OpenGLRenderer::finishDrawTexture() {
Romain Guy70ca14e2010-12-13 18:24:33 -08002005}
2006
2007///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002008// Drawing
2009///////////////////////////////////////////////////////////////////////////////
2010
Chris Craikff785832013-03-08 13:12:16 -08002011status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2012 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002013 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002014 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2015 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002016 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002017 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002018 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002019 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2020 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002021 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002022 }
2023
Chris Craik28ce94a2013-05-31 11:38:03 -07002024 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2025 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002026 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2027 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002028
2029 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002030 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002031
Chet Haase58d110a2013-04-10 07:43:29 -07002032 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002033 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002034
Romain Guy65549432012-03-26 16:45:05 -07002035 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002036}
2037
Chris Craikc3566d02013-02-04 16:16:33 -08002038void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002039 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002040 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002041 }
2042}
2043
Romain Guya168d732011-03-18 16:50:13 -07002044void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2045 int alpha;
2046 SkXfermode::Mode mode;
2047 getAlphaAndMode(paint, &alpha, &mode);
2048
Romain Guy886b2752013-01-04 12:26:18 -08002049 int color = paint != NULL ? paint->getColor() : 0;
2050
Romain Guya168d732011-03-18 16:50:13 -07002051 float x = left;
2052 float y = top;
2053
Romain Guy886b2752013-01-04 12:26:18 -08002054 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2055
Romain Guya168d732011-03-18 16:50:13 -07002056 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002057 if (currentTransform().isPureTranslate()) {
2058 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2059 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002060 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002061
2062 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002063 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002064 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002065 }
2066
Romain Guy3b748a42013-04-17 18:54:38 -07002067 // No need to check for a UV mapper on the texture object, only ARGB_8888
2068 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002069 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002070 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2071 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002072}
2073
Romain Guy03c00b52013-06-20 18:30:28 -07002074/**
2075 * Important note: this method is intended to draw batches of bitmaps and
2076 * will not set the scissor enable or dirty the current layer, if any.
2077 * The caller is responsible for properly dirtying the current layer.
2078 */
Romain Guy55b6f952013-06-27 15:27:09 -07002079status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
2080 TextureVertex* vertices, bool transformed, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002081 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002082 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002083 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002084
Chris Craik527a3aa2013-03-04 10:19:31 -08002085 const AutoTexture autoCleanup(texture);
2086
2087 int alpha;
2088 SkXfermode::Mode mode;
2089 getAlphaAndMode(paint, &alpha, &mode);
2090
2091 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy2db5e992013-05-21 15:29:59 -07002092 texture->setFilter(transformed ? FILTER(paint) : GL_NEAREST, true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002093
2094 const float x = (int) floorf(bounds.left + 0.5f);
2095 const float y = (int) floorf(bounds.top + 0.5f);
2096 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2097 int color = paint != NULL ? paint->getColor() : 0;
2098 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2099 texture->id, paint != NULL, color, alpha, mode,
2100 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002101 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002102 } else {
2103 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2104 texture->id, alpha / 255.0f, mode, texture->blend,
2105 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002106 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002107 }
2108
2109 return DrawGlInfo::kStatusDrew;
2110}
2111
Chet Haase48659092012-05-31 15:21:51 -07002112status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002113 const float right = left + bitmap->width();
2114 const float bottom = top + bitmap->height();
2115
2116 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002117 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002118 }
2119
Romain Guya1d3c912011-12-13 14:55:06 -08002120 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002121 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002122 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002123 const AutoTexture autoCleanup(texture);
2124
Romain Guy211370f2012-02-01 16:10:55 -08002125 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002126 drawAlphaBitmap(texture, left, top, paint);
2127 } else {
2128 drawTextureRect(left, top, right, bottom, texture, paint);
2129 }
Chet Haase48659092012-05-31 15:21:51 -07002130
2131 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002132}
2133
Chet Haase48659092012-05-31 15:21:51 -07002134status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002135 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2136 const mat4 transform(*matrix);
2137 transform.mapRect(r);
2138
Romain Guy6926c722010-07-12 20:20:03 -07002139 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002140 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002141 }
2142
Romain Guya1d3c912011-12-13 14:55:06 -08002143 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002144 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002145 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002146 const AutoTexture autoCleanup(texture);
2147
Romain Guy5b3b3522010-10-27 18:57:51 -07002148 // This could be done in a cheaper way, all we need is pass the matrix
2149 // to the vertex shader. The save/restore is a bit overkill.
2150 save(SkCanvas::kMatrix_SaveFlag);
2151 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002152 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2153 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2154 } else {
2155 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2156 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002157 restore();
Chet Haase48659092012-05-31 15:21:51 -07002158
2159 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002160}
2161
Chet Haase48659092012-05-31 15:21:51 -07002162status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002163 const float right = left + bitmap->width();
2164 const float bottom = top + bitmap->height();
2165
2166 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002167 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002168 }
2169
2170 mCaches.activeTexture(0);
2171 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2172 const AutoTexture autoCleanup(texture);
2173
Romain Guy886b2752013-01-04 12:26:18 -08002174 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2175 drawAlphaBitmap(texture, left, top, paint);
2176 } else {
2177 drawTextureRect(left, top, right, bottom, texture, paint);
2178 }
Chet Haase48659092012-05-31 15:21:51 -07002179
2180 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002181}
2182
Chet Haase48659092012-05-31 15:21:51 -07002183status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002184 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002185 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002186 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002187 }
2188
Chris Craik39a908c2013-06-13 14:39:01 -07002189 // TODO: use quickReject on bounds from vertices
2190 mCaches.enableScissor();
2191
Romain Guyb18d2d02011-02-10 15:52:54 -08002192 float left = FLT_MAX;
2193 float top = FLT_MAX;
2194 float right = FLT_MIN;
2195 float bottom = FLT_MIN;
2196
Romain Guya92bb4d2012-10-16 11:08:44 -07002197 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002198
Romain Guyff316ec2013-02-13 18:39:43 -08002199 ColorTextureVertex mesh[count];
2200 ColorTextureVertex* vertex = mesh;
2201
2202 bool cleanupColors = false;
2203 if (!colors) {
2204 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2205 colors = new int[colorsCount];
2206 memset(colors, 0xff, colorsCount * sizeof(int));
2207 cleanupColors = true;
2208 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002209
Romain Guy3b748a42013-04-17 18:54:38 -07002210 mCaches.activeTexture(0);
2211 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2212 const UvMapper& mapper(getMapper(texture));
2213
Romain Guy5a7b4662011-01-20 19:09:30 -08002214 for (int32_t y = 0; y < meshHeight; y++) {
2215 for (int32_t x = 0; x < meshWidth; x++) {
2216 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2217
2218 float u1 = float(x) / meshWidth;
2219 float u2 = float(x + 1) / meshWidth;
2220 float v1 = float(y) / meshHeight;
2221 float v2 = float(y + 1) / meshHeight;
2222
Romain Guy3b748a42013-04-17 18:54:38 -07002223 mapper.map(u1, v1, u2, v2);
2224
Romain Guy5a7b4662011-01-20 19:09:30 -08002225 int ax = i + (meshWidth + 1) * 2;
2226 int ay = ax + 1;
2227 int bx = i;
2228 int by = bx + 1;
2229 int cx = i + 2;
2230 int cy = cx + 1;
2231 int dx = i + (meshWidth + 1) * 2 + 2;
2232 int dy = dx + 1;
2233
Romain Guyff316ec2013-02-13 18:39:43 -08002234 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2235 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2236 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002237
Romain Guyff316ec2013-02-13 18:39:43 -08002238 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2239 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2240 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002241
Romain Guya92bb4d2012-10-16 11:08:44 -07002242 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2243 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2244 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2245 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002246 }
2247 }
2248
Romain Guya92bb4d2012-10-16 11:08:44 -07002249 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002250 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002251 return DrawGlInfo::kStatusDone;
2252 }
2253
Romain Guyff316ec2013-02-13 18:39:43 -08002254 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002255 texture = mCaches.textureCache.get(bitmap);
2256 if (!texture) {
2257 if (cleanupColors) delete[] colors;
2258 return DrawGlInfo::kStatusDone;
2259 }
Romain Guyff316ec2013-02-13 18:39:43 -08002260 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002261 const AutoTexture autoCleanup(texture);
2262
2263 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2264 texture->setFilter(FILTER(paint), true);
2265
2266 int alpha;
2267 SkXfermode::Mode mode;
2268 getAlphaAndMode(paint, &alpha, &mode);
2269
Romain Guyff316ec2013-02-13 18:39:43 -08002270 float a = alpha / 255.0f;
2271
Romain Guya92bb4d2012-10-16 11:08:44 -07002272 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002273 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002274 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002275
Romain Guyff316ec2013-02-13 18:39:43 -08002276 setupDraw();
2277 setupDrawWithTextureAndColor();
2278 setupDrawColor(a, a, a, a);
2279 setupDrawColorFilter();
2280 setupDrawBlending(true, mode, false);
2281 setupDrawProgram();
2282 setupDrawDirtyRegionsDisabled();
2283 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2284 setupDrawTexture(texture->id);
2285 setupDrawPureColorUniforms();
2286 setupDrawColorFilterUniforms();
2287 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2288
2289 glDrawArrays(GL_TRIANGLES, 0, count);
2290
2291 finishDrawTexture();
2292
2293 int slot = mCaches.currentProgram->getAttrib("colors");
2294 if (slot >= 0) {
2295 glDisableVertexAttribArray(slot);
2296 }
2297
2298 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002299
2300 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002301}
2302
Chet Haase48659092012-05-31 15:21:51 -07002303status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002304 float srcLeft, float srcTop, float srcRight, float srcBottom,
2305 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002306 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002307 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002308 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002309 }
2310
Romain Guya1d3c912011-12-13 14:55:06 -08002311 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002312 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002313 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002314 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002315
Romain Guy8ba548f2010-06-30 19:21:21 -07002316 const float width = texture->width;
2317 const float height = texture->height;
2318
Romain Guy3b748a42013-04-17 18:54:38 -07002319 float u1 = fmax(0.0f, srcLeft / width);
2320 float v1 = fmax(0.0f, srcTop / height);
2321 float u2 = fmin(1.0f, srcRight / width);
2322 float v2 = fmin(1.0f, srcBottom / height);
2323
2324 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002325
Romain Guy03750a02010-10-18 14:06:08 -07002326 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002327 resetDrawTextureTexCoords(u1, v1, u2, v2);
2328
Romain Guy03750a02010-10-18 14:06:08 -07002329 int alpha;
2330 SkXfermode::Mode mode;
2331 getAlphaAndMode(paint, &alpha, &mode);
2332
Romain Guyd21b6e12011-11-30 20:21:23 -08002333 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2334
Romain Guy886b2752013-01-04 12:26:18 -08002335 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2336 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002337
Romain Guy886b2752013-01-04 12:26:18 -08002338 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2339 // Apply a scale transform on the canvas only when a shader is in use
2340 // Skia handles the ratio between the dst and src rects as a scale factor
2341 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002342 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002343 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002344
Romain Guy3b753822013-03-05 10:27:35 -08002345 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2346 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2347 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002348
2349 dstRight = x + (dstRight - dstLeft);
2350 dstBottom = y + (dstBottom - dstTop);
2351
2352 dstLeft = x;
2353 dstTop = y;
2354
2355 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2356 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002357 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002358 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002359 }
2360
2361 if (CC_UNLIKELY(useScaleTransform)) {
2362 save(SkCanvas::kMatrix_SaveFlag);
2363 translate(dstLeft, dstTop);
2364 scale(scaleX, scaleY);
2365
2366 dstLeft = 0.0f;
2367 dstTop = 0.0f;
2368
2369 dstRight = srcRight - srcLeft;
2370 dstBottom = srcBottom - srcTop;
2371 }
2372
2373 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2374 int color = paint ? paint->getColor() : 0;
2375 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2376 texture->id, paint != NULL, color, alpha, mode,
2377 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2378 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2379 } else {
2380 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2381 texture->id, alpha / 255.0f, mode, texture->blend,
2382 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2383 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2384 }
2385
2386 if (CC_UNLIKELY(useScaleTransform)) {
2387 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002388 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002389
2390 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002391
2392 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002393}
2394
Romain Guy3b748a42013-04-17 18:54:38 -07002395status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002396 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002397 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002398 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002399 }
2400
Romain Guy4c2547f2013-06-11 16:19:24 -07002401 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002402 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2403 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002404
Romain Guy03c00b52013-06-20 18:30:28 -07002405 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002406}
2407
Romain Guy03c00b52013-06-20 18:30:28 -07002408status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2409 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002410 if (quickReject(left, top, right, bottom)) {
2411 return DrawGlInfo::kStatusDone;
2412 }
2413
Romain Guy211370f2012-02-01 16:10:55 -08002414 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002415 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002416 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002417 if (!texture) return DrawGlInfo::kStatusDone;
2418 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002419
Romain Guya4adcf02013-02-28 12:15:35 -08002420 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2421 texture->setFilter(GL_LINEAR, true);
2422
Romain Guy03c00b52013-06-20 18:30:28 -07002423 int alpha;
2424 SkXfermode::Mode mode;
2425 getAlphaAndMode(paint, &alpha, &mode);
2426
Romain Guy3b753822013-03-05 10:27:35 -08002427 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002428 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002429 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002430 const float offsetX = left + currentTransform().getTranslateX();
2431 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002432 const size_t count = mesh->quads.size();
2433 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002434 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002435 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002436 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2437 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2438 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002439 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002440 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002441 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002442 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002443 }
2444 }
2445
Romain Guy211370f2012-02-01 16:10:55 -08002446 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002447 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2448 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002449
Romain Guy3b748a42013-04-17 18:54:38 -07002450 right = x + right - left;
2451 bottom = y + bottom - top;
2452 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2453 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2454 GL_TRIANGLES, mesh->indexCount, false, true,
2455 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002456 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002457 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2458 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2459 GL_TRIANGLES, mesh->indexCount, false, false,
2460 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002461 }
Romain Guy054dc182010-10-15 17:55:25 -07002462 }
Chet Haase48659092012-05-31 15:21:51 -07002463
2464 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002465}
2466
Romain Guy03c00b52013-06-20 18:30:28 -07002467/**
2468 * Important note: this method is intended to draw batches of 9-patch objects and
2469 * will not set the scissor enable or dirty the current layer, if any.
2470 * The caller is responsible for properly dirtying the current layer.
2471 */
2472status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2473 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2474 mCaches.activeTexture(0);
2475 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2476 if (!texture) return DrawGlInfo::kStatusDone;
2477 const AutoTexture autoCleanup(texture);
2478
2479 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2480 texture->setFilter(GL_LINEAR, true);
2481
2482 int alpha;
2483 SkXfermode::Mode mode;
2484 getAlphaAndMode(paint, &alpha, &mode);
2485
2486 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2487 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2488 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2489
2490 return DrawGlInfo::kStatusDrew;
2491}
2492
Chris Craik65cd6122012-12-10 17:56:27 -08002493status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2494 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002495 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002496 // no vertices to draw
2497 return DrawGlInfo::kStatusDone;
2498 }
2499
Chris Craikcb4d6002012-09-25 12:00:29 -07002500 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002501 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2502 bool isAA = paint->isAntiAlias();
2503
Chris Craik710f46d2012-09-17 17:25:49 -07002504 setupDraw();
2505 setupDrawNoTexture();
2506 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002507 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2508 setupDrawColorFilter();
2509 setupDrawShader();
2510 setupDrawBlending(isAA, mode);
2511 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002512 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002513 setupDrawColorUniforms();
2514 setupDrawColorFilterUniforms();
2515 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002516
Chris Craik710f46d2012-09-17 17:25:49 -07002517 void* vertices = vertexBuffer.getBuffer();
2518 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002519 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002520 mCaches.resetTexCoordsVertexPointer();
2521 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002522
Chris Craik710f46d2012-09-17 17:25:49 -07002523 int alphaSlot = -1;
2524 if (isAA) {
2525 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2526 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002527
Chris Craik710f46d2012-09-17 17:25:49 -07002528 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002529 glEnableVertexAttribArray(alphaSlot);
2530 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002531 }
Romain Guy04299382012-07-18 17:15:41 -07002532
Chris Craik6d29c8d2013-05-08 18:35:44 -07002533 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002534
Chris Craik710f46d2012-09-17 17:25:49 -07002535 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002536 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002537 }
Chris Craik65cd6122012-12-10 17:56:27 -08002538
2539 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002540}
2541
2542/**
Chris Craik65cd6122012-12-10 17:56:27 -08002543 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2544 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2545 * screen space in all directions. However, instead of using a fragment shader to compute the
2546 * translucency of the color from its position, we simply use a varying parameter to define how far
2547 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2548 *
2549 * Doesn't yet support joins, caps, or path effects.
2550 */
2551status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2552 VertexBuffer vertexBuffer;
2553 // TODO: try clipping large paths to viewport
2554 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2555
Romain Guyc46d07a2013-03-15 19:06:39 -07002556 if (hasLayer()) {
2557 SkRect bounds = path.getBounds();
2558 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2559 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2560 }
Chris Craik65cd6122012-12-10 17:56:27 -08002561
2562 return drawVertexBuffer(vertexBuffer, paint);
2563}
2564
2565/**
2566 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2567 * and additional geometry for defining an alpha slope perimeter.
2568 *
2569 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2570 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2571 * in-shader alpha region, but found it to be taxing on some GPUs.
2572 *
2573 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2574 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002575 */
Chet Haase48659092012-05-31 15:21:51 -07002576status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002577 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002578
Chris Craik65cd6122012-12-10 17:56:27 -08002579 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002580
Chris Craik65cd6122012-12-10 17:56:27 -08002581 VertexBuffer buffer;
2582 SkRect bounds;
2583 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002584
2585 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2586 return DrawGlInfo::kStatusDone;
2587 }
2588
Romain Guy3b753822013-03-05 10:27:35 -08002589 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002590
Chris Craik65cd6122012-12-10 17:56:27 -08002591 bool useOffset = !paint->isAntiAlias();
2592 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002593}
2594
Chet Haase48659092012-05-31 15:21:51 -07002595status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002596 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002597
Chris Craik6d29c8d2013-05-08 18:35:44 -07002598 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002599
Chris Craik6d29c8d2013-05-08 18:35:44 -07002600 VertexBuffer buffer;
2601 SkRect bounds;
2602 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002603
Chris Craik6d29c8d2013-05-08 18:35:44 -07002604 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2605 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002606 }
2607
Chris Craik6d29c8d2013-05-08 18:35:44 -07002608 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002609
Chris Craik6d29c8d2013-05-08 18:35:44 -07002610 bool useOffset = !paint->isAntiAlias();
2611 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002612}
2613
Chet Haase48659092012-05-31 15:21:51 -07002614status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002615 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002616 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002617
Romain Guyae88e5e2010-10-22 17:49:18 -07002618 Rect& clip(*mSnapshot->clipRect);
2619 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002620
Romain Guy3d58c032010-07-14 16:34:53 -07002621 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002622
2623 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002624}
Romain Guy9d5316e2010-06-24 19:30:36 -07002625
Chet Haase48659092012-05-31 15:21:51 -07002626status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2627 SkPaint* paint) {
2628 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002629 const AutoTexture autoCleanup(texture);
2630
2631 const float x = left + texture->left - texture->offset;
2632 const float y = top + texture->top - texture->offset;
2633
2634 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002635
2636 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002637}
2638
Chet Haase48659092012-05-31 15:21:51 -07002639status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002640 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002641 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2642 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002643 return DrawGlInfo::kStatusDone;
2644 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002645
Chris Craikcb4d6002012-09-25 12:00:29 -07002646 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002647 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002648 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002649 right - left, bottom - top, rx, ry, p);
2650 return drawShape(left, top, texture, p);
2651 }
2652
2653 SkPath path;
2654 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002655 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2656 float outset = p->getStrokeWidth() / 2;
2657 rect.outset(outset, outset);
2658 rx += outset;
2659 ry += outset;
2660 }
Chris Craik710f46d2012-09-17 17:25:49 -07002661 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002662 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002663}
2664
Chris Craik710f46d2012-09-17 17:25:49 -07002665status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002666 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002667 x + radius, y + radius, p) ||
2668 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002669 return DrawGlInfo::kStatusDone;
2670 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002671 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002672 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002673 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002674 return drawShape(x - radius, y - radius, texture, p);
2675 }
2676
2677 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002678 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2679 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2680 } else {
2681 path.addCircle(x, y, radius);
2682 }
Chris Craik65cd6122012-12-10 17:56:27 -08002683 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002684}
Romain Guy01d58e42011-01-19 21:54:02 -08002685
Chet Haase48659092012-05-31 15:21:51 -07002686status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002687 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002688 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2689 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002690 return DrawGlInfo::kStatusDone;
2691 }
Romain Guy01d58e42011-01-19 21:54:02 -08002692
Chris Craikcb4d6002012-09-25 12:00:29 -07002693 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002694 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002695 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002696 return drawShape(left, top, texture, p);
2697 }
2698
2699 SkPath path;
2700 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002701 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2702 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2703 }
Chris Craik710f46d2012-09-17 17:25:49 -07002704 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002705 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002706}
2707
Chet Haase48659092012-05-31 15:21:51 -07002708status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002709 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002710 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2711 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002712 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002713 }
2714
Chris Craik780c1282012-10-04 14:10:49 -07002715 if (fabs(sweepAngle) >= 360.0f) {
2716 return drawOval(left, top, right, bottom, p);
2717 }
2718
2719 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002720 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002721 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002722 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002723 startAngle, sweepAngle, useCenter, p);
2724 return drawShape(left, top, texture, p);
2725 }
2726
2727 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2728 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2729 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2730 }
2731
2732 SkPath path;
2733 if (useCenter) {
2734 path.moveTo(rect.centerX(), rect.centerY());
2735 }
2736 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2737 if (useCenter) {
2738 path.close();
2739 }
Chris Craik65cd6122012-12-10 17:56:27 -08002740 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002741}
2742
Romain Guycf8675e2012-10-02 12:32:25 -07002743// See SkPaintDefaults.h
2744#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2745
Chet Haase48659092012-05-31 15:21:51 -07002746status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002747 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2748 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002749 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002750 }
2751
Chris Craik710f46d2012-09-17 17:25:49 -07002752 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002753 // only fill style is supported by drawConvexPath, since others have to handle joins
2754 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2755 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2756 mCaches.activeTexture(0);
2757 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002758 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002759 return drawShape(left, top, texture, p);
2760 }
2761
2762 SkPath path;
2763 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2764 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2765 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2766 }
2767 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002768 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002769 }
2770
Romain Guy3b753822013-03-05 10:27:35 -08002771 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002772 SkPath path;
2773 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002774 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002775 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002776 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002777 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002778 }
Romain Guyc7d53492010-06-25 13:41:57 -07002779}
Romain Guy9d5316e2010-06-24 19:30:36 -07002780
Raph Levien416a8472012-07-19 22:48:17 -07002781void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2782 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2783 float x, float y) {
2784 mCaches.activeTexture(0);
2785
2786 // NOTE: The drop shadow will not perform gamma correction
2787 // if shader-based correction is enabled
2788 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2789 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002790 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002791 // If the drop shadow exceeds the max texture size or couldn't be
2792 // allocated, skip drawing
2793 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002794 const AutoTexture autoCleanup(shadow);
2795
Chris Craikc3566d02013-02-04 16:16:33 -08002796 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2797 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002798
Chris Craikc3566d02013-02-04 16:16:33 -08002799 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2800 int shadowColor = mDrawModifiers.mShadowColor;
2801 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002802 shadowColor = 0xffffffff;
2803 }
2804
2805 setupDraw();
2806 setupDrawWithTexture(true);
2807 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2808 setupDrawColorFilter();
2809 setupDrawShader();
2810 setupDrawBlending(true, mode);
2811 setupDrawProgram();
2812 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2813 setupDrawTexture(shadow->id);
2814 setupDrawPureColorUniforms();
2815 setupDrawColorFilterUniforms();
2816 setupDrawShaderUniforms();
2817 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2818
2819 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2820}
2821
Romain Guy768bffc2013-02-27 13:50:45 -08002822bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2823 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2824 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2825}
2826
Romain Guy257ae352013-03-20 16:31:12 -07002827class TextSetupFunctor: public Functor {
2828public:
2829 TextSetupFunctor(OpenGLRenderer& renderer, float x, float y, bool pureTranslate,
2830 int alpha, SkXfermode::Mode mode, SkPaint* paint): Functor(),
2831 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate),
2832 alpha(alpha), mode(mode), paint(paint) {
2833 }
2834 ~TextSetupFunctor() { }
2835
2836 status_t operator ()(int what, void* data) {
2837 renderer.setupDraw();
2838 renderer.setupDrawTextGamma(paint);
2839 renderer.setupDrawDirtyRegionsDisabled();
2840 renderer.setupDrawWithTexture(true);
2841 renderer.setupDrawAlpha8Color(paint->getColor(), alpha);
2842 renderer.setupDrawColorFilter();
2843 renderer.setupDrawShader();
2844 renderer.setupDrawBlending(true, mode);
2845 renderer.setupDrawProgram();
2846 renderer.setupDrawModelView(x, y, x, y, pureTranslate, true);
2847 // Calling setupDrawTexture with the name 0 will enable the
2848 // uv attributes and increase the texture unit count
2849 // texture binding will be performed by the font renderer as
2850 // needed
2851 renderer.setupDrawTexture(0);
2852 renderer.setupDrawPureColorUniforms();
2853 renderer.setupDrawColorFilterUniforms();
2854 renderer.setupDrawShaderUniforms(pureTranslate);
2855 renderer.setupDrawTextGammaUniforms();
2856
2857 return NO_ERROR;
2858 }
2859
2860 OpenGLRenderer& renderer;
2861 float x;
2862 float y;
2863 bool pureTranslate;
2864 int alpha;
2865 SkXfermode::Mode mode;
2866 SkPaint* paint;
2867};
2868
Chet Haase48659092012-05-31 15:21:51 -07002869status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002870 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002871 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002872 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002873 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002874
Romain Guy671d6cf2012-01-18 12:39:17 -08002875 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002876 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002877 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002878 }
2879
Chris Craik39a908c2013-06-13 14:39:01 -07002880 mCaches.enableScissor();
2881
Romain Guy671d6cf2012-01-18 12:39:17 -08002882 float x = 0.0f;
2883 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002884 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002885 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002886 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2887 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002888 }
2889
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002890 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002891 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002892
2893 int alpha;
2894 SkXfermode::Mode mode;
2895 getAlphaAndMode(paint, &alpha, &mode);
2896
Chris Craikc3566d02013-02-04 16:16:33 -08002897 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002898 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2899 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002900 }
2901
Romain Guy671d6cf2012-01-18 12:39:17 -08002902 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002903 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002904 if (pureTranslate && !linearFilter) {
2905 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2906 }
Romain Guy257ae352013-03-20 16:31:12 -07002907 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002908
2909 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2910 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2911
Romain Guy211370f2012-02-01 16:10:55 -08002912 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002913
Romain Guy257ae352013-03-20 16:31:12 -07002914 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002915 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002916 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002917 if (hasActiveLayer) {
2918 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002919 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002920 }
2921 dirtyLayerUnchecked(bounds, getRegion());
2922 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002923 }
Chet Haase48659092012-05-31 15:21:51 -07002924
2925 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002926}
2927
Romain Guy624234f2013-03-05 16:43:31 -08002928mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2929 mat4 fontTransform;
2930 if (CC_LIKELY(transform.isPureTranslate())) {
2931 fontTransform = mat4::identity();
2932 } else {
2933 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002934 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002935 } else {
2936 float sx, sy;
2937 currentTransform().decomposeScale(sx, sy);
2938 fontTransform.loadScale(sx, sy, 1.0f);
2939 }
2940 }
2941 return fontTransform;
2942}
2943
Chris Craik41541822013-05-03 16:35:54 -07002944status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2945 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002946 DrawOpMode drawOpMode) {
2947
Chris Craik527a3aa2013-03-04 10:19:31 -08002948 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002949 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2950 // drawing as ops from DeferredDisplayList are already filtered for these
2951 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2952 quickReject(bounds)) {
2953 return DrawGlInfo::kStatusDone;
2954 }
Romain Guycac5fd32011-12-01 20:08:50 -08002955 }
2956
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002957 const float oldX = x;
2958 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002959
2960 const mat4& transform = currentTransform();
2961 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002962
Romain Guy211370f2012-02-01 16:10:55 -08002963 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002964 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2965 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002966 }
2967
Romain Guy86568192010-12-14 15:55:39 -08002968 int alpha;
2969 SkXfermode::Mode mode;
2970 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002971
Romain Guyc74f45a2013-02-26 19:10:14 -08002972 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2973
Chris Craikc3566d02013-02-04 16:16:33 -08002974 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002975 fontRenderer.setFont(paint, mat4::identity());
2976 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2977 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002978 }
2979
Romain Guy19d4dd82013-03-04 11:14:26 -08002980 const bool hasActiveLayer = hasLayer();
2981
Romain Guy624234f2013-03-05 16:43:31 -08002982 // We only pass a partial transform to the font renderer. That partial
2983 // matrix defines how glyphs are rasterized. Typically we want glyphs
2984 // to be rasterized at their final size on screen, which means the partial
2985 // matrix needs to take the scale factor into account.
2986 // When a partial matrix is used to transform glyphs during rasterization,
2987 // the mesh is generated with the inverse transform (in the case of scale,
2988 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2989 // apply the full transform matrix at draw time in the vertex shader.
2990 // Applying the full matrix in the shader is the easiest way to handle
2991 // rotation and perspective and allows us to always generated quads in the
2992 // font renderer which greatly simplifies the code, clipping in particular.
2993 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002994 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002995
Romain Guy6620c6d2010-12-06 18:07:02 -08002996 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002997 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002998 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002999
Romain Guy624234f2013-03-05 16:43:31 -08003000 // TODO: Implement better clipping for scaled/rotated text
3001 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003002 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 -07003003
Raph Levien996e57c2012-07-23 15:22:52 -07003004 bool status;
Romain Guy257ae352013-03-20 16:31:12 -07003005 TextSetupFunctor functor(*this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003006
3007 // don't call issuedrawcommand, do it at end of batch
3008 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003009 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003010 SkPaint paintCopy(*paint);
3011 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3012 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003013 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003014 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003015 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003016 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003017 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003018
Chris Craik527a3aa2013-03-04 10:19:31 -08003019 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003020 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003021 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003022 }
Chris Craik41541822013-05-03 16:35:54 -07003023 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003024 }
Romain Guy694b5192010-07-21 21:33:20 -07003025
Chris Craik41541822013-05-03 16:35:54 -07003026 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003027
3028 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003029}
3030
Chet Haase48659092012-05-31 15:21:51 -07003031status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003032 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003033 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003034 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003035 }
3036
Chris Craik39a908c2013-06-13 14:39:01 -07003037 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3038 mCaches.enableScissor();
3039
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003040 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003041 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003042 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003043
3044 int alpha;
3045 SkXfermode::Mode mode;
3046 getAlphaAndMode(paint, &alpha, &mode);
3047
Romain Guy03d58522012-02-24 17:54:07 -08003048 setupDraw();
Romain Guy41210632012-07-16 17:04:24 -07003049 setupDrawTextGamma(paint);
Romain Guy03d58522012-02-24 17:54:07 -08003050 setupDrawDirtyRegionsDisabled();
3051 setupDrawWithTexture(true);
3052 setupDrawAlpha8Color(paint->getColor(), alpha);
3053 setupDrawColorFilter();
3054 setupDrawShader();
3055 setupDrawBlending(true, mode);
3056 setupDrawProgram();
Romain Guy97771732012-02-28 18:17:02 -08003057 setupDrawModelView(0.0f, 0.0f, 0.0f, 0.0f, false, true);
Romain Guy257ae352013-03-20 16:31:12 -07003058 // Calling setupDrawTexture with the name 0 will enable the
3059 // uv attributes and increase the texture unit count
3060 // texture binding will be performed by the font renderer as
3061 // needed
3062 setupDrawTexture(0);
Romain Guy03d58522012-02-24 17:54:07 -08003063 setupDrawPureColorUniforms();
3064 setupDrawColorFilterUniforms();
Romain Guy97771732012-02-28 18:17:02 -08003065 setupDrawShaderUniforms(false);
Romain Guy41210632012-07-16 17:04:24 -07003066 setupDrawTextGammaUniforms();
Romain Guy03d58522012-02-24 17:54:07 -08003067
Romain Guy97771732012-02-28 18:17:02 -08003068 const Rect* clip = &mSnapshot->getLocalClip();
3069 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 -08003070
Romain Guy97771732012-02-28 18:17:02 -08003071 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003072
3073 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
3074 hOffset, vOffset, hasActiveLayer ? &bounds : NULL)) {
Romain Guy97771732012-02-28 18:17:02 -08003075 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003076 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003077 dirtyLayerUnchecked(bounds, getRegion());
3078 }
Romain Guy97771732012-02-28 18:17:02 -08003079 }
Chet Haase48659092012-05-31 15:21:51 -07003080
3081 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003082}
3083
Chet Haase48659092012-05-31 15:21:51 -07003084status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3085 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003086
Romain Guya1d3c912011-12-13 14:55:06 -08003087 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003088
Romain Guyfb8b7632010-08-23 21:05:08 -07003089 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003090 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003091 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003092
Romain Guy8b55f372010-08-18 17:10:07 -07003093 const float x = texture->left - texture->offset;
3094 const float y = texture->top - texture->offset;
3095
Romain Guy01d58e42011-01-19 21:54:02 -08003096 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003097
3098 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003099}
3100
Chris Craika08f95c2013-03-15 17:24:33 -07003101status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003102 if (!layer) {
3103 return DrawGlInfo::kStatusDone;
3104 }
3105
Romain Guyb2e2f242012-10-17 18:18:35 -07003106 mat4* transform = NULL;
3107 if (layer->isTextureLayer()) {
3108 transform = &layer->getTransform();
3109 if (!transform->isIdentity()) {
3110 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003111 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003112 }
3113 }
3114
Chris Craik39a908c2013-06-13 14:39:01 -07003115 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003116 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik39a908c2013-06-13 14:39:01 -07003117 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003118
3119 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003120 if (transform && !transform->isIdentity()) {
3121 restore();
3122 }
Chet Haase48659092012-05-31 15:21:51 -07003123 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003124 }
3125
Romain Guy5bb3c732012-11-29 17:52:58 -08003126 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003127
Chris Craik39a908c2013-06-13 14:39:01 -07003128 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003129 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003130
Romain Guy211370f2012-02-01 16:10:55 -08003131 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003132 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3133 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003134
Romain Guyc88e3572011-01-22 00:32:12 -08003135 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003136 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3137 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003138 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003139 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003140 setupDraw();
3141 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003142 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003143 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003144 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003145 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003146 setupDrawPureColorUniforms();
3147 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003148 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003149 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3150 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3151 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003152
Romain Guyd21b6e12011-11-30 20:21:23 -08003153 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003154 setupDrawModelViewTranslate(tx, ty,
3155 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003156 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003157 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003158 setupDrawModelViewTranslate(x, y,
3159 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3160 }
Romain Guyf219da52011-01-16 12:54:25 -08003161
Romain Guy448455f2013-07-22 13:57:50 -07003162 TextureVertex* mesh = &layer->mesh[0];
3163 GLsizei elementsCount = layer->meshElementCount;
3164
3165 while (elementsCount > 0) {
3166 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3167
3168 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3169 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3170 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3171
3172 elementsCount -= drawCount;
3173 // Though there are 4 vertices in a quad, we use 6 indices per
3174 // quad to draw with GL_TRIANGLES
3175 mesh += (drawCount / 6) * 4;
3176 }
Romain Guyf219da52011-01-16 12:54:25 -08003177
Romain Guyc88e3572011-01-22 00:32:12 -08003178 finishDrawTexture();
Romain Guy3a3133d2011-02-01 22:59:58 -08003179
3180#if DEBUG_LAYERS_AS_REGIONS
3181 drawRegionRects(layer->region);
3182#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003183 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003184
Chris Craikc3566d02013-02-04 16:16:33 -08003185 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003186
Romain Guy5bb3c732012-11-29 17:52:58 -08003187 if (layer->debugDrawUpdate) {
3188 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003189 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3190 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3191 }
Romain Guyf219da52011-01-16 12:54:25 -08003192 }
Chris Craik34416ea2013-04-15 16:08:28 -07003193 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003194
Romain Guyb2e2f242012-10-17 18:18:35 -07003195 if (transform && !transform->isIdentity()) {
3196 restore();
3197 }
3198
Chet Haase48659092012-05-31 15:21:51 -07003199 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003200}
3201
Romain Guy6926c722010-07-12 20:20:03 -07003202///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003203// Shaders
3204///////////////////////////////////////////////////////////////////////////////
3205
3206void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003207 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003208}
3209
Romain Guy06f96e22010-07-30 19:18:16 -07003210void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003211 mDrawModifiers.mShader = shader;
3212 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003213 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003214 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003215}
3216
Romain Guyd27977d2010-07-14 19:18:51 -07003217///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003218// Color filters
3219///////////////////////////////////////////////////////////////////////////////
3220
3221void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003222 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003223}
3224
3225void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003226 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003227}
3228
3229///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003230// Drop shadow
3231///////////////////////////////////////////////////////////////////////////////
3232
3233void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003234 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003235}
3236
3237void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003238 mDrawModifiers.mHasShadow = true;
3239 mDrawModifiers.mShadowRadius = radius;
3240 mDrawModifiers.mShadowDx = dx;
3241 mDrawModifiers.mShadowDy = dy;
3242 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003243}
3244
3245///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003246// Draw filters
3247///////////////////////////////////////////////////////////////////////////////
3248
3249void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003250 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3251 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003252 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003253 mDrawModifiers.mPaintFilterClearBits = 0;
3254 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003255}
3256
3257void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003258 mDrawModifiers.mHasDrawFilter = true;
3259 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3260 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003261}
3262
Chris Craika08f95c2013-03-15 17:24:33 -07003263SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003264 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003265 return paint;
3266 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003267
3268 uint32_t flags = paint->getFlags();
3269
3270 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003271 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3272 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003273
3274 return &mFilteredPaint;
3275}
3276
3277///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003278// Drawing implementation
3279///////////////////////////////////////////////////////////////////////////////
3280
Romain Guy3b748a42013-04-17 18:54:38 -07003281Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3282 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3283 if (!texture) {
3284 return mCaches.textureCache.get(bitmap);
3285 }
3286 return texture;
3287}
3288
Romain Guy01d58e42011-01-19 21:54:02 -08003289void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3290 float x, float y, SkPaint* paint) {
3291 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3292 return;
3293 }
3294
3295 int alpha;
3296 SkXfermode::Mode mode;
3297 getAlphaAndMode(paint, &alpha, &mode);
3298
3299 setupDraw();
3300 setupDrawWithTexture(true);
3301 setupDrawAlpha8Color(paint->getColor(), alpha);
3302 setupDrawColorFilter();
3303 setupDrawShader();
3304 setupDrawBlending(true, mode);
3305 setupDrawProgram();
3306 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3307 setupDrawTexture(texture->id);
3308 setupDrawPureColorUniforms();
3309 setupDrawColorFilterUniforms();
3310 setupDrawShaderUniforms();
3311 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3312
3313 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3314
3315 finishDrawTexture();
3316}
3317
Romain Guyf607bdc2010-09-10 19:20:06 -07003318// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003319#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3320#define kStdUnderline_Offset (1.0f / 9.0f)
3321#define kStdUnderline_Thickness (1.0f / 18.0f)
3322
Chris Craik41541822013-05-03 16:35:54 -07003323void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003324 float x, float y, SkPaint* paint) {
3325 // Handle underline and strike-through
3326 uint32_t flags = paint->getFlags();
3327 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003328 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003329
Romain Guy211370f2012-02-01 16:10:55 -08003330 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003331 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003332 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003333
Raph Levien8b4072d2012-07-30 15:50:00 -07003334 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003335 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003336
Romain Guyf6834472011-01-23 13:32:12 -08003337 int linesCount = 0;
3338 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3339 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3340
3341 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003342 float points[pointsCount];
3343 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003344
3345 if (flags & SkPaint::kUnderlineText_Flag) {
3346 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003347 points[currentPoint++] = left;
3348 points[currentPoint++] = top;
3349 points[currentPoint++] = left + underlineWidth;
3350 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003351 }
3352
3353 if (flags & SkPaint::kStrikeThruText_Flag) {
3354 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003355 points[currentPoint++] = left;
3356 points[currentPoint++] = top;
3357 points[currentPoint++] = left + underlineWidth;
3358 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003359 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003360
Romain Guy726aeba2011-06-01 14:52:00 -07003361 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003362
Romain Guy726aeba2011-06-01 14:52:00 -07003363 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003364 }
3365 }
3366}
3367
Romain Guy672433d2013-01-04 19:05:13 -08003368status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3369 if (mSnapshot->isIgnored()) {
3370 return DrawGlInfo::kStatusDone;
3371 }
3372
Romain Guy735738c2012-12-03 12:34:51 -08003373 int color = paint->getColor();
3374 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003375 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003376 color |= 0x00ffffff;
3377 }
3378 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3379
3380 return drawColorRects(rects, count, color, mode);
3381}
3382
3383status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003384 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003385 if (count == 0) {
3386 return DrawGlInfo::kStatusDone;
3387 }
Romain Guy735738c2012-12-03 12:34:51 -08003388
Romain Guy672433d2013-01-04 19:05:13 -08003389 float left = FLT_MAX;
3390 float top = FLT_MAX;
3391 float right = FLT_MIN;
3392 float bottom = FLT_MIN;
3393
Romain Guy448455f2013-07-22 13:57:50 -07003394 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003395 Vertex* vertex = mesh;
3396
Chris Craik2af46352012-11-26 18:30:17 -08003397 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003398 float l = rects[index + 0];
3399 float t = rects[index + 1];
3400 float r = rects[index + 2];
3401 float b = rects[index + 3];
3402
Romain Guy3b753822013-03-05 10:27:35 -08003403 Vertex::set(vertex++, l, t);
3404 Vertex::set(vertex++, r, t);
3405 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003406 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003407
Romain Guy3b753822013-03-05 10:27:35 -08003408 left = fminf(left, l);
3409 top = fminf(top, t);
3410 right = fmaxf(right, r);
3411 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003412 }
3413
Romain Guy3b753822013-03-05 10:27:35 -08003414 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003415 return DrawGlInfo::kStatusDone;
3416 }
Romain Guy672433d2013-01-04 19:05:13 -08003417
Romain Guy672433d2013-01-04 19:05:13 -08003418 setupDraw();
3419 setupDrawNoTexture();
3420 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3421 setupDrawShader();
3422 setupDrawColorFilter();
3423 setupDrawBlending(mode);
3424 setupDrawProgram();
3425 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003426 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003427 setupDrawColorUniforms();
3428 setupDrawShaderUniforms();
3429 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003430
Romain Guy8ce00302013-01-15 18:51:42 -08003431 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003432 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003433 }
3434
Romain Guy448455f2013-07-22 13:57:50 -07003435 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003436
3437 return DrawGlInfo::kStatusDrew;
3438}
3439
Romain Guy026c5e162010-06-28 17:12:22 -07003440void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003441 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003442 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003443 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003444 color |= 0x00ffffff;
3445 }
3446
Romain Guy70ca14e2010-12-13 18:24:33 -08003447 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003448 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003449 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003450 setupDrawShader();
3451 setupDrawColorFilter();
3452 setupDrawBlending(mode);
3453 setupDrawProgram();
3454 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3455 setupDrawColorUniforms();
3456 setupDrawShaderUniforms(ignoreTransform);
3457 setupDrawColorFilterUniforms();
3458 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003459
Romain Guyc95c8d62010-09-17 15:31:32 -07003460 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3461}
3462
Romain Guy82ba8142010-07-09 13:25:56 -07003463void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003464 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003465 int alpha;
3466 SkXfermode::Mode mode;
3467 getAlphaAndMode(paint, &alpha, &mode);
3468
Romain Guyd21b6e12011-11-30 20:21:23 -08003469 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003470
Romain Guy3b748a42013-04-17 18:54:38 -07003471 GLvoid* vertices = (GLvoid*) NULL;
3472 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3473
3474 if (texture->uvMapper) {
3475 vertices = &mMeshVertices[0].position[0];
3476 texCoords = &mMeshVertices[0].texture[0];
3477
3478 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3479 texture->uvMapper->map(uvs);
3480
3481 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3482 }
3483
Romain Guy3b753822013-03-05 10:27:35 -08003484 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3485 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3486 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003487
Romain Guyd21b6e12011-11-30 20:21:23 -08003488 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003489 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003490 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3491 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003492 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003493 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003494 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003495 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3496 }
3497
3498 if (texture->uvMapper) {
3499 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003500 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003501}
3502
Romain Guybd6b79b2010-06-26 00:13:53 -07003503void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003504 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3505 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003506 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003507}
3508
3509void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003510 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003511 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003512 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003513
Romain Guy746b7402010-10-26 16:27:31 -07003514 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003515 setupDrawWithTexture();
3516 setupDrawColor(alpha, alpha, alpha, alpha);
3517 setupDrawColorFilter();
3518 setupDrawBlending(blend, mode, swapSrcDst);
3519 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003520 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003521 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003522 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003523 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003524 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003525 }
Romain Guy886b2752013-01-04 12:26:18 -08003526 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003527 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003528 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003529 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003530
Romain Guy6820ac82010-09-15 18:11:50 -07003531 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy70ca14e2010-12-13 18:24:33 -08003532
3533 finishDrawTexture();
Romain Guy82ba8142010-07-09 13:25:56 -07003534}
3535
Romain Guy3b748a42013-04-17 18:54:38 -07003536void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3537 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3538 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3539 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3540
3541 setupDraw();
3542 setupDrawWithTexture();
3543 setupDrawColor(alpha, alpha, alpha, alpha);
3544 setupDrawColorFilter();
3545 setupDrawBlending(blend, mode, swapSrcDst);
3546 setupDrawProgram();
3547 if (!dirty) setupDrawDirtyRegionsDisabled();
3548 if (!ignoreScale) {
3549 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3550 } else {
3551 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3552 }
3553 setupDrawTexture(texture);
3554 setupDrawPureColorUniforms();
3555 setupDrawColorFilterUniforms();
3556 setupDrawMeshIndices(vertices, texCoords, vbo);
3557
3558 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
3559
3560 finishDrawTexture();
3561}
3562
Romain Guy886b2752013-01-04 12:26:18 -08003563void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3564 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3565 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003566 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003567
3568 setupDraw();
3569 setupDrawWithTexture(true);
3570 if (hasColor) {
3571 setupDrawAlpha8Color(color, alpha);
3572 }
3573 setupDrawColorFilter();
3574 setupDrawShader();
3575 setupDrawBlending(true, mode);
3576 setupDrawProgram();
3577 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003578 if (!ignoreScale) {
3579 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3580 } else {
3581 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3582 }
Romain Guy886b2752013-01-04 12:26:18 -08003583 setupDrawTexture(texture);
3584 setupDrawPureColorUniforms();
3585 setupDrawColorFilterUniforms();
3586 setupDrawShaderUniforms();
3587 setupDrawMesh(vertices, texCoords);
3588
3589 glDrawArrays(drawMode, 0, elementsCount);
3590
3591 finishDrawTexture();
3592}
3593
Romain Guya5aed0d2010-09-09 14:42:43 -07003594void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003595 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003596 if (mCountOverdraw) {
3597 if (!mCaches.blend) glEnable(GL_BLEND);
3598 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3599 glBlendFunc(GL_ONE, GL_ONE);
3600 }
3601
3602 mCaches.blend = true;
3603 mCaches.lastSrcMode = GL_ONE;
3604 mCaches.lastDstMode = GL_ONE;
3605
3606 return;
3607 }
3608
Romain Guy82ba8142010-07-09 13:25:56 -07003609 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003610
Romain Guy82ba8142010-07-09 13:25:56 -07003611 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003612 // These blend modes are not supported by OpenGL directly and have
3613 // to be implemented using shaders. Since the shader will perform
3614 // the blending, turn blending off here
3615 // If the blend mode cannot be implemented using shaders, fall
3616 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003617 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003618 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003619 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003620 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003621
Romain Guy82bc7a72012-01-03 14:13:39 -08003622 if (mCaches.blend) {
3623 glDisable(GL_BLEND);
3624 mCaches.blend = false;
3625 }
3626
3627 return;
3628 } else {
3629 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003630 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003631 }
3632
3633 if (!mCaches.blend) {
3634 glEnable(GL_BLEND);
3635 }
3636
3637 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3638 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3639
3640 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3641 glBlendFunc(sourceMode, destMode);
3642 mCaches.lastSrcMode = sourceMode;
3643 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003644 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003645 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003646 glDisable(GL_BLEND);
3647 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003648 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003649}
3650
Romain Guy889f8d12010-07-29 14:37:42 -07003651bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003652 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003653 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003654 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003655 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003656 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003657 }
Romain Guy6926c722010-07-12 20:20:03 -07003658 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003659}
3660
Romain Guy026c5e162010-06-28 17:12:22 -07003661void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003662 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003663 TextureVertex::setUV(v++, u1, v1);
3664 TextureVertex::setUV(v++, u2, v1);
3665 TextureVertex::setUV(v++, u1, v2);
3666 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003667}
3668
Chris Craik16ecda52013-03-29 10:59:59 -07003669void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003670 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003671 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3672 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003673 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003674 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003675 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003676}
3677
Chris Craik16ecda52013-03-29 10:59:59 -07003678float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3679 float alpha;
3680 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3681 alpha = mDrawModifiers.mOverrideLayerAlpha;
3682 } else {
3683 alpha = layer->getAlpha() / 255.0f;
3684 }
3685 return alpha * mSnapshot->alpha;
3686}
3687
Romain Guy9d5316e2010-06-24 19:30:36 -07003688}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003689}; // namespace android