blob: aabfbd02f6d8f643b888157b0a2714a4972926c4 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Romain Guy260e1022010-07-12 14:41:06 -0700183 mOrthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guyd643bb52011-03-01 14:55:21 -0800454
Romain Guy80911b82011-03-16 15:30:12 -0700455 Rect clip(*mSnapshot->clipRect);
456 clip.snapToPixelBoundaries();
457
Romain Guyd643bb52011-03-01 14:55:21 -0800458 // Since we don't know what the functor will draw, let's dirty
459 // tne entire clip region
460 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800461 dirtyLayerUnchecked(clip, getRegion());
462 }
Romain Guyd643bb52011-03-01 14:55:21 -0800463
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 DrawGlInfo info;
465 info.clipLeft = clip.left;
466 info.clipTop = clip.top;
467 info.clipRight = clip.right;
468 info.clipBottom = clip.bottom;
469 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700470 info.width = getSnapshot()->viewport.getWidth();
471 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700473
John Reck25d2f7b2013-09-10 13:12:09 -0700474 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700475 // setup GL state for functor
476 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700477 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
478 }
John Reck25d2f7b2013-09-10 13:12:09 -0700479 if (mCaches.enableScissor() || dirtyClip) {
480 setScissorFromClip();
481 }
Chris Craik54f574a2013-08-26 11:23:46 -0700482 interrupt();
483
484 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700485 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800486
Romain Guy8f3b8e32012-03-27 16:33:45 -0700487 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700488 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800489 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700490
Chris Craik65924a32012-04-05 17:52:11 -0700491 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700492 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700493 }
Romain Guycabfcc12011-03-07 18:06:46 -0800494 }
495
Chet Haasedaf98e92011-01-10 14:10:36 -0800496 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700497 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800498}
499
Romain Guyf6a11b82010-06-23 17:47:49 -0700500///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700501// Debug
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guy0f6675332013-03-01 14:31:04 -0800504void OpenGLRenderer::eventMark(const char* name) const {
505 mCaches.eventMark(0, name);
506}
507
Romain Guy87e2f7572012-09-24 11:37:12 -0700508void OpenGLRenderer::startMark(const char* name) const {
509 mCaches.startMark(0, name);
510}
511
512void OpenGLRenderer::endMark() const {
513 mCaches.endMark();
514}
515
516void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
517 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
518 if (clear) {
519 mCaches.disableScissor();
520 mCaches.stencil.clear();
521 }
522 if (enable) {
523 mCaches.stencil.enableDebugWrite();
524 } else {
525 mCaches.stencil.disable();
526 }
527 }
528}
529
530void OpenGLRenderer::renderOverdraw() {
531 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700532 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700533
534 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700535 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700536 clip->right - clip->left, clip->bottom - clip->top);
537
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700539 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700540 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
541
542 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700543 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700544 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
545
546 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700547 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700548 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
549
550 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700551 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700552 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
553
Romain Guy87e2f7572012-09-24 11:37:12 -0700554 mCaches.stencil.disable();
555 }
556}
557
Romain Guy78dd96d2013-05-03 14:24:16 -0700558void OpenGLRenderer::countOverdraw() {
559 size_t count = mWidth * mHeight;
560 uint32_t* buffer = new uint32_t[count];
561 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
562
563 size_t total = 0;
564 for (size_t i = 0; i < count; i++) {
565 total += buffer[i] & 0xff;
566 }
567
568 mOverdraw = total / float(count);
569
570 delete[] buffer;
571}
572
Romain Guy87e2f7572012-09-24 11:37:12 -0700573///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700574// Layers
575///////////////////////////////////////////////////////////////////////////////
576
577bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (layer->deferredUpdateScheduled && layer->renderer &&
579 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700580 ATRACE_CALL();
581
Romain Guy11cb6422012-09-21 00:39:43 -0700582 Rect& dirty = layer->dirtyRect;
583
Romain Guy7c450aa2012-09-21 19:15:00 -0700584 if (inFrame) {
585 endTiling();
586 debugOverdraw(false, false);
587 }
Romain Guy11cb6422012-09-21 00:39:43 -0700588
Romain Guy96885eb2013-03-26 15:05:58 -0700589 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700590 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700591 } else {
592 layer->defer();
593 }
Romain Guy11cb6422012-09-21 00:39:43 -0700594
595 if (inFrame) {
596 resumeAfterLayer();
597 startTiling(mSnapshot);
598 }
599
Romain Guy5bb3c732012-11-29 17:52:58 -0800600 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700601 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700602
603 return true;
604 }
605
606 return false;
607}
608
609void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700610 // If draw deferring is enabled this method will simply defer
611 // the display list of each individual layer. The layers remain
612 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700613 int count = mLayerUpdates.size();
614 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700615 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
616 startMark("Layer Updates");
617 } else {
618 startMark("Defer Layer Updates");
619 }
Romain Guy11cb6422012-09-21 00:39:43 -0700620
Chris Craik1206b9b2013-04-04 14:46:24 -0700621 // Note: it is very important to update the layers in order
622 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700623 Layer* layer = mLayerUpdates.itemAt(i);
624 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700625 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
626 mCaches.resourceCache.decrementRefcount(layer);
627 }
Romain Guy11cb6422012-09-21 00:39:43 -0700628 }
Romain Guy11cb6422012-09-21 00:39:43 -0700629
Romain Guy96885eb2013-03-26 15:05:58 -0700630 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
631 mLayerUpdates.clear();
632 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
633 }
634 endMark();
635 }
636}
637
638void OpenGLRenderer::flushLayers() {
639 int count = mLayerUpdates.size();
640 if (count > 0) {
641 startMark("Apply Layer Updates");
642 char layerName[12];
643
Chris Craik1206b9b2013-04-04 14:46:24 -0700644 // Note: it is very important to update the layers in order
645 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700646 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700647 startMark(layerName);
648
Romain Guy40543602013-06-12 15:31:28 -0700649 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700650 Layer* layer = mLayerUpdates.itemAt(i);
651 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700652 ATRACE_END();
653
Romain Guy02b49b72013-03-29 12:37:16 -0700654 mCaches.resourceCache.decrementRefcount(layer);
655
Romain Guy96885eb2013-03-26 15:05:58 -0700656 endMark();
657 }
658
659 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700660 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700661
Romain Guy11cb6422012-09-21 00:39:43 -0700662 endMark();
663 }
664}
665
666void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
667 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700668 // Make sure we don't introduce duplicates.
669 // SortedVector would do this automatically but we need to respect
670 // the insertion order. The linear search is not an issue since
671 // this list is usually very short (typically one item, at most a few)
672 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
673 if (mLayerUpdates.itemAt(i) == layer) {
674 return;
675 }
676 }
Romain Guy11cb6422012-09-21 00:39:43 -0700677 mLayerUpdates.push_back(layer);
678 mCaches.resourceCache.incrementRefcount(layer);
679 }
680}
681
Romain Guye93482f2013-06-17 13:14:51 -0700682void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
683 if (layer) {
684 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
685 if (mLayerUpdates.itemAt(i) == layer) {
686 mLayerUpdates.removeAt(i);
687 mCaches.resourceCache.decrementRefcount(layer);
688 break;
689 }
690 }
691 }
692}
693
Romain Guy11cb6422012-09-21 00:39:43 -0700694void OpenGLRenderer::clearLayerUpdates() {
695 size_t count = mLayerUpdates.size();
696 if (count > 0) {
697 mCaches.resourceCache.lock();
698 for (size_t i = 0; i < count; i++) {
699 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
700 }
701 mCaches.resourceCache.unlock();
702 mLayerUpdates.clear();
703 }
704}
705
Romain Guy40543602013-06-12 15:31:28 -0700706void OpenGLRenderer::flushLayerUpdates() {
707 syncState();
708 updateLayers();
709 flushLayers();
710 // Wait for all the layer updates to be executed
711 AutoFence fence;
712}
713
Romain Guy11cb6422012-09-21 00:39:43 -0700714///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700715// State management
716///////////////////////////////////////////////////////////////////////////////
717
Romain Guybb9524b2010-06-22 18:56:38 -0700718int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700719 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700720}
721
722int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700723 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
726void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700727 if (mSaveCount > 1) {
728 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700729 }
Romain Guybb9524b2010-06-22 18:56:38 -0700730}
731
732void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700733 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700734
Romain Guy8fb95422010-08-17 18:38:51 -0700735 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700736 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700737 }
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
Romain Guy8aef54f2010-09-01 15:13:49 -0700740int OpenGLRenderer::saveSnapshot(int flags) {
741 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700742 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700743}
744
745bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700746 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700747 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700748 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700749
Romain Guybd6b79b2010-06-26 00:13:53 -0700750 sp<Snapshot> current = mSnapshot;
751 sp<Snapshot> previous = mSnapshot->previous;
752
Romain Guyeb993562010-10-05 18:14:38 -0700753 if (restoreOrtho) {
754 Rect& r = previous->viewport;
755 glViewport(r.left, r.top, r.right, r.bottom);
756 mOrthoMatrix.load(current->orthoMatrix);
757 }
758
Romain Guy8b55f372010-08-18 17:10:07 -0700759 mSaveCount--;
760 mSnapshot = previous;
761
Romain Guy2542d192010-08-18 11:47:12 -0700762 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700763 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700764 }
Romain Guy2542d192010-08-18 11:47:12 -0700765
Romain Guy5ec99242010-11-03 16:19:08 -0700766 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700767 endMark(); // Savelayer
768 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700769 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700770 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700771 }
772
Romain Guy2542d192010-08-18 11:47:12 -0700773 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700774}
775
Romain Guyf6a11b82010-06-23 17:47:49 -0700776///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700777// Layers
778///////////////////////////////////////////////////////////////////////////////
779
780int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800781 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700782 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700783
Romain Guyaf636eb2010-12-09 17:47:21 -0800784 if (!mSnapshot->isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700785 createLayer(left, top, right, bottom, alpha, mode, flags);
Romain Guydbc26d22010-10-11 17:58:29 -0700786 }
Romain Guyd55a8612010-06-28 17:42:46 -0700787
788 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700789}
790
Chris Craikd90144d2013-03-19 15:03:48 -0700791void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
792 const Rect untransformedBounds(bounds);
793
794 currentTransform().mapRect(bounds);
795
796 // Layers only make sense if they are in the framebuffer's bounds
797 if (bounds.intersect(*mSnapshot->clipRect)) {
798 // We cannot work with sub-pixels in this case
799 bounds.snapToPixelBoundaries();
800
801 // When the layer is not an FBO, we may use glCopyTexImage so we
802 // need to make sure the layer does not extend outside the bounds
803 // of the framebuffer
804 if (!bounds.intersect(mSnapshot->previous->viewport)) {
805 bounds.setEmpty();
806 } else if (fboLayer) {
807 clip.set(bounds);
808 mat4 inverse;
809 inverse.loadInverse(currentTransform());
810 inverse.mapRect(clip);
811 clip.snapToPixelBoundaries();
812 if (clip.intersect(untransformedBounds)) {
813 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
814 bounds.set(untransformedBounds);
815 } else {
816 clip.setEmpty();
817 }
818 }
819 } else {
820 bounds.setEmpty();
821 }
822}
823
Chris Craik408eb122013-03-26 18:55:15 -0700824void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
825 bool fboLayer, int alpha) {
826 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
827 bounds.getHeight() > mCaches.maxTextureSize ||
828 (fboLayer && clip.isEmpty())) {
829 mSnapshot->empty = fboLayer;
830 } else {
831 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
832 }
833}
834
Chris Craikd90144d2013-03-19 15:03:48 -0700835int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
836 int alpha, SkXfermode::Mode mode, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700837 const int count = saveSnapshot(flags);
838
839 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
840 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
841 // operations will be able to store and restore the current clip and transform info, and
842 // quick rejection will be correct (for display lists)
843
844 Rect bounds(left, top, right, bottom);
845 Rect clip;
846 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700847 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700848
Chris Craik408eb122013-03-26 18:55:15 -0700849 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700850 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
851 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700852 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700853 }
854 }
855
856 return count;
857}
858
859
Romain Guy1c740bc2010-09-13 18:00:09 -0700860/**
861 * Layers are viewed by Skia are slightly different than layers in image editing
862 * programs (for instance.) When a layer is created, previously created layers
863 * and the frame buffer still receive every drawing command. For instance, if a
864 * layer is created and a shape intersecting the bounds of the layers and the
865 * framebuffer is draw, the shape will be drawn on both (unless the layer was
866 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
867 *
868 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
869 * texture. Unfortunately, this is inefficient as it requires every primitive to
870 * be drawn n + 1 times, where n is the number of active layers. In practice this
871 * means, for every primitive:
872 * - Switch active frame buffer
873 * - Change viewport, clip and projection matrix
874 * - Issue the drawing
875 *
876 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700877 * To avoid this, layers are implemented in a different way here, at least in the
878 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
879 * is set. When this flag is set we can redirect all drawing operations into a
880 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700881 *
882 * This implementation relies on the frame buffer being at least RGBA 8888. When
883 * a layer is created, only a texture is created, not an FBO. The content of the
884 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700885 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700886 * buffer and drawing continues as normal. This technique therefore treats the
887 * frame buffer as a scratch buffer for the layers.
888 *
889 * To compose the layers back onto the frame buffer, each layer texture
890 * (containing the original frame buffer data) is drawn as a simple quad over
891 * the frame buffer. The trick is that the quad is set as the composition
892 * destination in the blending equation, and the frame buffer becomes the source
893 * of the composition.
894 *
895 * Drawing layers with an alpha value requires an extra step before composition.
896 * An empty quad is drawn over the layer's region in the frame buffer. This quad
897 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
898 * quad is used to multiply the colors in the frame buffer. This is achieved by
899 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
900 * GL_ZERO, GL_SRC_ALPHA.
901 *
902 * Because glCopyTexImage2D() can be slow, an alternative implementation might
903 * be use to draw a single clipped layer. The implementation described above
904 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700905 *
906 * (1) The frame buffer is actually not cleared right away. To allow the GPU
907 * to potentially optimize series of calls to glCopyTexImage2D, the frame
908 * buffer is left untouched until the first drawing operation. Only when
909 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700910 */
Chet Haased48885a2012-08-28 17:43:28 -0700911bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700912 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700913 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700915
Romain Guyeb993562010-10-05 18:14:38 -0700916 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
917
Romain Guyf607bdc2010-09-10 19:20:06 -0700918 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700919 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700920 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700921 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700922 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700923
924 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700925 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700926 return false;
927 }
Romain Guyf18fd992010-07-08 11:45:51 -0700928
Romain Guya1d3c912011-12-13 14:55:06 -0800929 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700930 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700931 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700932 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700933 }
934
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
938 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800939 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700940 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700941 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700942
Romain Guy8fb95422010-08-17 18:38:51 -0700943 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700944 mSnapshot->flags |= Snapshot::kFlagIsLayer;
945 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700946
Chris Craik7273daa2013-03-28 11:25:24 -0700947 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700948 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700949 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700950 } else {
951 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800953 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700955 // Workaround for some GL drivers. When reading pixels lying outside
956 // of the window we should get undefined values for those pixels.
957 // Unfortunately some drivers will turn the entire target texture black
958 // when reading outside of the window.
959 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
960 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800962 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700963
Romain Guyb254c242013-06-27 17:15:24 -0700964 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
965 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
966
Romain Guy54be1cd2011-06-13 19:04:27 -0700967 // Enqueue the buffer coordinates to clear the corresponding region later
968 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700969 }
Romain Guyeb993562010-10-05 18:14:38 -0700970 }
Romain Guyf86ef572010-07-01 11:05:42 -0700971
Romain Guyd55a8612010-06-28 17:42:46 -0700972 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700973}
974
Chris Craike63f7c622013-10-17 10:30:55 -0700975bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800976 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
Chet Haased48885a2012-08-28 17:43:28 -0700979 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800980 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
981 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700982 mSnapshot->fbo = layer->getFbo();
983 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
984 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
985 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
986 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -0700987 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988
Romain Guy2b7028e2012-09-19 17:25:38 -0700989 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700990 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700991 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
993 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
995 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700997 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 }
1000
1001 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
Romain Guyf735c8e2013-01-31 17:45:55 -08001004 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
1006 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001007 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001008 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001009 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 glClear(GL_COLOR_BUFFER_BIT);
1011
1012 dirtyClip();
1013
1014 // Change the ortho projection
1015 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1016 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1017
1018 return true;
1019}
1020
Romain Guy1c740bc2010-09-13 18:00:09 -07001021/**
1022 * Read the documentation of createLayer() before doing anything in this method.
1023 */
Romain Guy1d83e192010-08-17 11:37:00 -07001024void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1025 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001026 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001027 return;
1028 }
1029
Romain Guy8ce00302013-01-15 18:51:42 -08001030 Layer* layer = current->layer;
1031 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001032 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001033
Chris Craik39a908c2013-06-13 14:39:01 -07001034 bool clipRequired = false;
1035 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1036 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1037
Romain Guyeb993562010-10-05 18:14:38 -07001038 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001039 endTiling();
1040
Romain Guye0aa84b2012-04-03 19:30:26 -07001041 // Detach the texture from the FBO
1042 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001043
1044 layer->removeFbo(false);
1045
Romain Guyeb993562010-10-05 18:14:38 -07001046 // Unbind current FBO and restore previous one
1047 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001048 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001049
1050 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001051 }
1052
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001054 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001055 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001056 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001057 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001058 }
1059
Romain Guy03750a02010-10-18 14:06:08 -07001060 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001061
Romain Guya1d3c912011-12-13 14:55:06 -08001062 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001063
Romain Guy5b3b3522010-10-27 18:57:51 -07001064 // When the layer is stored in an FBO, we can save a bit of fillrate by
1065 // drawing only the dirty region
1066 if (fboLayer) {
1067 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001068 if (layer->getColorFilter()) {
1069 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001070 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001071 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001073 resetColorFilter();
1074 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 } else if (!rect.isEmpty()) {
1076 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301077
1078 save(0);
1079 // the layer contains screen buffer content that shouldn't be alpha modulated
1080 // (and any necessary alpha modulation was handled drawing into the layer)
1081 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001082 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301083 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001084 }
Romain Guy8b55f372010-08-18 17:10:07 -07001085
Romain Guy746b7402010-10-26 16:27:31 -07001086 dirtyClip();
1087
Romain Guyeb993562010-10-05 18:14:38 -07001088 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001089 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001090 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001091 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001092 }
1093}
1094
Romain Guyaa6c24c2011-04-28 18:40:04 -07001095void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001096 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001097
1098 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001099 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001100 setupDrawWithTexture();
1101 } else {
1102 setupDrawWithExternalTexture();
1103 }
1104 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001105 setupDrawColor(alpha, alpha, alpha, alpha);
1106 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001107 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001108 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001109 setupDrawPureColorUniforms();
1110 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001111 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1112 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001113 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001115 }
Romain Guy3b753822013-03-05 10:27:35 -08001116 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001117 layer->getWidth() == (uint32_t) rect.getWidth() &&
1118 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001119 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1120 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001121
Romain Guyd21b6e12011-11-30 20:21:23 -08001122 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001123 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1124 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001125 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001126 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1127 }
1128 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001129 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001130
1131 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001132}
1133
Romain Guy5b3b3522010-10-27 18:57:51 -07001134void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001135 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001136 const Rect& texCoords = layer->texCoords;
1137 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1138 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001139
Romain Guy9ace8f52011-07-07 20:50:11 -07001140 float x = rect.left;
1141 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001142 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001143 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001144 layer->getHeight() == (uint32_t) rect.getHeight();
1145
1146 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001147 // When we're swapping, the layer is already in screen coordinates
1148 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001149 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1150 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001151 }
1152
Romain Guyd21b6e12011-11-30 20:21:23 -08001153 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001154 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001155 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 }
1157
Chris Craik16ecda52013-03-29 10:59:59 -07001158 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001159 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001160 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001161 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001162 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001163 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001164
Romain Guyaa6c24c2011-04-28 18:40:04 -07001165 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1166 } else {
1167 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1168 drawTextureLayer(layer, rect);
1169 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1170 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001171}
1172
Chris Craik34416ea2013-04-15 16:08:28 -07001173/**
1174 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1175 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1176 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1177 * by saveLayer's restore
1178 */
1179#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1180 DRAW_COMMAND; \
1181 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1182 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1183 DRAW_COMMAND; \
1184 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1185 } \
1186 }
1187
1188#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1189
Romain Guy5b3b3522010-10-27 18:57:51 -07001190void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001191 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001192 layer->setRegionAsRect();
1193
Chris Craik34416ea2013-04-15 16:08:28 -07001194 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001195
Romain Guy5b3b3522010-10-27 18:57:51 -07001196 layer->region.clear();
1197 return;
1198 }
1199
Romain Guy211370f2012-02-01 16:10:55 -08001200 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001201 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001202 const android::Rect* rects;
1203 Region safeRegion;
1204 if (CC_LIKELY(hasRectToRectTransform())) {
1205 rects = layer->region.getArray(&count);
1206 } else {
1207 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1208 rects = safeRegion.getArray(&count);
1209 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001210
Chris Craik16ecda52013-03-29 10:59:59 -07001211 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001212 const float texX = 1.0f / float(layer->getWidth());
1213 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001214 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001215
Romain Guy8ce00302013-01-15 18:51:42 -08001216 setupDraw();
1217
1218 // We must get (and therefore bind) the region mesh buffer
1219 // after we setup drawing in case we need to mess with the
1220 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001221 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001222 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001223
Romain Guy7230a742011-01-10 22:26:16 -08001224 setupDrawWithTexture();
1225 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001226 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001227 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001228 setupDrawProgram();
1229 setupDrawDirtyRegionsDisabled();
1230 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001231 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001232 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001233 if (currentTransform().isPureTranslate()) {
1234 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1235 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001236
Romain Guyd21b6e12011-11-30 20:21:23 -08001237 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001238 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1239 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001240 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001241 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1242 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001243 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001244
1245 for (size_t i = 0; i < count; i++) {
1246 const android::Rect* r = &rects[i];
1247
1248 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001249 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001250 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001251 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001252
1253 // TODO: Reject quads outside of the clip
1254 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1255 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1256 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1257 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1258
1259 numQuads++;
1260
Romain Guy31e08e92013-06-18 15:53:53 -07001261 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001262 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1263 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001264 numQuads = 0;
1265 mesh = mCaches.getRegionMesh();
1266 }
1267 }
1268
1269 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001270 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1271 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001272 }
1273
Romain Guy5b3b3522010-10-27 18:57:51 -07001274#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001275 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001276#endif
1277
1278 layer->region.clear();
1279 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001280}
1281
Romain Guy3a3133d2011-02-01 22:59:58 -08001282#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001283void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001284 size_t count;
1285 const android::Rect* rects = region.getArray(&count);
1286
1287 uint32_t colors[] = {
1288 0x7fff0000, 0x7f00ff00,
1289 0x7f0000ff, 0x7fff00ff,
1290 };
1291
1292 int offset = 0;
1293 int32_t top = rects[0].top;
1294
1295 for (size_t i = 0; i < count; i++) {
1296 if (top != rects[i].top) {
1297 offset ^= 0x2;
1298 top = rects[i].top;
1299 }
1300
1301 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1302 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1303 SkXfermode::kSrcOver_Mode);
1304 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001305}
Chris Craike63f7c622013-10-17 10:30:55 -07001306#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001307
Romain Guy8ce00302013-01-15 18:51:42 -08001308void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1309 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001310 Vector<float> rects;
1311
1312 SkRegion::Iterator it(region);
1313 while (!it.done()) {
1314 const SkIRect& r = it.rect();
1315 rects.push(r.fLeft);
1316 rects.push(r.fTop);
1317 rects.push(r.fRight);
1318 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001319 it.next();
1320 }
1321
Romain Guy448455f2013-07-22 13:57:50 -07001322 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001323}
1324
Romain Guy5b3b3522010-10-27 18:57:51 -07001325void OpenGLRenderer::dirtyLayer(const float left, const float top,
1326 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001327 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001328 Rect bounds(left, top, right, bottom);
1329 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001330 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001331 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001332}
1333
1334void OpenGLRenderer::dirtyLayer(const float left, const float top,
1335 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001336 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001337 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001338 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001339 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001340}
1341
1342void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001343 if (bounds.intersect(*mSnapshot->clipRect)) {
1344 bounds.snapToPixelBoundaries();
1345 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1346 if (!dirty.isEmpty()) {
1347 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001348 }
1349 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001350}
1351
Romain Guy448455f2013-07-22 13:57:50 -07001352void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1353 GLsizei elementsCount = quadsCount * 6;
1354 while (elementsCount > 0) {
1355 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1356
Romain Guy3380cfd2013-08-15 16:57:57 -07001357 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001358 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1359
1360 elementsCount -= drawCount;
1361 // Though there are 4 vertices in a quad, we use 6 indices per
1362 // quad to draw with GL_TRIANGLES
1363 mesh += (drawCount / 6) * 4;
1364 }
1365}
1366
Romain Guy54be1cd2011-06-13 19:04:27 -07001367void OpenGLRenderer::clearLayerRegions() {
1368 const size_t count = mLayers.size();
1369 if (count == 0) return;
1370
1371 if (!mSnapshot->isIgnored()) {
1372 // Doing several glScissor/glClear here can negatively impact
1373 // GPUs with a tiler architecture, instead we draw quads with
1374 // the Clear blending mode
1375
1376 // The list contains bounds that have already been clipped
1377 // against their initial clip rect, and the current clip
1378 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001379 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001380
Romain Guy448455f2013-07-22 13:57:50 -07001381 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001382 Vertex* vertex = mesh;
1383
1384 for (uint32_t i = 0; i < count; i++) {
1385 Rect* bounds = mLayers.itemAt(i);
1386
Romain Guy54be1cd2011-06-13 19:04:27 -07001387 Vertex::set(vertex++, bounds->left, bounds->top);
1388 Vertex::set(vertex++, bounds->right, bounds->top);
1389 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001390 Vertex::set(vertex++, bounds->right, bounds->bottom);
1391
1392 delete bounds;
1393 }
Romain Guye67307c2013-02-11 18:01:20 -08001394 // We must clear the list of dirty rects before we
1395 // call setupDraw() to prevent stencil setup to do
1396 // the same thing again
1397 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001398
1399 setupDraw(false);
1400 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1401 setupDrawBlending(true, SkXfermode::kClear_Mode);
1402 setupDrawProgram();
1403 setupDrawPureColorUniforms();
1404 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1405
Romain Guy448455f2013-07-22 13:57:50 -07001406 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001407
1408 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001409 } else {
1410 for (uint32_t i = 0; i < count; i++) {
1411 delete mLayers.itemAt(i);
1412 }
Romain Guye67307c2013-02-11 18:01:20 -08001413 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001414 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001415}
1416
Romain Guybd6b79b2010-06-26 00:13:53 -07001417///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001418// State Deferral
1419///////////////////////////////////////////////////////////////////////////////
1420
Chris Craikff785832013-03-08 13:12:16 -08001421bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001422 const Rect& currentClip = *(mSnapshot->clipRect);
1423 const mat4& currentMatrix = *(mSnapshot->transform);
1424
Chris Craikff785832013-03-08 13:12:16 -08001425 if (stateDeferFlags & kStateDeferFlag_Draw) {
1426 // state has bounds initialized in local coordinates
1427 if (!state.mBounds.isEmpty()) {
1428 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001429 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001430 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1431 // is used, it should more closely duplicate the quickReject logic (in how it uses
1432 // snapToPixelBoundaries)
1433
Chris Craik28ce94a2013-05-31 11:38:03 -07001434 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001435 // quick rejected
1436 return true;
1437 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001438
Chris Craika02c4ed2013-06-14 13:43:58 -07001439 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001440 if (!currentClip.contains(state.mBounds)) {
1441 int& flags = state.mClipSideFlags;
1442 // op partially clipped, so record which sides are clipped for clip-aware merging
1443 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1444 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1445 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1446 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1447 }
1448 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001449 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001450 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1451 // overdraw avoidance (since we don't know what it overlaps)
1452 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001453 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001454 }
1455 }
1456
Chris Craik527a3aa2013-03-04 10:19:31 -08001457 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1458 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001459 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001460 }
1461
Chris Craik7273daa2013-03-28 11:25:24 -07001462 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1463 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001464 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001465 state.mDrawModifiers = mDrawModifiers;
1466 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001467 return false;
1468}
1469
Chris Craik527a3aa2013-03-04 10:19:31 -08001470void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001471 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001472 mDrawModifiers = state.mDrawModifiers;
1473 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001474
Chris Craik527a3aa2013-03-04 10:19:31 -08001475 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001476 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1477 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001478 dirtyClip();
1479 }
Chris Craikc3566d02013-02-04 16:16:33 -08001480}
1481
Chris Craik28ce94a2013-05-31 11:38:03 -07001482/**
1483 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1484 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1485 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1486 *
1487 * This method should be called when restoreDisplayState() won't be restoring the clip
1488 */
1489void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1490 if (clipRect != NULL) {
1491 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1492 } else {
1493 mSnapshot->setClip(0, 0, mWidth, mHeight);
1494 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001495 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001496 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001497}
1498
Chris Craikc3566d02013-02-04 16:16:33 -08001499///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001500// Transforms
1501///////////////////////////////////////////////////////////////////////////////
1502
1503void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001504 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001505}
1506
1507void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001508 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001509}
1510
1511void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001512 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001513}
1514
Romain Guy807daf72011-01-18 11:19:19 -08001515void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001516 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001517}
1518
Romain Guyf6a11b82010-06-23 17:47:49 -07001519void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001520 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001521 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001522 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001523 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001524 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001525}
1526
Chris Craikb98a0162013-02-21 11:30:22 -08001527bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001528 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001529}
1530
Romain Guyf6a11b82010-06-23 17:47:49 -07001531void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001532 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001533}
1534
1535void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001536 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001537 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001538 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001539 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001540}
1541
1542///////////////////////////////////////////////////////////////////////////////
1543// Clipping
1544///////////////////////////////////////////////////////////////////////////////
1545
Romain Guybb9524b2010-06-22 18:56:38 -07001546void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001547 Rect clip(*mSnapshot->clipRect);
1548 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001549
Romain Guy8a4ac612012-07-17 17:32:48 -07001550 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1551 clip.getWidth(), clip.getHeight())) {
1552 mDirtyClip = false;
1553 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001554}
1555
Romain Guy8ce00302013-01-15 18:51:42 -08001556void OpenGLRenderer::ensureStencilBuffer() {
1557 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1558 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1559 // just hope we have one when hasLayer() returns false.
1560 if (hasLayer()) {
1561 attachStencilBufferToLayer(mSnapshot->layer);
1562 }
1563}
1564
1565void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1566 // The layer's FBO is already bound when we reach this stage
1567 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001568 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1569 // is attached after we initiated tiling. We must turn it off,
1570 // attach the new render buffer then turn tiling back on
1571 endTiling();
1572
Romain Guy8d4aeb72013-02-12 16:08:55 -08001573 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001574 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001575 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001576
Romain Guyf735c8e2013-01-31 17:45:55 -08001577 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001578 }
1579}
1580
1581void OpenGLRenderer::setStencilFromClip() {
1582 if (!mCaches.debugOverdraw) {
1583 if (!mSnapshot->clipRegion->isEmpty()) {
1584 // NOTE: The order here is important, we must set dirtyClip to false
1585 // before any draw call to avoid calling back into this method
1586 mDirtyClip = false;
1587
1588 ensureStencilBuffer();
1589
1590 mCaches.stencil.enableWrite();
1591
1592 // Clear the stencil but first make sure we restrict drawing
1593 // to the region's bounds
1594 bool resetScissor = mCaches.enableScissor();
1595 if (resetScissor) {
1596 // The scissor was not set so we now need to update it
1597 setScissorFromClip();
1598 }
1599 mCaches.stencil.clear();
1600 if (resetScissor) mCaches.disableScissor();
1601
1602 // NOTE: We could use the region contour path to generate a smaller mesh
1603 // Since we are using the stencil we could use the red book path
1604 // drawing technique. It might increase bandwidth usage though.
1605
1606 // The last parameter is important: we are not drawing in the color buffer
1607 // so we don't want to dirty the current layer, if any
1608 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1609
1610 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001611
1612 // Draw the region used to generate the stencil if the appropriate debug
1613 // mode is enabled
1614 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1615 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1616 }
Romain Guy8ce00302013-01-15 18:51:42 -08001617 } else {
1618 mCaches.stencil.disable();
1619 }
1620 }
1621}
1622
Romain Guy9d5316e2010-06-24 19:30:36 -07001623const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001624 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001625}
1626
Chris Craik39a908c2013-06-13 14:39:01 -07001627bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001628 bool snapOut, bool* clipRequired) {
Chris Craik39a908c2013-06-13 14:39:01 -07001629 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001630 return true;
1631 }
1632
1633 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001634 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001635 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001636
1637 Rect clipRect(*mSnapshot->clipRect);
1638 clipRect.snapToPixelBoundaries();
1639
Chris Craik39a908c2013-06-13 14:39:01 -07001640 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001641
Chris Craik39a908c2013-06-13 14:39:01 -07001642 if (clipRequired) *clipRequired = !clipRect.contains(r);
1643 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001644}
1645
Romain Guy672433d2013-01-04 19:05:13 -08001646bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
1647 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001648 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1649 // the final mapped rect to ensure correct clipping behavior for the ramp.
1650 bool snapOut = paint->isAntiAlias();
1651
Chris Craikcb4d6002012-09-25 12:00:29 -07001652 if (paint->getStyle() != SkPaint::kFill_Style) {
1653 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001654 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001655 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001656 return quickReject(left, top, right, bottom, snapOut);
Chris Craikcb4d6002012-09-25 12:00:29 -07001657 }
1658}
1659
Chris Craik5e49b302013-07-30 19:05:20 -07001660bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001661 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001662 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001663 return true;
1664 }
1665
Chris Craik39a908c2013-06-13 14:39:01 -07001666 if (!isDeferred()) {
1667 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001668 }
Chris Craik39a908c2013-06-13 14:39:01 -07001669 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001670}
1671
Romain Guy8ce00302013-01-15 18:51:42 -08001672void OpenGLRenderer::debugClip() {
1673#if DEBUG_CLIP_REGIONS
1674 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1675 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1676 }
1677#endif
1678}
1679
Romain Guy079ba2c2010-07-16 14:12:24 -07001680bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001681 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001682 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1683 if (clipped) {
1684 dirtyClip();
1685 }
1686 return !mSnapshot->clipRect->isEmpty();
1687 }
1688
1689 SkPath path;
1690 path.addRect(left, top, right, bottom);
1691
Romain Guyb7b93e02013-08-01 15:29:25 -07001692 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001693}
1694
1695bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1696 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001697 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001698
1699 SkPath transformed;
1700 path->transform(transform, &transformed);
1701
1702 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001703 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1704 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001705 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001706 if (mSnapshot->previous == mFirstSnapshot) {
1707 clip.setRect(0, 0, mWidth, mHeight);
1708 } else {
1709 Rect* bounds = mSnapshot->previous->clipRect;
1710 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1711 }
Romain Guy8ce00302013-01-15 18:51:42 -08001712 }
1713
1714 SkRegion region;
1715 region.setPath(transformed, clip);
1716
1717 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001718 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001719 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001720 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001721 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001722}
1723
Romain Guy735738c2012-12-03 12:34:51 -08001724bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001725 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1726 if (clipped) {
1727 dirtyClip();
1728 }
1729 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001730}
1731
Chet Haasea23eed82012-04-12 15:19:04 -07001732Rect* OpenGLRenderer::getClipRect() {
1733 return mSnapshot->clipRect;
1734}
1735
Romain Guyf6a11b82010-06-23 17:47:49 -07001736///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001737// Drawing commands
1738///////////////////////////////////////////////////////////////////////////////
1739
Romain Guy54be1cd2011-06-13 19:04:27 -07001740void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001741 // TODO: It would be best if we could do this before quickReject()
1742 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001743 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001744 // Make sure setScissor & setStencil happen at the beginning of
1745 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001746 if (mDirtyClip) {
1747 if (mCaches.scissorEnabled) {
1748 setScissorFromClip();
1749 }
Romain Guy8ce00302013-01-15 18:51:42 -08001750 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001751 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001752
Romain Guy70ca14e2010-12-13 18:24:33 -08001753 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001754
Romain Guy70ca14e2010-12-13 18:24:33 -08001755 mSetShaderColor = false;
1756 mColorSet = false;
1757 mColorA = mColorR = mColorG = mColorB = 0.0f;
1758 mTextureUnit = 0;
1759 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001760
1761 // Enable debug highlight when what we're about to draw is tested against
1762 // the stencil buffer and if stencil highlight debugging is on
1763 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1764 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1765 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001766
1767 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001768}
1769
1770void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1771 mDescription.hasTexture = true;
1772 mDescription.hasAlpha8Texture = isAlpha8;
1773}
1774
Romain Guyff316ec2013-02-13 18:39:43 -08001775void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1776 mDescription.hasTexture = true;
1777 mDescription.hasColors = true;
1778 mDescription.hasAlpha8Texture = isAlpha8;
1779}
1780
Romain Guyaa6c24c2011-04-28 18:40:04 -07001781void OpenGLRenderer::setupDrawWithExternalTexture() {
1782 mDescription.hasExternalTexture = true;
1783}
1784
Romain Guy15bc6432011-12-13 13:11:32 -08001785void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001786 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001787}
1788
Chris Craik710f46d2012-09-17 17:25:49 -07001789void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001790 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001791}
1792
Romain Guy8d0d4782010-12-14 20:13:35 -08001793void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1794 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001795 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1796 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1797 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001798 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001799 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001800}
1801
Romain Guy86568192010-12-14 15:55:39 -08001802void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1803 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001804 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1805 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1806 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001807 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001808 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001809}
1810
Romain Guy41210632012-07-16 17:04:24 -07001811void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1812 mCaches.fontRenderer->describe(mDescription, paint);
1813}
1814
Romain Guy70ca14e2010-12-13 18:24:33 -08001815void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1816 mColorA = a;
1817 mColorR = r;
1818 mColorG = g;
1819 mColorB = b;
1820 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001821 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001822}
1823
1824void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001825 if (mDrawModifiers.mShader) {
1826 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001827 }
1828}
1829
1830void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001831 if (mDrawModifiers.mColorFilter) {
1832 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001833 }
1834}
1835
Romain Guyf09ef512011-05-27 11:43:46 -07001836void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1837 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1838 mColorA = 1.0f;
1839 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001840 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001841 }
1842}
1843
Romain Guy70ca14e2010-12-13 18:24:33 -08001844void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001845 // When the blending mode is kClear_Mode, we need to use a modulate color
1846 // argb=1,0,0,0
1847 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001848 bool blend = (mColorSet && mColorA < 1.0f) ||
1849 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1850 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001851}
1852
1853void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001854 // When the blending mode is kClear_Mode, we need to use a modulate color
1855 // argb=1,0,0,0
1856 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001857 blend |= (mColorSet && mColorA < 1.0f) ||
1858 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1859 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1860 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001861}
1862
1863void OpenGLRenderer::setupDrawProgram() {
1864 useProgram(mCaches.programCache.get(mDescription));
1865}
1866
1867void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1868 mTrackDirtyRegions = false;
1869}
1870
1871void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1872 bool ignoreTransform) {
1873 mModelView.loadTranslate(left, top, 0.0f);
1874 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001875 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1876 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001877 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001878 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001879 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1880 }
1881}
1882
Chet Haase8a5cc922011-04-26 07:28:09 -07001883void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001884 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001885}
1886
Romain Guy70ca14e2010-12-13 18:24:33 -08001887void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1888 bool ignoreTransform, bool ignoreModelView) {
1889 if (!ignoreModelView) {
1890 mModelView.loadTranslate(left, top, 0.0f);
1891 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001892 } else {
1893 mModelView.loadIdentity();
1894 }
Romain Guy86568192010-12-14 15:55:39 -08001895 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1896 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001897 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001898 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001899 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001900 }
1901 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001902 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001903 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1904 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001905}
1906
1907void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001908 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001909 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1910 }
1911}
1912
Romain Guy86568192010-12-14 15:55:39 -08001913void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001914 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001915 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001916 }
1917}
1918
Romain Guy70ca14e2010-12-13 18:24:33 -08001919void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001920 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001921 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001922 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001923 }
Chris Craikc3566d02013-02-04 16:16:33 -08001924 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1925 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001926 }
1927}
1928
Romain Guy8d0d4782010-12-14 20:13:35 -08001929void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001930 if (mDrawModifiers.mShader) {
1931 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001932 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001933 }
1934}
1935
Romain Guy70ca14e2010-12-13 18:24:33 -08001936void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001937 if (mDrawModifiers.mColorFilter) {
1938 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001939 }
1940}
1941
Romain Guy41210632012-07-16 17:04:24 -07001942void OpenGLRenderer::setupDrawTextGammaUniforms() {
1943 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1944}
1945
Romain Guy70ca14e2010-12-13 18:24:33 -08001946void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001947 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001948 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001949 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001950}
1951
1952void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001953 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001954 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001955 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001956}
1957
Romain Guyaa6c24c2011-04-28 18:40:04 -07001958void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1959 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001960 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001961 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001962}
1963
Romain Guy8f0095c2011-05-02 17:24:22 -07001964void OpenGLRenderer::setupDrawTextureTransform() {
1965 mDescription.hasTextureTransform = true;
1966}
1967
1968void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001969 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1970 GL_FALSE, &transform.data[0]);
1971}
1972
Romain Guy70ca14e2010-12-13 18:24:33 -08001973void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001974 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001975 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001976 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001977 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001978 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001979 }
Romain Guyd71dd362011-12-12 19:03:35 -08001980
Chris Craikcb4d6002012-09-25 12:00:29 -07001981 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001982 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001983 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001984 }
1985
1986 mCaches.unbindIndicesBuffer();
1987}
1988
Romain Guyff316ec2013-02-13 18:39:43 -08001989void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1990 bool force = mCaches.unbindMeshBuffer();
1991 GLsizei stride = sizeof(ColorTextureVertex);
1992
1993 mCaches.bindPositionVertexPointer(force, vertices, stride);
1994 if (mCaches.currentProgram->texCoords >= 0) {
1995 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1996 }
1997 int slot = mCaches.currentProgram->getAttrib("colors");
1998 if (slot >= 0) {
1999 glEnableVertexAttribArray(slot);
2000 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2001 }
2002
2003 mCaches.unbindIndicesBuffer();
2004}
2005
Romain Guy3b748a42013-04-17 18:54:38 -07002006void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2007 bool force = false;
2008 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2009 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2010 // use the default VBO found in Caches
2011 if (!vertices || vbo) {
2012 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2013 } else {
2014 force = mCaches.unbindMeshBuffer();
2015 }
2016 mCaches.bindIndicesBuffer();
2017
Chris Craikcb4d6002012-09-25 12:00:29 -07002018 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002019 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002020 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002021 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002022}
2023
Romain Guy448455f2013-07-22 13:57:50 -07002024void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002025 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002026 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002027 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002028}
2029
Romain Guy70ca14e2010-12-13 18:24:33 -08002030///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002031// Drawing
2032///////////////////////////////////////////////////////////////////////////////
2033
Chris Craikff785832013-03-08 13:12:16 -08002034status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2035 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002036 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002037 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2038 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002039 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002040 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002041 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002042 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2043 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002044 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002045 }
2046
Chris Craik28ce94a2013-05-31 11:38:03 -07002047 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2048 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002049 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2050 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002051
2052 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002053 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002054
Chet Haase58d110a2013-04-10 07:43:29 -07002055 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002056 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002057
Romain Guy65549432012-03-26 16:45:05 -07002058 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002059}
2060
Chris Craikc3566d02013-02-04 16:16:33 -08002061void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002062 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002063 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002064 }
2065}
2066
Romain Guya168d732011-03-18 16:50:13 -07002067void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2068 int alpha;
2069 SkXfermode::Mode mode;
2070 getAlphaAndMode(paint, &alpha, &mode);
2071
Romain Guy886b2752013-01-04 12:26:18 -08002072 int color = paint != NULL ? paint->getColor() : 0;
2073
Romain Guya168d732011-03-18 16:50:13 -07002074 float x = left;
2075 float y = top;
2076
Romain Guy886b2752013-01-04 12:26:18 -08002077 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2078
Romain Guya168d732011-03-18 16:50:13 -07002079 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002080 if (currentTransform().isPureTranslate()) {
2081 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2082 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002083 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002084
2085 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002086 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002087 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002088 }
2089
Romain Guy3b748a42013-04-17 18:54:38 -07002090 // No need to check for a UV mapper on the texture object, only ARGB_8888
2091 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002092 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002093 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2094 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002095}
2096
Romain Guy03c00b52013-06-20 18:30:28 -07002097/**
2098 * Important note: this method is intended to draw batches of bitmaps and
2099 * will not set the scissor enable or dirty the current layer, if any.
2100 * The caller is responsible for properly dirtying the current layer.
2101 */
Romain Guy55b6f952013-06-27 15:27:09 -07002102status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002103 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002104 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002105 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002106 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002107
Chris Craik527a3aa2013-03-04 10:19:31 -08002108 const AutoTexture autoCleanup(texture);
2109
2110 int alpha;
2111 SkXfermode::Mode mode;
2112 getAlphaAndMode(paint, &alpha, &mode);
2113
2114 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002115 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002116
2117 const float x = (int) floorf(bounds.left + 0.5f);
2118 const float y = (int) floorf(bounds.top + 0.5f);
2119 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2120 int color = paint != NULL ? paint->getColor() : 0;
2121 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2122 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002123 &vertices[0].x, &vertices[0].u,
Romain Guy03c00b52013-06-20 18:30:28 -07002124 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002125 } else {
2126 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2127 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002128 &vertices[0].x, &vertices[0].u,
Romain Guy03c00b52013-06-20 18:30:28 -07002129 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002130 }
2131
2132 return DrawGlInfo::kStatusDrew;
2133}
2134
Chet Haase48659092012-05-31 15:21:51 -07002135status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002136 const float right = left + bitmap->width();
2137 const float bottom = top + bitmap->height();
2138
2139 if (quickReject(left, top, right, 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 Guy211370f2012-02-01 16:10:55 -08002148 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002149 drawAlphaBitmap(texture, left, top, paint);
2150 } else {
2151 drawTextureRect(left, top, right, bottom, texture, paint);
2152 }
Chet Haase48659092012-05-31 15:21:51 -07002153
2154 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002155}
2156
Chet Haase48659092012-05-31 15:21:51 -07002157status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002158 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2159 const mat4 transform(*matrix);
2160 transform.mapRect(r);
2161
Romain Guy6926c722010-07-12 20:20:03 -07002162 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002163 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002164 }
2165
Romain Guya1d3c912011-12-13 14:55:06 -08002166 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002167 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002168 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002169 const AutoTexture autoCleanup(texture);
2170
Romain Guy5b3b3522010-10-27 18:57:51 -07002171 // This could be done in a cheaper way, all we need is pass the matrix
2172 // to the vertex shader. The save/restore is a bit overkill.
2173 save(SkCanvas::kMatrix_SaveFlag);
2174 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002175 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2176 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2177 } else {
2178 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2179 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002180 restore();
Chet Haase48659092012-05-31 15:21:51 -07002181
2182 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002183}
2184
Chet Haase48659092012-05-31 15:21:51 -07002185status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002186 const float right = left + bitmap->width();
2187 const float bottom = top + bitmap->height();
2188
2189 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002190 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002191 }
2192
2193 mCaches.activeTexture(0);
2194 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2195 const AutoTexture autoCleanup(texture);
2196
Romain Guy886b2752013-01-04 12:26:18 -08002197 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2198 drawAlphaBitmap(texture, left, top, paint);
2199 } else {
2200 drawTextureRect(left, top, right, bottom, texture, paint);
2201 }
Chet Haase48659092012-05-31 15:21:51 -07002202
2203 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002204}
2205
Chet Haase48659092012-05-31 15:21:51 -07002206status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002207 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002208 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002209 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002210 }
2211
Chris Craik39a908c2013-06-13 14:39:01 -07002212 // TODO: use quickReject on bounds from vertices
2213 mCaches.enableScissor();
2214
Romain Guyb18d2d02011-02-10 15:52:54 -08002215 float left = FLT_MAX;
2216 float top = FLT_MAX;
2217 float right = FLT_MIN;
2218 float bottom = FLT_MIN;
2219
Romain Guya92bb4d2012-10-16 11:08:44 -07002220 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002221
Romain Guyff316ec2013-02-13 18:39:43 -08002222 ColorTextureVertex mesh[count];
2223 ColorTextureVertex* vertex = mesh;
2224
2225 bool cleanupColors = false;
2226 if (!colors) {
2227 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2228 colors = new int[colorsCount];
2229 memset(colors, 0xff, colorsCount * sizeof(int));
2230 cleanupColors = true;
2231 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002232
Romain Guy3b748a42013-04-17 18:54:38 -07002233 mCaches.activeTexture(0);
2234 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2235 const UvMapper& mapper(getMapper(texture));
2236
Romain Guy5a7b4662011-01-20 19:09:30 -08002237 for (int32_t y = 0; y < meshHeight; y++) {
2238 for (int32_t x = 0; x < meshWidth; x++) {
2239 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2240
2241 float u1 = float(x) / meshWidth;
2242 float u2 = float(x + 1) / meshWidth;
2243 float v1 = float(y) / meshHeight;
2244 float v2 = float(y + 1) / meshHeight;
2245
Romain Guy3b748a42013-04-17 18:54:38 -07002246 mapper.map(u1, v1, u2, v2);
2247
Romain Guy5a7b4662011-01-20 19:09:30 -08002248 int ax = i + (meshWidth + 1) * 2;
2249 int ay = ax + 1;
2250 int bx = i;
2251 int by = bx + 1;
2252 int cx = i + 2;
2253 int cy = cx + 1;
2254 int dx = i + (meshWidth + 1) * 2 + 2;
2255 int dy = dx + 1;
2256
Romain Guyff316ec2013-02-13 18:39:43 -08002257 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2258 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2259 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002260
Romain Guyff316ec2013-02-13 18:39:43 -08002261 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2262 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2263 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002264
Romain Guya92bb4d2012-10-16 11:08:44 -07002265 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2266 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2267 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2268 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002269 }
2270 }
2271
Romain Guya92bb4d2012-10-16 11:08:44 -07002272 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002273 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002274 return DrawGlInfo::kStatusDone;
2275 }
2276
Romain Guyff316ec2013-02-13 18:39:43 -08002277 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002278 texture = mCaches.textureCache.get(bitmap);
2279 if (!texture) {
2280 if (cleanupColors) delete[] colors;
2281 return DrawGlInfo::kStatusDone;
2282 }
Romain Guyff316ec2013-02-13 18:39:43 -08002283 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002284 const AutoTexture autoCleanup(texture);
2285
2286 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2287 texture->setFilter(FILTER(paint), true);
2288
2289 int alpha;
2290 SkXfermode::Mode mode;
2291 getAlphaAndMode(paint, &alpha, &mode);
2292
Romain Guyff316ec2013-02-13 18:39:43 -08002293 float a = alpha / 255.0f;
2294
Romain Guya92bb4d2012-10-16 11:08:44 -07002295 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002296 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002297 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002298
Romain Guyff316ec2013-02-13 18:39:43 -08002299 setupDraw();
2300 setupDrawWithTextureAndColor();
2301 setupDrawColor(a, a, a, a);
2302 setupDrawColorFilter();
2303 setupDrawBlending(true, mode, false);
2304 setupDrawProgram();
2305 setupDrawDirtyRegionsDisabled();
2306 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2307 setupDrawTexture(texture->id);
2308 setupDrawPureColorUniforms();
2309 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002310 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002311
2312 glDrawArrays(GL_TRIANGLES, 0, count);
2313
Romain Guyff316ec2013-02-13 18:39:43 -08002314 int slot = mCaches.currentProgram->getAttrib("colors");
2315 if (slot >= 0) {
2316 glDisableVertexAttribArray(slot);
2317 }
2318
2319 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002320
2321 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002322}
2323
Chet Haase48659092012-05-31 15:21:51 -07002324status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002325 float srcLeft, float srcTop, float srcRight, float srcBottom,
2326 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002327 SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002328 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002329 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002330 }
2331
Romain Guya1d3c912011-12-13 14:55:06 -08002332 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002333 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002334 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002335 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002336
Romain Guy8ba548f2010-06-30 19:21:21 -07002337 const float width = texture->width;
2338 const float height = texture->height;
2339
Romain Guy3b748a42013-04-17 18:54:38 -07002340 float u1 = fmax(0.0f, srcLeft / width);
2341 float v1 = fmax(0.0f, srcTop / height);
2342 float u2 = fmin(1.0f, srcRight / width);
2343 float v2 = fmin(1.0f, srcBottom / height);
2344
2345 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002346
Romain Guy03750a02010-10-18 14:06:08 -07002347 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002348 resetDrawTextureTexCoords(u1, v1, u2, v2);
2349
Romain Guy03750a02010-10-18 14:06:08 -07002350 int alpha;
2351 SkXfermode::Mode mode;
2352 getAlphaAndMode(paint, &alpha, &mode);
2353
Romain Guyd21b6e12011-11-30 20:21:23 -08002354 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2355
Romain Guy886b2752013-01-04 12:26:18 -08002356 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2357 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002358
Romain Guy886b2752013-01-04 12:26:18 -08002359 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2360 // Apply a scale transform on the canvas only when a shader is in use
2361 // Skia handles the ratio between the dst and src rects as a scale factor
2362 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002363 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002364 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002365
Romain Guy3b753822013-03-05 10:27:35 -08002366 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2367 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2368 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002369
2370 dstRight = x + (dstRight - dstLeft);
2371 dstBottom = y + (dstBottom - dstTop);
2372
2373 dstLeft = x;
2374 dstTop = y;
2375
2376 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2377 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002378 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002379 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002380 }
2381
2382 if (CC_UNLIKELY(useScaleTransform)) {
2383 save(SkCanvas::kMatrix_SaveFlag);
2384 translate(dstLeft, dstTop);
2385 scale(scaleX, scaleY);
2386
2387 dstLeft = 0.0f;
2388 dstTop = 0.0f;
2389
2390 dstRight = srcRight - srcLeft;
2391 dstBottom = srcBottom - srcTop;
2392 }
2393
2394 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2395 int color = paint ? paint->getColor() : 0;
2396 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2397 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002398 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002399 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2400 } else {
2401 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2402 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002403 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002404 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2405 }
2406
2407 if (CC_UNLIKELY(useScaleTransform)) {
2408 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002409 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002410
2411 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002412
2413 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002414}
2415
Romain Guy3b748a42013-04-17 18:54:38 -07002416status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002417 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002418 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002419 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002420 }
2421
Romain Guy4c2547f2013-06-11 16:19:24 -07002422 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002423 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2424 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002425
Romain Guy03c00b52013-06-20 18:30:28 -07002426 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002427}
2428
Romain Guy03c00b52013-06-20 18:30:28 -07002429status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2430 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002431 if (quickReject(left, top, right, bottom)) {
2432 return DrawGlInfo::kStatusDone;
2433 }
2434
Romain Guy211370f2012-02-01 16:10:55 -08002435 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002436 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002437 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002438 if (!texture) return DrawGlInfo::kStatusDone;
2439 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002440
Romain Guya4adcf02013-02-28 12:15:35 -08002441 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2442 texture->setFilter(GL_LINEAR, true);
2443
Romain Guy03c00b52013-06-20 18:30:28 -07002444 int alpha;
2445 SkXfermode::Mode mode;
2446 getAlphaAndMode(paint, &alpha, &mode);
2447
Romain Guy3b753822013-03-05 10:27:35 -08002448 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002449 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002450 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002451 const float offsetX = left + currentTransform().getTranslateX();
2452 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002453 const size_t count = mesh->quads.size();
2454 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002455 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002456 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002457 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2458 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2459 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002460 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002461 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002462 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002463 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002464 }
2465 }
2466
Romain Guy211370f2012-02-01 16:10:55 -08002467 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002468 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2469 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002470
Romain Guy3b748a42013-04-17 18:54:38 -07002471 right = x + right - left;
2472 bottom = y + bottom - top;
2473 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2474 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2475 GL_TRIANGLES, mesh->indexCount, false, true,
2476 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002477 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002478 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2479 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2480 GL_TRIANGLES, mesh->indexCount, false, false,
2481 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002482 }
Romain Guy054dc182010-10-15 17:55:25 -07002483 }
Chet Haase48659092012-05-31 15:21:51 -07002484
2485 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002486}
2487
Romain Guy03c00b52013-06-20 18:30:28 -07002488/**
2489 * Important note: this method is intended to draw batches of 9-patch objects and
2490 * will not set the scissor enable or dirty the current layer, if any.
2491 * The caller is responsible for properly dirtying the current layer.
2492 */
2493status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2494 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2495 mCaches.activeTexture(0);
2496 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2497 if (!texture) return DrawGlInfo::kStatusDone;
2498 const AutoTexture autoCleanup(texture);
2499
2500 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2501 texture->setFilter(GL_LINEAR, true);
2502
2503 int alpha;
2504 SkXfermode::Mode mode;
2505 getAlphaAndMode(paint, &alpha, &mode);
2506
2507 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002508 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Romain Guy03c00b52013-06-20 18:30:28 -07002509 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2510
2511 return DrawGlInfo::kStatusDrew;
2512}
2513
Chris Craik65cd6122012-12-10 17:56:27 -08002514status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2515 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002516 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002517 // no vertices to draw
2518 return DrawGlInfo::kStatusDone;
2519 }
2520
Chris Craikcb4d6002012-09-25 12:00:29 -07002521 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002522 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2523 bool isAA = paint->isAntiAlias();
2524
Chris Craik710f46d2012-09-17 17:25:49 -07002525 setupDraw();
2526 setupDrawNoTexture();
2527 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002528 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2529 setupDrawColorFilter();
2530 setupDrawShader();
2531 setupDrawBlending(isAA, mode);
2532 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002533 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002534 setupDrawColorUniforms();
2535 setupDrawColorFilterUniforms();
2536 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002537
Chris Craik710f46d2012-09-17 17:25:49 -07002538 void* vertices = vertexBuffer.getBuffer();
2539 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002540 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002541 mCaches.resetTexCoordsVertexPointer();
2542 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002543
Chris Craik710f46d2012-09-17 17:25:49 -07002544 int alphaSlot = -1;
2545 if (isAA) {
2546 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2547 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002548
Chris Craik710f46d2012-09-17 17:25:49 -07002549 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002550 glEnableVertexAttribArray(alphaSlot);
2551 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002552 }
Romain Guy04299382012-07-18 17:15:41 -07002553
Chris Craik6d29c8d2013-05-08 18:35:44 -07002554 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002555
Chris Craik710f46d2012-09-17 17:25:49 -07002556 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002557 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002558 }
Chris Craik65cd6122012-12-10 17:56:27 -08002559
2560 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002561}
2562
2563/**
Chris Craik65cd6122012-12-10 17:56:27 -08002564 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2565 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2566 * screen space in all directions. However, instead of using a fragment shader to compute the
2567 * translucency of the color from its position, we simply use a varying parameter to define how far
2568 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2569 *
2570 * Doesn't yet support joins, caps, or path effects.
2571 */
2572status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2573 VertexBuffer vertexBuffer;
2574 // TODO: try clipping large paths to viewport
2575 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2576
Romain Guyc46d07a2013-03-15 19:06:39 -07002577 if (hasLayer()) {
2578 SkRect bounds = path.getBounds();
2579 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2580 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2581 }
Chris Craik65cd6122012-12-10 17:56:27 -08002582
2583 return drawVertexBuffer(vertexBuffer, paint);
2584}
2585
2586/**
2587 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2588 * and additional geometry for defining an alpha slope perimeter.
2589 *
2590 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2591 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2592 * in-shader alpha region, but found it to be taxing on some GPUs.
2593 *
2594 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2595 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002596 */
Chet Haase48659092012-05-31 15:21:51 -07002597status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002598 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002599
Chris Craik65cd6122012-12-10 17:56:27 -08002600 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002601
Chris Craik65cd6122012-12-10 17:56:27 -08002602 VertexBuffer buffer;
2603 SkRect bounds;
2604 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002605
2606 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2607 return DrawGlInfo::kStatusDone;
2608 }
2609
Romain Guy3b753822013-03-05 10:27:35 -08002610 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002611
Chris Craik65cd6122012-12-10 17:56:27 -08002612 bool useOffset = !paint->isAntiAlias();
2613 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002614}
2615
Chet Haase48659092012-05-31 15:21:51 -07002616status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002617 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002618
Chris Craik6d29c8d2013-05-08 18:35:44 -07002619 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002620
Chris Craik6d29c8d2013-05-08 18:35:44 -07002621 VertexBuffer buffer;
2622 SkRect bounds;
2623 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002624
Chris Craik6d29c8d2013-05-08 18:35:44 -07002625 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2626 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002627 }
2628
Chris Craik6d29c8d2013-05-08 18:35:44 -07002629 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002630
Chris Craik6d29c8d2013-05-08 18:35:44 -07002631 bool useOffset = !paint->isAntiAlias();
2632 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002633}
2634
Chet Haase48659092012-05-31 15:21:51 -07002635status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002636 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002637 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002638
Romain Guyae88e5e2010-10-22 17:49:18 -07002639 Rect& clip(*mSnapshot->clipRect);
2640 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002641
Romain Guy3d58c032010-07-14 16:34:53 -07002642 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002643
2644 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002645}
Romain Guy9d5316e2010-06-24 19:30:36 -07002646
Chet Haase48659092012-05-31 15:21:51 -07002647status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2648 SkPaint* paint) {
2649 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002650 const AutoTexture autoCleanup(texture);
2651
2652 const float x = left + texture->left - texture->offset;
2653 const float y = top + texture->top - texture->offset;
2654
2655 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002656
2657 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002658}
2659
Chet Haase48659092012-05-31 15:21:51 -07002660status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002661 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002662 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2663 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002664 return DrawGlInfo::kStatusDone;
2665 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002666
Chris Craikcb4d6002012-09-25 12:00:29 -07002667 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002668 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002669 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002670 right - left, bottom - top, rx, ry, p);
2671 return drawShape(left, top, texture, p);
2672 }
2673
2674 SkPath path;
2675 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002676 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2677 float outset = p->getStrokeWidth() / 2;
2678 rect.outset(outset, outset);
2679 rx += outset;
2680 ry += outset;
2681 }
Chris Craik710f46d2012-09-17 17:25:49 -07002682 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002683 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002684}
2685
Chris Craik710f46d2012-09-17 17:25:49 -07002686status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002687 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002688 x + radius, y + radius, p) ||
2689 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002690 return DrawGlInfo::kStatusDone;
2691 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002692 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002693 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002694 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002695 return drawShape(x - radius, y - radius, texture, p);
2696 }
2697
2698 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002699 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2700 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2701 } else {
2702 path.addCircle(x, y, radius);
2703 }
Chris Craik65cd6122012-12-10 17:56:27 -08002704 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002705}
Romain Guy01d58e42011-01-19 21:54:02 -08002706
Chet Haase48659092012-05-31 15:21:51 -07002707status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002708 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002709 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2710 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002711 return DrawGlInfo::kStatusDone;
2712 }
Romain Guy01d58e42011-01-19 21:54:02 -08002713
Chris Craikcb4d6002012-09-25 12:00:29 -07002714 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002715 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002716 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002717 return drawShape(left, top, texture, p);
2718 }
2719
2720 SkPath path;
2721 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002722 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2723 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2724 }
Chris Craik710f46d2012-09-17 17:25:49 -07002725 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002726 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002727}
2728
Chet Haase48659092012-05-31 15:21:51 -07002729status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002730 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002731 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2732 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002733 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002734 }
2735
Chris Craik780c1282012-10-04 14:10:49 -07002736 if (fabs(sweepAngle) >= 360.0f) {
2737 return drawOval(left, top, right, bottom, p);
2738 }
2739
2740 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002741 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002742 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002743 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002744 startAngle, sweepAngle, useCenter, p);
2745 return drawShape(left, top, texture, p);
2746 }
2747
2748 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2749 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2750 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2751 }
2752
2753 SkPath path;
2754 if (useCenter) {
2755 path.moveTo(rect.centerX(), rect.centerY());
2756 }
2757 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2758 if (useCenter) {
2759 path.close();
2760 }
Chris Craik65cd6122012-12-10 17:56:27 -08002761 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002762}
2763
Romain Guycf8675e2012-10-02 12:32:25 -07002764// See SkPaintDefaults.h
2765#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2766
Chet Haase48659092012-05-31 15:21:51 -07002767status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002768 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2769 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002770 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002771 }
2772
Chris Craik710f46d2012-09-17 17:25:49 -07002773 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002774 // only fill style is supported by drawConvexPath, since others have to handle joins
2775 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2776 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2777 mCaches.activeTexture(0);
2778 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002779 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002780 return drawShape(left, top, texture, p);
2781 }
2782
2783 SkPath path;
2784 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2785 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2786 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2787 }
2788 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002789 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002790 }
2791
Romain Guy3b753822013-03-05 10:27:35 -08002792 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002793 SkPath path;
2794 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002795 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002796 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002797 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002798 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002799 }
Romain Guyc7d53492010-06-25 13:41:57 -07002800}
Romain Guy9d5316e2010-06-24 19:30:36 -07002801
Raph Levien416a8472012-07-19 22:48:17 -07002802void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2803 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2804 float x, float y) {
2805 mCaches.activeTexture(0);
2806
2807 // NOTE: The drop shadow will not perform gamma correction
2808 // if shader-based correction is enabled
2809 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2810 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002811 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002812 // If the drop shadow exceeds the max texture size or couldn't be
2813 // allocated, skip drawing
2814 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002815 const AutoTexture autoCleanup(shadow);
2816
Chris Craikc3566d02013-02-04 16:16:33 -08002817 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2818 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002819
Chris Craikc3566d02013-02-04 16:16:33 -08002820 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2821 int shadowColor = mDrawModifiers.mShadowColor;
2822 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002823 shadowColor = 0xffffffff;
2824 }
2825
2826 setupDraw();
2827 setupDrawWithTexture(true);
2828 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2829 setupDrawColorFilter();
2830 setupDrawShader();
2831 setupDrawBlending(true, mode);
2832 setupDrawProgram();
2833 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2834 setupDrawTexture(shadow->id);
2835 setupDrawPureColorUniforms();
2836 setupDrawColorFilterUniforms();
2837 setupDrawShaderUniforms();
2838 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2839
2840 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2841}
2842
Romain Guy768bffc2013-02-27 13:50:45 -08002843bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2844 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2845 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2846}
2847
Chet Haase48659092012-05-31 15:21:51 -07002848status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002849 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002850 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002851 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002852 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002853
Romain Guy671d6cf2012-01-18 12:39:17 -08002854 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002855 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002856 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002857 }
2858
Chris Craik39a908c2013-06-13 14:39:01 -07002859 mCaches.enableScissor();
2860
Romain Guy671d6cf2012-01-18 12:39:17 -08002861 float x = 0.0f;
2862 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002863 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002864 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002865 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2866 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002867 }
2868
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002869 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002870 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002871
2872 int alpha;
2873 SkXfermode::Mode mode;
2874 getAlphaAndMode(paint, &alpha, &mode);
2875
Chris Craikc3566d02013-02-04 16:16:33 -08002876 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002877 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2878 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002879 }
2880
Romain Guy671d6cf2012-01-18 12:39:17 -08002881 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002882 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002883 if (pureTranslate && !linearFilter) {
2884 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2885 }
Romain Guy257ae352013-03-20 16:31:12 -07002886 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002887
2888 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2889 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2890
Romain Guy211370f2012-02-01 16:10:55 -08002891 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002892
Victoria Lease1e546812013-06-25 14:25:17 -07002893 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002894 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002895 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002896 if (hasActiveLayer) {
2897 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002898 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002899 }
2900 dirtyLayerUnchecked(bounds, getRegion());
2901 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002902 }
Chet Haase48659092012-05-31 15:21:51 -07002903
2904 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002905}
2906
Romain Guy624234f2013-03-05 16:43:31 -08002907mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2908 mat4 fontTransform;
2909 if (CC_LIKELY(transform.isPureTranslate())) {
2910 fontTransform = mat4::identity();
2911 } else {
2912 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002913 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002914 } else {
2915 float sx, sy;
2916 currentTransform().decomposeScale(sx, sy);
2917 fontTransform.loadScale(sx, sy, 1.0f);
2918 }
2919 }
2920 return fontTransform;
2921}
2922
Chris Craik41541822013-05-03 16:35:54 -07002923status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2924 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002925 DrawOpMode drawOpMode) {
2926
Chris Craik527a3aa2013-03-04 10:19:31 -08002927 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002928 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2929 // drawing as ops from DeferredDisplayList are already filtered for these
2930 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2931 quickReject(bounds)) {
2932 return DrawGlInfo::kStatusDone;
2933 }
Romain Guycac5fd32011-12-01 20:08:50 -08002934 }
2935
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002936 const float oldX = x;
2937 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002938
2939 const mat4& transform = currentTransform();
2940 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002941
Romain Guy211370f2012-02-01 16:10:55 -08002942 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002943 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2944 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002945 }
2946
Romain Guy86568192010-12-14 15:55:39 -08002947 int alpha;
2948 SkXfermode::Mode mode;
2949 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002950
Romain Guyc74f45a2013-02-26 19:10:14 -08002951 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2952
Chris Craikc3566d02013-02-04 16:16:33 -08002953 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002954 fontRenderer.setFont(paint, mat4::identity());
2955 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2956 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002957 }
2958
Romain Guy19d4dd82013-03-04 11:14:26 -08002959 const bool hasActiveLayer = hasLayer();
2960
Romain Guy624234f2013-03-05 16:43:31 -08002961 // We only pass a partial transform to the font renderer. That partial
2962 // matrix defines how glyphs are rasterized. Typically we want glyphs
2963 // to be rasterized at their final size on screen, which means the partial
2964 // matrix needs to take the scale factor into account.
2965 // When a partial matrix is used to transform glyphs during rasterization,
2966 // the mesh is generated with the inverse transform (in the case of scale,
2967 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2968 // apply the full transform matrix at draw time in the vertex shader.
2969 // Applying the full matrix in the shader is the easiest way to handle
2970 // rotation and perspective and allows us to always generated quads in the
2971 // font renderer which greatly simplifies the code, clipping in particular.
2972 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002973 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002974
Romain Guy6620c6d2010-12-06 18:07:02 -08002975 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002976 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002977 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002978
Romain Guy624234f2013-03-05 16:43:31 -08002979 // TODO: Implement better clipping for scaled/rotated text
2980 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002981 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 -07002982
Raph Levien996e57c2012-07-23 15:22:52 -07002983 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002984 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002985
2986 // don't call issuedrawcommand, do it at end of batch
2987 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002988 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002989 SkPaint paintCopy(*paint);
2990 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2991 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002992 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002993 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002994 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002995 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002996 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002997
Chris Craik527a3aa2013-03-04 10:19:31 -08002998 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002999 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003000 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003001 }
Chris Craik41541822013-05-03 16:35:54 -07003002 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003003 }
Romain Guy694b5192010-07-21 21:33:20 -07003004
Chris Craike63f7c622013-10-17 10:30:55 -07003005 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003006
3007 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003008}
3009
Chet Haase48659092012-05-31 15:21:51 -07003010status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003011 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003012 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003013 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003014 }
3015
Chris Craik39a908c2013-06-13 14:39:01 -07003016 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3017 mCaches.enableScissor();
3018
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003019 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003020 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003021 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003022
3023 int alpha;
3024 SkXfermode::Mode mode;
3025 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003026 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003027
Romain Guy97771732012-02-28 18:17:02 -08003028 const Rect* clip = &mSnapshot->getLocalClip();
3029 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 -08003030
Romain Guy97771732012-02-28 18:17:02 -08003031 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003032
3033 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003034 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003035 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003036 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003037 dirtyLayerUnchecked(bounds, getRegion());
3038 }
Romain Guy97771732012-02-28 18:17:02 -08003039 }
Chet Haase48659092012-05-31 15:21:51 -07003040
3041 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003042}
3043
Chet Haase48659092012-05-31 15:21:51 -07003044status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3045 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003046
Romain Guya1d3c912011-12-13 14:55:06 -08003047 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003048
Romain Guyfb8b7632010-08-23 21:05:08 -07003049 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003050 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003051 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003052
Romain Guy8b55f372010-08-18 17:10:07 -07003053 const float x = texture->left - texture->offset;
3054 const float y = texture->top - texture->offset;
3055
Romain Guy01d58e42011-01-19 21:54:02 -08003056 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003057
3058 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003059}
3060
Chris Craika08f95c2013-03-15 17:24:33 -07003061status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003062 if (!layer) {
3063 return DrawGlInfo::kStatusDone;
3064 }
3065
Romain Guyb2e2f242012-10-17 18:18:35 -07003066 mat4* transform = NULL;
3067 if (layer->isTextureLayer()) {
3068 transform = &layer->getTransform();
3069 if (!transform->isIdentity()) {
3070 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003071 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003072 }
3073 }
3074
Chris Craik39a908c2013-06-13 14:39:01 -07003075 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003076 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003077 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003078
3079 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003080 if (transform && !transform->isIdentity()) {
3081 restore();
3082 }
Chet Haase48659092012-05-31 15:21:51 -07003083 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003084 }
3085
Romain Guy5bb3c732012-11-29 17:52:58 -08003086 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003087
Chris Craik39a908c2013-06-13 14:39:01 -07003088 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003089 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003090
Romain Guy211370f2012-02-01 16:10:55 -08003091 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003092 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3093 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003094
Romain Guyc88e3572011-01-22 00:32:12 -08003095 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003096 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3097 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003098 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003099 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003100 setupDraw();
3101 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003102 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003103 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003104 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003105 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003106 setupDrawPureColorUniforms();
3107 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003108 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003109 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3110 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3111 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003112
Romain Guyd21b6e12011-11-30 20:21:23 -08003113 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003114 setupDrawModelViewTranslate(tx, ty,
3115 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003116 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003117 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003118 setupDrawModelViewTranslate(x, y,
3119 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3120 }
Romain Guyf219da52011-01-16 12:54:25 -08003121
Romain Guy448455f2013-07-22 13:57:50 -07003122 TextureVertex* mesh = &layer->mesh[0];
3123 GLsizei elementsCount = layer->meshElementCount;
3124
3125 while (elementsCount > 0) {
3126 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3127
Romain Guy3380cfd2013-08-15 16:57:57 -07003128 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003129 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3130 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3131
3132 elementsCount -= drawCount;
3133 // Though there are 4 vertices in a quad, we use 6 indices per
3134 // quad to draw with GL_TRIANGLES
3135 mesh += (drawCount / 6) * 4;
3136 }
Romain Guyf219da52011-01-16 12:54:25 -08003137
Romain Guy3a3133d2011-02-01 22:59:58 -08003138#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003139 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003140#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003141 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003142
Chris Craikc3566d02013-02-04 16:16:33 -08003143 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003144
Romain Guy5bb3c732012-11-29 17:52:58 -08003145 if (layer->debugDrawUpdate) {
3146 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003147 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3148 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3149 }
Romain Guyf219da52011-01-16 12:54:25 -08003150 }
Chris Craik34416ea2013-04-15 16:08:28 -07003151 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003152
Romain Guyb2e2f242012-10-17 18:18:35 -07003153 if (transform && !transform->isIdentity()) {
3154 restore();
3155 }
3156
Chet Haase48659092012-05-31 15:21:51 -07003157 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003158}
3159
Romain Guy6926c722010-07-12 20:20:03 -07003160///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003161// Shaders
3162///////////////////////////////////////////////////////////////////////////////
3163
3164void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003165 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003166}
3167
Romain Guy06f96e22010-07-30 19:18:16 -07003168void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003169 mDrawModifiers.mShader = shader;
3170 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003171 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003172 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003173}
3174
Romain Guyd27977d2010-07-14 19:18:51 -07003175///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003176// Color filters
3177///////////////////////////////////////////////////////////////////////////////
3178
3179void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003180 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003181}
3182
3183void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003184 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003185}
3186
3187///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003188// Drop shadow
3189///////////////////////////////////////////////////////////////////////////////
3190
3191void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003192 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003193}
3194
3195void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003196 mDrawModifiers.mHasShadow = true;
3197 mDrawModifiers.mShadowRadius = radius;
3198 mDrawModifiers.mShadowDx = dx;
3199 mDrawModifiers.mShadowDy = dy;
3200 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003201}
3202
3203///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003204// Draw filters
3205///////////////////////////////////////////////////////////////////////////////
3206
3207void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003208 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3209 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003210 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003211 mDrawModifiers.mPaintFilterClearBits = 0;
3212 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003213}
3214
3215void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003216 mDrawModifiers.mHasDrawFilter = true;
3217 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3218 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003219}
3220
Chris Craika08f95c2013-03-15 17:24:33 -07003221SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003222 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003223 return paint;
3224 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003225
3226 uint32_t flags = paint->getFlags();
3227
3228 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003229 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3230 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003231
3232 return &mFilteredPaint;
3233}
3234
3235///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003236// Drawing implementation
3237///////////////////////////////////////////////////////////////////////////////
3238
Romain Guy3b748a42013-04-17 18:54:38 -07003239Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3240 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3241 if (!texture) {
3242 return mCaches.textureCache.get(bitmap);
3243 }
3244 return texture;
3245}
3246
Romain Guy01d58e42011-01-19 21:54:02 -08003247void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3248 float x, float y, SkPaint* paint) {
3249 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3250 return;
3251 }
3252
3253 int alpha;
3254 SkXfermode::Mode mode;
3255 getAlphaAndMode(paint, &alpha, &mode);
3256
3257 setupDraw();
3258 setupDrawWithTexture(true);
3259 setupDrawAlpha8Color(paint->getColor(), alpha);
3260 setupDrawColorFilter();
3261 setupDrawShader();
3262 setupDrawBlending(true, mode);
3263 setupDrawProgram();
3264 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3265 setupDrawTexture(texture->id);
3266 setupDrawPureColorUniforms();
3267 setupDrawColorFilterUniforms();
3268 setupDrawShaderUniforms();
3269 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3270
3271 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003272}
3273
Romain Guyf607bdc2010-09-10 19:20:06 -07003274// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003275#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3276#define kStdUnderline_Offset (1.0f / 9.0f)
3277#define kStdUnderline_Thickness (1.0f / 18.0f)
3278
Chris Craike63f7c622013-10-17 10:30:55 -07003279void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003280 // Handle underline and strike-through
3281 uint32_t flags = paint->getFlags();
3282 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003283 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003284
Romain Guy211370f2012-02-01 16:10:55 -08003285 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003286 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003287 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003288
Raph Levien8b4072d2012-07-30 15:50:00 -07003289 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003290 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003291
Romain Guyf6834472011-01-23 13:32:12 -08003292 int linesCount = 0;
3293 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3294 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3295
3296 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003297 float points[pointsCount];
3298 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003299
3300 if (flags & SkPaint::kUnderlineText_Flag) {
3301 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003302 points[currentPoint++] = left;
3303 points[currentPoint++] = top;
3304 points[currentPoint++] = left + underlineWidth;
3305 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003306 }
3307
3308 if (flags & SkPaint::kStrikeThruText_Flag) {
3309 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003310 points[currentPoint++] = left;
3311 points[currentPoint++] = top;
3312 points[currentPoint++] = left + underlineWidth;
3313 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003314 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003315
Romain Guy726aeba2011-06-01 14:52:00 -07003316 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003317
Romain Guy726aeba2011-06-01 14:52:00 -07003318 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003319 }
3320 }
3321}
3322
Romain Guy672433d2013-01-04 19:05:13 -08003323status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3324 if (mSnapshot->isIgnored()) {
3325 return DrawGlInfo::kStatusDone;
3326 }
3327
Romain Guy735738c2012-12-03 12:34:51 -08003328 int color = paint->getColor();
3329 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003330 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003331 color |= 0x00ffffff;
3332 }
3333 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3334
3335 return drawColorRects(rects, count, color, mode);
3336}
3337
3338status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003339 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003340 if (count == 0) {
3341 return DrawGlInfo::kStatusDone;
3342 }
Romain Guy735738c2012-12-03 12:34:51 -08003343
Romain Guy672433d2013-01-04 19:05:13 -08003344 float left = FLT_MAX;
3345 float top = FLT_MAX;
3346 float right = FLT_MIN;
3347 float bottom = FLT_MIN;
3348
Romain Guy448455f2013-07-22 13:57:50 -07003349 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003350 Vertex* vertex = mesh;
3351
Chris Craik2af46352012-11-26 18:30:17 -08003352 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003353 float l = rects[index + 0];
3354 float t = rects[index + 1];
3355 float r = rects[index + 2];
3356 float b = rects[index + 3];
3357
Romain Guy3b753822013-03-05 10:27:35 -08003358 Vertex::set(vertex++, l, t);
3359 Vertex::set(vertex++, r, t);
3360 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003361 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003362
Romain Guy3b753822013-03-05 10:27:35 -08003363 left = fminf(left, l);
3364 top = fminf(top, t);
3365 right = fmaxf(right, r);
3366 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003367 }
3368
Romain Guy3b753822013-03-05 10:27:35 -08003369 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003370 return DrawGlInfo::kStatusDone;
3371 }
Romain Guy672433d2013-01-04 19:05:13 -08003372
Romain Guy672433d2013-01-04 19:05:13 -08003373 setupDraw();
3374 setupDrawNoTexture();
3375 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3376 setupDrawShader();
3377 setupDrawColorFilter();
3378 setupDrawBlending(mode);
3379 setupDrawProgram();
3380 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003381 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003382 setupDrawColorUniforms();
3383 setupDrawShaderUniforms();
3384 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003385
Romain Guy8ce00302013-01-15 18:51:42 -08003386 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003387 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003388 }
3389
Romain Guy448455f2013-07-22 13:57:50 -07003390 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003391
3392 return DrawGlInfo::kStatusDrew;
3393}
3394
Romain Guy026c5e162010-06-28 17:12:22 -07003395void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003396 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003397 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003398 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003399 color |= 0x00ffffff;
3400 }
3401
Romain Guy70ca14e2010-12-13 18:24:33 -08003402 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003403 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003404 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003405 setupDrawShader();
3406 setupDrawColorFilter();
3407 setupDrawBlending(mode);
3408 setupDrawProgram();
3409 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3410 setupDrawColorUniforms();
3411 setupDrawShaderUniforms(ignoreTransform);
3412 setupDrawColorFilterUniforms();
3413 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003414
Romain Guyc95c8d62010-09-17 15:31:32 -07003415 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3416}
3417
Romain Guy82ba8142010-07-09 13:25:56 -07003418void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003419 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003420 int alpha;
3421 SkXfermode::Mode mode;
3422 getAlphaAndMode(paint, &alpha, &mode);
3423
Romain Guyd21b6e12011-11-30 20:21:23 -08003424 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003425
Romain Guy3b748a42013-04-17 18:54:38 -07003426 GLvoid* vertices = (GLvoid*) NULL;
3427 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3428
3429 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003430 vertices = &mMeshVertices[0].x;
3431 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003432
3433 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3434 texture->uvMapper->map(uvs);
3435
3436 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3437 }
3438
Romain Guy3b753822013-03-05 10:27:35 -08003439 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3440 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3441 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003442
Romain Guyd21b6e12011-11-30 20:21:23 -08003443 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003444 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003445 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3446 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003447 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003448 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003449 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003450 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3451 }
3452
3453 if (texture->uvMapper) {
3454 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003455 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003456}
3457
Romain Guybd6b79b2010-06-26 00:13:53 -07003458void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003459 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3460 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003461 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003462}
3463
3464void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003465 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003466 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003467 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003468
Romain Guy746b7402010-10-26 16:27:31 -07003469 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003470 setupDrawWithTexture();
3471 setupDrawColor(alpha, alpha, alpha, alpha);
3472 setupDrawColorFilter();
3473 setupDrawBlending(blend, mode, swapSrcDst);
3474 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003475 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003476 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003477 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003478 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003479 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003480 }
Romain Guy886b2752013-01-04 12:26:18 -08003481 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003482 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003483 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003484 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003485
Romain Guy6820ac82010-09-15 18:11:50 -07003486 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003487}
3488
Romain Guy3b748a42013-04-17 18:54:38 -07003489void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3490 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3491 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3492 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3493
3494 setupDraw();
3495 setupDrawWithTexture();
3496 setupDrawColor(alpha, alpha, alpha, alpha);
3497 setupDrawColorFilter();
3498 setupDrawBlending(blend, mode, swapSrcDst);
3499 setupDrawProgram();
3500 if (!dirty) setupDrawDirtyRegionsDisabled();
3501 if (!ignoreScale) {
3502 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3503 } else {
3504 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3505 }
3506 setupDrawTexture(texture);
3507 setupDrawPureColorUniforms();
3508 setupDrawColorFilterUniforms();
3509 setupDrawMeshIndices(vertices, texCoords, vbo);
3510
3511 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003512}
3513
Romain Guy886b2752013-01-04 12:26:18 -08003514void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3515 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3516 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003517 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003518
3519 setupDraw();
3520 setupDrawWithTexture(true);
3521 if (hasColor) {
3522 setupDrawAlpha8Color(color, alpha);
3523 }
3524 setupDrawColorFilter();
3525 setupDrawShader();
3526 setupDrawBlending(true, mode);
3527 setupDrawProgram();
3528 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003529 if (!ignoreScale) {
3530 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3531 } else {
3532 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3533 }
Romain Guy886b2752013-01-04 12:26:18 -08003534 setupDrawTexture(texture);
3535 setupDrawPureColorUniforms();
3536 setupDrawColorFilterUniforms();
3537 setupDrawShaderUniforms();
3538 setupDrawMesh(vertices, texCoords);
3539
3540 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003541}
3542
Romain Guya5aed0d2010-09-09 14:42:43 -07003543void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003544 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003545 if (mCountOverdraw) {
3546 if (!mCaches.blend) glEnable(GL_BLEND);
3547 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3548 glBlendFunc(GL_ONE, GL_ONE);
3549 }
3550
3551 mCaches.blend = true;
3552 mCaches.lastSrcMode = GL_ONE;
3553 mCaches.lastDstMode = GL_ONE;
3554
3555 return;
3556 }
3557
Romain Guy82ba8142010-07-09 13:25:56 -07003558 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003559
Romain Guy82ba8142010-07-09 13:25:56 -07003560 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003561 // These blend modes are not supported by OpenGL directly and have
3562 // to be implemented using shaders. Since the shader will perform
3563 // the blending, turn blending off here
3564 // If the blend mode cannot be implemented using shaders, fall
3565 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003566 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003567 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003568 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003569 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003570
Romain Guy82bc7a72012-01-03 14:13:39 -08003571 if (mCaches.blend) {
3572 glDisable(GL_BLEND);
3573 mCaches.blend = false;
3574 }
3575
3576 return;
3577 } else {
3578 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003579 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003580 }
3581
3582 if (!mCaches.blend) {
3583 glEnable(GL_BLEND);
3584 }
3585
3586 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3587 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3588
3589 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3590 glBlendFunc(sourceMode, destMode);
3591 mCaches.lastSrcMode = sourceMode;
3592 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003593 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003594 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003595 glDisable(GL_BLEND);
3596 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003597 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003598}
3599
Romain Guy889f8d12010-07-29 14:37:42 -07003600bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003601 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003602 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003603 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003604 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003605 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003606 }
Romain Guy6926c722010-07-12 20:20:03 -07003607 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003608}
3609
Romain Guy026c5e162010-06-28 17:12:22 -07003610void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003611 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003612 TextureVertex::setUV(v++, u1, v1);
3613 TextureVertex::setUV(v++, u2, v1);
3614 TextureVertex::setUV(v++, u1, v2);
3615 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003616}
3617
Chris Craik16ecda52013-03-29 10:59:59 -07003618void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003619 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003620 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3621 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003622 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003623 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003624 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003625}
3626
Chris Craik16ecda52013-03-29 10:59:59 -07003627float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3628 float alpha;
3629 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3630 alpha = mDrawModifiers.mOverrideLayerAlpha;
3631 } else {
3632 alpha = layer->getAlpha() / 255.0f;
3633 }
3634 return alpha * mSnapshot->alpha;
3635}
3636
Romain Guy9d5316e2010-06-24 19:30:36 -07003637}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003638}; // namespace android