blob: 8eb1f9283c95c56eb37af3e03d6939a2f2ef8893 [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
henry.uh_chen20adb6c2014-07-02 19:36:56 +0800286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque, bool expand) {
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
henry.uh_chen20adb6c2014-07-02 19:36:56 +0800293 startTiling(*clip, s->height, opaque, expand);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800294 }
295}
296
henry.uh_chen20adb6c2014-07-02 19:36:56 +0800297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque, bool expand) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800298 if (!mSuppressTiling) {
henry.uh_chen20adb6c2014-07-02 19:36:56 +0800299 if(expand) {
300 // Expand the startTiling region by 1
301 int leftNotZero = (clip.left > 0) ? 1 : 0;
302 int topNotZero = (windowHeight - clip.bottom > 0) ? 1 : 0;
303
304 mCaches.startTiling(
305 clip.left - leftNotZero,
306 windowHeight - clip.bottom - topNotZero,
307 clip.right - clip.left + leftNotZero + 1,
308 clip.bottom - clip.top + topNotZero + 1,
309 opaque);
310 } else {
311 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800312 clip.right - clip.left, clip.bottom - clip.top, opaque);
henry.uh_chen20adb6c2014-07-02 19:36:56 +0800313 }
Romain Guy54c1a642012-09-27 17:55:46 -0700314 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700315}
316
317void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700318 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700319}
320
Romain Guyb025b9c2010-09-16 14:16:48 -0700321void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700322 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700323 endTiling();
324
Romain Guyca89e2a2013-03-08 17:44:20 -0800325 // When finish() is invoked on FBO 0 we've reached the end
326 // of the current frame
327 if (getTargetFbo() == 0) {
328 mCaches.pathCache.trim();
329 }
330
Romain Guy11cb6422012-09-21 00:39:43 -0700331 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700332#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700333 GLenum status = GL_NO_ERROR;
334 while ((status = glGetError()) != GL_NO_ERROR) {
335 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
336 switch (status) {
337 case GL_INVALID_ENUM:
338 ALOGE(" GL_INVALID_ENUM");
339 break;
340 case GL_INVALID_VALUE:
341 ALOGE(" GL_INVALID_VALUE");
342 break;
343 case GL_INVALID_OPERATION:
344 ALOGE(" GL_INVALID_OPERATION");
345 break;
346 case GL_OUT_OF_MEMORY:
347 ALOGE(" Out of memory!");
348 break;
349 }
Romain Guya07105b2011-01-10 21:14:18 -0800350 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700351#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700352
Romain Guyc15008e2010-11-10 11:59:15 -0800353#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800354 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700355#else
356 if (mCaches.getDebugLevel() & kDebugMemory) {
357 mCaches.dumpMemoryUsage();
358 }
Romain Guyc15008e2010-11-10 11:59:15 -0800359#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700360 }
Romain Guy96885eb2013-03-26 15:05:58 -0700361
Romain Guy78dd96d2013-05-03 14:24:16 -0700362 if (mCountOverdraw) {
363 countOverdraw();
364 }
365
Romain Guy96885eb2013-03-26 15:05:58 -0700366 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700367}
368
Romain Guy6c319ca2011-01-11 14:29:25 -0800369void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700370 if (mCaches.currentProgram) {
371 if (mCaches.currentProgram->isInUse()) {
372 mCaches.currentProgram->remove();
373 mCaches.currentProgram = NULL;
374 }
375 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700376 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700377 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800378 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800379 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800380 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700381 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700382}
383
Romain Guy6c319ca2011-01-11 14:29:25 -0800384void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800385 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800386 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700387 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700388 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700389
Romain Guy3e263fa2011-12-12 16:47:48 -0800390 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
391
Chet Haase80250612012-08-15 13:46:54 -0700392 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700393 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800394 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700395 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700396
Romain Guya1d3c912011-12-13 14:55:06 -0800397 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700398 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700399
Romain Guy50c0f092010-10-19 11:42:22 -0700400 mCaches.blend = true;
401 glEnable(GL_BLEND);
402 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
403 glBlendEquation(GL_FUNC_ADD);
Chris Craikc47aba22014-07-22 12:30:39 -0700404
405 glStencilMask(0xff);
Romain Guyda8532c2010-08-31 11:50:35 -0700406}
407
Romain Guy35643dd2012-09-18 15:40:58 -0700408void OpenGLRenderer::resumeAfterLayer() {
409 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
410 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
411 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700412 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700413
414 mCaches.resetScissor();
415 dirtyClip();
416}
417
Romain Guyba6be8a2012-04-23 18:22:09 -0700418void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700419 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700420}
421
422void OpenGLRenderer::attachFunctor(Functor* functor) {
423 mFunctors.add(functor);
424}
425
Romain Guy8f3b8e32012-03-27 16:33:45 -0700426status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
427 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700428 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700429
Romain Guyba6be8a2012-04-23 18:22:09 -0700430 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800431 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700432 SortedVector<Functor*> functors(mFunctors);
433 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700434
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 DrawGlInfo info;
436 info.clipLeft = 0;
437 info.clipTop = 0;
438 info.clipRight = 0;
439 info.clipBottom = 0;
440 info.isLayer = false;
441 info.width = 0;
442 info.height = 0;
443 memset(info.transform, 0, sizeof(float) * 16);
444
445 for (size_t i = 0; i < count; i++) {
446 Functor* f = functors.itemAt(i);
447 result |= (*f)(DrawGlInfo::kModeProcess, &info);
448
Chris Craikc2c95432012-04-25 15:13:52 -0700449 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700450 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
451 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700452 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700453
Chris Craikc2c95432012-04-25 15:13:52 -0700454 if (result & DrawGlInfo::kStatusInvoke) {
455 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700456 }
457 }
Chris Craikd15321b2012-11-28 14:45:04 -0800458 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700459 }
460
461 return result;
462}
463
464status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700465 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
466
Chris Craik932b7f62012-06-06 13:59:33 -0700467 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700468
Romain Guyd643bb52011-03-01 14:55:21 -0800469
Romain Guy80911b82011-03-16 15:30:12 -0700470 Rect clip(*mSnapshot->clipRect);
471 clip.snapToPixelBoundaries();
472
Romain Guyd643bb52011-03-01 14:55:21 -0800473 // Since we don't know what the functor will draw, let's dirty
474 // tne entire clip region
475 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800476 dirtyLayerUnchecked(clip, getRegion());
477 }
Romain Guyd643bb52011-03-01 14:55:21 -0800478
Romain Guy08aa2cb2011-03-17 11:06:57 -0700479 DrawGlInfo info;
480 info.clipLeft = clip.left;
481 info.clipTop = clip.top;
482 info.clipRight = clip.right;
483 info.clipBottom = clip.bottom;
484 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700485 info.width = getSnapshot()->viewport.getWidth();
486 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700487 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700488
John Reck25d2f7b2013-09-10 13:12:09 -0700489 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700490 // setup GL state for functor
491 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700492 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
493 }
John Reck25d2f7b2013-09-10 13:12:09 -0700494 if (mCaches.enableScissor() || dirtyClip) {
495 setScissorFromClip();
496 }
Chris Craik54f574a2013-08-26 11:23:46 -0700497 interrupt();
498
499 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700500 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800501
Romain Guy8f3b8e32012-03-27 16:33:45 -0700502 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700503 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800504 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700505
Chris Craik65924a32012-04-05 17:52:11 -0700506 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700507 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700508 }
Romain Guycabfcc12011-03-07 18:06:46 -0800509 }
510
Chet Haasedaf98e92011-01-10 14:10:36 -0800511 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700512 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800513}
514
Romain Guyf6a11b82010-06-23 17:47:49 -0700515///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700516// Debug
517///////////////////////////////////////////////////////////////////////////////
518
Romain Guy0f6675332013-03-01 14:31:04 -0800519void OpenGLRenderer::eventMark(const char* name) const {
520 mCaches.eventMark(0, name);
521}
522
Romain Guy87e2f7572012-09-24 11:37:12 -0700523void OpenGLRenderer::startMark(const char* name) const {
524 mCaches.startMark(0, name);
525}
526
527void OpenGLRenderer::endMark() const {
528 mCaches.endMark();
529}
530
531void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
532 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
533 if (clear) {
534 mCaches.disableScissor();
535 mCaches.stencil.clear();
536 }
537 if (enable) {
538 mCaches.stencil.enableDebugWrite();
539 } else {
540 mCaches.stencil.disable();
541 }
542 }
543}
544
545void OpenGLRenderer::renderOverdraw() {
546 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700547 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700548
549 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700550 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700551 clip->right - clip->left, clip->bottom - clip->top);
552
Romain Guy627c6fd2013-08-21 11:53:18 -0700553 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700554 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700555 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
556
557 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700558 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700559 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
560
561 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700562 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700563 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
564
565 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700566 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700567 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
568
Romain Guy87e2f7572012-09-24 11:37:12 -0700569 mCaches.stencil.disable();
570 }
571}
572
Romain Guy78dd96d2013-05-03 14:24:16 -0700573void OpenGLRenderer::countOverdraw() {
574 size_t count = mWidth * mHeight;
575 uint32_t* buffer = new uint32_t[count];
576 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
577
578 size_t total = 0;
579 for (size_t i = 0; i < count; i++) {
580 total += buffer[i] & 0xff;
581 }
582
583 mOverdraw = total / float(count);
584
585 delete[] buffer;
586}
587
Romain Guy87e2f7572012-09-24 11:37:12 -0700588///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700589// Layers
590///////////////////////////////////////////////////////////////////////////////
591
592bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700593 if (layer->deferredUpdateScheduled && layer->renderer &&
594 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700595 ATRACE_CALL();
596
Romain Guy11cb6422012-09-21 00:39:43 -0700597 Rect& dirty = layer->dirtyRect;
598
Romain Guy7c450aa2012-09-21 19:15:00 -0700599 if (inFrame) {
600 endTiling();
601 debugOverdraw(false, false);
602 }
Romain Guy11cb6422012-09-21 00:39:43 -0700603
Romain Guy96885eb2013-03-26 15:05:58 -0700604 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700605 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700606 } else {
607 layer->defer();
608 }
Romain Guy11cb6422012-09-21 00:39:43 -0700609
610 if (inFrame) {
611 resumeAfterLayer();
612 startTiling(mSnapshot);
613 }
614
Romain Guy5bb3c732012-11-29 17:52:58 -0800615 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700616 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700617
618 return true;
619 }
620
621 return false;
622}
623
624void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700625 // If draw deferring is enabled this method will simply defer
626 // the display list of each individual layer. The layers remain
627 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700628 int count = mLayerUpdates.size();
629 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700630 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
631 startMark("Layer Updates");
632 } else {
633 startMark("Defer Layer Updates");
634 }
Romain Guy11cb6422012-09-21 00:39:43 -0700635
Chris Craik1206b9b2013-04-04 14:46:24 -0700636 // Note: it is very important to update the layers in order
637 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700638 Layer* layer = mLayerUpdates.itemAt(i);
639 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700640 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
641 mCaches.resourceCache.decrementRefcount(layer);
642 }
Romain Guy11cb6422012-09-21 00:39:43 -0700643 }
Romain Guy11cb6422012-09-21 00:39:43 -0700644
Romain Guy96885eb2013-03-26 15:05:58 -0700645 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
646 mLayerUpdates.clear();
647 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
648 }
649 endMark();
650 }
651}
652
653void OpenGLRenderer::flushLayers() {
654 int count = mLayerUpdates.size();
655 if (count > 0) {
656 startMark("Apply Layer Updates");
657 char layerName[12];
658
Chris Craik1206b9b2013-04-04 14:46:24 -0700659 // Note: it is very important to update the layers in order
660 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700661 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700662 startMark(layerName);
663
Romain Guy40543602013-06-12 15:31:28 -0700664 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700665 Layer* layer = mLayerUpdates.itemAt(i);
666 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700667 ATRACE_END();
668
Romain Guy02b49b72013-03-29 12:37:16 -0700669 mCaches.resourceCache.decrementRefcount(layer);
670
Romain Guy96885eb2013-03-26 15:05:58 -0700671 endMark();
672 }
673
674 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700675 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700676
Romain Guy11cb6422012-09-21 00:39:43 -0700677 endMark();
678 }
679}
680
681void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
682 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700683 // Make sure we don't introduce duplicates.
684 // SortedVector would do this automatically but we need to respect
685 // the insertion order. The linear search is not an issue since
686 // this list is usually very short (typically one item, at most a few)
687 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
688 if (mLayerUpdates.itemAt(i) == layer) {
689 return;
690 }
691 }
Romain Guy11cb6422012-09-21 00:39:43 -0700692 mLayerUpdates.push_back(layer);
693 mCaches.resourceCache.incrementRefcount(layer);
694 }
695}
696
Romain Guye93482f2013-06-17 13:14:51 -0700697void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
698 if (layer) {
699 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
700 if (mLayerUpdates.itemAt(i) == layer) {
701 mLayerUpdates.removeAt(i);
702 mCaches.resourceCache.decrementRefcount(layer);
703 break;
704 }
705 }
706 }
707}
708
Romain Guy11cb6422012-09-21 00:39:43 -0700709void OpenGLRenderer::clearLayerUpdates() {
710 size_t count = mLayerUpdates.size();
711 if (count > 0) {
712 mCaches.resourceCache.lock();
713 for (size_t i = 0; i < count; i++) {
714 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
715 }
716 mCaches.resourceCache.unlock();
717 mLayerUpdates.clear();
718 }
719}
720
Romain Guy40543602013-06-12 15:31:28 -0700721void OpenGLRenderer::flushLayerUpdates() {
722 syncState();
723 updateLayers();
724 flushLayers();
725 // Wait for all the layer updates to be executed
726 AutoFence fence;
727}
728
Romain Guy11cb6422012-09-21 00:39:43 -0700729///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700730// State management
731///////////////////////////////////////////////////////////////////////////////
732
Romain Guybb9524b2010-06-22 18:56:38 -0700733int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700734 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700735}
736
737int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700738 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700739}
740
741void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700742 if (mSaveCount > 1) {
743 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700744 }
Romain Guybb9524b2010-06-22 18:56:38 -0700745}
746
747void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700748 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700749
Romain Guy8fb95422010-08-17 18:38:51 -0700750 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700751 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700752 }
Romain Guybb9524b2010-06-22 18:56:38 -0700753}
754
Romain Guy8aef54f2010-09-01 15:13:49 -0700755int OpenGLRenderer::saveSnapshot(int flags) {
756 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700757 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700758}
759
760bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700761 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700762 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700763 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700764
Romain Guybd6b79b2010-06-26 00:13:53 -0700765 sp<Snapshot> current = mSnapshot;
766 sp<Snapshot> previous = mSnapshot->previous;
767
Romain Guyeb993562010-10-05 18:14:38 -0700768 if (restoreOrtho) {
769 Rect& r = previous->viewport;
770 glViewport(r.left, r.top, r.right, r.bottom);
771 mOrthoMatrix.load(current->orthoMatrix);
772 }
773
Romain Guy8b55f372010-08-18 17:10:07 -0700774 mSaveCount--;
775 mSnapshot = previous;
776
Romain Guy2542d192010-08-18 11:47:12 -0700777 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700778 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700779 }
Romain Guy2542d192010-08-18 11:47:12 -0700780
Romain Guy5ec99242010-11-03 16:19:08 -0700781 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700782 endMark(); // Savelayer
783 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700784 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700785 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700786 }
787
Romain Guy2542d192010-08-18 11:47:12 -0700788 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700789}
790
Romain Guyf6a11b82010-06-23 17:47:49 -0700791///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700792// Layers
793///////////////////////////////////////////////////////////////////////////////
794
795int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800796 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700797 const GLuint previousFbo = mSnapshot->fbo;
798 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700799
Romain Guyaf636eb2010-12-09 17:47:21 -0800800 if (!mSnapshot->isIgnored()) {
Chet Haased48885a2012-08-28 17:43:28 -0700801 createLayer(left, top, right, bottom, alpha, mode, flags, previousFbo);
Romain Guydbc26d22010-10-11 17:58:29 -0700802 }
Romain Guyd55a8612010-06-28 17:42:46 -0700803
804 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700805}
806
Chris Craikd90144d2013-03-19 15:03:48 -0700807void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
808 const Rect untransformedBounds(bounds);
809
810 currentTransform().mapRect(bounds);
811
812 // Layers only make sense if they are in the framebuffer's bounds
813 if (bounds.intersect(*mSnapshot->clipRect)) {
814 // We cannot work with sub-pixels in this case
815 bounds.snapToPixelBoundaries();
816
817 // When the layer is not an FBO, we may use glCopyTexImage so we
818 // need to make sure the layer does not extend outside the bounds
819 // of the framebuffer
820 if (!bounds.intersect(mSnapshot->previous->viewport)) {
821 bounds.setEmpty();
822 } else if (fboLayer) {
823 clip.set(bounds);
824 mat4 inverse;
825 inverse.loadInverse(currentTransform());
826 inverse.mapRect(clip);
827 clip.snapToPixelBoundaries();
828 if (clip.intersect(untransformedBounds)) {
829 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
830 bounds.set(untransformedBounds);
831 } else {
832 clip.setEmpty();
833 }
834 }
835 } else {
836 bounds.setEmpty();
837 }
838}
839
Chris Craik408eb122013-03-26 18:55:15 -0700840void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
841 bool fboLayer, int alpha) {
842 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
843 bounds.getHeight() > mCaches.maxTextureSize ||
844 (fboLayer && clip.isEmpty())) {
845 mSnapshot->empty = fboLayer;
846 } else {
847 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
848 }
849}
850
Chris Craikd90144d2013-03-19 15:03:48 -0700851int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
852 int alpha, SkXfermode::Mode mode, int flags) {
853 const GLuint previousFbo = mSnapshot->fbo;
854 const int count = saveSnapshot(flags);
855
856 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
857 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
858 // operations will be able to store and restore the current clip and transform info, and
859 // quick rejection will be correct (for display lists)
860
861 Rect bounds(left, top, right, bottom);
862 Rect clip;
863 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700864 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700865
Chris Craik408eb122013-03-26 18:55:15 -0700866 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700867 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
868 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700869 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700870 }
871 }
872
873 return count;
874}
875
876
Romain Guy1c740bc2010-09-13 18:00:09 -0700877/**
878 * Layers are viewed by Skia are slightly different than layers in image editing
879 * programs (for instance.) When a layer is created, previously created layers
880 * and the frame buffer still receive every drawing command. For instance, if a
881 * layer is created and a shape intersecting the bounds of the layers and the
882 * framebuffer is draw, the shape will be drawn on both (unless the layer was
883 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
884 *
885 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
886 * texture. Unfortunately, this is inefficient as it requires every primitive to
887 * be drawn n + 1 times, where n is the number of active layers. In practice this
888 * means, for every primitive:
889 * - Switch active frame buffer
890 * - Change viewport, clip and projection matrix
891 * - Issue the drawing
892 *
893 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700894 * To avoid this, layers are implemented in a different way here, at least in the
895 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
896 * is set. When this flag is set we can redirect all drawing operations into a
897 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700898 *
899 * This implementation relies on the frame buffer being at least RGBA 8888. When
900 * a layer is created, only a texture is created, not an FBO. The content of the
901 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700902 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700903 * buffer and drawing continues as normal. This technique therefore treats the
904 * frame buffer as a scratch buffer for the layers.
905 *
906 * To compose the layers back onto the frame buffer, each layer texture
907 * (containing the original frame buffer data) is drawn as a simple quad over
908 * the frame buffer. The trick is that the quad is set as the composition
909 * destination in the blending equation, and the frame buffer becomes the source
910 * of the composition.
911 *
912 * Drawing layers with an alpha value requires an extra step before composition.
913 * An empty quad is drawn over the layer's region in the frame buffer. This quad
914 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
915 * quad is used to multiply the colors in the frame buffer. This is achieved by
916 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
917 * GL_ZERO, GL_SRC_ALPHA.
918 *
919 * Because glCopyTexImage2D() can be slow, an alternative implementation might
920 * be use to draw a single clipped layer. The implementation described above
921 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700922 *
923 * (1) The frame buffer is actually not cleared right away. To allow the GPU
924 * to potentially optimize series of calls to glCopyTexImage2D, the frame
925 * buffer is left untouched until the first drawing operation. Only when
926 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700927 */
Chet Haased48885a2012-08-28 17:43:28 -0700928bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
929 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo) {
Romain Guyeb993562010-10-05 18:14:38 -0700930 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700931 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700932
Romain Guyeb993562010-10-05 18:14:38 -0700933 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
934
Romain Guyf607bdc2010-09-10 19:20:06 -0700935 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700936 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700937 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700938 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700939 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700940
941 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700942 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700943 return false;
944 }
Romain Guyf18fd992010-07-08 11:45:51 -0700945
Romain Guya1d3c912011-12-13 14:55:06 -0800946 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700947 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700948 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700949 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700950 }
951
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700953 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
955 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800956 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700957 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700958 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700959
Romain Guy8fb95422010-08-17 18:38:51 -0700960 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700961 mSnapshot->flags |= Snapshot::kFlagIsLayer;
962 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700963
Chris Craik7273daa2013-03-28 11:25:24 -0700964 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700965 if (fboLayer) {
Chet Haased48885a2012-08-28 17:43:28 -0700966 return createFboLayer(layer, bounds, clip, previousFbo);
Romain Guyeb993562010-10-05 18:14:38 -0700967 } else {
968 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700969 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800970 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700971 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700972 // Workaround for some GL drivers. When reading pixels lying outside
973 // of the window we should get undefined values for those pixels.
974 // Unfortunately some drivers will turn the entire target texture black
975 // when reading outside of the window.
976 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
977 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800979 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700980
Romain Guyb254c242013-06-27 17:15:24 -0700981 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
982 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
983
Romain Guy54be1cd2011-06-13 19:04:27 -0700984 // Enqueue the buffer coordinates to clear the corresponding region later
985 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700986 }
Romain Guyeb993562010-10-05 18:14:38 -0700987 }
Romain Guyf86ef572010-07-01 11:05:42 -0700988
Romain Guyd55a8612010-06-28 17:42:46 -0700989 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700990}
991
Chet Haased48885a2012-08-28 17:43:28 -0700992bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip, GLuint previousFbo) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800993 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700994 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700995
Chet Haased48885a2012-08-28 17:43:28 -0700996 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800997 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
998 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700999 mSnapshot->fbo = layer->getFbo();
1000 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
1001 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
1002 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
1003 mSnapshot->height = bounds.getHeight();
Chet Haased48885a2012-08-28 17:43:28 -07001004 mSnapshot->orthoMatrix.load(mOrthoMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
Romain Guy2b7028e2012-09-19 17:25:38 -07001006 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -07001007 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001008 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
1010 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001011
1012 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -07001013 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -07001014 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -07001015 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001016 }
1017
1018 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001019 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001020
henry.uh_chen20adb6c2014-07-02 19:36:56 +08001021 // Expand the startTiling region by 1
1022 startTiling(mSnapshot, true, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001023
1024 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001025 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001026 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001027 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001028 glClear(GL_COLOR_BUFFER_BIT);
1029
1030 dirtyClip();
1031
1032 // Change the ortho projection
1033 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
1034 mOrthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
1035
1036 return true;
1037}
1038
Romain Guy1c740bc2010-09-13 18:00:09 -07001039/**
1040 * Read the documentation of createLayer() before doing anything in this method.
1041 */
Romain Guy1d83e192010-08-17 11:37:00 -07001042void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1043 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001044 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001045 return;
1046 }
1047
Romain Guy8ce00302013-01-15 18:51:42 -08001048 Layer* layer = current->layer;
1049 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001050 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001051
Chris Craik39a908c2013-06-13 14:39:01 -07001052 bool clipRequired = false;
1053 quickRejectNoScissor(rect, &clipRequired); // safely ignore return, should never be rejected
1054 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1055
Romain Guyeb993562010-10-05 18:14:38 -07001056 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001057 endTiling();
1058
Romain Guye0aa84b2012-04-03 19:30:26 -07001059 // Detach the texture from the FBO
1060 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001061
1062 layer->removeFbo(false);
1063
Romain Guyeb993562010-10-05 18:14:38 -07001064 // Unbind current FBO and restore previous one
1065 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001066 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001067
1068 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001069 }
1070
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001072 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001073 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001074 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001076 }
1077
Romain Guy03750a02010-10-18 14:06:08 -07001078 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001079
Romain Guya1d3c912011-12-13 14:55:06 -08001080 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001081
Romain Guy5b3b3522010-10-27 18:57:51 -07001082 // When the layer is stored in an FBO, we can save a bit of fillrate by
1083 // drawing only the dirty region
1084 if (fboLayer) {
1085 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001086 if (layer->getColorFilter()) {
1087 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001088 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001089 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001090 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001091 resetColorFilter();
1092 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001093 } else if (!rect.isEmpty()) {
1094 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301095
1096 save(0);
1097 // the layer contains screen buffer content that shouldn't be alpha modulated
1098 // (and any necessary alpha modulation was handled drawing into the layer)
1099 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001100 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301101 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001102 }
Romain Guy8b55f372010-08-18 17:10:07 -07001103
Romain Guy746b7402010-10-26 16:27:31 -07001104 dirtyClip();
1105
Romain Guyeb993562010-10-05 18:14:38 -07001106 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001107 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001108 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001109 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001110 }
1111}
1112
Romain Guyaa6c24c2011-04-28 18:40:04 -07001113void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001114 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001115
1116 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001117 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001118 setupDrawWithTexture();
1119 } else {
1120 setupDrawWithExternalTexture();
1121 }
1122 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001123 setupDrawColor(alpha, alpha, alpha, alpha);
1124 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001125 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001126 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001127 setupDrawPureColorUniforms();
1128 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001129 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1130 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001131 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001132 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001133 }
Romain Guy3b753822013-03-05 10:27:35 -08001134 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001135 layer->getWidth() == (uint32_t) rect.getWidth() &&
1136 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001137 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1138 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001139
Romain Guyd21b6e12011-11-30 20:21:23 -08001140 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001141 setupDrawModelView(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1142 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001143 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144 setupDrawModelView(rect.left, rect.top, rect.right, rect.bottom);
1145 }
1146 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001147 setupDrawMesh(&mMeshVertices[0].position[0], &mMeshVertices[0].texture[0]);
1148
1149 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001150}
1151
Romain Guy5b3b3522010-10-27 18:57:51 -07001152void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001153 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001154 const Rect& texCoords = layer->texCoords;
1155 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1156 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001157
Romain Guy9ace8f52011-07-07 20:50:11 -07001158 float x = rect.left;
1159 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001160 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001161 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001162 layer->getHeight() == (uint32_t) rect.getHeight();
1163
1164 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001165 // When we're swapping, the layer is already in screen coordinates
1166 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001167 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1168 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001169 }
1170
Romain Guyd21b6e12011-11-30 20:21:23 -08001171 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001172 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001173 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001174 }
1175
Chris Craik16ecda52013-03-29 10:59:59 -07001176 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001177 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001178 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001179 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy9ace8f52011-07-07 20:50:11 -07001180 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
1181 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001182
Romain Guyaa6c24c2011-04-28 18:40:04 -07001183 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1184 } else {
1185 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1186 drawTextureLayer(layer, rect);
1187 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1188 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001189}
1190
Chris Craik34416ea2013-04-15 16:08:28 -07001191/**
1192 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1193 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1194 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1195 * by saveLayer's restore
1196 */
1197#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1198 DRAW_COMMAND; \
1199 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1200 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1201 DRAW_COMMAND; \
1202 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1203 } \
1204 }
1205
1206#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1207
Romain Guy5b3b3522010-10-27 18:57:51 -07001208void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001209 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001210 layer->setRegionAsRect();
1211
Chris Craik34416ea2013-04-15 16:08:28 -07001212 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001213
Romain Guy5b3b3522010-10-27 18:57:51 -07001214 layer->region.clear();
1215 return;
1216 }
1217
Romain Guy211370f2012-02-01 16:10:55 -08001218 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001219 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001220 const android::Rect* rects;
1221 Region safeRegion;
1222 if (CC_LIKELY(hasRectToRectTransform())) {
1223 rects = layer->region.getArray(&count);
1224 } else {
1225 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1226 rects = safeRegion.getArray(&count);
1227 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001228
Chris Craik16ecda52013-03-29 10:59:59 -07001229 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001230 const float texX = 1.0f / float(layer->getWidth());
1231 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001232 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001233
Romain Guy8ce00302013-01-15 18:51:42 -08001234 setupDraw();
1235
1236 // We must get (and therefore bind) the region mesh buffer
1237 // after we setup drawing in case we need to mess with the
1238 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001239 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001240 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001241
Romain Guy7230a742011-01-10 22:26:16 -08001242 setupDrawWithTexture();
1243 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001244 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001245 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001246 setupDrawProgram();
1247 setupDrawDirtyRegionsDisabled();
1248 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001249 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001250 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001251 if (currentTransform().isPureTranslate()) {
1252 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1253 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001254
Romain Guyd21b6e12011-11-30 20:21:23 -08001255 layer->setFilter(GL_NEAREST);
Romain Guy9ace8f52011-07-07 20:50:11 -07001256 setupDrawModelViewTranslate(x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1257 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001258 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07001259 setupDrawModelViewTranslate(rect.left, rect.top, rect.right, rect.bottom);
1260 }
Romain Guy15bc6432011-12-13 13:11:32 -08001261 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
Romain Guy5b3b3522010-10-27 18:57:51 -07001262
1263 for (size_t i = 0; i < count; i++) {
1264 const android::Rect* r = &rects[i];
1265
1266 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001267 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001268 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001269 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001270
1271 // TODO: Reject quads outside of the clip
1272 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1273 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1274 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1275 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1276
1277 numQuads++;
1278
Romain Guy31e08e92013-06-18 15:53:53 -07001279 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001280 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1281 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001282 numQuads = 0;
1283 mesh = mCaches.getRegionMesh();
1284 }
1285 }
1286
1287 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001288 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1289 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001290 }
1291
Romain Guy5b3b3522010-10-27 18:57:51 -07001292#if DEBUG_LAYERS_AS_REGIONS
Romain Guy3a3133d2011-02-01 22:59:58 -08001293 drawRegionRects(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001294#endif
1295
1296 layer->region.clear();
1297 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001298}
1299
Romain Guy3a3133d2011-02-01 22:59:58 -08001300void OpenGLRenderer::drawRegionRects(const Region& region) {
1301#if DEBUG_LAYERS_AS_REGIONS
1302 size_t count;
1303 const android::Rect* rects = region.getArray(&count);
1304
1305 uint32_t colors[] = {
1306 0x7fff0000, 0x7f00ff00,
1307 0x7f0000ff, 0x7fff00ff,
1308 };
1309
1310 int offset = 0;
1311 int32_t top = rects[0].top;
1312
1313 for (size_t i = 0; i < count; i++) {
1314 if (top != rects[i].top) {
1315 offset ^= 0x2;
1316 top = rects[i].top;
1317 }
1318
1319 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1320 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1321 SkXfermode::kSrcOver_Mode);
1322 }
1323#endif
1324}
1325
Romain Guy8ce00302013-01-15 18:51:42 -08001326void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1327 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001328 Vector<float> rects;
1329
1330 SkRegion::Iterator it(region);
1331 while (!it.done()) {
1332 const SkIRect& r = it.rect();
1333 rects.push(r.fLeft);
1334 rects.push(r.fTop);
1335 rects.push(r.fRight);
1336 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001337 it.next();
1338 }
1339
Romain Guy448455f2013-07-22 13:57:50 -07001340 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001341}
1342
Romain Guy5b3b3522010-10-27 18:57:51 -07001343void OpenGLRenderer::dirtyLayer(const float left, const float top,
1344 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001345 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001346 Rect bounds(left, top, right, bottom);
1347 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001348 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001349 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001350}
1351
1352void OpenGLRenderer::dirtyLayer(const float left, const float top,
1353 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001354 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001355 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001356 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001357 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001358}
1359
1360void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001361 if (bounds.intersect(*mSnapshot->clipRect)) {
1362 bounds.snapToPixelBoundaries();
1363 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1364 if (!dirty.isEmpty()) {
1365 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001366 }
1367 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001368}
1369
Romain Guy448455f2013-07-22 13:57:50 -07001370void OpenGLRenderer::drawIndexedQuads(Vertex* mesh, GLsizei quadsCount) {
1371 GLsizei elementsCount = quadsCount * 6;
1372 while (elementsCount > 0) {
1373 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1374
1375 setupDrawIndexedVertices(&mesh[0].position[0]);
1376 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1377
1378 elementsCount -= drawCount;
1379 // Though there are 4 vertices in a quad, we use 6 indices per
1380 // quad to draw with GL_TRIANGLES
1381 mesh += (drawCount / 6) * 4;
1382 }
1383}
1384
Romain Guy54be1cd2011-06-13 19:04:27 -07001385void OpenGLRenderer::clearLayerRegions() {
1386 const size_t count = mLayers.size();
1387 if (count == 0) return;
1388
1389 if (!mSnapshot->isIgnored()) {
1390 // Doing several glScissor/glClear here can negatively impact
1391 // GPUs with a tiler architecture, instead we draw quads with
1392 // the Clear blending mode
1393
1394 // The list contains bounds that have already been clipped
1395 // against their initial clip rect, and the current clip
1396 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001397 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001398
Romain Guy448455f2013-07-22 13:57:50 -07001399 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001400 Vertex* vertex = mesh;
1401
1402 for (uint32_t i = 0; i < count; i++) {
1403 Rect* bounds = mLayers.itemAt(i);
1404
Romain Guy54be1cd2011-06-13 19:04:27 -07001405 Vertex::set(vertex++, bounds->left, bounds->top);
1406 Vertex::set(vertex++, bounds->right, bounds->top);
1407 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001408 Vertex::set(vertex++, bounds->right, bounds->bottom);
1409
1410 delete bounds;
1411 }
Romain Guye67307c2013-02-11 18:01:20 -08001412 // We must clear the list of dirty rects before we
1413 // call setupDraw() to prevent stencil setup to do
1414 // the same thing again
1415 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001416
1417 setupDraw(false);
1418 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1419 setupDrawBlending(true, SkXfermode::kClear_Mode);
1420 setupDrawProgram();
1421 setupDrawPureColorUniforms();
1422 setupDrawModelViewTranslate(0.0f, 0.0f, 0.0f, 0.0f, true);
1423
Romain Guy448455f2013-07-22 13:57:50 -07001424 drawIndexedQuads(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001425
1426 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001427 } else {
1428 for (uint32_t i = 0; i < count; i++) {
1429 delete mLayers.itemAt(i);
1430 }
Romain Guye67307c2013-02-11 18:01:20 -08001431 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001432 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001433}
1434
Romain Guybd6b79b2010-06-26 00:13:53 -07001435///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001436// State Deferral
1437///////////////////////////////////////////////////////////////////////////////
1438
Chris Craikff785832013-03-08 13:12:16 -08001439bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001440 const Rect& currentClip = *(mSnapshot->clipRect);
1441 const mat4& currentMatrix = *(mSnapshot->transform);
1442
Chris Craikff785832013-03-08 13:12:16 -08001443 if (stateDeferFlags & kStateDeferFlag_Draw) {
1444 // state has bounds initialized in local coordinates
1445 if (!state.mBounds.isEmpty()) {
1446 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001447 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001448 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1449 // is used, it should more closely duplicate the quickReject logic (in how it uses
1450 // snapToPixelBoundaries)
1451
Chris Craik28ce94a2013-05-31 11:38:03 -07001452 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001453 // quick rejected
1454 return true;
1455 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001456
Chris Craika02c4ed2013-06-14 13:43:58 -07001457 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001458 if (!currentClip.contains(state.mBounds)) {
1459 int& flags = state.mClipSideFlags;
1460 // op partially clipped, so record which sides are clipped for clip-aware merging
1461 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1462 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1463 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1464 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1465 }
1466 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001467 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001468 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1469 // overdraw avoidance (since we don't know what it overlaps)
1470 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001471 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001472 }
1473 }
1474
Chris Craik527a3aa2013-03-04 10:19:31 -08001475 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1476 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001477 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001478 }
1479
Chris Craik7273daa2013-03-28 11:25:24 -07001480 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1481 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001482 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001483 state.mDrawModifiers = mDrawModifiers;
1484 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001485 return false;
1486}
1487
Chris Craik527a3aa2013-03-04 10:19:31 -08001488void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001489 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001490 mDrawModifiers = state.mDrawModifiers;
1491 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001492
Chris Craik527a3aa2013-03-04 10:19:31 -08001493 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001494 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1495 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001496 dirtyClip();
1497 }
Chris Craikc3566d02013-02-04 16:16:33 -08001498}
1499
Chris Craik28ce94a2013-05-31 11:38:03 -07001500/**
1501 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1502 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1503 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1504 *
1505 * This method should be called when restoreDisplayState() won't be restoring the clip
1506 */
1507void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1508 if (clipRect != NULL) {
1509 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1510 } else {
1511 mSnapshot->setClip(0, 0, mWidth, mHeight);
1512 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001513 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001514 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001515}
1516
Chris Craikc3566d02013-02-04 16:16:33 -08001517///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001518// Transforms
1519///////////////////////////////////////////////////////////////////////////////
1520
1521void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001522 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001523}
1524
1525void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001526 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001527}
1528
1529void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001530 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001531}
1532
Romain Guy807daf72011-01-18 11:19:19 -08001533void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001534 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001535}
1536
Romain Guyf6a11b82010-06-23 17:47:49 -07001537void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001538 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001539 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001540 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001541 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001542 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001543}
1544
Chris Craikb98a0162013-02-21 11:30:22 -08001545bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001546 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001547}
1548
Romain Guyf6a11b82010-06-23 17:47:49 -07001549void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001550 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001551}
1552
1553void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Romain Guye5ebcb02010-10-15 13:57:28 -07001554 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001555 currentTransform().copyTo(transform);
Romain Guye5ebcb02010-10-15 13:57:28 -07001556 transform.preConcat(*matrix);
Romain Guy3b753822013-03-05 10:27:35 -08001557 currentTransform().load(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001558}
1559
1560///////////////////////////////////////////////////////////////////////////////
1561// Clipping
1562///////////////////////////////////////////////////////////////////////////////
1563
Romain Guybb9524b2010-06-22 18:56:38 -07001564void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001565 Rect clip(*mSnapshot->clipRect);
1566 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001567
Romain Guy8a4ac612012-07-17 17:32:48 -07001568 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1569 clip.getWidth(), clip.getHeight())) {
1570 mDirtyClip = false;
1571 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001572}
1573
Romain Guy8ce00302013-01-15 18:51:42 -08001574void OpenGLRenderer::ensureStencilBuffer() {
1575 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1576 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1577 // just hope we have one when hasLayer() returns false.
1578 if (hasLayer()) {
1579 attachStencilBufferToLayer(mSnapshot->layer);
1580 }
1581}
1582
1583void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1584 // The layer's FBO is already bound when we reach this stage
1585 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001586 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1587 // is attached after we initiated tiling. We must turn it off,
1588 // attach the new render buffer then turn tiling back on
1589 endTiling();
1590
Romain Guy8d4aeb72013-02-12 16:08:55 -08001591 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001592 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001593 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001594
Romain Guyf735c8e2013-01-31 17:45:55 -08001595 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001596 }
1597}
1598
1599void OpenGLRenderer::setStencilFromClip() {
1600 if (!mCaches.debugOverdraw) {
1601 if (!mSnapshot->clipRegion->isEmpty()) {
1602 // NOTE: The order here is important, we must set dirtyClip to false
1603 // before any draw call to avoid calling back into this method
1604 mDirtyClip = false;
1605
1606 ensureStencilBuffer();
1607
1608 mCaches.stencil.enableWrite();
1609
1610 // Clear the stencil but first make sure we restrict drawing
1611 // to the region's bounds
1612 bool resetScissor = mCaches.enableScissor();
1613 if (resetScissor) {
1614 // The scissor was not set so we now need to update it
1615 setScissorFromClip();
1616 }
1617 mCaches.stencil.clear();
1618 if (resetScissor) mCaches.disableScissor();
1619
1620 // NOTE: We could use the region contour path to generate a smaller mesh
1621 // Since we are using the stencil we could use the red book path
1622 // drawing technique. It might increase bandwidth usage though.
1623
1624 // The last parameter is important: we are not drawing in the color buffer
1625 // so we don't want to dirty the current layer, if any
1626 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1627
1628 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001629
1630 // Draw the region used to generate the stencil if the appropriate debug
1631 // mode is enabled
1632 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1633 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1634 }
Romain Guy8ce00302013-01-15 18:51:42 -08001635 } else {
1636 mCaches.stencil.disable();
1637 }
1638 }
1639}
1640
Romain Guy9d5316e2010-06-24 19:30:36 -07001641const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001642 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001643}
1644
Romain Guy35643dd2012-09-18 15:40:58 -07001645bool OpenGLRenderer::quickRejectNoScissor(float left, float top, float right, float bottom,
Chris Craik5e49b302013-07-30 19:05:20 -07001646 bool snapOut, bool* clipRequired) {
Chris Craikbf09ffb2012-10-01 13:50:37 -07001647 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guydbc26d22010-10-11 17:58:29 -07001648 return true;
1649 }
1650
Romain Guy1d83e192010-08-17 11:37:00 -07001651 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001652 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001653 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guyd2a1ff02010-10-14 14:46:44 -07001654
1655 Rect clipRect(*mSnapshot->clipRect);
1656 clipRect.snapToPixelBoundaries();
1657
Chris Craik39a908c2013-06-13 14:39:01 -07001658 if (!clipRect.intersects(r)) return true;
Romain Guy6926c722010-07-12 20:20:03 -07001659
Chris Craik39a908c2013-06-13 14:39:01 -07001660 if (clipRequired) *clipRequired = !clipRect.contains(r);
1661 return false;
Romain Guy8ba548f2010-06-30 19:21:21 -07001662}
1663
Romain Guy82ba8142010-07-09 13:25:56 -07001664bool OpenGLRenderer::quickRejectPreStroke(float left, float top, float right, float bottom,
Romain Guy8ba548f2010-06-30 19:21:21 -07001665 SkPaint* paint) {
Chris Craik5e49b302013-07-30 19:05:20 -07001666 // AA geometry will likely have a ramp around it (not accounted for in local bounds). Snap out
1667 // the final mapped rect to ensure correct clipping behavior for the ramp.
1668 bool snapOut = paint->isAntiAlias();
1669
Romain Guy8ba548f2010-06-30 19:21:21 -07001670 if (paint->getStyle() != SkPaint::kFill_Style) {
1671 float outset = paint->getStrokeWidth() * 0.5f;
Chris Craik5e49b302013-07-30 19:05:20 -07001672 return quickReject(left - outset, top - outset, right + outset, bottom + outset, snapOut);
Romain Guy8ba548f2010-06-30 19:21:21 -07001673 } else {
Chris Craik5e49b302013-07-30 19:05:20 -07001674 return quickReject(left, top, right, bottom, snapOut);
Romain Guy8ba548f2010-06-30 19:21:21 -07001675 }
1676}
1677
Chris Craik5e49b302013-07-30 19:05:20 -07001678bool OpenGLRenderer::quickReject(float left, float top, float right, float bottom, bool snapOut) {
Chris Craik39a908c2013-06-13 14:39:01 -07001679 bool clipRequired = false;
Chris Craik5e49b302013-07-30 19:05:20 -07001680 if (quickRejectNoScissor(left, top, right, bottom, snapOut, &clipRequired)) {
Romain Guyc1396e92010-06-30 17:56:19 -07001681 return true;
Romain Guy586cae32012-07-13 15:28:31 -07001682 }
1683
Chris Craik39a908c2013-06-13 14:39:01 -07001684 if (!isDeferred()) {
1685 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001686 }
Chris Craik39a908c2013-06-13 14:39:01 -07001687 return false;
Romain Guyc1396e92010-06-30 17:56:19 -07001688}
1689
Romain Guy8ce00302013-01-15 18:51:42 -08001690void OpenGLRenderer::debugClip() {
1691#if DEBUG_CLIP_REGIONS
1692 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1693 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1694 }
1695#endif
1696}
1697
Romain Guyc1396e92010-06-30 17:56:19 -07001698bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001699 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001700 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1701 if (clipped) {
1702 dirtyClip();
1703 }
1704 return !mSnapshot->clipRect->isEmpty();
1705 }
1706
1707 SkPath path;
1708 path.addRect(left, top, right, bottom);
1709
Romain Guyb7b93e02013-08-01 15:29:25 -07001710 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001711}
1712
1713bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1714 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001715 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001716
1717 SkPath transformed;
1718 path->transform(transform, &transformed);
1719
1720 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001721 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1722 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001723 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001724 if (mSnapshot->previous == mFirstSnapshot) {
1725 clip.setRect(0, 0, mWidth, mHeight);
1726 } else {
1727 Rect* bounds = mSnapshot->previous->clipRect;
1728 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1729 }
Romain Guy8ce00302013-01-15 18:51:42 -08001730 }
1731
1732 SkRegion region;
1733 region.setPath(transformed, clip);
1734
1735 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guyce0537b2010-06-29 21:05:21 -07001736 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001737 dirtyClip();
Romain Guy8ba548f2010-06-30 19:21:21 -07001738 }
1739 return !mSnapshot->clipRect->isEmpty();
1740}
1741
Romain Guy735738c2012-12-03 12:34:51 -08001742bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001743 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1744 if (clipped) {
1745 dirtyClip();
1746 }
1747 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001748}
1749
Chet Haasea23eed82012-04-12 15:19:04 -07001750Rect* OpenGLRenderer::getClipRect() {
1751 return mSnapshot->clipRect;
1752}
1753
Romain Guy8ba548f2010-06-30 19:21:21 -07001754///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001755// Drawing commands
1756///////////////////////////////////////////////////////////////////////////////
1757
Romain Guy54be1cd2011-06-13 19:04:27 -07001758void OpenGLRenderer::setupDraw(bool clear) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001759 // TODO: It would be best if we could do this before quickReject()
1760 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001761 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001762 // Make sure setScissor & setStencil happen at the beginning of
1763 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001764 if (mDirtyClip) {
1765 if (mCaches.scissorEnabled) {
1766 setScissorFromClip();
1767 }
Romain Guy8ce00302013-01-15 18:51:42 -08001768 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001769 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001770
Romain Guy70ca14e2010-12-13 18:24:33 -08001771 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001772
Romain Guy70ca14e2010-12-13 18:24:33 -08001773 mSetShaderColor = false;
1774 mColorSet = false;
1775 mColorA = mColorR = mColorG = mColorB = 0.0f;
1776 mTextureUnit = 0;
1777 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001778
1779 // Enable debug highlight when what we're about to draw is tested against
1780 // the stencil buffer and if stencil highlight debugging is on
1781 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1782 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1783 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001784
1785 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001786}
1787
1788void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1789 mDescription.hasTexture = true;
1790 mDescription.hasAlpha8Texture = isAlpha8;
1791}
1792
Romain Guyff316ec2013-02-13 18:39:43 -08001793void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1794 mDescription.hasTexture = true;
1795 mDescription.hasColors = true;
1796 mDescription.hasAlpha8Texture = isAlpha8;
1797}
1798
Romain Guyaa6c24c2011-04-28 18:40:04 -07001799void OpenGLRenderer::setupDrawWithExternalTexture() {
1800 mDescription.hasExternalTexture = true;
1801}
1802
Romain Guy15bc6432011-12-13 13:11:32 -08001803void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001804 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001805}
1806
Chris Craik710f46d2012-09-17 17:25:49 -07001807void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001808 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001809}
1810
Romain Guy8d0d4782010-12-14 20:13:35 -08001811void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1812 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001813 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1814 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1815 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001816 mColorSet = true;
1817 mSetShaderColor = mDescription.setColor(mColorR, mColorG, mColorB, mColorA);
1818}
1819
Romain Guy86568192010-12-14 15:55:39 -08001820void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1821 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001822 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1823 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1824 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001825 mColorSet = true;
1826 mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
1827}
1828
Romain Guy41210632012-07-16 17:04:24 -07001829void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1830 mCaches.fontRenderer->describe(mDescription, paint);
1831}
1832
Romain Guy70ca14e2010-12-13 18:24:33 -08001833void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1834 mColorA = a;
1835 mColorR = r;
1836 mColorG = g;
1837 mColorB = b;
1838 mColorSet = true;
1839 mSetShaderColor = mDescription.setColor(r, g, b, a);
1840}
1841
1842void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001843 if (mDrawModifiers.mShader) {
1844 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001845 }
1846}
1847
1848void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001849 if (mDrawModifiers.mColorFilter) {
1850 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001851 }
1852}
1853
Romain Guyf09ef512011-05-27 11:43:46 -07001854void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1855 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1856 mColorA = 1.0f;
1857 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001858 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001859 }
1860}
1861
Romain Guy70ca14e2010-12-13 18:24:33 -08001862void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001863 // When the blending mode is kClear_Mode, we need to use a modulate color
1864 // argb=1,0,0,0
1865 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001866 bool blend = (mColorSet && mColorA < 1.0f) ||
1867 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1868 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001869}
1870
1871void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001872 // When the blending mode is kClear_Mode, we need to use a modulate color
1873 // argb=1,0,0,0
1874 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001875 blend |= (mColorSet && mColorA < 1.0f) ||
1876 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1877 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1878 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001879}
1880
1881void OpenGLRenderer::setupDrawProgram() {
1882 useProgram(mCaches.programCache.get(mDescription));
1883}
1884
1885void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1886 mTrackDirtyRegions = false;
1887}
1888
1889void OpenGLRenderer::setupDrawModelViewTranslate(float left, float top, float right, float bottom,
1890 bool ignoreTransform) {
1891 mModelView.loadTranslate(left, top, 0.0f);
1892 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001893 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
1894 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001895 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001896 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy70ca14e2010-12-13 18:24:33 -08001897 if (mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
1898 }
1899}
1900
Chet Haase8a5cc922011-04-26 07:28:09 -07001901void OpenGLRenderer::setupDrawModelViewIdentity(bool offset) {
Romain Guy3b753822013-03-05 10:27:35 -08001902 mCaches.currentProgram->set(mOrthoMatrix, mat4::identity(), currentTransform(), offset);
Romain Guy8d0d4782010-12-14 20:13:35 -08001903}
1904
Romain Guy70ca14e2010-12-13 18:24:33 -08001905void OpenGLRenderer::setupDrawModelView(float left, float top, float right, float bottom,
1906 bool ignoreTransform, bool ignoreModelView) {
1907 if (!ignoreModelView) {
1908 mModelView.loadTranslate(left, top, 0.0f);
1909 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001910 } else {
1911 mModelView.loadIdentity();
1912 }
Romain Guy86568192010-12-14 15:55:39 -08001913 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1914 if (!ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001915 mCaches.currentProgram->set(mOrthoMatrix, mModelView, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001916 if (mTrackDirtyRegions && dirty) {
Romain Guy3b753822013-03-05 10:27:35 -08001917 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001918 }
1919 } else {
Romain Guyc74f45a2013-02-26 19:10:14 -08001920 mCaches.currentProgram->set(mOrthoMatrix, mModelView, mat4::identity());
Romain Guy86568192010-12-14 15:55:39 -08001921 if (mTrackDirtyRegions && dirty) dirtyLayer(left, top, right, bottom);
1922 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001923}
1924
1925void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001926 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001927 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1928 }
1929}
1930
Romain Guy86568192010-12-14 15:55:39 -08001931void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001932 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001933 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001934 }
1935}
1936
Romain Guy70ca14e2010-12-13 18:24:33 -08001937void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001938 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001939 if (ignoreTransform) {
Romain Guy3b753822013-03-05 10:27:35 -08001940 mModelView.loadInverse(currentTransform());
Romain Guy70ca14e2010-12-13 18:24:33 -08001941 }
Chris Craikc3566d02013-02-04 16:16:33 -08001942 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1943 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001944 }
1945}
1946
Romain Guy8d0d4782010-12-14 20:13:35 -08001947void OpenGLRenderer::setupDrawShaderIdentityUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001948 if (mDrawModifiers.mShader) {
1949 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Romain Guyc74f45a2013-02-26 19:10:14 -08001950 mat4::identity(), *mSnapshot, &mTextureUnit);
Romain Guy8d0d4782010-12-14 20:13:35 -08001951 }
1952}
1953
Romain Guy70ca14e2010-12-13 18:24:33 -08001954void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001955 if (mDrawModifiers.mColorFilter) {
1956 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001957 }
1958}
1959
Romain Guy41210632012-07-16 17:04:24 -07001960void OpenGLRenderer::setupDrawTextGammaUniforms() {
1961 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1962}
1963
Romain Guy70ca14e2010-12-13 18:24:33 -08001964void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001965 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001966 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001967 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001968}
1969
1970void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001971 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001972 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001973 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001974}
1975
Romain Guyaa6c24c2011-04-28 18:40:04 -07001976void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1977 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001978 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001979 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001980}
1981
Romain Guy8f0095c2011-05-02 17:24:22 -07001982void OpenGLRenderer::setupDrawTextureTransform() {
1983 mDescription.hasTextureTransform = true;
1984}
1985
1986void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001987 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1988 GL_FALSE, &transform.data[0]);
1989}
1990
Romain Guy70ca14e2010-12-13 18:24:33 -08001991void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001992 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001993 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001994 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001995 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001996 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001997 }
Romain Guyd71dd362011-12-12 19:03:35 -08001998
Chris Craikcb4d6002012-09-25 12:00:29 -07001999 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002000 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002001 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08002002 }
2003
2004 mCaches.unbindIndicesBuffer();
2005}
2006
Romain Guyff316ec2013-02-13 18:39:43 -08002007void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
2008 bool force = mCaches.unbindMeshBuffer();
2009 GLsizei stride = sizeof(ColorTextureVertex);
2010
2011 mCaches.bindPositionVertexPointer(force, vertices, stride);
2012 if (mCaches.currentProgram->texCoords >= 0) {
2013 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
2014 }
2015 int slot = mCaches.currentProgram->getAttrib("colors");
2016 if (slot >= 0) {
2017 glEnableVertexAttribArray(slot);
2018 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2019 }
2020
2021 mCaches.unbindIndicesBuffer();
2022}
2023
Romain Guy3b748a42013-04-17 18:54:38 -07002024void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2025 bool force = false;
2026 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2027 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2028 // use the default VBO found in Caches
2029 if (!vertices || vbo) {
2030 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2031 } else {
2032 force = mCaches.unbindMeshBuffer();
2033 }
2034 mCaches.bindIndicesBuffer();
2035
Chris Craikcb4d6002012-09-25 12:00:29 -07002036 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002037 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002038 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002039 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002040}
2041
Romain Guy448455f2013-07-22 13:57:50 -07002042void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002043 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002044 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002045 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Romain Guy70ca14e2010-12-13 18:24:33 -08002046}
2047
2048///////////////////////////////////////////////////////////////////////////////
Romain Guy8ba548f2010-06-30 19:21:21 -07002049// Drawing
2050///////////////////////////////////////////////////////////////////////////////
2051
Chris Craikff785832013-03-08 13:12:16 -08002052status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2053 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002054 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002055 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2056 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002057 if (displayList && displayList->isRenderable()) {
Chris Craikd90144d2013-03-19 15:03:48 -07002058 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002059 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002060 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2061 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002062 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002063 }
2064
Chris Craik28ce94a2013-05-31 11:38:03 -07002065 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2066 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002067 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2068 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002069
2070 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002071 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002072
Chet Haase58d110a2013-04-10 07:43:29 -07002073 return status | deferredList.flush(*this, dirty);
Romain Guy0fe478e2010-11-08 12:08:41 -08002074 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002075
henry.uh_chen3a1bffa2014-07-03 18:01:37 +08002076 // Even if there is no drawing command(Ex: invisible),
2077 // it still needs startFrame to clear buffer and start tiling.
2078 return startFrame();
Romain Guy0fe478e2010-11-08 12:08:41 -08002079}
2080
Chris Craikc3566d02013-02-04 16:16:33 -08002081void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002082 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002083 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002084 }
2085}
2086
Romain Guya168d732011-03-18 16:50:13 -07002087void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2088 int alpha;
2089 SkXfermode::Mode mode;
2090 getAlphaAndMode(paint, &alpha, &mode);
2091
Romain Guy886b2752013-01-04 12:26:18 -08002092 int color = paint != NULL ? paint->getColor() : 0;
2093
Romain Guya168d732011-03-18 16:50:13 -07002094 float x = left;
2095 float y = top;
2096
Romain Guy886b2752013-01-04 12:26:18 -08002097 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2098
Romain Guya168d732011-03-18 16:50:13 -07002099 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002100 if (currentTransform().isPureTranslate()) {
2101 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2102 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002103 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002104
2105 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002106 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002107 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002108 }
2109
Romain Guy3b748a42013-04-17 18:54:38 -07002110 // No need to check for a UV mapper on the texture object, only ARGB_8888
2111 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002112 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002113 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2114 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002115}
2116
Romain Guy03c00b52013-06-20 18:30:28 -07002117/**
2118 * Important note: this method is intended to draw batches of bitmaps and
2119 * will not set the scissor enable or dirty the current layer, if any.
2120 * The caller is responsible for properly dirtying the current layer.
2121 */
Romain Guy55b6f952013-06-27 15:27:09 -07002122status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002123 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002124 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002125 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002126 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002127
Chris Craik527a3aa2013-03-04 10:19:31 -08002128 const AutoTexture autoCleanup(texture);
2129
2130 int alpha;
2131 SkXfermode::Mode mode;
2132 getAlphaAndMode(paint, &alpha, &mode);
2133
2134 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002135 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002136
2137 const float x = (int) floorf(bounds.left + 0.5f);
2138 const float y = (int) floorf(bounds.top + 0.5f);
2139 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2140 int color = paint != NULL ? paint->getColor() : 0;
2141 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2142 texture->id, paint != NULL, color, alpha, mode,
2143 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002144 GL_TRIANGLES, bitmapCount * 6, true, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002145 } else {
2146 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2147 texture->id, alpha / 255.0f, mode, texture->blend,
2148 &vertices[0].position[0], &vertices[0].texture[0],
Romain Guy03c00b52013-06-20 18:30:28 -07002149 GL_TRIANGLES, bitmapCount * 6, false, true, 0, true, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002150 }
2151
2152 return DrawGlInfo::kStatusDrew;
2153}
2154
Chet Haase48659092012-05-31 15:21:51 -07002155status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002156 const float right = left + bitmap->width();
2157 const float bottom = top + bitmap->height();
2158
2159 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002160 return DrawGlInfo::kStatusDone;
Romain Guy8ba548f2010-06-30 19:21:21 -07002161 }
2162
Romain Guya1d3c912011-12-13 14:55:06 -08002163 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002164 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002165 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy8ba548f2010-06-30 19:21:21 -07002166 const AutoTexture autoCleanup(texture);
2167
Romain Guy211370f2012-02-01 16:10:55 -08002168 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002169 drawAlphaBitmap(texture, left, top, paint);
2170 } else {
2171 drawTextureRect(left, top, right, bottom, texture, paint);
2172 }
Chet Haase48659092012-05-31 15:21:51 -07002173
2174 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002175}
2176
Chet Haase48659092012-05-31 15:21:51 -07002177status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guy8ba548f2010-06-30 19:21:21 -07002178 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2179 const mat4 transform(*matrix);
2180 transform.mapRect(r);
2181
2182 if (quickReject(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002183 return DrawGlInfo::kStatusDone;
Romain Guy8ba548f2010-06-30 19:21:21 -07002184 }
2185
Romain Guya1d3c912011-12-13 14:55:06 -08002186 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002187 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002188 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy8ba548f2010-06-30 19:21:21 -07002189 const AutoTexture autoCleanup(texture);
2190
Romain Guy5b3b3522010-10-27 18:57:51 -07002191 // This could be done in a cheaper way, all we need is pass the matrix
2192 // to the vertex shader. The save/restore is a bit overkill.
2193 save(SkCanvas::kMatrix_SaveFlag);
2194 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002195 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2196 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2197 } else {
2198 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2199 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002200 restore();
Chet Haase48659092012-05-31 15:21:51 -07002201
2202 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002203}
2204
Chet Haase48659092012-05-31 15:21:51 -07002205status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002206 const float right = left + bitmap->width();
2207 const float bottom = top + bitmap->height();
2208
2209 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002210 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002211 }
2212
2213 mCaches.activeTexture(0);
2214 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2215 const AutoTexture autoCleanup(texture);
2216
Romain Guy886b2752013-01-04 12:26:18 -08002217 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2218 drawAlphaBitmap(texture, left, top, paint);
2219 } else {
2220 drawTextureRect(left, top, right, bottom, texture, paint);
2221 }
Chet Haase48659092012-05-31 15:21:51 -07002222
2223 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002224}
2225
Chet Haase48659092012-05-31 15:21:51 -07002226status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002227 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002228 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002229 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002230 }
2231
Chris Craik39a908c2013-06-13 14:39:01 -07002232 // TODO: use quickReject on bounds from vertices
2233 mCaches.enableScissor();
2234
Romain Guyb18d2d02011-02-10 15:52:54 -08002235 float left = FLT_MAX;
2236 float top = FLT_MAX;
2237 float right = FLT_MIN;
2238 float bottom = FLT_MIN;
2239
Romain Guya92bb4d2012-10-16 11:08:44 -07002240 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002241
Chih-Hung Hsiehd3448e42014-09-15 14:28:52 -07002242 Vector<ColorTextureVertex> meshVector; // TODO: use C++11 unique_ptr
2243 meshVector.setCapacity(count);
2244 ColorTextureVertex* mesh = meshVector.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002245 ColorTextureVertex* vertex = mesh;
2246
2247 bool cleanupColors = false;
2248 if (!colors) {
2249 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2250 colors = new int[colorsCount];
2251 memset(colors, 0xff, colorsCount * sizeof(int));
2252 cleanupColors = true;
2253 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002254
Romain Guy3b748a42013-04-17 18:54:38 -07002255 mCaches.activeTexture(0);
2256 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2257 const UvMapper& mapper(getMapper(texture));
2258
Romain Guy5a7b4662011-01-20 19:09:30 -08002259 for (int32_t y = 0; y < meshHeight; y++) {
2260 for (int32_t x = 0; x < meshWidth; x++) {
2261 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2262
2263 float u1 = float(x) / meshWidth;
2264 float u2 = float(x + 1) / meshWidth;
2265 float v1 = float(y) / meshHeight;
2266 float v2 = float(y + 1) / meshHeight;
2267
Romain Guy3b748a42013-04-17 18:54:38 -07002268 mapper.map(u1, v1, u2, v2);
2269
Romain Guy5a7b4662011-01-20 19:09:30 -08002270 int ax = i + (meshWidth + 1) * 2;
2271 int ay = ax + 1;
2272 int bx = i;
2273 int by = bx + 1;
2274 int cx = i + 2;
2275 int cy = cx + 1;
2276 int dx = i + (meshWidth + 1) * 2 + 2;
2277 int dy = dx + 1;
2278
Romain Guyff316ec2013-02-13 18:39:43 -08002279 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2280 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2281 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002282
Romain Guyff316ec2013-02-13 18:39:43 -08002283 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2284 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2285 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002286
Romain Guya92bb4d2012-10-16 11:08:44 -07002287 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2288 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2289 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2290 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002291 }
2292 }
2293
Romain Guya92bb4d2012-10-16 11:08:44 -07002294 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002295 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002296 return DrawGlInfo::kStatusDone;
2297 }
2298
Romain Guyff316ec2013-02-13 18:39:43 -08002299 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002300 texture = mCaches.textureCache.get(bitmap);
2301 if (!texture) {
2302 if (cleanupColors) delete[] colors;
2303 return DrawGlInfo::kStatusDone;
2304 }
Romain Guyff316ec2013-02-13 18:39:43 -08002305 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002306 const AutoTexture autoCleanup(texture);
2307
2308 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2309 texture->setFilter(FILTER(paint), true);
2310
2311 int alpha;
2312 SkXfermode::Mode mode;
2313 getAlphaAndMode(paint, &alpha, &mode);
2314
Romain Guyff316ec2013-02-13 18:39:43 -08002315 float a = alpha / 255.0f;
2316
Romain Guya92bb4d2012-10-16 11:08:44 -07002317 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002318 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002319 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002320
Romain Guyff316ec2013-02-13 18:39:43 -08002321 setupDraw();
2322 setupDrawWithTextureAndColor();
2323 setupDrawColor(a, a, a, a);
2324 setupDrawColorFilter();
2325 setupDrawBlending(true, mode, false);
2326 setupDrawProgram();
2327 setupDrawDirtyRegionsDisabled();
2328 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2329 setupDrawTexture(texture->id);
2330 setupDrawPureColorUniforms();
2331 setupDrawColorFilterUniforms();
2332 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2333
2334 glDrawArrays(GL_TRIANGLES, 0, count);
2335
Romain Guyff316ec2013-02-13 18:39:43 -08002336 int slot = mCaches.currentProgram->getAttrib("colors");
2337 if (slot >= 0) {
2338 glDisableVertexAttribArray(slot);
2339 }
2340
2341 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002342
2343 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002344}
2345
Chet Haase48659092012-05-31 15:21:51 -07002346status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guyc7d53492010-06-25 13:41:57 -07002347 float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guy026c5e162010-06-28 17:12:22 -07002348 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002349 SkPaint* paint) {
Romain Guy9d5316e2010-06-24 19:30:36 -07002350 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002351 return DrawGlInfo::kStatusDone;
Romain Guy026c5e162010-06-28 17:12:22 -07002352 }
2353
Romain Guya1d3c912011-12-13 14:55:06 -08002354 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002355 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002356 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy026c5e162010-06-28 17:12:22 -07002357 const AutoTexture autoCleanup(texture);
2358
2359 const float width = texture->width;
2360 const float height = texture->height;
2361
Romain Guy3b748a42013-04-17 18:54:38 -07002362 float u1 = fmax(0.0f, srcLeft / width);
2363 float v1 = fmax(0.0f, srcTop / height);
2364 float u2 = fmin(1.0f, srcRight / width);
2365 float v2 = fmin(1.0f, srcBottom / height);
2366
2367 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy026c5e162010-06-28 17:12:22 -07002368
Romain Guy03750a02010-10-18 14:06:08 -07002369 mCaches.unbindMeshBuffer();
Romain Guy026c5e162010-06-28 17:12:22 -07002370 resetDrawTextureTexCoords(u1, v1, u2, v2);
Romain Guyc7d53492010-06-25 13:41:57 -07002371
Romain Guy03750a02010-10-18 14:06:08 -07002372 int alpha;
2373 SkXfermode::Mode mode;
2374 getAlphaAndMode(paint, &alpha, &mode);
2375
Romain Guyd21b6e12011-11-30 20:21:23 -08002376 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2377
Romain Guy886b2752013-01-04 12:26:18 -08002378 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2379 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002380
Romain Guy886b2752013-01-04 12:26:18 -08002381 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2382 // Apply a scale transform on the canvas only when a shader is in use
2383 // Skia handles the ratio between the dst and src rects as a scale factor
2384 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002385 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002386 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002387
Romain Guy3b753822013-03-05 10:27:35 -08002388 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2389 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2390 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002391
2392 dstRight = x + (dstRight - dstLeft);
2393 dstBottom = y + (dstBottom - dstTop);
2394
2395 dstLeft = x;
2396 dstTop = y;
2397
2398 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2399 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002400 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002401 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002402 }
2403
2404 if (CC_UNLIKELY(useScaleTransform)) {
2405 save(SkCanvas::kMatrix_SaveFlag);
2406 translate(dstLeft, dstTop);
2407 scale(scaleX, scaleY);
2408
2409 dstLeft = 0.0f;
2410 dstTop = 0.0f;
2411
2412 dstRight = srcRight - srcLeft;
2413 dstBottom = srcBottom - srcTop;
2414 }
2415
2416 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2417 int color = paint ? paint->getColor() : 0;
2418 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2419 texture->id, paint != NULL, color, alpha, mode,
2420 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2421 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2422 } else {
2423 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2424 texture->id, alpha / 255.0f, mode, texture->blend,
2425 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2426 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2427 }
2428
2429 if (CC_UNLIKELY(useScaleTransform)) {
2430 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002431 }
Romain Guybb9524b2010-06-22 18:56:38 -07002432
2433 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002434
2435 return DrawGlInfo::kStatusDrew;
Romain Guybb9524b2010-06-22 18:56:38 -07002436}
2437
Romain Guy3b748a42013-04-17 18:54:38 -07002438status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002439 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002440 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002441 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002442 }
2443
Romain Guy4c2547f2013-06-11 16:19:24 -07002444 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002445 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2446 right - left, bottom - top, patch);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002447
Romain Guy03c00b52013-06-20 18:30:28 -07002448 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002449}
2450
Romain Guy03c00b52013-06-20 18:30:28 -07002451status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2452 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002453 if (quickReject(left, top, right, bottom)) {
2454 return DrawGlInfo::kStatusDone;
2455 }
Romain Guyf7f93552010-07-08 19:17:03 -07002456
Romain Guy211370f2012-02-01 16:10:55 -08002457 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002458 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002459 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002460 if (!texture) return DrawGlInfo::kStatusDone;
2461 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002462
Romain Guya4adcf02013-02-28 12:15:35 -08002463 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2464 texture->setFilter(GL_LINEAR, true);
2465
Romain Guy03c00b52013-06-20 18:30:28 -07002466 int alpha;
2467 SkXfermode::Mode mode;
2468 getAlphaAndMode(paint, &alpha, &mode);
2469
Romain Guy3b753822013-03-05 10:27:35 -08002470 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002471 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002472 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002473 const float offsetX = left + currentTransform().getTranslateX();
2474 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002475 const size_t count = mesh->quads.size();
2476 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002477 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002478 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002479 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2480 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2481 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002482 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002483 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002484 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002485 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002486 }
2487 }
2488
Romain Guy211370f2012-02-01 16:10:55 -08002489 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002490 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2491 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002492
Romain Guy3b748a42013-04-17 18:54:38 -07002493 right = x + right - left;
2494 bottom = y + bottom - top;
2495 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2496 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2497 GL_TRIANGLES, mesh->indexCount, false, true,
2498 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002499 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002500 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2501 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2502 GL_TRIANGLES, mesh->indexCount, false, false,
2503 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002504 }
Romain Guy054dc182010-10-15 17:55:25 -07002505 }
Chet Haase48659092012-05-31 15:21:51 -07002506
2507 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002508}
2509
Romain Guy03c00b52013-06-20 18:30:28 -07002510/**
2511 * Important note: this method is intended to draw batches of 9-patch objects and
2512 * will not set the scissor enable or dirty the current layer, if any.
2513 * The caller is responsible for properly dirtying the current layer.
2514 */
2515status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2516 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2517 mCaches.activeTexture(0);
2518 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2519 if (!texture) return DrawGlInfo::kStatusDone;
2520 const AutoTexture autoCleanup(texture);
2521
2522 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2523 texture->setFilter(GL_LINEAR, true);
2524
2525 int alpha;
2526 SkXfermode::Mode mode;
2527 getAlphaAndMode(paint, &alpha, &mode);
2528
2529 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2530 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2531 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2532
2533 return DrawGlInfo::kStatusDrew;
2534}
2535
Chris Craik65cd6122012-12-10 17:56:27 -08002536status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2537 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002538 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002539 // no vertices to draw
2540 return DrawGlInfo::kStatusDone;
2541 }
2542
Chris Craikcb4d6002012-09-25 12:00:29 -07002543 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002544 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2545 bool isAA = paint->isAntiAlias();
2546
Chris Craik710f46d2012-09-17 17:25:49 -07002547 setupDraw();
2548 setupDrawNoTexture();
2549 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002550 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2551 setupDrawColorFilter();
2552 setupDrawShader();
2553 setupDrawBlending(isAA, mode);
2554 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002555 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002556 setupDrawColorUniforms();
2557 setupDrawColorFilterUniforms();
2558 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002559
Chris Craik710f46d2012-09-17 17:25:49 -07002560 void* vertices = vertexBuffer.getBuffer();
2561 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002562 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002563 mCaches.resetTexCoordsVertexPointer();
2564 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002565
Chris Craik710f46d2012-09-17 17:25:49 -07002566 int alphaSlot = -1;
2567 if (isAA) {
2568 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2569 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002570
Chris Craik710f46d2012-09-17 17:25:49 -07002571 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002572 glEnableVertexAttribArray(alphaSlot);
2573 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002574 }
Romain Guy04299382012-07-18 17:15:41 -07002575
Chris Craik6d29c8d2013-05-08 18:35:44 -07002576 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002577
Chris Craik710f46d2012-09-17 17:25:49 -07002578 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002579 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002580 }
Chris Craik65cd6122012-12-10 17:56:27 -08002581
2582 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002583}
2584
2585/**
Chris Craik65cd6122012-12-10 17:56:27 -08002586 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2587 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2588 * screen space in all directions. However, instead of using a fragment shader to compute the
2589 * translucency of the color from its position, we simply use a varying parameter to define how far
2590 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2591 *
2592 * Doesn't yet support joins, caps, or path effects.
2593 */
2594status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2595 VertexBuffer vertexBuffer;
2596 // TODO: try clipping large paths to viewport
2597 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2598
Romain Guyc46d07a2013-03-15 19:06:39 -07002599 if (hasLayer()) {
2600 SkRect bounds = path.getBounds();
2601 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2602 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2603 }
Chris Craik65cd6122012-12-10 17:56:27 -08002604
2605 return drawVertexBuffer(vertexBuffer, paint);
2606}
2607
2608/**
2609 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2610 * and additional geometry for defining an alpha slope perimeter.
2611 *
2612 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2613 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2614 * in-shader alpha region, but found it to be taxing on some GPUs.
2615 *
2616 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2617 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002618 */
Chet Haase48659092012-05-31 15:21:51 -07002619status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002620 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002621
Chris Craik65cd6122012-12-10 17:56:27 -08002622 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002623
Chris Craik65cd6122012-12-10 17:56:27 -08002624 VertexBuffer buffer;
2625 SkRect bounds;
2626 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002627
2628 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2629 return DrawGlInfo::kStatusDone;
2630 }
2631
Romain Guy3b753822013-03-05 10:27:35 -08002632 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002633
Chris Craik65cd6122012-12-10 17:56:27 -08002634 bool useOffset = !paint->isAntiAlias();
2635 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002636}
2637
Chet Haase48659092012-05-31 15:21:51 -07002638status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002639 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002640
Chris Craik6d29c8d2013-05-08 18:35:44 -07002641 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002642
Chris Craik6d29c8d2013-05-08 18:35:44 -07002643 VertexBuffer buffer;
2644 SkRect bounds;
2645 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002646
Chris Craik6d29c8d2013-05-08 18:35:44 -07002647 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2648 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002649 }
2650
Chris Craik6d29c8d2013-05-08 18:35:44 -07002651 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002652
Chris Craik6d29c8d2013-05-08 18:35:44 -07002653 bool useOffset = !paint->isAntiAlias();
2654 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002655}
2656
Chet Haase48659092012-05-31 15:21:51 -07002657status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002658 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002659 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002660
Romain Guyae88e5e2010-10-22 17:49:18 -07002661 Rect& clip(*mSnapshot->clipRect);
2662 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002663
Romain Guy3d58c032010-07-14 16:34:53 -07002664 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002665
2666 return DrawGlInfo::kStatusDrew;
Romain Guybb9524b2010-06-22 18:56:38 -07002667}
2668
Chet Haase48659092012-05-31 15:21:51 -07002669status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2670 SkPaint* paint) {
2671 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002672 const AutoTexture autoCleanup(texture);
2673
2674 const float x = left + texture->left - texture->offset;
2675 const float y = top + texture->top - texture->offset;
2676
2677 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002678
2679 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002680}
2681
Chet Haase48659092012-05-31 15:21:51 -07002682status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002683 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002684 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2685 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002686 return DrawGlInfo::kStatusDone;
2687 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002688
Chris Craikcb4d6002012-09-25 12:00:29 -07002689 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002690 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002691 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002692 right - left, bottom - top, rx, ry, p);
2693 return drawShape(left, top, texture, p);
2694 }
2695
2696 SkPath path;
2697 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002698 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2699 float outset = p->getStrokeWidth() / 2;
2700 rect.outset(outset, outset);
2701 rx += outset;
2702 ry += outset;
2703 }
Chris Craik710f46d2012-09-17 17:25:49 -07002704 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002705 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002706}
2707
Chris Craik710f46d2012-09-17 17:25:49 -07002708status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002709 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002710 x + radius, y + radius, p) ||
2711 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002712 return DrawGlInfo::kStatusDone;
2713 }
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.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002717 return drawShape(x - radius, y - radius, texture, p);
2718 }
2719
2720 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002721 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2722 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2723 } else {
2724 path.addCircle(x, y, radius);
2725 }
Chris Craik65cd6122012-12-10 17:56:27 -08002726 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002727}
Romain Guy01d58e42011-01-19 21:54:02 -08002728
Chet Haase48659092012-05-31 15:21:51 -07002729status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002730 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 Craik710f46d2012-09-17 17:25:49 -07002733 return DrawGlInfo::kStatusDone;
2734 }
Romain Guy01d58e42011-01-19 21:54:02 -08002735
Chris Craikcb4d6002012-09-25 12:00:29 -07002736 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002737 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002738 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002739 return drawShape(left, top, texture, p);
2740 }
2741
2742 SkPath path;
2743 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002744 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2745 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2746 }
Chris Craik710f46d2012-09-17 17:25:49 -07002747 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002748 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002749}
2750
Chet Haase48659092012-05-31 15:21:51 -07002751status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002752 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002753 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2754 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002755 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002756 }
2757
Chris Craik780c1282012-10-04 14:10:49 -07002758 if (fabs(sweepAngle) >= 360.0f) {
2759 return drawOval(left, top, right, bottom, p);
2760 }
2761
2762 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002763 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002764 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002765 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002766 startAngle, sweepAngle, useCenter, p);
2767 return drawShape(left, top, texture, p);
2768 }
2769
2770 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2771 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2772 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2773 }
2774
2775 SkPath path;
2776 if (useCenter) {
2777 path.moveTo(rect.centerX(), rect.centerY());
2778 }
2779 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2780 if (useCenter) {
2781 path.close();
2782 }
Chris Craik65cd6122012-12-10 17:56:27 -08002783 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002784}
2785
Romain Guycf8675e2012-10-02 12:32:25 -07002786// See SkPaintDefaults.h
2787#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2788
Chet Haase48659092012-05-31 15:21:51 -07002789status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002790 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2791 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002792 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002793 }
2794
Chris Craik710f46d2012-09-17 17:25:49 -07002795 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002796 // only fill style is supported by drawConvexPath, since others have to handle joins
2797 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2798 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2799 mCaches.activeTexture(0);
2800 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002801 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002802 return drawShape(left, top, texture, p);
2803 }
2804
2805 SkPath path;
2806 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2807 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2808 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2809 }
2810 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002811 return drawConvexPath(path, p);
Romain Guyf6a11b82010-06-23 17:47:49 -07002812 }
2813
Romain Guy3b753822013-03-05 10:27:35 -08002814 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002815 SkPath path;
2816 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002817 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002818 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002819 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002820 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002821 }
Romain Guy9d5316e2010-06-24 19:30:36 -07002822}
2823
Raph Levien416a8472012-07-19 22:48:17 -07002824void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2825 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2826 float x, float y) {
2827 mCaches.activeTexture(0);
2828
2829 // NOTE: The drop shadow will not perform gamma correction
2830 // if shader-based correction is enabled
2831 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2832 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002833 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002834 // If the drop shadow exceeds the max texture size or couldn't be
2835 // allocated, skip drawing
2836 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002837 const AutoTexture autoCleanup(shadow);
2838
Chris Craikc3566d02013-02-04 16:16:33 -08002839 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2840 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002841
Chris Craikc3566d02013-02-04 16:16:33 -08002842 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2843 int shadowColor = mDrawModifiers.mShadowColor;
2844 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002845 shadowColor = 0xffffffff;
2846 }
2847
2848 setupDraw();
2849 setupDrawWithTexture(true);
2850 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2851 setupDrawColorFilter();
2852 setupDrawShader();
2853 setupDrawBlending(true, mode);
2854 setupDrawProgram();
2855 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2856 setupDrawTexture(shadow->id);
2857 setupDrawPureColorUniforms();
2858 setupDrawColorFilterUniforms();
2859 setupDrawShaderUniforms();
2860 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2861
2862 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2863}
2864
Romain Guy768bffc2013-02-27 13:50:45 -08002865bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2866 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2867 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2868}
2869
Chet Haase48659092012-05-31 15:21:51 -07002870status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002871 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002872 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002873 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002874 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002875
Romain Guy671d6cf2012-01-18 12:39:17 -08002876 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002877 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002878 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002879 }
2880
Chris Craik39a908c2013-06-13 14:39:01 -07002881 mCaches.enableScissor();
2882
Romain Guy671d6cf2012-01-18 12:39:17 -08002883 float x = 0.0f;
2884 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002885 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002886 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002887 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2888 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002889 }
2890
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002891 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002892 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002893
2894 int alpha;
2895 SkXfermode::Mode mode;
2896 getAlphaAndMode(paint, &alpha, &mode);
2897
Chris Craikc3566d02013-02-04 16:16:33 -08002898 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002899 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2900 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002901 }
2902
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002904 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002905 if (pureTranslate && !linearFilter) {
2906 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2907 }
Romain Guy257ae352013-03-20 16:31:12 -07002908 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002909
2910 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2911 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2912
Romain Guy211370f2012-02-01 16:10:55 -08002913 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002914
Victoria Lease1e546812013-06-25 14:25:17 -07002915 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002916 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002917 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002918 if (hasActiveLayer) {
2919 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002920 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002921 }
2922 dirtyLayerUnchecked(bounds, getRegion());
2923 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002924 }
Chet Haase48659092012-05-31 15:21:51 -07002925
2926 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002927}
2928
Romain Guy624234f2013-03-05 16:43:31 -08002929mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2930 mat4 fontTransform;
2931 if (CC_LIKELY(transform.isPureTranslate())) {
2932 fontTransform = mat4::identity();
2933 } else {
2934 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002935 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002936 } else {
2937 float sx, sy;
2938 currentTransform().decomposeScale(sx, sy);
2939 fontTransform.loadScale(sx, sy, 1.0f);
2940 }
2941 }
2942 return fontTransform;
2943}
2944
Chris Craik41541822013-05-03 16:35:54 -07002945status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2946 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002947 DrawOpMode drawOpMode) {
2948
Chris Craik527a3aa2013-03-04 10:19:31 -08002949 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002950 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2951 // drawing as ops from DeferredDisplayList are already filtered for these
2952 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2953 quickReject(bounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002954 return DrawGlInfo::kStatusDone;
2955 }
Romain Guycac5fd32011-12-01 20:08:50 -08002956 }
2957
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002958 const float oldX = x;
2959 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002960
2961 const mat4& transform = currentTransform();
2962 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002963
Romain Guy211370f2012-02-01 16:10:55 -08002964 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002965 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2966 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002967 }
2968
Romain Guy86568192010-12-14 15:55:39 -08002969 int alpha;
2970 SkXfermode::Mode mode;
2971 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002972
Romain Guyc74f45a2013-02-26 19:10:14 -08002973 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2974
Chris Craikc3566d02013-02-04 16:16:33 -08002975 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002976 fontRenderer.setFont(paint, mat4::identity());
2977 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2978 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002979 }
2980
Romain Guy19d4dd82013-03-04 11:14:26 -08002981 const bool hasActiveLayer = hasLayer();
2982
Romain Guy624234f2013-03-05 16:43:31 -08002983 // We only pass a partial transform to the font renderer. That partial
2984 // matrix defines how glyphs are rasterized. Typically we want glyphs
2985 // to be rasterized at their final size on screen, which means the partial
2986 // matrix needs to take the scale factor into account.
2987 // When a partial matrix is used to transform glyphs during rasterization,
2988 // the mesh is generated with the inverse transform (in the case of scale,
2989 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2990 // apply the full transform matrix at draw time in the vertex shader.
2991 // Applying the full matrix in the shader is the easiest way to handle
2992 // rotation and perspective and allows us to always generated quads in the
2993 // font renderer which greatly simplifies the code, clipping in particular.
2994 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002995 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002996
Romain Guy6620c6d2010-12-06 18:07:02 -08002997 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002998 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002999 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003000
Romain Guy624234f2013-03-05 16:43:31 -08003001 // TODO: Implement better clipping for scaled/rotated text
3002 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003003 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 -07003004
Raph Levien996e57c2012-07-23 15:22:52 -07003005 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003006 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003007
3008 // don't call issuedrawcommand, do it at end of batch
3009 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003010 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003011 SkPaint paintCopy(*paint);
3012 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3013 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003014 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003015 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003016 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003017 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003018 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003019
Chris Craik527a3aa2013-03-04 10:19:31 -08003020 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003021 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003022 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003023 }
Chris Craik41541822013-05-03 16:35:54 -07003024 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003025 }
Romain Guy694b5192010-07-21 21:33:20 -07003026
Chris Craik41541822013-05-03 16:35:54 -07003027 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003028
3029 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003030}
3031
Chet Haase48659092012-05-31 15:21:51 -07003032status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003033 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003034 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003035 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003036 }
3037
Chris Craik39a908c2013-06-13 14:39:01 -07003038 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3039 mCaches.enableScissor();
3040
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003041 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003042 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003043 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003044
3045 int alpha;
3046 SkXfermode::Mode mode;
3047 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003048 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003049
Romain Guy97771732012-02-28 18:17:02 -08003050 const Rect* clip = &mSnapshot->getLocalClip();
3051 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 -08003052
Romain Guy97771732012-02-28 18:17:02 -08003053 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003054
3055 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003056 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003057 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003058 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003059 dirtyLayerUnchecked(bounds, getRegion());
3060 }
Romain Guy97771732012-02-28 18:17:02 -08003061 }
Chet Haase48659092012-05-31 15:21:51 -07003062
3063 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003064}
3065
Chet Haase48659092012-05-31 15:21:51 -07003066status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3067 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003068
Romain Guya1d3c912011-12-13 14:55:06 -08003069 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003070
Romain Guyfb8b7632010-08-23 21:05:08 -07003071 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003072 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003073 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003074
Romain Guy8b55f372010-08-18 17:10:07 -07003075 const float x = texture->left - texture->offset;
3076 const float y = texture->top - texture->offset;
3077
Romain Guy01d58e42011-01-19 21:54:02 -08003078 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003079
3080 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003081}
3082
Chris Craika08f95c2013-03-15 17:24:33 -07003083status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003084 if (!layer) {
3085 return DrawGlInfo::kStatusDone;
3086 }
3087
Romain Guyb2e2f242012-10-17 18:18:35 -07003088 mat4* transform = NULL;
3089 if (layer->isTextureLayer()) {
3090 transform = &layer->getTransform();
3091 if (!transform->isIdentity()) {
3092 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003093 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003094 }
3095 }
3096
Chris Craik39a908c2013-06-13 14:39:01 -07003097 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003098 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003099 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003100
3101 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003102 if (transform && !transform->isIdentity()) {
3103 restore();
3104 }
Chet Haase48659092012-05-31 15:21:51 -07003105 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003106 }
3107
Romain Guy5bb3c732012-11-29 17:52:58 -08003108 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003109
Chris Craik39a908c2013-06-13 14:39:01 -07003110 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003111 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003112
Romain Guy211370f2012-02-01 16:10:55 -08003113 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003114 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3115 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003116
Romain Guyc88e3572011-01-22 00:32:12 -08003117 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003118 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3119 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003120 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003121 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003122 setupDraw();
3123 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003124 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003125 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003126 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003127 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003128 setupDrawPureColorUniforms();
3129 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003130 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003131 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3132 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3133 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003134
Romain Guyd21b6e12011-11-30 20:21:23 -08003135 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003136 setupDrawModelViewTranslate(tx, ty,
3137 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003138 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003139 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003140 setupDrawModelViewTranslate(x, y,
3141 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3142 }
Romain Guyf219da52011-01-16 12:54:25 -08003143
Romain Guy448455f2013-07-22 13:57:50 -07003144 TextureVertex* mesh = &layer->mesh[0];
3145 GLsizei elementsCount = layer->meshElementCount;
Romain Guyf219da52011-01-16 12:54:25 -08003146
Romain Guy448455f2013-07-22 13:57:50 -07003147 while (elementsCount > 0) {
3148 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3149
3150 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3151 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3152 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3153
3154 elementsCount -= drawCount;
3155 // Though there are 4 vertices in a quad, we use 6 indices per
3156 // quad to draw with GL_TRIANGLES
3157 mesh += (drawCount / 6) * 4;
3158 }
Romain Guy3a3133d2011-02-01 22:59:58 -08003159
3160#if DEBUG_LAYERS_AS_REGIONS
3161 drawRegionRects(layer->region);
3162#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003163 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003164
Chris Craikc3566d02013-02-04 16:16:33 -08003165 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003166
Romain Guy5bb3c732012-11-29 17:52:58 -08003167 if (layer->debugDrawUpdate) {
3168 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003169 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3170 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3171 }
Romain Guyf219da52011-01-16 12:54:25 -08003172 }
Chris Craik34416ea2013-04-15 16:08:28 -07003173 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003174
Romain Guyb2e2f242012-10-17 18:18:35 -07003175 if (transform && !transform->isIdentity()) {
3176 restore();
3177 }
3178
Chet Haase48659092012-05-31 15:21:51 -07003179 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003180}
3181
Romain Guy6926c722010-07-12 20:20:03 -07003182///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003183// Shaders
3184///////////////////////////////////////////////////////////////////////////////
3185
3186void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003187 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003188}
3189
Romain Guy06f96e22010-07-30 19:18:16 -07003190void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003191 mDrawModifiers.mShader = shader;
3192 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003193 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003194 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003195}
3196
Romain Guyd27977d2010-07-14 19:18:51 -07003197///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003198// Color filters
3199///////////////////////////////////////////////////////////////////////////////
3200
3201void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003202 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003203}
3204
3205void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003206 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003207}
3208
3209///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003210// Drop shadow
3211///////////////////////////////////////////////////////////////////////////////
3212
3213void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003214 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003215}
3216
3217void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003218 mDrawModifiers.mHasShadow = true;
3219 mDrawModifiers.mShadowRadius = radius;
3220 mDrawModifiers.mShadowDx = dx;
3221 mDrawModifiers.mShadowDy = dy;
3222 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003223}
3224
3225///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003226// Draw filters
3227///////////////////////////////////////////////////////////////////////////////
3228
3229void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003230 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3231 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003232 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003233 mDrawModifiers.mPaintFilterClearBits = 0;
3234 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003235}
3236
3237void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003238 mDrawModifiers.mHasDrawFilter = true;
3239 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3240 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003241}
3242
Chris Craika08f95c2013-03-15 17:24:33 -07003243SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003244 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003245 return paint;
3246 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003247
3248 uint32_t flags = paint->getFlags();
3249
3250 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003251 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3252 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003253
3254 return &mFilteredPaint;
3255}
3256
3257///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003258// Drawing implementation
3259///////////////////////////////////////////////////////////////////////////////
3260
Romain Guy3b748a42013-04-17 18:54:38 -07003261Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3262 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3263 if (!texture) {
3264 return mCaches.textureCache.get(bitmap);
3265 }
3266 return texture;
3267}
3268
Romain Guy01d58e42011-01-19 21:54:02 -08003269void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3270 float x, float y, SkPaint* paint) {
3271 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3272 return;
3273 }
3274
3275 int alpha;
3276 SkXfermode::Mode mode;
3277 getAlphaAndMode(paint, &alpha, &mode);
3278
3279 setupDraw();
3280 setupDrawWithTexture(true);
3281 setupDrawAlpha8Color(paint->getColor(), alpha);
3282 setupDrawColorFilter();
3283 setupDrawShader();
3284 setupDrawBlending(true, mode);
3285 setupDrawProgram();
3286 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3287 setupDrawTexture(texture->id);
3288 setupDrawPureColorUniforms();
3289 setupDrawColorFilterUniforms();
3290 setupDrawShaderUniforms();
3291 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3292
3293 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003294}
3295
Romain Guyf607bdc2010-09-10 19:20:06 -07003296// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003297#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3298#define kStdUnderline_Offset (1.0f / 9.0f)
3299#define kStdUnderline_Thickness (1.0f / 18.0f)
3300
Chris Craik41541822013-05-03 16:35:54 -07003301void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003302 float x, float y, SkPaint* paint) {
3303 // Handle underline and strike-through
3304 uint32_t flags = paint->getFlags();
3305 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003306 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003307
Romain Guy211370f2012-02-01 16:10:55 -08003308 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003309 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003310 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003311
Raph Levien8b4072d2012-07-30 15:50:00 -07003312 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003313 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003314
Romain Guyf6834472011-01-23 13:32:12 -08003315 int linesCount = 0;
3316 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3317 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3318
3319 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003320 float points[pointsCount];
3321 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003322
3323 if (flags & SkPaint::kUnderlineText_Flag) {
3324 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003325 points[currentPoint++] = left;
3326 points[currentPoint++] = top;
3327 points[currentPoint++] = left + underlineWidth;
3328 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003329 }
3330
3331 if (flags & SkPaint::kStrikeThruText_Flag) {
3332 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003333 points[currentPoint++] = left;
3334 points[currentPoint++] = top;
3335 points[currentPoint++] = left + underlineWidth;
3336 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003337 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003338
Romain Guy726aeba2011-06-01 14:52:00 -07003339 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003340
Romain Guy726aeba2011-06-01 14:52:00 -07003341 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003342 }
3343 }
3344}
3345
Romain Guy672433d2013-01-04 19:05:13 -08003346status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3347 if (mSnapshot->isIgnored()) {
3348 return DrawGlInfo::kStatusDone;
3349 }
3350
Romain Guy735738c2012-12-03 12:34:51 -08003351 int color = paint->getColor();
3352 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003353 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003354 color |= 0x00ffffff;
3355 }
3356 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3357
3358 return drawColorRects(rects, count, color, mode);
3359}
3360
3361status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003362 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003363 if (count == 0) {
3364 return DrawGlInfo::kStatusDone;
3365 }
Romain Guy735738c2012-12-03 12:34:51 -08003366
Romain Guy672433d2013-01-04 19:05:13 -08003367 float left = FLT_MAX;
3368 float top = FLT_MAX;
3369 float right = FLT_MIN;
3370 float bottom = FLT_MIN;
3371
Romain Guy448455f2013-07-22 13:57:50 -07003372 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003373 Vertex* vertex = mesh;
3374
Chris Craik2af46352012-11-26 18:30:17 -08003375 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003376 float l = rects[index + 0];
3377 float t = rects[index + 1];
3378 float r = rects[index + 2];
3379 float b = rects[index + 3];
3380
Romain Guy3b753822013-03-05 10:27:35 -08003381 Vertex::set(vertex++, l, t);
3382 Vertex::set(vertex++, r, t);
3383 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003384 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003385
Romain Guy3b753822013-03-05 10:27:35 -08003386 left = fminf(left, l);
3387 top = fminf(top, t);
3388 right = fmaxf(right, r);
3389 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003390 }
3391
Romain Guy3b753822013-03-05 10:27:35 -08003392 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003393 return DrawGlInfo::kStatusDone;
3394 }
Romain Guy672433d2013-01-04 19:05:13 -08003395
Romain Guy672433d2013-01-04 19:05:13 -08003396 setupDraw();
3397 setupDrawNoTexture();
3398 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3399 setupDrawShader();
3400 setupDrawColorFilter();
3401 setupDrawBlending(mode);
3402 setupDrawProgram();
3403 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003404 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003405 setupDrawColorUniforms();
3406 setupDrawShaderUniforms();
3407 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003408
Romain Guy8ce00302013-01-15 18:51:42 -08003409 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003410 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003411 }
3412
Romain Guy448455f2013-07-22 13:57:50 -07003413 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003414
3415 return DrawGlInfo::kStatusDrew;
3416}
3417
Romain Guy026c5e162010-06-28 17:12:22 -07003418void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003419 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003420 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003421 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003422 color |= 0x00ffffff;
3423 }
3424
Romain Guy70ca14e2010-12-13 18:24:33 -08003425 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003426 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003427 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003428 setupDrawShader();
3429 setupDrawColorFilter();
3430 setupDrawBlending(mode);
3431 setupDrawProgram();
3432 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3433 setupDrawColorUniforms();
3434 setupDrawShaderUniforms(ignoreTransform);
3435 setupDrawColorFilterUniforms();
3436 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003437
Romain Guyc95c8d62010-09-17 15:31:32 -07003438 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3439}
3440
Romain Guy82ba8142010-07-09 13:25:56 -07003441void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003442 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003443 int alpha;
3444 SkXfermode::Mode mode;
3445 getAlphaAndMode(paint, &alpha, &mode);
3446
Romain Guyd21b6e12011-11-30 20:21:23 -08003447 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003448
Romain Guy3b748a42013-04-17 18:54:38 -07003449 GLvoid* vertices = (GLvoid*) NULL;
3450 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3451
3452 if (texture->uvMapper) {
3453 vertices = &mMeshVertices[0].position[0];
3454 texCoords = &mMeshVertices[0].texture[0];
3455
3456 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3457 texture->uvMapper->map(uvs);
3458
3459 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3460 }
3461
Romain Guy3b753822013-03-05 10:27:35 -08003462 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3463 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3464 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003465
Romain Guyd21b6e12011-11-30 20:21:23 -08003466 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003467 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003468 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3469 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003470 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003471 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003472 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003473 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3474 }
3475
3476 if (texture->uvMapper) {
3477 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003478 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003479}
3480
Romain Guybd6b79b2010-06-26 00:13:53 -07003481void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003482 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3483 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003484 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003485}
3486
3487void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003488 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003489 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003490 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003491
Romain Guy746b7402010-10-26 16:27:31 -07003492 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003493 setupDrawWithTexture();
3494 setupDrawColor(alpha, alpha, alpha, alpha);
3495 setupDrawColorFilter();
3496 setupDrawBlending(blend, mode, swapSrcDst);
3497 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003498 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003499 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003500 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003501 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003502 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003503 }
Romain Guy886b2752013-01-04 12:26:18 -08003504 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003505 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003506 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003507 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003508
Romain Guy6820ac82010-09-15 18:11:50 -07003509 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003510}
Romain Guyac670c02010-07-27 17:39:27 -07003511
Romain Guy3b748a42013-04-17 18:54:38 -07003512void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3513 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3514 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3515 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3516
3517 setupDraw();
3518 setupDrawWithTexture();
3519 setupDrawColor(alpha, alpha, alpha, alpha);
3520 setupDrawColorFilter();
3521 setupDrawBlending(blend, mode, swapSrcDst);
3522 setupDrawProgram();
3523 if (!dirty) setupDrawDirtyRegionsDisabled();
3524 if (!ignoreScale) {
3525 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3526 } else {
3527 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3528 }
3529 setupDrawTexture(texture);
3530 setupDrawPureColorUniforms();
3531 setupDrawColorFilterUniforms();
3532 setupDrawMeshIndices(vertices, texCoords, vbo);
3533
3534 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guye4d01122010-06-16 18:44:05 -07003535}
3536
Romain Guy886b2752013-01-04 12:26:18 -08003537void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3538 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3539 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003540 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003541
3542 setupDraw();
3543 setupDrawWithTexture(true);
3544 if (hasColor) {
3545 setupDrawAlpha8Color(color, alpha);
3546 }
3547 setupDrawColorFilter();
3548 setupDrawShader();
3549 setupDrawBlending(true, mode);
3550 setupDrawProgram();
3551 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003552 if (!ignoreScale) {
3553 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3554 } else {
3555 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3556 }
Romain Guy886b2752013-01-04 12:26:18 -08003557 setupDrawTexture(texture);
3558 setupDrawPureColorUniforms();
3559 setupDrawColorFilterUniforms();
3560 setupDrawShaderUniforms();
3561 setupDrawMesh(vertices, texCoords);
3562
3563 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003564}
3565
Romain Guye4d01122010-06-16 18:44:05 -07003566void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
3567 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003568 if (mCountOverdraw) {
3569 if (!mCaches.blend) glEnable(GL_BLEND);
3570 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3571 glBlendFunc(GL_ONE, GL_ONE);
3572 }
3573
3574 mCaches.blend = true;
3575 mCaches.lastSrcMode = GL_ONE;
3576 mCaches.lastDstMode = GL_ONE;
3577
3578 return;
3579 }
3580
Romain Guybd6b79b2010-06-26 00:13:53 -07003581 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003582
Romain Guyc1396e92010-06-30 17:56:19 -07003583 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003584 // These blend modes are not supported by OpenGL directly and have
3585 // to be implemented using shaders. Since the shader will perform
3586 // the blending, turn blending off here
3587 // If the blend mode cannot be implemented using shaders, fall
3588 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003589 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003590 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guy82ba8142010-07-09 13:25:56 -07003591 description.framebufferMode = mode;
3592 description.swapSrcDst = swapSrcDst;
Romain Guyf7f93552010-07-08 19:17:03 -07003593
Romain Guy82bc7a72012-01-03 14:13:39 -08003594 if (mCaches.blend) {
3595 glDisable(GL_BLEND);
3596 mCaches.blend = false;
3597 }
3598
3599 return;
3600 } else {
3601 mode = SkXfermode::kSrcOver_Mode;
Romain Guybd6b79b2010-06-26 00:13:53 -07003602 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003603 }
3604
3605 if (!mCaches.blend) {
3606 glEnable(GL_BLEND);
3607 }
3608
3609 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3610 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3611
3612 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3613 glBlendFunc(sourceMode, destMode);
3614 mCaches.lastSrcMode = sourceMode;
3615 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003616 }
3617 } else if (mCaches.blend) {
3618 glDisable(GL_BLEND);
3619 }
3620 mCaches.blend = blend;
3621}
3622
3623bool OpenGLRenderer::useProgram(Program* program) {
3624 if (!program->isInUse()) {
3625 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
3626 program->use();
3627 mCaches.currentProgram = program;
3628 return false;
3629 }
3630 return true;
3631}
3632
3633void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
3634 TextureVertex* v = &mMeshVertices[0];
3635 TextureVertex::setUV(v++, u1, v1);
3636 TextureVertex::setUV(v++, u2, v1);
3637 TextureVertex::setUV(v++, u1, v2);
3638 TextureVertex::setUV(v++, u2, v2);
3639}
3640
Chris Craik16ecda52013-03-29 10:59:59 -07003641void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003642 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003643 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3644 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003645 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003646 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003647 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003648}
3649
Chris Craik16ecda52013-03-29 10:59:59 -07003650float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3651 float alpha;
3652 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3653 alpha = mDrawModifiers.mOverrideLayerAlpha;
3654 } else {
3655 alpha = layer->getAlpha() / 255.0f;
3656 }
3657 return alpha * mSnapshot->alpha;
3658}
3659
Romain Guye4d01122010-06-16 18:44:05 -07003660}; // namespace uirenderer
3661}; // namespace android