blob: c13e61430008c3c957a8baa2d2531da169aaa17a [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
Romain Guyff316ec2013-02-13 18:39:43 -08002242 ColorTextureVertex mesh[count];
2243 ColorTextureVertex* vertex = mesh;
2244
2245 bool cleanupColors = false;
2246 if (!colors) {
2247 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2248 colors = new int[colorsCount];
2249 memset(colors, 0xff, colorsCount * sizeof(int));
2250 cleanupColors = true;
2251 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002252
Romain Guy3b748a42013-04-17 18:54:38 -07002253 mCaches.activeTexture(0);
2254 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2255 const UvMapper& mapper(getMapper(texture));
2256
Romain Guy5a7b4662011-01-20 19:09:30 -08002257 for (int32_t y = 0; y < meshHeight; y++) {
2258 for (int32_t x = 0; x < meshWidth; x++) {
2259 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2260
2261 float u1 = float(x) / meshWidth;
2262 float u2 = float(x + 1) / meshWidth;
2263 float v1 = float(y) / meshHeight;
2264 float v2 = float(y + 1) / meshHeight;
2265
Romain Guy3b748a42013-04-17 18:54:38 -07002266 mapper.map(u1, v1, u2, v2);
2267
Romain Guy5a7b4662011-01-20 19:09:30 -08002268 int ax = i + (meshWidth + 1) * 2;
2269 int ay = ax + 1;
2270 int bx = i;
2271 int by = bx + 1;
2272 int cx = i + 2;
2273 int cy = cx + 1;
2274 int dx = i + (meshWidth + 1) * 2 + 2;
2275 int dy = dx + 1;
2276
Romain Guyff316ec2013-02-13 18:39:43 -08002277 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2278 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2279 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002280
Romain Guyff316ec2013-02-13 18:39:43 -08002281 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2282 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2283 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002284
Romain Guya92bb4d2012-10-16 11:08:44 -07002285 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2286 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2287 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2288 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002289 }
2290 }
2291
Romain Guya92bb4d2012-10-16 11:08:44 -07002292 if (quickReject(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002293 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002294 return DrawGlInfo::kStatusDone;
2295 }
2296
Romain Guyff316ec2013-02-13 18:39:43 -08002297 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002298 texture = mCaches.textureCache.get(bitmap);
2299 if (!texture) {
2300 if (cleanupColors) delete[] colors;
2301 return DrawGlInfo::kStatusDone;
2302 }
Romain Guyff316ec2013-02-13 18:39:43 -08002303 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002304 const AutoTexture autoCleanup(texture);
2305
2306 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2307 texture->setFilter(FILTER(paint), true);
2308
2309 int alpha;
2310 SkXfermode::Mode mode;
2311 getAlphaAndMode(paint, &alpha, &mode);
2312
Romain Guyff316ec2013-02-13 18:39:43 -08002313 float a = alpha / 255.0f;
2314
Romain Guya92bb4d2012-10-16 11:08:44 -07002315 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002316 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002317 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002318
Romain Guyff316ec2013-02-13 18:39:43 -08002319 setupDraw();
2320 setupDrawWithTextureAndColor();
2321 setupDrawColor(a, a, a, a);
2322 setupDrawColorFilter();
2323 setupDrawBlending(true, mode, false);
2324 setupDrawProgram();
2325 setupDrawDirtyRegionsDisabled();
2326 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, false);
2327 setupDrawTexture(texture->id);
2328 setupDrawPureColorUniforms();
2329 setupDrawColorFilterUniforms();
2330 setupDrawMesh(&mesh[0].position[0], &mesh[0].texture[0], &mesh[0].color[0]);
2331
2332 glDrawArrays(GL_TRIANGLES, 0, count);
2333
Romain Guyff316ec2013-02-13 18:39:43 -08002334 int slot = mCaches.currentProgram->getAttrib("colors");
2335 if (slot >= 0) {
2336 glDisableVertexAttribArray(slot);
2337 }
2338
2339 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002340
2341 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002342}
2343
Chet Haase48659092012-05-31 15:21:51 -07002344status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guyc7d53492010-06-25 13:41:57 -07002345 float srcLeft, float srcTop, float srcRight, float srcBottom,
Romain Guy026c5e162010-06-28 17:12:22 -07002346 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002347 SkPaint* paint) {
Romain Guy9d5316e2010-06-24 19:30:36 -07002348 if (quickReject(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002349 return DrawGlInfo::kStatusDone;
Romain Guy026c5e162010-06-28 17:12:22 -07002350 }
2351
Romain Guya1d3c912011-12-13 14:55:06 -08002352 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002353 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002354 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy026c5e162010-06-28 17:12:22 -07002355 const AutoTexture autoCleanup(texture);
2356
2357 const float width = texture->width;
2358 const float height = texture->height;
2359
Romain Guy3b748a42013-04-17 18:54:38 -07002360 float u1 = fmax(0.0f, srcLeft / width);
2361 float v1 = fmax(0.0f, srcTop / height);
2362 float u2 = fmin(1.0f, srcRight / width);
2363 float v2 = fmin(1.0f, srcBottom / height);
2364
2365 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy026c5e162010-06-28 17:12:22 -07002366
Romain Guy03750a02010-10-18 14:06:08 -07002367 mCaches.unbindMeshBuffer();
Romain Guy026c5e162010-06-28 17:12:22 -07002368 resetDrawTextureTexCoords(u1, v1, u2, v2);
Romain Guyc7d53492010-06-25 13:41:57 -07002369
Romain Guy03750a02010-10-18 14:06:08 -07002370 int alpha;
2371 SkXfermode::Mode mode;
2372 getAlphaAndMode(paint, &alpha, &mode);
2373
Romain Guyd21b6e12011-11-30 20:21:23 -08002374 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2375
Romain Guy886b2752013-01-04 12:26:18 -08002376 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2377 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002378
Romain Guy886b2752013-01-04 12:26:18 -08002379 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2380 // Apply a scale transform on the canvas only when a shader is in use
2381 // Skia handles the ratio between the dst and src rects as a scale factor
2382 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002383 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002384 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002385
Romain Guy3b753822013-03-05 10:27:35 -08002386 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2387 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2388 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002389
2390 dstRight = x + (dstRight - dstLeft);
2391 dstBottom = y + (dstBottom - dstTop);
2392
2393 dstLeft = x;
2394 dstTop = y;
2395
2396 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2397 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002398 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002399 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002400 }
2401
2402 if (CC_UNLIKELY(useScaleTransform)) {
2403 save(SkCanvas::kMatrix_SaveFlag);
2404 translate(dstLeft, dstTop);
2405 scale(scaleX, scaleY);
2406
2407 dstLeft = 0.0f;
2408 dstTop = 0.0f;
2409
2410 dstRight = srcRight - srcLeft;
2411 dstBottom = srcBottom - srcTop;
2412 }
2413
2414 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2415 int color = paint ? paint->getColor() : 0;
2416 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2417 texture->id, paint != NULL, color, alpha, mode,
2418 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2419 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2420 } else {
2421 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2422 texture->id, alpha / 255.0f, mode, texture->blend,
2423 &mMeshVertices[0].position[0], &mMeshVertices[0].texture[0],
2424 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2425 }
2426
2427 if (CC_UNLIKELY(useScaleTransform)) {
2428 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002429 }
Romain Guybb9524b2010-06-22 18:56:38 -07002430
2431 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002432
2433 return DrawGlInfo::kStatusDrew;
Romain Guybb9524b2010-06-22 18:56:38 -07002434}
2435
Romain Guy3b748a42013-04-17 18:54:38 -07002436status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002437 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002438 if (quickReject(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002439 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002440 }
2441
Romain Guy4c2547f2013-06-11 16:19:24 -07002442 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002443 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2444 right - left, bottom - top, patch);
Romain Guybe6f9dc2012-07-16 12:41:17 -07002445
Romain Guy03c00b52013-06-20 18:30:28 -07002446 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002447}
2448
Romain Guy03c00b52013-06-20 18:30:28 -07002449status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2450 float left, float top, float right, float bottom, SkPaint* paint) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002451 if (quickReject(left, top, right, bottom)) {
2452 return DrawGlInfo::kStatusDone;
2453 }
Romain Guyf7f93552010-07-08 19:17:03 -07002454
Romain Guy211370f2012-02-01 16:10:55 -08002455 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002456 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002457 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002458 if (!texture) return DrawGlInfo::kStatusDone;
2459 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002460
Romain Guya4adcf02013-02-28 12:15:35 -08002461 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2462 texture->setFilter(GL_LINEAR, true);
2463
Romain Guy03c00b52013-06-20 18:30:28 -07002464 int alpha;
2465 SkXfermode::Mode mode;
2466 getAlphaAndMode(paint, &alpha, &mode);
2467
Romain Guy3b753822013-03-05 10:27:35 -08002468 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002469 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002470 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002471 const float offsetX = left + currentTransform().getTranslateX();
2472 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002473 const size_t count = mesh->quads.size();
2474 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002475 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002476 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002477 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2478 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2479 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002480 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002481 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002482 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002483 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002484 }
2485 }
2486
Romain Guy211370f2012-02-01 16:10:55 -08002487 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002488 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2489 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002490
Romain Guy3b748a42013-04-17 18:54:38 -07002491 right = x + right - left;
2492 bottom = y + bottom - top;
2493 drawIndexedTextureMesh(x, y, right, bottom, texture->id, alpha / 255.0f,
2494 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2495 GL_TRIANGLES, mesh->indexCount, false, true,
2496 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002497 } else {
Romain Guy3b748a42013-04-17 18:54:38 -07002498 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2499 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2500 GL_TRIANGLES, mesh->indexCount, false, false,
2501 mCaches.patchCache.getMeshBuffer(), true, !mesh->hasEmptyQuads);
Romain Guy6620c6d2010-12-06 18:07:02 -08002502 }
Romain Guy054dc182010-10-15 17:55:25 -07002503 }
Chet Haase48659092012-05-31 15:21:51 -07002504
2505 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002506}
2507
Romain Guy03c00b52013-06-20 18:30:28 -07002508/**
2509 * Important note: this method is intended to draw batches of 9-patch objects and
2510 * will not set the scissor enable or dirty the current layer, if any.
2511 * The caller is responsible for properly dirtying the current layer.
2512 */
2513status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2514 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2515 mCaches.activeTexture(0);
2516 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2517 if (!texture) return DrawGlInfo::kStatusDone;
2518 const AutoTexture autoCleanup(texture);
2519
2520 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2521 texture->setFilter(GL_LINEAR, true);
2522
2523 int alpha;
2524 SkXfermode::Mode mode;
2525 getAlphaAndMode(paint, &alpha, &mode);
2526
2527 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
2528 mode, texture->blend, &vertices[0].position[0], &vertices[0].texture[0],
2529 GL_TRIANGLES, indexCount, false, true, 0, true, false);
2530
2531 return DrawGlInfo::kStatusDrew;
2532}
2533
Chris Craik65cd6122012-12-10 17:56:27 -08002534status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2535 bool useOffset) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002536 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002537 // no vertices to draw
2538 return DrawGlInfo::kStatusDone;
2539 }
2540
Chris Craikcb4d6002012-09-25 12:00:29 -07002541 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002542 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2543 bool isAA = paint->isAntiAlias();
2544
Chris Craik710f46d2012-09-17 17:25:49 -07002545 setupDraw();
2546 setupDrawNoTexture();
2547 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002548 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2549 setupDrawColorFilter();
2550 setupDrawShader();
2551 setupDrawBlending(isAA, mode);
2552 setupDrawProgram();
Chris Craik65cd6122012-12-10 17:56:27 -08002553 setupDrawModelViewIdentity(useOffset);
Chris Craik710f46d2012-09-17 17:25:49 -07002554 setupDrawColorUniforms();
2555 setupDrawColorFilterUniforms();
2556 setupDrawShaderIdentityUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002557
Chris Craik710f46d2012-09-17 17:25:49 -07002558 void* vertices = vertexBuffer.getBuffer();
2559 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002560 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002561 mCaches.resetTexCoordsVertexPointer();
2562 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002563
Chris Craik710f46d2012-09-17 17:25:49 -07002564 int alphaSlot = -1;
2565 if (isAA) {
2566 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2567 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002568
Chris Craik710f46d2012-09-17 17:25:49 -07002569 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002570 glEnableVertexAttribArray(alphaSlot);
2571 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002572 }
Romain Guy04299382012-07-18 17:15:41 -07002573
Chris Craik6d29c8d2013-05-08 18:35:44 -07002574 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002575
Chris Craik710f46d2012-09-17 17:25:49 -07002576 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002577 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002578 }
Chris Craik65cd6122012-12-10 17:56:27 -08002579
2580 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002581}
2582
2583/**
Chris Craik65cd6122012-12-10 17:56:27 -08002584 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2585 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2586 * screen space in all directions. However, instead of using a fragment shader to compute the
2587 * translucency of the color from its position, we simply use a varying parameter to define how far
2588 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2589 *
2590 * Doesn't yet support joins, caps, or path effects.
2591 */
2592status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2593 VertexBuffer vertexBuffer;
2594 // TODO: try clipping large paths to viewport
2595 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2596
Romain Guyc46d07a2013-03-15 19:06:39 -07002597 if (hasLayer()) {
2598 SkRect bounds = path.getBounds();
2599 PathTessellator::expandBoundsForStroke(bounds, paint, false);
2600 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2601 }
Chris Craik65cd6122012-12-10 17:56:27 -08002602
2603 return drawVertexBuffer(vertexBuffer, paint);
2604}
2605
2606/**
2607 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2608 * and additional geometry for defining an alpha slope perimeter.
2609 *
2610 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2611 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2612 * in-shader alpha region, but found it to be taxing on some GPUs.
2613 *
2614 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2615 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002616 */
Chet Haase48659092012-05-31 15:21:51 -07002617status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002618 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002619
Chris Craik65cd6122012-12-10 17:56:27 -08002620 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002621
Chris Craik65cd6122012-12-10 17:56:27 -08002622 VertexBuffer buffer;
2623 SkRect bounds;
2624 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002625
2626 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2627 return DrawGlInfo::kStatusDone;
2628 }
2629
Romain Guy3b753822013-03-05 10:27:35 -08002630 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002631
Chris Craik65cd6122012-12-10 17:56:27 -08002632 bool useOffset = !paint->isAntiAlias();
2633 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002634}
2635
Chet Haase48659092012-05-31 15:21:51 -07002636status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002637 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002638
Chris Craik6d29c8d2013-05-08 18:35:44 -07002639 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002640
Chris Craik6d29c8d2013-05-08 18:35:44 -07002641 VertexBuffer buffer;
2642 SkRect bounds;
2643 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002644
Chris Craik6d29c8d2013-05-08 18:35:44 -07002645 if (quickReject(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
2646 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002647 }
2648
Chris Craik6d29c8d2013-05-08 18:35:44 -07002649 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002650
Chris Craik6d29c8d2013-05-08 18:35:44 -07002651 bool useOffset = !paint->isAntiAlias();
2652 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002653}
2654
Chet Haase48659092012-05-31 15:21:51 -07002655status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002656 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002657 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002658
Romain Guyae88e5e2010-10-22 17:49:18 -07002659 Rect& clip(*mSnapshot->clipRect);
2660 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002661
Romain Guy3d58c032010-07-14 16:34:53 -07002662 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002663
2664 return DrawGlInfo::kStatusDrew;
Romain Guybb9524b2010-06-22 18:56:38 -07002665}
2666
Chet Haase48659092012-05-31 15:21:51 -07002667status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2668 SkPaint* paint) {
2669 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002670 const AutoTexture autoCleanup(texture);
2671
2672 const float x = left + texture->left - texture->offset;
2673 const float y = top + texture->top - texture->offset;
2674
2675 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002676
2677 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002678}
2679
Chet Haase48659092012-05-31 15:21:51 -07002680status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002681 float rx, float ry, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002682 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2683 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002684 return DrawGlInfo::kStatusDone;
2685 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002686
Chris Craikcb4d6002012-09-25 12:00:29 -07002687 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002688 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002689 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002690 right - left, bottom - top, rx, ry, p);
2691 return drawShape(left, top, texture, p);
2692 }
2693
2694 SkPath path;
2695 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002696 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2697 float outset = p->getStrokeWidth() / 2;
2698 rect.outset(outset, outset);
2699 rx += outset;
2700 ry += outset;
2701 }
Chris Craik710f46d2012-09-17 17:25:49 -07002702 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002703 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002704}
2705
Chris Craik710f46d2012-09-17 17:25:49 -07002706status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002707 if (mSnapshot->isIgnored() || quickRejectPreStroke(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002708 x + radius, y + radius, p) ||
2709 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002710 return DrawGlInfo::kStatusDone;
2711 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002712 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002713 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002714 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002715 return drawShape(x - radius, y - radius, texture, p);
2716 }
2717
2718 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002719 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2720 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2721 } else {
2722 path.addCircle(x, y, radius);
2723 }
Chris Craik65cd6122012-12-10 17:56:27 -08002724 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002725}
Romain Guy01d58e42011-01-19 21:54:02 -08002726
Chet Haase48659092012-05-31 15:21:51 -07002727status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002728 SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002729 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2730 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002731 return DrawGlInfo::kStatusDone;
2732 }
Romain Guy01d58e42011-01-19 21:54:02 -08002733
Chris Craikcb4d6002012-09-25 12:00:29 -07002734 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002735 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002736 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002737 return drawShape(left, top, texture, p);
2738 }
2739
2740 SkPath path;
2741 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002742 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2743 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2744 }
Chris Craik710f46d2012-09-17 17:25:49 -07002745 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002746 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002747}
2748
Chet Haase48659092012-05-31 15:21:51 -07002749status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002750 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002751 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2752 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002753 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002754 }
2755
Chris Craik780c1282012-10-04 14:10:49 -07002756 if (fabs(sweepAngle) >= 360.0f) {
2757 return drawOval(left, top, right, bottom, p);
2758 }
2759
2760 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002761 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002762 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002763 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002764 startAngle, sweepAngle, useCenter, p);
2765 return drawShape(left, top, texture, p);
2766 }
2767
2768 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2769 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2770 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2771 }
2772
2773 SkPath path;
2774 if (useCenter) {
2775 path.moveTo(rect.centerX(), rect.centerY());
2776 }
2777 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2778 if (useCenter) {
2779 path.close();
2780 }
Chris Craik65cd6122012-12-10 17:56:27 -08002781 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002782}
2783
Romain Guycf8675e2012-10-02 12:32:25 -07002784// See SkPaintDefaults.h
2785#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2786
Chet Haase48659092012-05-31 15:21:51 -07002787status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Romain Guy257ae352013-03-20 16:31:12 -07002788 if (mSnapshot->isIgnored() || quickRejectPreStroke(left, top, right, bottom, p) ||
2789 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002790 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002791 }
2792
Chris Craik710f46d2012-09-17 17:25:49 -07002793 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002794 // only fill style is supported by drawConvexPath, since others have to handle joins
2795 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2796 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2797 mCaches.activeTexture(0);
2798 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002799 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002800 return drawShape(left, top, texture, p);
2801 }
2802
2803 SkPath path;
2804 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2805 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2806 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2807 }
2808 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002809 return drawConvexPath(path, p);
Romain Guyf6a11b82010-06-23 17:47:49 -07002810 }
2811
Romain Guy3b753822013-03-05 10:27:35 -08002812 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002813 SkPath path;
2814 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002815 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002816 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002817 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002818 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002819 }
Romain Guy9d5316e2010-06-24 19:30:36 -07002820}
2821
Raph Levien416a8472012-07-19 22:48:17 -07002822void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2823 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2824 float x, float y) {
2825 mCaches.activeTexture(0);
2826
2827 // NOTE: The drop shadow will not perform gamma correction
2828 // if shader-based correction is enabled
2829 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2830 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002831 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002832 // If the drop shadow exceeds the max texture size or couldn't be
2833 // allocated, skip drawing
2834 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002835 const AutoTexture autoCleanup(shadow);
2836
Chris Craikc3566d02013-02-04 16:16:33 -08002837 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2838 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002839
Chris Craikc3566d02013-02-04 16:16:33 -08002840 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2841 int shadowColor = mDrawModifiers.mShadowColor;
2842 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002843 shadowColor = 0xffffffff;
2844 }
2845
2846 setupDraw();
2847 setupDrawWithTexture(true);
2848 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2849 setupDrawColorFilter();
2850 setupDrawShader();
2851 setupDrawBlending(true, mode);
2852 setupDrawProgram();
2853 setupDrawModelView(sx, sy, sx + shadow->width, sy + shadow->height);
2854 setupDrawTexture(shadow->id);
2855 setupDrawPureColorUniforms();
2856 setupDrawColorFilterUniforms();
2857 setupDrawShaderUniforms();
2858 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2859
2860 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2861}
2862
Romain Guy768bffc2013-02-27 13:50:45 -08002863bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2864 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2865 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2866}
2867
Chet Haase48659092012-05-31 15:21:51 -07002868status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002869 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002870 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002871 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002872 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002873
Romain Guy671d6cf2012-01-18 12:39:17 -08002874 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002875 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002876 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002877 }
2878
Chris Craik39a908c2013-06-13 14:39:01 -07002879 mCaches.enableScissor();
2880
Romain Guy671d6cf2012-01-18 12:39:17 -08002881 float x = 0.0f;
2882 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002883 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002884 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002885 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2886 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002887 }
2888
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002889 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002890 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002891
2892 int alpha;
2893 SkXfermode::Mode mode;
2894 getAlphaAndMode(paint, &alpha, &mode);
2895
Chris Craikc3566d02013-02-04 16:16:33 -08002896 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002897 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2898 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002899 }
2900
Romain Guy671d6cf2012-01-18 12:39:17 -08002901 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002902 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 if (pureTranslate && !linearFilter) {
2904 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2905 }
Romain Guy257ae352013-03-20 16:31:12 -07002906 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002907
2908 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2909 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2910
Romain Guy211370f2012-02-01 16:10:55 -08002911 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002912
Victoria Lease1e546812013-06-25 14:25:17 -07002913 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002914 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002915 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002916 if (hasActiveLayer) {
2917 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002918 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002919 }
2920 dirtyLayerUnchecked(bounds, getRegion());
2921 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002922 }
Chet Haase48659092012-05-31 15:21:51 -07002923
2924 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002925}
2926
Romain Guy624234f2013-03-05 16:43:31 -08002927mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2928 mat4 fontTransform;
2929 if (CC_LIKELY(transform.isPureTranslate())) {
2930 fontTransform = mat4::identity();
2931 } else {
2932 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002933 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002934 } else {
2935 float sx, sy;
2936 currentTransform().decomposeScale(sx, sy);
2937 fontTransform.loadScale(sx, sy, 1.0f);
2938 }
2939 }
2940 return fontTransform;
2941}
2942
Chris Craik41541822013-05-03 16:35:54 -07002943status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2944 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002945 DrawOpMode drawOpMode) {
2946
Chris Craik527a3aa2013-03-04 10:19:31 -08002947 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002948 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2949 // drawing as ops from DeferredDisplayList are already filtered for these
2950 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
2951 quickReject(bounds)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002952 return DrawGlInfo::kStatusDone;
2953 }
Romain Guycac5fd32011-12-01 20:08:50 -08002954 }
2955
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002956 const float oldX = x;
2957 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002958
2959 const mat4& transform = currentTransform();
2960 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002961
Romain Guy211370f2012-02-01 16:10:55 -08002962 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002963 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2964 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002965 }
2966
Romain Guy86568192010-12-14 15:55:39 -08002967 int alpha;
2968 SkXfermode::Mode mode;
2969 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002970
Romain Guyc74f45a2013-02-26 19:10:14 -08002971 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2972
Chris Craikc3566d02013-02-04 16:16:33 -08002973 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002974 fontRenderer.setFont(paint, mat4::identity());
2975 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2976 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002977 }
2978
Romain Guy19d4dd82013-03-04 11:14:26 -08002979 const bool hasActiveLayer = hasLayer();
2980
Romain Guy624234f2013-03-05 16:43:31 -08002981 // We only pass a partial transform to the font renderer. That partial
2982 // matrix defines how glyphs are rasterized. Typically we want glyphs
2983 // to be rasterized at their final size on screen, which means the partial
2984 // matrix needs to take the scale factor into account.
2985 // When a partial matrix is used to transform glyphs during rasterization,
2986 // the mesh is generated with the inverse transform (in the case of scale,
2987 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2988 // apply the full transform matrix at draw time in the vertex shader.
2989 // Applying the full matrix in the shader is the easiest way to handle
2990 // rotation and perspective and allows us to always generated quads in the
2991 // font renderer which greatly simplifies the code, clipping in particular.
2992 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002993 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002994
Romain Guy6620c6d2010-12-06 18:07:02 -08002995 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002996 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002997 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002998
Romain Guy624234f2013-03-05 16:43:31 -08002999 // TODO: Implement better clipping for scaled/rotated text
3000 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003001 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 -07003002
Raph Levien996e57c2012-07-23 15:22:52 -07003003 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003004 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003005
3006 // don't call issuedrawcommand, do it at end of batch
3007 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003008 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003009 SkPaint paintCopy(*paint);
3010 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3011 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003012 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003013 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003014 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003015 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003016 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003017
Chris Craik527a3aa2013-03-04 10:19:31 -08003018 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003019 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003020 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003021 }
Chris Craik41541822013-05-03 16:35:54 -07003022 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003023 }
Romain Guy694b5192010-07-21 21:33:20 -07003024
Chris Craik41541822013-05-03 16:35:54 -07003025 drawTextDecorations(text, bytesCount, totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003026
3027 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003028}
3029
Chet Haase48659092012-05-31 15:21:51 -07003030status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003031 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003032 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003033 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003034 }
3035
Chris Craik39a908c2013-06-13 14:39:01 -07003036 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3037 mCaches.enableScissor();
3038
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003039 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003040 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003041 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003042
3043 int alpha;
3044 SkXfermode::Mode mode;
3045 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003046 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003047
Romain Guy97771732012-02-28 18:17:02 -08003048 const Rect* clip = &mSnapshot->getLocalClip();
3049 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 -08003050
Romain Guy97771732012-02-28 18:17:02 -08003051 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003052
3053 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003054 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003055 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003056 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003057 dirtyLayerUnchecked(bounds, getRegion());
3058 }
Romain Guy97771732012-02-28 18:17:02 -08003059 }
Chet Haase48659092012-05-31 15:21:51 -07003060
3061 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003062}
3063
Chet Haase48659092012-05-31 15:21:51 -07003064status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3065 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003066
Romain Guya1d3c912011-12-13 14:55:06 -08003067 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003068
Romain Guyfb8b7632010-08-23 21:05:08 -07003069 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003070 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003071 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003072
Romain Guy8b55f372010-08-18 17:10:07 -07003073 const float x = texture->left - texture->offset;
3074 const float y = texture->top - texture->offset;
3075
Romain Guy01d58e42011-01-19 21:54:02 -08003076 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003077
3078 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003079}
3080
Chris Craika08f95c2013-03-15 17:24:33 -07003081status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003082 if (!layer) {
3083 return DrawGlInfo::kStatusDone;
3084 }
3085
Romain Guyb2e2f242012-10-17 18:18:35 -07003086 mat4* transform = NULL;
3087 if (layer->isTextureLayer()) {
3088 transform = &layer->getTransform();
3089 if (!transform->isIdentity()) {
3090 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003091 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003092 }
3093 }
3094
Chris Craik39a908c2013-06-13 14:39:01 -07003095 bool clipRequired = false;
Romain Guy35643dd2012-09-18 15:40:58 -07003096 const bool rejected = quickRejectNoScissor(x, y,
Chris Craik5e49b302013-07-30 19:05:20 -07003097 x + layer->layer.getWidth(), y + layer->layer.getHeight(), false, &clipRequired);
Romain Guy35643dd2012-09-18 15:40:58 -07003098
3099 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003100 if (transform && !transform->isIdentity()) {
3101 restore();
3102 }
Chet Haase48659092012-05-31 15:21:51 -07003103 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003104 }
3105
Romain Guy5bb3c732012-11-29 17:52:58 -08003106 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003107
Chris Craik39a908c2013-06-13 14:39:01 -07003108 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003109 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003110
Romain Guy211370f2012-02-01 16:10:55 -08003111 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003112 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3113 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003114
Romain Guyc88e3572011-01-22 00:32:12 -08003115 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003116 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3117 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003118 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003119 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003120 setupDraw();
3121 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003122 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003123 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003124 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003125 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003126 setupDrawPureColorUniforms();
3127 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003128 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003129 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3130 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3131 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003132
Romain Guyd21b6e12011-11-30 20:21:23 -08003133 layer->setFilter(GL_NEAREST);
Romain Guy4ff0cf42012-08-06 14:51:10 -07003134 setupDrawModelViewTranslate(tx, ty,
3135 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003136 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003137 layer->setFilter(GL_LINEAR);
Romain Guy9ace8f52011-07-07 20:50:11 -07003138 setupDrawModelViewTranslate(x, y,
3139 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3140 }
Romain Guyf219da52011-01-16 12:54:25 -08003141
Romain Guy448455f2013-07-22 13:57:50 -07003142 TextureVertex* mesh = &layer->mesh[0];
3143 GLsizei elementsCount = layer->meshElementCount;
Romain Guyf219da52011-01-16 12:54:25 -08003144
Romain Guy448455f2013-07-22 13:57:50 -07003145 while (elementsCount > 0) {
3146 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3147
3148 setupDrawMeshIndices(&mesh[0].position[0], &mesh[0].texture[0]);
3149 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3150 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3151
3152 elementsCount -= drawCount;
3153 // Though there are 4 vertices in a quad, we use 6 indices per
3154 // quad to draw with GL_TRIANGLES
3155 mesh += (drawCount / 6) * 4;
3156 }
Romain Guy3a3133d2011-02-01 22:59:58 -08003157
3158#if DEBUG_LAYERS_AS_REGIONS
3159 drawRegionRects(layer->region);
3160#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003161 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003162
Chris Craikc3566d02013-02-04 16:16:33 -08003163 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003164
Romain Guy5bb3c732012-11-29 17:52:58 -08003165 if (layer->debugDrawUpdate) {
3166 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003167 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3168 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3169 }
Romain Guyf219da52011-01-16 12:54:25 -08003170 }
Chris Craik34416ea2013-04-15 16:08:28 -07003171 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003172
Romain Guyb2e2f242012-10-17 18:18:35 -07003173 if (transform && !transform->isIdentity()) {
3174 restore();
3175 }
3176
Chet Haase48659092012-05-31 15:21:51 -07003177 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003178}
3179
Romain Guy6926c722010-07-12 20:20:03 -07003180///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003181// Shaders
3182///////////////////////////////////////////////////////////////////////////////
3183
3184void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003185 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003186}
3187
Romain Guy06f96e22010-07-30 19:18:16 -07003188void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003189 mDrawModifiers.mShader = shader;
3190 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003191 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003192 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003193}
3194
Romain Guyd27977d2010-07-14 19:18:51 -07003195///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003196// Color filters
3197///////////////////////////////////////////////////////////////////////////////
3198
3199void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003200 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003201}
3202
3203void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003204 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003205}
3206
3207///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003208// Drop shadow
3209///////////////////////////////////////////////////////////////////////////////
3210
3211void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003212 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003213}
3214
3215void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003216 mDrawModifiers.mHasShadow = true;
3217 mDrawModifiers.mShadowRadius = radius;
3218 mDrawModifiers.mShadowDx = dx;
3219 mDrawModifiers.mShadowDy = dy;
3220 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003221}
3222
3223///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003224// Draw filters
3225///////////////////////////////////////////////////////////////////////////////
3226
3227void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003228 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3229 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003230 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003231 mDrawModifiers.mPaintFilterClearBits = 0;
3232 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003233}
3234
3235void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003236 mDrawModifiers.mHasDrawFilter = true;
3237 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3238 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003239}
3240
Chris Craika08f95c2013-03-15 17:24:33 -07003241SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003242 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003243 return paint;
3244 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003245
3246 uint32_t flags = paint->getFlags();
3247
3248 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003249 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3250 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003251
3252 return &mFilteredPaint;
3253}
3254
3255///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003256// Drawing implementation
3257///////////////////////////////////////////////////////////////////////////////
3258
Romain Guy3b748a42013-04-17 18:54:38 -07003259Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3260 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3261 if (!texture) {
3262 return mCaches.textureCache.get(bitmap);
3263 }
3264 return texture;
3265}
3266
Romain Guy01d58e42011-01-19 21:54:02 -08003267void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3268 float x, float y, SkPaint* paint) {
3269 if (quickReject(x, y, x + texture->width, y + texture->height)) {
3270 return;
3271 }
3272
3273 int alpha;
3274 SkXfermode::Mode mode;
3275 getAlphaAndMode(paint, &alpha, &mode);
3276
3277 setupDraw();
3278 setupDrawWithTexture(true);
3279 setupDrawAlpha8Color(paint->getColor(), alpha);
3280 setupDrawColorFilter();
3281 setupDrawShader();
3282 setupDrawBlending(true, mode);
3283 setupDrawProgram();
3284 setupDrawModelView(x, y, x + texture->width, y + texture->height);
3285 setupDrawTexture(texture->id);
3286 setupDrawPureColorUniforms();
3287 setupDrawColorFilterUniforms();
3288 setupDrawShaderUniforms();
3289 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3290
3291 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003292}
3293
Romain Guyf607bdc2010-09-10 19:20:06 -07003294// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003295#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3296#define kStdUnderline_Offset (1.0f / 9.0f)
3297#define kStdUnderline_Thickness (1.0f / 18.0f)
3298
Chris Craik41541822013-05-03 16:35:54 -07003299void OpenGLRenderer::drawTextDecorations(const char* text, int bytesCount, float underlineWidth,
Romain Guy0a417492010-08-16 20:26:20 -07003300 float x, float y, SkPaint* paint) {
3301 // Handle underline and strike-through
3302 uint32_t flags = paint->getFlags();
3303 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003304 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003305
Romain Guy211370f2012-02-01 16:10:55 -08003306 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003307 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003308 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003309
Raph Levien8b4072d2012-07-30 15:50:00 -07003310 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003311 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003312
Romain Guyf6834472011-01-23 13:32:12 -08003313 int linesCount = 0;
3314 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3315 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3316
3317 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003318 float points[pointsCount];
3319 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003320
3321 if (flags & SkPaint::kUnderlineText_Flag) {
3322 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003323 points[currentPoint++] = left;
3324 points[currentPoint++] = top;
3325 points[currentPoint++] = left + underlineWidth;
3326 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003327 }
3328
3329 if (flags & SkPaint::kStrikeThruText_Flag) {
3330 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003331 points[currentPoint++] = left;
3332 points[currentPoint++] = top;
3333 points[currentPoint++] = left + underlineWidth;
3334 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003335 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003336
Romain Guy726aeba2011-06-01 14:52:00 -07003337 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003338
Romain Guy726aeba2011-06-01 14:52:00 -07003339 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003340 }
3341 }
3342}
3343
Romain Guy672433d2013-01-04 19:05:13 -08003344status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3345 if (mSnapshot->isIgnored()) {
3346 return DrawGlInfo::kStatusDone;
3347 }
3348
Romain Guy735738c2012-12-03 12:34:51 -08003349 int color = paint->getColor();
3350 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003351 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003352 color |= 0x00ffffff;
3353 }
3354 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3355
3356 return drawColorRects(rects, count, color, mode);
3357}
3358
3359status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003360 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003361 if (count == 0) {
3362 return DrawGlInfo::kStatusDone;
3363 }
Romain Guy735738c2012-12-03 12:34:51 -08003364
Romain Guy672433d2013-01-04 19:05:13 -08003365 float left = FLT_MAX;
3366 float top = FLT_MAX;
3367 float right = FLT_MIN;
3368 float bottom = FLT_MIN;
3369
Romain Guy448455f2013-07-22 13:57:50 -07003370 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003371 Vertex* vertex = mesh;
3372
Chris Craik2af46352012-11-26 18:30:17 -08003373 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003374 float l = rects[index + 0];
3375 float t = rects[index + 1];
3376 float r = rects[index + 2];
3377 float b = rects[index + 3];
3378
Romain Guy3b753822013-03-05 10:27:35 -08003379 Vertex::set(vertex++, l, t);
3380 Vertex::set(vertex++, r, t);
3381 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003382 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003383
Romain Guy3b753822013-03-05 10:27:35 -08003384 left = fminf(left, l);
3385 top = fminf(top, t);
3386 right = fmaxf(right, r);
3387 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003388 }
3389
Romain Guy3b753822013-03-05 10:27:35 -08003390 if (clip && quickReject(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003391 return DrawGlInfo::kStatusDone;
3392 }
Romain Guy672433d2013-01-04 19:05:13 -08003393
Romain Guy672433d2013-01-04 19:05:13 -08003394 setupDraw();
3395 setupDrawNoTexture();
3396 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3397 setupDrawShader();
3398 setupDrawColorFilter();
3399 setupDrawBlending(mode);
3400 setupDrawProgram();
3401 setupDrawDirtyRegionsDisabled();
Romain Guy735738c2012-12-03 12:34:51 -08003402 setupDrawModelView(0.0f, 0.0f, 1.0f, 1.0f, ignoreTransform, true);
Romain Guy672433d2013-01-04 19:05:13 -08003403 setupDrawColorUniforms();
3404 setupDrawShaderUniforms();
3405 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003406
Romain Guy8ce00302013-01-15 18:51:42 -08003407 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003408 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003409 }
3410
Romain Guy448455f2013-07-22 13:57:50 -07003411 drawIndexedQuads(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003412
3413 return DrawGlInfo::kStatusDrew;
3414}
3415
Romain Guy026c5e162010-06-28 17:12:22 -07003416void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003417 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003418 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003419 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003420 color |= 0x00ffffff;
3421 }
3422
Romain Guy70ca14e2010-12-13 18:24:33 -08003423 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003424 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003425 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003426 setupDrawShader();
3427 setupDrawColorFilter();
3428 setupDrawBlending(mode);
3429 setupDrawProgram();
3430 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3431 setupDrawColorUniforms();
3432 setupDrawShaderUniforms(ignoreTransform);
3433 setupDrawColorFilterUniforms();
3434 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003435
Romain Guyc95c8d62010-09-17 15:31:32 -07003436 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3437}
3438
Romain Guy82ba8142010-07-09 13:25:56 -07003439void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003440 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003441 int alpha;
3442 SkXfermode::Mode mode;
3443 getAlphaAndMode(paint, &alpha, &mode);
3444
Romain Guyd21b6e12011-11-30 20:21:23 -08003445 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003446
Romain Guy3b748a42013-04-17 18:54:38 -07003447 GLvoid* vertices = (GLvoid*) NULL;
3448 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3449
3450 if (texture->uvMapper) {
3451 vertices = &mMeshVertices[0].position[0];
3452 texCoords = &mMeshVertices[0].texture[0];
3453
3454 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3455 texture->uvMapper->map(uvs);
3456
3457 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3458 }
3459
Romain Guy3b753822013-03-05 10:27:35 -08003460 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3461 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3462 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003463
Romain Guyd21b6e12011-11-30 20:21:23 -08003464 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003465 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003466 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3467 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003468 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003469 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003470 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003471 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3472 }
3473
3474 if (texture->uvMapper) {
3475 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003476 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003477}
3478
Romain Guybd6b79b2010-06-26 00:13:53 -07003479void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003480 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3481 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003482 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003483}
3484
3485void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003486 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003487 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Romain Guy5b3b3522010-10-27 18:57:51 -07003488 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003489
Romain Guy746b7402010-10-26 16:27:31 -07003490 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003491 setupDrawWithTexture();
3492 setupDrawColor(alpha, alpha, alpha, alpha);
3493 setupDrawColorFilter();
3494 setupDrawBlending(blend, mode, swapSrcDst);
3495 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003496 if (!dirty) setupDrawDirtyRegionsDisabled();
Romain Guy5b3b3522010-10-27 18:57:51 -07003497 if (!ignoreScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003498 setupDrawModelView(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003499 } else {
Romain Guy70ca14e2010-12-13 18:24:33 -08003500 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
Romain Guyf607bdc2010-09-10 19:20:06 -07003501 }
Romain Guy886b2752013-01-04 12:26:18 -08003502 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003503 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003504 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003505 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003506
Romain Guy6820ac82010-09-15 18:11:50 -07003507 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003508}
Romain Guyac670c02010-07-27 17:39:27 -07003509
Romain Guy3b748a42013-04-17 18:54:38 -07003510void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3511 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3512 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
3513 bool swapSrcDst, bool ignoreTransform, GLuint vbo, bool ignoreScale, bool dirty) {
3514
3515 setupDraw();
3516 setupDrawWithTexture();
3517 setupDrawColor(alpha, alpha, alpha, alpha);
3518 setupDrawColorFilter();
3519 setupDrawBlending(blend, mode, swapSrcDst);
3520 setupDrawProgram();
3521 if (!dirty) setupDrawDirtyRegionsDisabled();
3522 if (!ignoreScale) {
3523 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3524 } else {
3525 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3526 }
3527 setupDrawTexture(texture);
3528 setupDrawPureColorUniforms();
3529 setupDrawColorFilterUniforms();
3530 setupDrawMeshIndices(vertices, texCoords, vbo);
3531
3532 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guye4d01122010-06-16 18:44:05 -07003533}
3534
Romain Guy886b2752013-01-04 12:26:18 -08003535void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3536 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3537 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik527a3aa2013-03-04 10:19:31 -08003538 bool ignoreTransform, bool ignoreScale, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003539
3540 setupDraw();
3541 setupDrawWithTexture(true);
3542 if (hasColor) {
3543 setupDrawAlpha8Color(color, alpha);
3544 }
3545 setupDrawColorFilter();
3546 setupDrawShader();
3547 setupDrawBlending(true, mode);
3548 setupDrawProgram();
3549 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik527a3aa2013-03-04 10:19:31 -08003550 if (!ignoreScale) {
3551 setupDrawModelView(left, top, right, bottom, ignoreTransform);
3552 } else {
3553 setupDrawModelViewTranslate(left, top, right, bottom, ignoreTransform);
3554 }
Romain Guy886b2752013-01-04 12:26:18 -08003555 setupDrawTexture(texture);
3556 setupDrawPureColorUniforms();
3557 setupDrawColorFilterUniforms();
3558 setupDrawShaderUniforms();
3559 setupDrawMesh(vertices, texCoords);
3560
3561 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003562}
3563
Romain Guye4d01122010-06-16 18:44:05 -07003564void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
3565 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003566 if (mCountOverdraw) {
3567 if (!mCaches.blend) glEnable(GL_BLEND);
3568 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3569 glBlendFunc(GL_ONE, GL_ONE);
3570 }
3571
3572 mCaches.blend = true;
3573 mCaches.lastSrcMode = GL_ONE;
3574 mCaches.lastDstMode = GL_ONE;
3575
3576 return;
3577 }
3578
Romain Guybd6b79b2010-06-26 00:13:53 -07003579 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003580
Romain Guyc1396e92010-06-30 17:56:19 -07003581 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003582 // These blend modes are not supported by OpenGL directly and have
3583 // to be implemented using shaders. Since the shader will perform
3584 // the blending, turn blending off here
3585 // If the blend mode cannot be implemented using shaders, fall
3586 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003587 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003588 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guy82ba8142010-07-09 13:25:56 -07003589 description.framebufferMode = mode;
3590 description.swapSrcDst = swapSrcDst;
Romain Guyf7f93552010-07-08 19:17:03 -07003591
Romain Guy82bc7a72012-01-03 14:13:39 -08003592 if (mCaches.blend) {
3593 glDisable(GL_BLEND);
3594 mCaches.blend = false;
3595 }
3596
3597 return;
3598 } else {
3599 mode = SkXfermode::kSrcOver_Mode;
Romain Guybd6b79b2010-06-26 00:13:53 -07003600 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003601 }
3602
3603 if (!mCaches.blend) {
3604 glEnable(GL_BLEND);
3605 }
3606
3607 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3608 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3609
3610 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3611 glBlendFunc(sourceMode, destMode);
3612 mCaches.lastSrcMode = sourceMode;
3613 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003614 }
3615 } else if (mCaches.blend) {
3616 glDisable(GL_BLEND);
3617 }
3618 mCaches.blend = blend;
3619}
3620
3621bool OpenGLRenderer::useProgram(Program* program) {
3622 if (!program->isInUse()) {
3623 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
3624 program->use();
3625 mCaches.currentProgram = program;
3626 return false;
3627 }
3628 return true;
3629}
3630
3631void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
3632 TextureVertex* v = &mMeshVertices[0];
3633 TextureVertex::setUV(v++, u1, v1);
3634 TextureVertex::setUV(v++, u2, v1);
3635 TextureVertex::setUV(v++, u1, v2);
3636 TextureVertex::setUV(v++, u2, v2);
3637}
3638
Chris Craik16ecda52013-03-29 10:59:59 -07003639void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003640 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003641 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3642 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003643 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003644 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003645 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003646}
3647
Chris Craik16ecda52013-03-29 10:59:59 -07003648float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3649 float alpha;
3650 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3651 alpha = mDrawModifiers.mOverrideLayerAlpha;
3652 } else {
3653 alpha = layer->getAlpha() / 255.0f;
3654 }
3655 return alpha * mSnapshot->alpha;
3656}
3657
Romain Guye4d01122010-06-16 18:44:05 -07003658}; // namespace uirenderer
3659}; // namespace android