blob: cdef94e29a8e1146652d2f420c235340f95ac6f3 [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"
ztenghui55bfb4e2013-12-03 10:38:55 -080039#include "ShadowTessellator.h"
Romain Guya957eea2010-12-08 18:34:42 -080040#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080041#include "VertexBuffer.h"
Romain Guye4d01122010-06-16 18:44:05 -070042
43namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070044namespace uirenderer {
45
46///////////////////////////////////////////////////////////////////////////////
47// Defines
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy759ea802010-09-16 20:49:46 -070050#define RAD_TO_DEG (180.0f / 3.14159265f)
51#define MIN_ANGLE 0.001f
52
Romain Guyf8773082012-07-12 18:01:00 -070053#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070054
Romain Guy713e1bb2012-10-16 18:44:09 -070055#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080056
Romain Guy9d5316e2010-06-24 19:30:36 -070057///////////////////////////////////////////////////////////////////////////////
58// Globals
59///////////////////////////////////////////////////////////////////////////////
60
Romain Guy889f8d12010-07-29 14:37:42 -070061/**
62 * Structure mapping Skia xfermodes to OpenGL blending factors.
63 */
64struct Blender {
65 SkXfermode::Mode mode;
66 GLenum src;
67 GLenum dst;
68}; // struct Blender
69
Romain Guy026c5e162010-06-28 17:12:22 -070070// In this array, the index of each Blender equals the value of the first
71// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
72static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070073 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
75 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
76 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
78 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
80 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
81 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
84 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050086 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070087 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070088};
Romain Guye4d01122010-06-16 18:44:05 -070089
Romain Guy87a76572010-09-13 18:11:21 -070090// This array contains the swapped version of each SkXfermode. For instance
91// this array's SrcOver blending mode is actually DstOver. You can refer to
92// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070093static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070094 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
95 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
96 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
97 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
98 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
100 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
103 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
104 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
105 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
106 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500107 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700108 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700109};
110
Romain Guyf6a11b82010-06-23 17:47:49 -0700111///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700112// Functions
113///////////////////////////////////////////////////////////////////////////////
114
115template<typename T>
116static inline T min(T a, T b) {
117 return a < b ? a : b;
118}
119
120///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700121// Constructors/destructor
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guy3bbacf22013-02-06 16:51:04 -0800124OpenGLRenderer::OpenGLRenderer():
125 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800126 // *set* draw modifiers to be 0
127 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700128 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700129
Romain Guyac670c02010-07-27 17:39:27 -0700130 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
131
Romain Guyae5575b2010-07-29 18:48:04 -0700132 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700133 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700134 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700135
136 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700137}
138
Romain Guy85bf02f2010-06-22 13:11:24 -0700139OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700140 // The context has already been destroyed at this point, do not call
141 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700142}
143
Romain Guy87e2f7572012-09-24 11:37:12 -0700144void OpenGLRenderer::initProperties() {
145 char property[PROPERTY_VALUE_MAX];
146 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
147 mScissorOptimizationDisabled = !strcasecmp(property, "true");
148 INIT_LOGD(" Scissor optimization %s",
149 mScissorOptimizationDisabled ? "disabled" : "enabled");
150 } else {
151 INIT_LOGD(" Scissor optimization enabled");
152 }
Romain Guy13631f32012-01-30 17:41:55 -0800153}
154
155///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700156// Setup
157///////////////////////////////////////////////////////////////////////////////
158
Romain Guyef359272013-01-31 19:07:29 -0800159void OpenGLRenderer::setName(const char* name) {
160 if (name) {
161 mName.setTo(name);
162 } else {
163 mName.clear();
164 }
165}
166
167const char* OpenGLRenderer::getName() const {
168 return mName.string();
169}
170
Romain Guy49c5fc02012-05-15 11:10:01 -0700171bool OpenGLRenderer::isDeferred() {
172 return false;
173}
174
Romain Guy85bf02f2010-06-22 13:11:24 -0700175void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700176 initViewport(width, height);
177
178 glDisable(GL_DITHER);
179 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
180
181 glEnableVertexAttribArray(Program::kBindingPosition);
182}
183
184void OpenGLRenderer::initViewport(int width, int height) {
Chris Craikba9b6132013-12-15 17:10:19 -0800185 if (mCaches.propertyEnable3d) {
Chris Craikf57776b2013-10-25 18:30:17 -0700186 // TODO: make view proj app configurable
Chris Craikba9b6132013-12-15 17:10:19 -0800187 float dist = std::max(width, height) * 1.5;
188 dist *= mCaches.propertyCameraDistance;
Chris Craikf57776b2013-10-25 18:30:17 -0700189 Matrix4 projection;
190 projection.loadFrustum(-width / 2, -height / 2, width / 2, height / 2, dist, 0);
191 Matrix4 view;
192 view.loadLookAt(0, 0, dist,
193 0, 0, 0,
194 0, 1, 0);
195 mViewProjMatrix.loadMultiply(projection, view);
196 mViewProjMatrix.translate(-width/2, -height/2);
197 } else {
198 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
199 }
Romain Guybb9524b2010-06-22 18:56:38 -0700200
201 mWidth = width;
202 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700203
204 mFirstSnapshot->height = height;
205 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700206}
207
Romain Guy96885eb2013-03-26 15:05:58 -0700208void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800209 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800210 mCaches.clearGarbage();
211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700213 mSnapshot = new Snapshot(mFirstSnapshot,
214 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800215 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700216 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700217
Romain Guy7d7b5492011-01-24 16:33:45 -0800218 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700219 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700220}
221
222status_t OpenGLRenderer::startFrame() {
223 if (mFrameStarted) return DrawGlInfo::kStatusDone;
224 mFrameStarted = true;
225
Romain Guy41308e22012-10-22 20:02:43 -0700226 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700227
Romain Guy96885eb2013-03-26 15:05:58 -0700228 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700229
Romain Guy96885eb2013-03-26 15:05:58 -0700230 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800231
Romain Guy54c1a642012-09-27 17:55:46 -0700232 // Functors break the tiling extension in pretty spectacular ways
233 // This ensures we don't use tiling when a functor is going to be
234 // invoked during the frame
235 mSuppressTiling = mCaches.hasRegisteredFunctors();
236
Chris Craik5f803622013-03-21 14:39:04 -0700237 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700238
Romain Guy7c450aa2012-09-21 19:15:00 -0700239 debugOverdraw(true, true);
240
Romain Guy96885eb2013-03-26 15:05:58 -0700241 return clear(mTilingClip.left, mTilingClip.top,
242 mTilingClip.right, mTilingClip.bottom, mOpaque);
243}
244
245status_t OpenGLRenderer::prepare(bool opaque) {
246 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
247}
248
249status_t OpenGLRenderer::prepareDirty(float left, float top,
250 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700251
Romain Guy96885eb2013-03-26 15:05:58 -0700252 setupFrameState(left, top, right, bottom, opaque);
253
254 // Layer renderers will start the frame immediately
255 // The framebuffer renderer will first defer the display list
256 // for each layer and wait until the first drawing command
257 // to start the frame
258 if (mSnapshot->fbo == 0) {
259 syncState();
260 updateLayers();
261 } else {
262 return startFrame();
263 }
264
265 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700266}
267
Romain Guydcfc8362013-01-03 13:08:57 -0800268void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
269 // If we know that we are going to redraw the entire framebuffer,
270 // perform a discard to let the driver know we don't need to preserve
271 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800272 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800273 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800274 const bool isFbo = getTargetFbo() == 0;
275 const GLenum attachments[] = {
276 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
277 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800278 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
279 }
280}
281
Romain Guy7c25aab2012-10-18 15:05:02 -0700282status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700283 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700284 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700285 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700286 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700287 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700288 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700289
Romain Guy7c25aab2012-10-18 15:05:02 -0700290 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700291 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700292}
293
294void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700295 if (mCaches.blend) {
296 glEnable(GL_BLEND);
297 } else {
298 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700299 }
Romain Guybb9524b2010-06-22 18:56:38 -0700300}
301
Romain Guy57b52682012-09-20 17:38:46 -0700302void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700303 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700304 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800305 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700306 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700307 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700308
Romain Guyc3fedaf2013-01-29 17:26:25 -0800309 startTiling(*clip, s->height, opaque);
310 }
311}
312
313void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
314 if (!mSuppressTiling) {
315 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800316 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700317 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700318}
319
320void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700321 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700322}
323
Romain Guyb025b9c2010-09-16 14:16:48 -0700324void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700325 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700326 endTiling();
327
Romain Guyca89e2a2013-03-08 17:44:20 -0800328 // When finish() is invoked on FBO 0 we've reached the end
329 // of the current frame
330 if (getTargetFbo() == 0) {
331 mCaches.pathCache.trim();
332 }
333
Romain Guy11cb6422012-09-21 00:39:43 -0700334 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700335#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700336 GLenum status = GL_NO_ERROR;
337 while ((status = glGetError()) != GL_NO_ERROR) {
338 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
339 switch (status) {
340 case GL_INVALID_ENUM:
341 ALOGE(" GL_INVALID_ENUM");
342 break;
343 case GL_INVALID_VALUE:
344 ALOGE(" GL_INVALID_VALUE");
345 break;
346 case GL_INVALID_OPERATION:
347 ALOGE(" GL_INVALID_OPERATION");
348 break;
349 case GL_OUT_OF_MEMORY:
350 ALOGE(" Out of memory!");
351 break;
352 }
Romain Guya07105b2011-01-10 21:14:18 -0800353 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700354#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700355
Romain Guyc15008e2010-11-10 11:59:15 -0800356#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800357 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700358#else
359 if (mCaches.getDebugLevel() & kDebugMemory) {
360 mCaches.dumpMemoryUsage();
361 }
Romain Guyc15008e2010-11-10 11:59:15 -0800362#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700363 }
Romain Guy96885eb2013-03-26 15:05:58 -0700364
Romain Guy78dd96d2013-05-03 14:24:16 -0700365 if (mCountOverdraw) {
366 countOverdraw();
367 }
368
Romain Guy96885eb2013-03-26 15:05:58 -0700369 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700370}
371
Romain Guy6c319ca2011-01-11 14:29:25 -0800372void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700373 if (mCaches.currentProgram) {
374 if (mCaches.currentProgram->isInUse()) {
375 mCaches.currentProgram->remove();
376 mCaches.currentProgram = NULL;
377 }
378 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700379 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700380 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800381 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800382 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800383 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700384 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700385}
386
Romain Guy6c319ca2011-01-11 14:29:25 -0800387void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800388 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800389 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700390 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700391 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700392
Romain Guy3e263fa2011-12-12 16:47:48 -0800393 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
394
Chet Haase80250612012-08-15 13:46:54 -0700395 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700396 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800397 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700398 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700399
Romain Guya1d3c912011-12-13 14:55:06 -0800400 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700401 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700402
Romain Guy50c0f092010-10-19 11:42:22 -0700403 mCaches.blend = true;
404 glEnable(GL_BLEND);
405 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
406 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700407}
408
Romain Guy35643dd2012-09-18 15:40:58 -0700409void OpenGLRenderer::resumeAfterLayer() {
410 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
411 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
412 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700413 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700414
415 mCaches.resetScissor();
416 dirtyClip();
417}
418
Romain Guyba6be8a2012-04-23 18:22:09 -0700419void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700420 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700421}
422
423void OpenGLRenderer::attachFunctor(Functor* functor) {
424 mFunctors.add(functor);
425}
426
Romain Guy8f3b8e32012-03-27 16:33:45 -0700427status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
428 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700429 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700430
Romain Guyba6be8a2012-04-23 18:22:09 -0700431 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800432 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700433 SortedVector<Functor*> functors(mFunctors);
434 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700435
Romain Guyba6be8a2012-04-23 18:22:09 -0700436 DrawGlInfo info;
437 info.clipLeft = 0;
438 info.clipTop = 0;
439 info.clipRight = 0;
440 info.clipBottom = 0;
441 info.isLayer = false;
442 info.width = 0;
443 info.height = 0;
444 memset(info.transform, 0, sizeof(float) * 16);
445
446 for (size_t i = 0; i < count; i++) {
447 Functor* f = functors.itemAt(i);
448 result |= (*f)(DrawGlInfo::kModeProcess, &info);
449
Chris Craikc2c95432012-04-25 15:13:52 -0700450 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700451 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
452 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700453 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700454
Chris Craikc2c95432012-04-25 15:13:52 -0700455 if (result & DrawGlInfo::kStatusInvoke) {
456 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700457 }
458 }
Chris Craikd15321b2012-11-28 14:45:04 -0800459 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700460 }
461
462 return result;
463}
464
465status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700466 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
467
Chris Craik932b7f62012-06-06 13:59:33 -0700468 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700469
Romain Guyd643bb52011-03-01 14:55:21 -0800470
Romain Guy80911b82011-03-16 15:30:12 -0700471 Rect clip(*mSnapshot->clipRect);
472 clip.snapToPixelBoundaries();
473
Romain Guyd643bb52011-03-01 14:55:21 -0800474 // Since we don't know what the functor will draw, let's dirty
475 // tne entire clip region
476 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800477 dirtyLayerUnchecked(clip, getRegion());
478 }
Romain Guyd643bb52011-03-01 14:55:21 -0800479
Romain Guy08aa2cb2011-03-17 11:06:57 -0700480 DrawGlInfo info;
481 info.clipLeft = clip.left;
482 info.clipTop = clip.top;
483 info.clipRight = clip.right;
484 info.clipBottom = clip.bottom;
485 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700486 info.width = getSnapshot()->viewport.getWidth();
487 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700488 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700489
John Reck25d2f7b2013-09-10 13:12:09 -0700490 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700491 // setup GL state for functor
492 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700493 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
494 }
John Reck25d2f7b2013-09-10 13:12:09 -0700495 if (mCaches.enableScissor() || dirtyClip) {
496 setScissorFromClip();
497 }
Chris Craik54f574a2013-08-26 11:23:46 -0700498 interrupt();
499
500 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700501 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800502
Romain Guy8f3b8e32012-03-27 16:33:45 -0700503 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700504 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800505 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700506
Chris Craik65924a32012-04-05 17:52:11 -0700507 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700508 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700509 }
Romain Guycabfcc12011-03-07 18:06:46 -0800510 }
511
Chet Haasedaf98e92011-01-10 14:10:36 -0800512 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700513 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800514}
515
Romain Guyf6a11b82010-06-23 17:47:49 -0700516///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700517// Debug
518///////////////////////////////////////////////////////////////////////////////
519
Romain Guy0f6675332013-03-01 14:31:04 -0800520void OpenGLRenderer::eventMark(const char* name) const {
521 mCaches.eventMark(0, name);
522}
523
Romain Guy87e2f7572012-09-24 11:37:12 -0700524void OpenGLRenderer::startMark(const char* name) const {
525 mCaches.startMark(0, name);
526}
527
528void OpenGLRenderer::endMark() const {
529 mCaches.endMark();
530}
531
532void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
533 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
534 if (clear) {
535 mCaches.disableScissor();
536 mCaches.stencil.clear();
537 }
538 if (enable) {
539 mCaches.stencil.enableDebugWrite();
540 } else {
541 mCaches.stencil.disable();
542 }
543 }
544}
545
546void OpenGLRenderer::renderOverdraw() {
547 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700548 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700549
550 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700551 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700552 clip->right - clip->left, clip->bottom - clip->top);
553
Romain Guy627c6fd2013-08-21 11:53:18 -0700554 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700555 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700556 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
557
558 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700559 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700560 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
561
562 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700563 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700564 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
565
566 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700567 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700568 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
569
Romain Guy87e2f7572012-09-24 11:37:12 -0700570 mCaches.stencil.disable();
571 }
572}
573
Romain Guy78dd96d2013-05-03 14:24:16 -0700574void OpenGLRenderer::countOverdraw() {
575 size_t count = mWidth * mHeight;
576 uint32_t* buffer = new uint32_t[count];
577 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
578
579 size_t total = 0;
580 for (size_t i = 0; i < count; i++) {
581 total += buffer[i] & 0xff;
582 }
583
584 mOverdraw = total / float(count);
585
586 delete[] buffer;
587}
588
Romain Guy87e2f7572012-09-24 11:37:12 -0700589///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700590// Layers
591///////////////////////////////////////////////////////////////////////////////
592
593bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700594 if (layer->deferredUpdateScheduled && layer->renderer &&
595 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700596 ATRACE_CALL();
597
Romain Guy11cb6422012-09-21 00:39:43 -0700598 Rect& dirty = layer->dirtyRect;
599
Romain Guy7c450aa2012-09-21 19:15:00 -0700600 if (inFrame) {
601 endTiling();
602 debugOverdraw(false, false);
603 }
Romain Guy11cb6422012-09-21 00:39:43 -0700604
Romain Guy96885eb2013-03-26 15:05:58 -0700605 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700606 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700607 } else {
608 layer->defer();
609 }
Romain Guy11cb6422012-09-21 00:39:43 -0700610
611 if (inFrame) {
612 resumeAfterLayer();
613 startTiling(mSnapshot);
614 }
615
Romain Guy5bb3c732012-11-29 17:52:58 -0800616 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700617 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700618
619 return true;
620 }
621
622 return false;
623}
624
625void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700626 // If draw deferring is enabled this method will simply defer
627 // the display list of each individual layer. The layers remain
628 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700629 int count = mLayerUpdates.size();
630 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700631 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
632 startMark("Layer Updates");
633 } else {
634 startMark("Defer Layer Updates");
635 }
Romain Guy11cb6422012-09-21 00:39:43 -0700636
Chris Craik1206b9b2013-04-04 14:46:24 -0700637 // Note: it is very important to update the layers in order
638 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700639 Layer* layer = mLayerUpdates.itemAt(i);
640 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700641 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
642 mCaches.resourceCache.decrementRefcount(layer);
643 }
Romain Guy11cb6422012-09-21 00:39:43 -0700644 }
Romain Guy11cb6422012-09-21 00:39:43 -0700645
Romain Guy96885eb2013-03-26 15:05:58 -0700646 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
647 mLayerUpdates.clear();
648 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
649 }
650 endMark();
651 }
652}
653
654void OpenGLRenderer::flushLayers() {
655 int count = mLayerUpdates.size();
656 if (count > 0) {
657 startMark("Apply Layer Updates");
658 char layerName[12];
659
Chris Craik1206b9b2013-04-04 14:46:24 -0700660 // Note: it is very important to update the layers in order
661 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700662 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700663 startMark(layerName);
664
Romain Guy40543602013-06-12 15:31:28 -0700665 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700666 Layer* layer = mLayerUpdates.itemAt(i);
667 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700668 ATRACE_END();
669
Romain Guy02b49b72013-03-29 12:37:16 -0700670 mCaches.resourceCache.decrementRefcount(layer);
671
Romain Guy96885eb2013-03-26 15:05:58 -0700672 endMark();
673 }
674
675 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700676 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700677
Romain Guy11cb6422012-09-21 00:39:43 -0700678 endMark();
679 }
680}
681
682void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
683 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700684 // Make sure we don't introduce duplicates.
685 // SortedVector would do this automatically but we need to respect
686 // the insertion order. The linear search is not an issue since
687 // this list is usually very short (typically one item, at most a few)
688 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
689 if (mLayerUpdates.itemAt(i) == layer) {
690 return;
691 }
692 }
Romain Guy11cb6422012-09-21 00:39:43 -0700693 mLayerUpdates.push_back(layer);
694 mCaches.resourceCache.incrementRefcount(layer);
695 }
696}
697
Romain Guye93482f2013-06-17 13:14:51 -0700698void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
699 if (layer) {
700 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
701 if (mLayerUpdates.itemAt(i) == layer) {
702 mLayerUpdates.removeAt(i);
703 mCaches.resourceCache.decrementRefcount(layer);
704 break;
705 }
706 }
707 }
708}
709
Romain Guy11cb6422012-09-21 00:39:43 -0700710void OpenGLRenderer::clearLayerUpdates() {
711 size_t count = mLayerUpdates.size();
712 if (count > 0) {
713 mCaches.resourceCache.lock();
714 for (size_t i = 0; i < count; i++) {
715 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
716 }
717 mCaches.resourceCache.unlock();
718 mLayerUpdates.clear();
719 }
720}
721
Romain Guy40543602013-06-12 15:31:28 -0700722void OpenGLRenderer::flushLayerUpdates() {
723 syncState();
724 updateLayers();
725 flushLayers();
726 // Wait for all the layer updates to be executed
727 AutoFence fence;
728}
729
Romain Guy11cb6422012-09-21 00:39:43 -0700730///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700731// State management
732///////////////////////////////////////////////////////////////////////////////
733
Romain Guybb9524b2010-06-22 18:56:38 -0700734int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700735 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700736}
737
738int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700739 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700740}
741
742void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700743 if (mSaveCount > 1) {
744 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700745 }
Romain Guybb9524b2010-06-22 18:56:38 -0700746}
747
748void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700749 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700750
Romain Guy8fb95422010-08-17 18:38:51 -0700751 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700752 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700753 }
Romain Guybb9524b2010-06-22 18:56:38 -0700754}
755
Romain Guy8aef54f2010-09-01 15:13:49 -0700756int OpenGLRenderer::saveSnapshot(int flags) {
757 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700758 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700759}
760
761bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700762 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700763 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700764 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700765
Romain Guybd6b79b2010-06-26 00:13:53 -0700766 sp<Snapshot> current = mSnapshot;
767 sp<Snapshot> previous = mSnapshot->previous;
768
Romain Guyeb993562010-10-05 18:14:38 -0700769 if (restoreOrtho) {
770 Rect& r = previous->viewport;
771 glViewport(r.left, r.top, r.right, r.bottom);
Chris Craikf57776b2013-10-25 18:30:17 -0700772 mViewProjMatrix.load(current->orthoMatrix);
Romain Guyeb993562010-10-05 18:14:38 -0700773 }
774
Romain Guy8b55f372010-08-18 17:10:07 -0700775 mSaveCount--;
776 mSnapshot = previous;
777
Romain Guy2542d192010-08-18 11:47:12 -0700778 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700779 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700780 }
Romain Guy2542d192010-08-18 11:47:12 -0700781
Romain Guy5ec99242010-11-03 16:19:08 -0700782 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700783 endMark(); // Savelayer
784 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700785 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700786 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700787 }
788
Romain Guy2542d192010-08-18 11:47:12 -0700789 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700790}
791
Romain Guyf6a11b82010-06-23 17:47:49 -0700792///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700793// Layers
794///////////////////////////////////////////////////////////////////////////////
795
796int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800797 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700798 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700799
Romain Guyaf636eb2010-12-09 17:47:21 -0800800 if (!mSnapshot->isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700801 createLayer(left, top, right, bottom, alpha, mode, flags);
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) {
Chris Craikd90144d2013-03-19 15:03:48 -0700853 const int count = saveSnapshot(flags);
854
855 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
856 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
857 // operations will be able to store and restore the current clip and transform info, and
858 // quick rejection will be correct (for display lists)
859
860 Rect bounds(left, top, right, bottom);
861 Rect clip;
862 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700863 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700864
Chris Craik408eb122013-03-26 18:55:15 -0700865 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700866 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
867 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700868 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700869 }
870 }
871
872 return count;
873}
874
875
Romain Guy1c740bc2010-09-13 18:00:09 -0700876/**
877 * Layers are viewed by Skia are slightly different than layers in image editing
878 * programs (for instance.) When a layer is created, previously created layers
879 * and the frame buffer still receive every drawing command. For instance, if a
880 * layer is created and a shape intersecting the bounds of the layers and the
881 * framebuffer is draw, the shape will be drawn on both (unless the layer was
882 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
883 *
884 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
885 * texture. Unfortunately, this is inefficient as it requires every primitive to
886 * be drawn n + 1 times, where n is the number of active layers. In practice this
887 * means, for every primitive:
888 * - Switch active frame buffer
889 * - Change viewport, clip and projection matrix
890 * - Issue the drawing
891 *
892 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700893 * To avoid this, layers are implemented in a different way here, at least in the
894 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
895 * is set. When this flag is set we can redirect all drawing operations into a
896 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700897 *
898 * This implementation relies on the frame buffer being at least RGBA 8888. When
899 * a layer is created, only a texture is created, not an FBO. The content of the
900 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700901 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700902 * buffer and drawing continues as normal. This technique therefore treats the
903 * frame buffer as a scratch buffer for the layers.
904 *
905 * To compose the layers back onto the frame buffer, each layer texture
906 * (containing the original frame buffer data) is drawn as a simple quad over
907 * the frame buffer. The trick is that the quad is set as the composition
908 * destination in the blending equation, and the frame buffer becomes the source
909 * of the composition.
910 *
911 * Drawing layers with an alpha value requires an extra step before composition.
912 * An empty quad is drawn over the layer's region in the frame buffer. This quad
913 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
914 * quad is used to multiply the colors in the frame buffer. This is achieved by
915 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
916 * GL_ZERO, GL_SRC_ALPHA.
917 *
918 * Because glCopyTexImage2D() can be slow, an alternative implementation might
919 * be use to draw a single clipped layer. The implementation described above
920 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700921 *
922 * (1) The frame buffer is actually not cleared right away. To allow the GPU
923 * to potentially optimize series of calls to glCopyTexImage2D, the frame
924 * buffer is left untouched until the first drawing operation. Only when
925 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700926 */
Chet Haased48885a2012-08-28 17:43:28 -0700927bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700928 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700929 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700930 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700931
Romain Guyeb993562010-10-05 18:14:38 -0700932 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
933
Romain Guyf607bdc2010-09-10 19:20:06 -0700934 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700935 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700937 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700938 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700939
940 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700941 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700942 return false;
943 }
Romain Guyf18fd992010-07-08 11:45:51 -0700944
Romain Guya1d3c912011-12-13 14:55:06 -0800945 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700946 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700947 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700948 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700949 }
950
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700952 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
954 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800955 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700956 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700957 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700958
Romain Guy8fb95422010-08-17 18:38:51 -0700959 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700960 mSnapshot->flags |= Snapshot::kFlagIsLayer;
961 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700962
Chris Craik7273daa2013-03-28 11:25:24 -0700963 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700964 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700965 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700966 } else {
967 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800969 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700970 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700971 // Workaround for some GL drivers. When reading pixels lying outside
972 // of the window we should get undefined values for those pixels.
973 // Unfortunately some drivers will turn the entire target texture black
974 // when reading outside of the window.
975 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
976 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800978 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700979
Romain Guyb254c242013-06-27 17:15:24 -0700980 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
981 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
982
Romain Guy54be1cd2011-06-13 19:04:27 -0700983 // Enqueue the buffer coordinates to clear the corresponding region later
984 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700985 }
Romain Guyeb993562010-10-05 18:14:38 -0700986 }
Romain Guyf86ef572010-07-01 11:05:42 -0700987
Romain Guyd55a8612010-06-28 17:42:46 -0700988 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700989}
990
Chris Craike63f7c622013-10-17 10:30:55 -0700991bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800992 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
Chet Haased48885a2012-08-28 17:43:28 -0700995 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800996 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
997 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700998 mSnapshot->fbo = layer->getFbo();
999 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
1000 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
1001 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
1002 mSnapshot->height = bounds.getHeight();
Chris Craikf57776b2013-10-25 18:30:17 -07001003 mSnapshot->orthoMatrix.load(mViewProjMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -07001004
Romain Guy2b7028e2012-09-19 17:25:38 -07001005 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -07001006 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001007 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -07001008 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
1009 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001010
1011 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -07001013 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -07001014 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001015 }
1016
1017 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001018 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001019
Romain Guyf735c8e2013-01-31 17:45:55 -08001020 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001021
1022 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001023 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001024 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001025 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001026 glClear(GL_COLOR_BUFFER_BIT);
1027
1028 dirtyClip();
1029
1030 // Change the ortho projection
1031 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craikf57776b2013-10-25 18:30:17 -07001032
1033 // TODO: determine best way to support 3d drawing within HW layers
1034 mViewProjMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001035
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;
Chris Craikf0a59072013-11-19 18:00:46 -08001053 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
1054 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -07001055 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1056
Romain Guyeb993562010-10-05 18:14:38 -07001057 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001058 endTiling();
1059
Romain Guye0aa84b2012-04-03 19:30:26 -07001060 // Detach the texture from the FBO
1061 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001062
1063 layer->removeFbo(false);
1064
Romain Guyeb993562010-10-05 18:14:38 -07001065 // Unbind current FBO and restore previous one
1066 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001067 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001068
1069 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001070 }
1071
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001073 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001075 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001076 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001077 }
1078
Romain Guy03750a02010-10-18 14:06:08 -07001079 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001080
Romain Guya1d3c912011-12-13 14:55:06 -08001081 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001082
Romain Guy5b3b3522010-10-27 18:57:51 -07001083 // When the layer is stored in an FBO, we can save a bit of fillrate by
1084 // drawing only the dirty region
1085 if (fboLayer) {
1086 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001087 if (layer->getColorFilter()) {
1088 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001089 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001090 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001091 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001092 resetColorFilter();
1093 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001094 } else if (!rect.isEmpty()) {
1095 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301096
1097 save(0);
1098 // the layer contains screen buffer content that shouldn't be alpha modulated
1099 // (and any necessary alpha modulation was handled drawing into the layer)
1100 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001101 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301102 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001103 }
Romain Guy8b55f372010-08-18 17:10:07 -07001104
Romain Guy746b7402010-10-26 16:27:31 -07001105 dirtyClip();
1106
Romain Guyeb993562010-10-05 18:14:38 -07001107 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001108 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001109 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001110 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001111 }
1112}
1113
Romain Guyaa6c24c2011-04-28 18:40:04 -07001114void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001115 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001116
1117 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001118 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001119 setupDrawWithTexture();
1120 } else {
1121 setupDrawWithExternalTexture();
1122 }
1123 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001124 setupDrawColor(alpha, alpha, alpha, alpha);
1125 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001126 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001127 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001128 setupDrawPureColorUniforms();
1129 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001130 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1131 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001132 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001133 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001134 }
Romain Guy3b753822013-03-05 10:27:35 -08001135 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001136 layer->getWidth() == (uint32_t) rect.getWidth() &&
1137 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001138 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1139 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001140
Romain Guyd21b6e12011-11-30 20:21:23 -08001141 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001142 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1143 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001144 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001145 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001146 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1147 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001148 }
1149 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001150 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001151
1152 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001153}
1154
Romain Guy5b3b3522010-10-27 18:57:51 -07001155void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001157 const Rect& texCoords = layer->texCoords;
1158 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1159 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Romain Guy9ace8f52011-07-07 20:50:11 -07001161 float x = rect.left;
1162 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001163 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001164 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001165 layer->getHeight() == (uint32_t) rect.getHeight();
1166
1167 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001168 // When we're swapping, the layer is already in screen coordinates
1169 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001170 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1171 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001172 }
1173
Romain Guyd21b6e12011-11-30 20:21:23 -08001174 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001175 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001176 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001177 }
1178
Chris Craik16ecda52013-03-29 10:59:59 -07001179 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001180 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001181 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001182 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001183 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001184 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001185
Romain Guyaa6c24c2011-04-28 18:40:04 -07001186 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1187 } else {
1188 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1189 drawTextureLayer(layer, rect);
1190 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1191 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001192}
1193
Chris Craik34416ea2013-04-15 16:08:28 -07001194/**
1195 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1196 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1197 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1198 * by saveLayer's restore
1199 */
1200#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1201 DRAW_COMMAND; \
1202 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1203 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1204 DRAW_COMMAND; \
1205 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1206 } \
1207 }
1208
1209#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1210
Romain Guy5b3b3522010-10-27 18:57:51 -07001211void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001212 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001213 layer->setRegionAsRect();
1214
Chris Craik34416ea2013-04-15 16:08:28 -07001215 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001216
Romain Guy5b3b3522010-10-27 18:57:51 -07001217 layer->region.clear();
1218 return;
1219 }
1220
Romain Guy211370f2012-02-01 16:10:55 -08001221 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001222 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001223 const android::Rect* rects;
1224 Region safeRegion;
1225 if (CC_LIKELY(hasRectToRectTransform())) {
1226 rects = layer->region.getArray(&count);
1227 } else {
1228 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1229 rects = safeRegion.getArray(&count);
1230 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001231
Chris Craik16ecda52013-03-29 10:59:59 -07001232 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001233 const float texX = 1.0f / float(layer->getWidth());
1234 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001235 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001236
Romain Guy8ce00302013-01-15 18:51:42 -08001237 setupDraw();
1238
1239 // We must get (and therefore bind) the region mesh buffer
1240 // after we setup drawing in case we need to mess with the
1241 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001242 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001243 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001244
Romain Guy7230a742011-01-10 22:26:16 -08001245 setupDrawWithTexture();
1246 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001247 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001248 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001249 setupDrawProgram();
1250 setupDrawDirtyRegionsDisabled();
1251 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001252 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001253 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001254 if (currentTransform().isPureTranslate()) {
1255 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1256 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001257
Romain Guyd21b6e12011-11-30 20:21:23 -08001258 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001259 setupDrawModelView(kModelViewMode_Translate, false,
1260 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001261 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001262 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001263 setupDrawModelView(kModelViewMode_Translate, false,
1264 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001265 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001266 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001267
1268 for (size_t i = 0; i < count; i++) {
1269 const android::Rect* r = &rects[i];
1270
1271 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001272 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001273 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001274 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001275
1276 // TODO: Reject quads outside of the clip
1277 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1278 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1279 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1280 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1281
1282 numQuads++;
1283
Romain Guy31e08e92013-06-18 15:53:53 -07001284 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001285 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1286 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001287 numQuads = 0;
1288 mesh = mCaches.getRegionMesh();
1289 }
1290 }
1291
1292 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001293 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1294 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001295 }
1296
Romain Guy5b3b3522010-10-27 18:57:51 -07001297#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001298 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001299#endif
1300
1301 layer->region.clear();
1302 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001303}
1304
Romain Guy3a3133d2011-02-01 22:59:58 -08001305#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001306void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001307 size_t count;
1308 const android::Rect* rects = region.getArray(&count);
1309
1310 uint32_t colors[] = {
1311 0x7fff0000, 0x7f00ff00,
1312 0x7f0000ff, 0x7fff00ff,
1313 };
1314
1315 int offset = 0;
1316 int32_t top = rects[0].top;
1317
1318 for (size_t i = 0; i < count; i++) {
1319 if (top != rects[i].top) {
1320 offset ^= 0x2;
1321 top = rects[i].top;
1322 }
1323
1324 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1325 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1326 SkXfermode::kSrcOver_Mode);
1327 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001328}
Chris Craike63f7c622013-10-17 10:30:55 -07001329#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001330
Romain Guy8ce00302013-01-15 18:51:42 -08001331void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1332 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001333 Vector<float> rects;
1334
1335 SkRegion::Iterator it(region);
1336 while (!it.done()) {
1337 const SkIRect& r = it.rect();
1338 rects.push(r.fLeft);
1339 rects.push(r.fTop);
1340 rects.push(r.fRight);
1341 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001342 it.next();
1343 }
1344
Romain Guy448455f2013-07-22 13:57:50 -07001345 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001346}
1347
Romain Guy5b3b3522010-10-27 18:57:51 -07001348void OpenGLRenderer::dirtyLayer(const float left, const float top,
1349 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001350 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001351 Rect bounds(left, top, right, bottom);
1352 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001353 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001354 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001355}
1356
1357void OpenGLRenderer::dirtyLayer(const float left, const float top,
1358 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001359 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001360 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001361 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001362 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001363}
1364
1365void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001366 if (bounds.intersect(*mSnapshot->clipRect)) {
1367 bounds.snapToPixelBoundaries();
1368 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1369 if (!dirty.isEmpty()) {
1370 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001371 }
1372 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001373}
1374
Chris Craik4063a0e2013-11-15 16:06:56 -08001375void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001376 GLsizei elementsCount = quadsCount * 6;
1377 while (elementsCount > 0) {
1378 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1379
Romain Guy3380cfd2013-08-15 16:57:57 -07001380 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001381 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1382
1383 elementsCount -= drawCount;
1384 // Though there are 4 vertices in a quad, we use 6 indices per
1385 // quad to draw with GL_TRIANGLES
1386 mesh += (drawCount / 6) * 4;
1387 }
1388}
1389
Romain Guy54be1cd2011-06-13 19:04:27 -07001390void OpenGLRenderer::clearLayerRegions() {
1391 const size_t count = mLayers.size();
1392 if (count == 0) return;
1393
1394 if (!mSnapshot->isIgnored()) {
1395 // Doing several glScissor/glClear here can negatively impact
1396 // GPUs with a tiler architecture, instead we draw quads with
1397 // the Clear blending mode
1398
1399 // The list contains bounds that have already been clipped
1400 // against their initial clip rect, and the current clip
1401 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001402 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001403
Romain Guy448455f2013-07-22 13:57:50 -07001404 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001405 Vertex* vertex = mesh;
1406
1407 for (uint32_t i = 0; i < count; i++) {
1408 Rect* bounds = mLayers.itemAt(i);
1409
Romain Guy54be1cd2011-06-13 19:04:27 -07001410 Vertex::set(vertex++, bounds->left, bounds->top);
1411 Vertex::set(vertex++, bounds->right, bounds->top);
1412 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001413 Vertex::set(vertex++, bounds->right, bounds->bottom);
1414
1415 delete bounds;
1416 }
Romain Guye67307c2013-02-11 18:01:20 -08001417 // We must clear the list of dirty rects before we
1418 // call setupDraw() to prevent stencil setup to do
1419 // the same thing again
1420 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001421
1422 setupDraw(false);
1423 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1424 setupDrawBlending(true, SkXfermode::kClear_Mode);
1425 setupDrawProgram();
1426 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001427 setupDrawModelView(kModelViewMode_Translate, false,
1428 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001429
Chris Craik4063a0e2013-11-15 16:06:56 -08001430 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001431
1432 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001433 } else {
1434 for (uint32_t i = 0; i < count; i++) {
1435 delete mLayers.itemAt(i);
1436 }
Romain Guye67307c2013-02-11 18:01:20 -08001437 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001438 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001439}
1440
Romain Guybd6b79b2010-06-26 00:13:53 -07001441///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001442// State Deferral
1443///////////////////////////////////////////////////////////////////////////////
1444
Chris Craikff785832013-03-08 13:12:16 -08001445bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001446 const Rect& currentClip = *(mSnapshot->clipRect);
1447 const mat4& currentMatrix = *(mSnapshot->transform);
1448
Chris Craikff785832013-03-08 13:12:16 -08001449 if (stateDeferFlags & kStateDeferFlag_Draw) {
1450 // state has bounds initialized in local coordinates
1451 if (!state.mBounds.isEmpty()) {
1452 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001453 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001454 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1455 // is used, it should more closely duplicate the quickReject logic (in how it uses
1456 // snapToPixelBoundaries)
1457
Chris Craik28ce94a2013-05-31 11:38:03 -07001458 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001459 // quick rejected
1460 return true;
1461 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001462
Chris Craika02c4ed2013-06-14 13:43:58 -07001463 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001464 if (!currentClip.contains(state.mBounds)) {
1465 int& flags = state.mClipSideFlags;
1466 // op partially clipped, so record which sides are clipped for clip-aware merging
1467 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1468 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1469 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1470 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1471 }
1472 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001473 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001474 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1475 // overdraw avoidance (since we don't know what it overlaps)
1476 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001477 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001478 }
1479 }
1480
Chris Craik527a3aa2013-03-04 10:19:31 -08001481 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1482 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001483 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001484 }
1485
Chris Craik7273daa2013-03-28 11:25:24 -07001486 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1487 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001488 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001489 state.mDrawModifiers = mDrawModifiers;
1490 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001491 return false;
1492}
1493
Chris Craik527a3aa2013-03-04 10:19:31 -08001494void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001495 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001496 mDrawModifiers = state.mDrawModifiers;
1497 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001498
Chris Craik527a3aa2013-03-04 10:19:31 -08001499 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001500 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1501 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001502 dirtyClip();
1503 }
Chris Craikc3566d02013-02-04 16:16:33 -08001504}
1505
Chris Craik28ce94a2013-05-31 11:38:03 -07001506/**
1507 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1508 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1509 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1510 *
1511 * This method should be called when restoreDisplayState() won't be restoring the clip
1512 */
1513void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1514 if (clipRect != NULL) {
1515 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1516 } else {
1517 mSnapshot->setClip(0, 0, mWidth, mHeight);
1518 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001519 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001520 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001521}
1522
Chris Craikc3566d02013-02-04 16:16:33 -08001523///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001524// Transforms
1525///////////////////////////////////////////////////////////////////////////////
1526
1527void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001528 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001529}
1530
1531void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001532 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001533}
1534
1535void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001536 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001537}
1538
Romain Guy807daf72011-01-18 11:19:19 -08001539void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001540 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001541}
1542
Romain Guyf6a11b82010-06-23 17:47:49 -07001543void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001544 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001545 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001546 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001547 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001548 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001549}
1550
Chris Craikb98a0162013-02-21 11:30:22 -08001551bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001552 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001553}
1554
Romain Guyf6a11b82010-06-23 17:47:49 -07001555void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001556 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001557}
1558
1559void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Chris Craikf57776b2013-10-25 18:30:17 -07001560 mat4 transform(*matrix);
1561 currentTransform().multiply(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001562}
1563
1564///////////////////////////////////////////////////////////////////////////////
1565// Clipping
1566///////////////////////////////////////////////////////////////////////////////
1567
Romain Guybb9524b2010-06-22 18:56:38 -07001568void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001569 Rect clip(*mSnapshot->clipRect);
1570 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001571
Romain Guy8a4ac612012-07-17 17:32:48 -07001572 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1573 clip.getWidth(), clip.getHeight())) {
1574 mDirtyClip = false;
1575 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001576}
1577
Romain Guy8ce00302013-01-15 18:51:42 -08001578void OpenGLRenderer::ensureStencilBuffer() {
1579 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1580 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1581 // just hope we have one when hasLayer() returns false.
1582 if (hasLayer()) {
1583 attachStencilBufferToLayer(mSnapshot->layer);
1584 }
1585}
1586
1587void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1588 // The layer's FBO is already bound when we reach this stage
1589 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001590 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1591 // is attached after we initiated tiling. We must turn it off,
1592 // attach the new render buffer then turn tiling back on
1593 endTiling();
1594
Romain Guy8d4aeb72013-02-12 16:08:55 -08001595 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001596 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001597 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001598
Romain Guyf735c8e2013-01-31 17:45:55 -08001599 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001600 }
1601}
1602
1603void OpenGLRenderer::setStencilFromClip() {
1604 if (!mCaches.debugOverdraw) {
1605 if (!mSnapshot->clipRegion->isEmpty()) {
1606 // NOTE: The order here is important, we must set dirtyClip to false
1607 // before any draw call to avoid calling back into this method
1608 mDirtyClip = false;
1609
1610 ensureStencilBuffer();
1611
1612 mCaches.stencil.enableWrite();
1613
1614 // Clear the stencil but first make sure we restrict drawing
1615 // to the region's bounds
1616 bool resetScissor = mCaches.enableScissor();
1617 if (resetScissor) {
1618 // The scissor was not set so we now need to update it
1619 setScissorFromClip();
1620 }
1621 mCaches.stencil.clear();
1622 if (resetScissor) mCaches.disableScissor();
1623
1624 // NOTE: We could use the region contour path to generate a smaller mesh
1625 // Since we are using the stencil we could use the red book path
1626 // drawing technique. It might increase bandwidth usage though.
1627
1628 // The last parameter is important: we are not drawing in the color buffer
1629 // so we don't want to dirty the current layer, if any
1630 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1631
1632 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001633
1634 // Draw the region used to generate the stencil if the appropriate debug
1635 // mode is enabled
1636 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1637 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1638 }
Romain Guy8ce00302013-01-15 18:51:42 -08001639 } else {
1640 mCaches.stencil.disable();
1641 }
1642 }
1643}
1644
Romain Guy9d5316e2010-06-24 19:30:36 -07001645const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001646 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001647}
1648
Chris Craikf0a59072013-11-19 18:00:46 -08001649/**
1650 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
1651 * the clipRect. Does not modify the scissor.
1652 *
1653 * @param clipRequired if not null, will be set to true if element intersects clip
1654 * (and wasn't rejected)
1655 *
1656 * @param snapOut if set, the geometry will be treated as having an AA ramp.
1657 * See Rect::snapGeometryToPixelBoundaries()
1658 */
1659bool OpenGLRenderer::calculateQuickRejectForScissor(float left, float top,
1660 float right, float bottom, bool* clipRequired, bool snapOut) const {
Chris Craik39a908c2013-06-13 14:39:01 -07001661 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001662 return true;
1663 }
1664
1665 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001666 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001667 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001668
1669 Rect clipRect(*mSnapshot->clipRect);
1670 clipRect.snapToPixelBoundaries();
1671
Chris Craik39a908c2013-06-13 14:39:01 -07001672 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001673
Chris Craikf0a59072013-11-19 18:00:46 -08001674 // clip is required if geometry intersects clip rect
Chris Craik39a908c2013-06-13 14:39:01 -07001675 if (clipRequired) *clipRequired = !clipRect.contains(r);
1676 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001677}
1678
Chris Craikf0a59072013-11-19 18:00:46 -08001679/**
1680 * Returns false if drawing won't be clipped out.
1681 *
1682 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
1683 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
1684 * rejection is still desired.
1685 *
1686 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
1687 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
1688 * rejection where precise rejection isn't important, or precise information isn't available.
1689 */
1690bool OpenGLRenderer::quickRejectConservative(float left, float top,
1691 float right, float bottom) const {
1692 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
1693 return true;
Chris Craikcb4d6002012-09-25 12:00:29 -07001694 }
Chris Craikf0a59072013-11-19 18:00:46 -08001695
1696 Rect r(left, top, right, bottom);
1697 currentTransform().mapRect(r);
1698 r.roundOut(); // rounded out to be conservative
1699
1700 Rect clipRect(*mSnapshot->clipRect);
1701 clipRect.snapToPixelBoundaries();
1702
1703 if (!clipRect.intersects(r)) return true;
1704
1705 return false;
Chris Craikcb4d6002012-09-25 12:00:29 -07001706}
1707
Chris Craikf0a59072013-11-19 18:00:46 -08001708/**
1709 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1710 *
1711 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1712 * style, and tessellated AA ramp
1713 */
1714bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
1715 SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001716 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001717 bool snapOut = paint && paint->isAntiAlias();
1718
1719 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1720 float outset = paint->getStrokeWidth() * 0.5f;
1721 left -= outset;
1722 top -= outset;
1723 right += outset;
1724 bottom += outset;
1725 }
1726
1727 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001728 return true;
1729 }
1730
Chris Craik39a908c2013-06-13 14:39:01 -07001731 if (!isDeferred()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001732 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001733 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001734 }
Chris Craik39a908c2013-06-13 14:39:01 -07001735 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001736}
1737
Romain Guy8ce00302013-01-15 18:51:42 -08001738void OpenGLRenderer::debugClip() {
1739#if DEBUG_CLIP_REGIONS
1740 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1741 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1742 }
1743#endif
1744}
1745
Romain Guy079ba2c2010-07-16 14:12:24 -07001746bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001747 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001748 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1749 if (clipped) {
1750 dirtyClip();
1751 }
1752 return !mSnapshot->clipRect->isEmpty();
1753 }
1754
1755 SkPath path;
1756 path.addRect(left, top, right, bottom);
1757
Romain Guyb7b93e02013-08-01 15:29:25 -07001758 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001759}
1760
1761bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1762 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001763 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001764
1765 SkPath transformed;
1766 path->transform(transform, &transformed);
1767
1768 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001769 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1770 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001771 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001772 if (mSnapshot->previous == mFirstSnapshot) {
1773 clip.setRect(0, 0, mWidth, mHeight);
1774 } else {
1775 Rect* bounds = mSnapshot->previous->clipRect;
1776 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1777 }
Romain Guy8ce00302013-01-15 18:51:42 -08001778 }
1779
1780 SkRegion region;
1781 region.setPath(transformed, clip);
1782
1783 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001784 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001785 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001786 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001787 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001788}
1789
Romain Guy735738c2012-12-03 12:34:51 -08001790bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001791 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1792 if (clipped) {
1793 dirtyClip();
1794 }
1795 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001796}
1797
Chet Haasea23eed82012-04-12 15:19:04 -07001798Rect* OpenGLRenderer::getClipRect() {
1799 return mSnapshot->clipRect;
1800}
1801
Romain Guyf6a11b82010-06-23 17:47:49 -07001802///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001803// Drawing commands
1804///////////////////////////////////////////////////////////////////////////////
1805
Romain Guy54be1cd2011-06-13 19:04:27 -07001806void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001807 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001808 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001809 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001810 // Make sure setScissor & setStencil happen at the beginning of
1811 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001812 if (mDirtyClip) {
1813 if (mCaches.scissorEnabled) {
1814 setScissorFromClip();
1815 }
Romain Guy8ce00302013-01-15 18:51:42 -08001816 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001817 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001818
Romain Guy70ca14e2010-12-13 18:24:33 -08001819 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001820
Romain Guy70ca14e2010-12-13 18:24:33 -08001821 mSetShaderColor = false;
1822 mColorSet = false;
1823 mColorA = mColorR = mColorG = mColorB = 0.0f;
1824 mTextureUnit = 0;
1825 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001826
1827 // Enable debug highlight when what we're about to draw is tested against
1828 // the stencil buffer and if stencil highlight debugging is on
1829 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1830 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1831 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001832
1833 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001834}
1835
1836void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1837 mDescription.hasTexture = true;
1838 mDescription.hasAlpha8Texture = isAlpha8;
1839}
1840
Romain Guyff316ec2013-02-13 18:39:43 -08001841void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1842 mDescription.hasTexture = true;
1843 mDescription.hasColors = true;
1844 mDescription.hasAlpha8Texture = isAlpha8;
1845}
1846
Romain Guyaa6c24c2011-04-28 18:40:04 -07001847void OpenGLRenderer::setupDrawWithExternalTexture() {
1848 mDescription.hasExternalTexture = true;
1849}
1850
Romain Guy15bc6432011-12-13 13:11:32 -08001851void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001852 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001853}
1854
Chris Craik710f46d2012-09-17 17:25:49 -07001855void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001856 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001857}
1858
Romain Guy8d0d4782010-12-14 20:13:35 -08001859void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1860 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001861 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1862 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1863 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001864 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001865 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001866}
1867
Romain Guy86568192010-12-14 15:55:39 -08001868void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1869 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001870 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1871 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1872 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001873 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001874 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001875}
1876
Romain Guy41210632012-07-16 17:04:24 -07001877void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1878 mCaches.fontRenderer->describe(mDescription, paint);
1879}
1880
Romain Guy70ca14e2010-12-13 18:24:33 -08001881void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1882 mColorA = a;
1883 mColorR = r;
1884 mColorG = g;
1885 mColorB = b;
1886 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001887 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001888}
1889
1890void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001891 if (mDrawModifiers.mShader) {
1892 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001893 }
1894}
1895
1896void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001897 if (mDrawModifiers.mColorFilter) {
1898 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001899 }
1900}
1901
Romain Guyf09ef512011-05-27 11:43:46 -07001902void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1903 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1904 mColorA = 1.0f;
1905 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001906 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001907 }
1908}
1909
Romain Guy70ca14e2010-12-13 18:24:33 -08001910void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001911 // When the blending mode is kClear_Mode, we need to use a modulate color
1912 // argb=1,0,0,0
1913 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001914 bool blend = (mColorSet && mColorA < 1.0f) ||
1915 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1916 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001917}
1918
1919void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001920 // When the blending mode is kClear_Mode, we need to use a modulate color
1921 // argb=1,0,0,0
1922 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001923 blend |= (mColorSet && mColorA < 1.0f) ||
1924 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1925 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1926 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001927}
1928
1929void OpenGLRenderer::setupDrawProgram() {
1930 useProgram(mCaches.programCache.get(mDescription));
1931}
1932
1933void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1934 mTrackDirtyRegions = false;
1935}
1936
Chris Craik4063a0e2013-11-15 16:06:56 -08001937void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1938 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001939 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001940 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001941 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001942 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001943
Romain Guy86568192010-12-14 15:55:39 -08001944 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1945 if (!ignoreTransform) {
Chris Craikf57776b2013-10-25 18:30:17 -07001946 mCaches.currentProgram->set(mViewProjMatrix, mModelView, currentTransform(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001947 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001948 } else {
Chris Craikf57776b2013-10-25 18:30:17 -07001949 mCaches.currentProgram->set(mViewProjMatrix, mModelView, mat4::identity(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001950 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001951 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001952}
1953
1954void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001955 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001956 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1957 }
1958}
1959
Romain Guy86568192010-12-14 15:55:39 -08001960void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001961 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001962 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001963 }
1964}
1965
Romain Guy70ca14e2010-12-13 18:24:33 -08001966void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001967 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001968 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001969 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1970 // because it was built into modelView / the geometry, and the SkiaShader needs to
1971 // compensate.
1972 mat4 modelViewWithoutTransform;
1973 modelViewWithoutTransform.loadInverse(currentTransform());
1974 modelViewWithoutTransform.multiply(mModelView);
1975 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001976 }
Chris Craikc3566d02013-02-04 16:16:33 -08001977 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1978 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001979 }
1980}
1981
1982void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001983 if (mDrawModifiers.mColorFilter) {
1984 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001985 }
1986}
1987
Romain Guy41210632012-07-16 17:04:24 -07001988void OpenGLRenderer::setupDrawTextGammaUniforms() {
1989 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1990}
1991
Romain Guy70ca14e2010-12-13 18:24:33 -08001992void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001993 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001994 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001995 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001996}
1997
1998void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001999 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08002000 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08002001 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08002002}
2003
Romain Guyaa6c24c2011-04-28 18:40:04 -07002004void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
2005 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08002006 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08002007 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07002008}
2009
Romain Guy8f0095c2011-05-02 17:24:22 -07002010void OpenGLRenderer::setupDrawTextureTransform() {
2011 mDescription.hasTextureTransform = true;
2012}
2013
2014void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07002015 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
2016 GL_FALSE, &transform.data[0]);
2017}
2018
Romain Guy70ca14e2010-12-13 18:24:33 -08002019void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002020 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07002021 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002022 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08002023 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08002024 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08002025 }
Romain Guyd71dd362011-12-12 19:03:35 -08002026
Chris Craikcb4d6002012-09-25 12:00:29 -07002027 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002028 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002029 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08002030 }
2031
2032 mCaches.unbindIndicesBuffer();
2033}
2034
Romain Guyff316ec2013-02-13 18:39:43 -08002035void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
2036 bool force = mCaches.unbindMeshBuffer();
2037 GLsizei stride = sizeof(ColorTextureVertex);
2038
2039 mCaches.bindPositionVertexPointer(force, vertices, stride);
2040 if (mCaches.currentProgram->texCoords >= 0) {
2041 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
2042 }
2043 int slot = mCaches.currentProgram->getAttrib("colors");
2044 if (slot >= 0) {
2045 glEnableVertexAttribArray(slot);
2046 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2047 }
2048
2049 mCaches.unbindIndicesBuffer();
2050}
2051
Romain Guy3b748a42013-04-17 18:54:38 -07002052void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2053 bool force = false;
2054 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2055 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2056 // use the default VBO found in Caches
2057 if (!vertices || vbo) {
2058 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2059 } else {
2060 force = mCaches.unbindMeshBuffer();
2061 }
2062 mCaches.bindIndicesBuffer();
2063
Chris Craikcb4d6002012-09-25 12:00:29 -07002064 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002065 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002066 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002067 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002068}
2069
Romain Guy448455f2013-07-22 13:57:50 -07002070void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002071 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002072 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002073 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002074}
2075
Romain Guy70ca14e2010-12-13 18:24:33 -08002076///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002077// Drawing
2078///////////////////////////////////////////////////////////////////////////////
2079
Chris Craikff785832013-03-08 13:12:16 -08002080status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2081 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002082 status_t status;
Chris Craikf57776b2013-10-25 18:30:17 -07002083
Chris Craikba9b6132013-12-15 17:10:19 -08002084 if (mCaches.propertyDirtyViewport) {
2085 // force recalc of view/proj matrices
2086 setViewport(mWidth, mHeight);
2087 mCaches.propertyDirtyViewport = false;
2088 }
2089
Romain Guy0fe478e2010-11-08 12:08:41 -08002090 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2091 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002092 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07002093 // compute 3d ordering
2094 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07002095 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002096 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002097 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2098 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002099 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002100 }
2101
Chris Craik28ce94a2013-05-31 11:38:03 -07002102 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2103 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002104 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2105 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002106
2107 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002108 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002109
Chris Craikf57776b2013-10-25 18:30:17 -07002110 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002111 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002112
Romain Guy65549432012-03-26 16:45:05 -07002113 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002114}
2115
Romain Guya168d732011-03-18 16:50:13 -07002116void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2117 int alpha;
2118 SkXfermode::Mode mode;
2119 getAlphaAndMode(paint, &alpha, &mode);
2120
Romain Guy886b2752013-01-04 12:26:18 -08002121 int color = paint != NULL ? paint->getColor() : 0;
2122
Romain Guya168d732011-03-18 16:50:13 -07002123 float x = left;
2124 float y = top;
2125
Romain Guy886b2752013-01-04 12:26:18 -08002126 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2127
Romain Guya168d732011-03-18 16:50:13 -07002128 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002129 if (currentTransform().isPureTranslate()) {
2130 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2131 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002132 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002133
2134 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002135 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002136 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002137 }
2138
Romain Guy3b748a42013-04-17 18:54:38 -07002139 // No need to check for a UV mapper on the texture object, only ARGB_8888
2140 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002141 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002142 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2143 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002144}
2145
Romain Guy03c00b52013-06-20 18:30:28 -07002146/**
2147 * Important note: this method is intended to draw batches of bitmaps and
2148 * will not set the scissor enable or dirty the current layer, if any.
2149 * The caller is responsible for properly dirtying the current layer.
2150 */
Romain Guy55b6f952013-06-27 15:27:09 -07002151status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002152 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002153 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002154 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002155 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002156
Chris Craik527a3aa2013-03-04 10:19:31 -08002157 const AutoTexture autoCleanup(texture);
2158
2159 int alpha;
2160 SkXfermode::Mode mode;
2161 getAlphaAndMode(paint, &alpha, &mode);
2162
2163 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002164 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002165
2166 const float x = (int) floorf(bounds.left + 0.5f);
2167 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002168 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002169 int color = paint != NULL ? paint->getColor() : 0;
2170 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2171 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002172 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002173 GL_TRIANGLES, bitmapCount * 6, true,
2174 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002175 } else {
2176 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2177 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002178 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002179 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2180 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002181 }
2182
2183 return DrawGlInfo::kStatusDrew;
2184}
2185
Chet Haase48659092012-05-31 15:21:51 -07002186status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002187 const float right = left + bitmap->width();
2188 const float bottom = top + bitmap->height();
2189
Chris Craikf0a59072013-11-19 18:00:46 -08002190 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002191 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002192 }
2193
Romain Guya1d3c912011-12-13 14:55:06 -08002194 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002195 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002196 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002197 const AutoTexture autoCleanup(texture);
2198
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002199 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002200 drawAlphaBitmap(texture, left, top, paint);
2201 } else {
2202 drawTextureRect(left, top, right, bottom, texture, paint);
2203 }
Chet Haase48659092012-05-31 15:21:51 -07002204
2205 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002206}
2207
Chet Haase48659092012-05-31 15:21:51 -07002208status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002209 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2210 const mat4 transform(*matrix);
2211 transform.mapRect(r);
2212
Chris Craikf0a59072013-11-19 18:00:46 -08002213 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002214 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002215 }
2216
Romain Guya1d3c912011-12-13 14:55:06 -08002217 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002218 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002219 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002220 const AutoTexture autoCleanup(texture);
2221
Romain Guy5b3b3522010-10-27 18:57:51 -07002222 // This could be done in a cheaper way, all we need is pass the matrix
2223 // to the vertex shader. The save/restore is a bit overkill.
2224 save(SkCanvas::kMatrix_SaveFlag);
2225 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002226 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002227 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2228 } else {
2229 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2230 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002231 restore();
Chet Haase48659092012-05-31 15:21:51 -07002232
2233 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002234}
2235
Chet Haase48659092012-05-31 15:21:51 -07002236status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002237 const float right = left + bitmap->width();
2238 const float bottom = top + bitmap->height();
2239
Chris Craikf0a59072013-11-19 18:00:46 -08002240 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002241 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002242 }
2243
2244 mCaches.activeTexture(0);
2245 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2246 const AutoTexture autoCleanup(texture);
2247
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002248 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002249 drawAlphaBitmap(texture, left, top, paint);
2250 } else {
2251 drawTextureRect(left, top, right, bottom, texture, paint);
2252 }
Chet Haase48659092012-05-31 15:21:51 -07002253
2254 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002255}
2256
Chet Haase48659092012-05-31 15:21:51 -07002257status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002258 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002259 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002260 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002261 }
2262
Chris Craik39a908c2013-06-13 14:39:01 -07002263 // TODO: use quickReject on bounds from vertices
2264 mCaches.enableScissor();
2265
Romain Guyb18d2d02011-02-10 15:52:54 -08002266 float left = FLT_MAX;
2267 float top = FLT_MAX;
2268 float right = FLT_MIN;
2269 float bottom = FLT_MIN;
2270
Romain Guya92bb4d2012-10-16 11:08:44 -07002271 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002272
Romain Guyff316ec2013-02-13 18:39:43 -08002273 ColorTextureVertex mesh[count];
2274 ColorTextureVertex* vertex = mesh;
2275
2276 bool cleanupColors = false;
2277 if (!colors) {
2278 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2279 colors = new int[colorsCount];
2280 memset(colors, 0xff, colorsCount * sizeof(int));
2281 cleanupColors = true;
2282 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002283
Romain Guy3b748a42013-04-17 18:54:38 -07002284 mCaches.activeTexture(0);
2285 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2286 const UvMapper& mapper(getMapper(texture));
2287
Romain Guy5a7b4662011-01-20 19:09:30 -08002288 for (int32_t y = 0; y < meshHeight; y++) {
2289 for (int32_t x = 0; x < meshWidth; x++) {
2290 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2291
2292 float u1 = float(x) / meshWidth;
2293 float u2 = float(x + 1) / meshWidth;
2294 float v1 = float(y) / meshHeight;
2295 float v2 = float(y + 1) / meshHeight;
2296
Romain Guy3b748a42013-04-17 18:54:38 -07002297 mapper.map(u1, v1, u2, v2);
2298
Romain Guy5a7b4662011-01-20 19:09:30 -08002299 int ax = i + (meshWidth + 1) * 2;
2300 int ay = ax + 1;
2301 int bx = i;
2302 int by = bx + 1;
2303 int cx = i + 2;
2304 int cy = cx + 1;
2305 int dx = i + (meshWidth + 1) * 2 + 2;
2306 int dy = dx + 1;
2307
Romain Guyff316ec2013-02-13 18:39:43 -08002308 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2309 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2310 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002311
Romain Guyff316ec2013-02-13 18:39:43 -08002312 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2313 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2314 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002315
Romain Guya92bb4d2012-10-16 11:08:44 -07002316 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2317 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2318 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2319 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002320 }
2321 }
2322
Chris Craikf0a59072013-11-19 18:00:46 -08002323 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002324 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002325 return DrawGlInfo::kStatusDone;
2326 }
2327
Romain Guyff316ec2013-02-13 18:39:43 -08002328 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002329 texture = mCaches.textureCache.get(bitmap);
2330 if (!texture) {
2331 if (cleanupColors) delete[] colors;
2332 return DrawGlInfo::kStatusDone;
2333 }
Romain Guyff316ec2013-02-13 18:39:43 -08002334 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002335 const AutoTexture autoCleanup(texture);
2336
2337 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2338 texture->setFilter(FILTER(paint), true);
2339
2340 int alpha;
2341 SkXfermode::Mode mode;
2342 getAlphaAndMode(paint, &alpha, &mode);
2343
Romain Guyff316ec2013-02-13 18:39:43 -08002344 float a = alpha / 255.0f;
2345
Romain Guya92bb4d2012-10-16 11:08:44 -07002346 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002347 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002348 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002349
Romain Guyff316ec2013-02-13 18:39:43 -08002350 setupDraw();
2351 setupDrawWithTextureAndColor();
2352 setupDrawColor(a, a, a, a);
2353 setupDrawColorFilter();
2354 setupDrawBlending(true, mode, false);
2355 setupDrawProgram();
2356 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002357 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002358 setupDrawTexture(texture->id);
2359 setupDrawPureColorUniforms();
2360 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002361 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002362
2363 glDrawArrays(GL_TRIANGLES, 0, count);
2364
Romain Guyff316ec2013-02-13 18:39:43 -08002365 int slot = mCaches.currentProgram->getAttrib("colors");
2366 if (slot >= 0) {
2367 glDisableVertexAttribArray(slot);
2368 }
2369
2370 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002371
2372 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002373}
2374
Chet Haase48659092012-05-31 15:21:51 -07002375status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002376 float srcLeft, float srcTop, float srcRight, float srcBottom,
2377 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002378 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002379 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002380 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002381 }
2382
Romain Guya1d3c912011-12-13 14:55:06 -08002383 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002384 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002385 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002386 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002387
Romain Guy8ba548f2010-06-30 19:21:21 -07002388 const float width = texture->width;
2389 const float height = texture->height;
2390
Romain Guy3b748a42013-04-17 18:54:38 -07002391 float u1 = fmax(0.0f, srcLeft / width);
2392 float v1 = fmax(0.0f, srcTop / height);
2393 float u2 = fmin(1.0f, srcRight / width);
2394 float v2 = fmin(1.0f, srcBottom / height);
2395
2396 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002397
Romain Guy03750a02010-10-18 14:06:08 -07002398 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002399 resetDrawTextureTexCoords(u1, v1, u2, v2);
2400
Romain Guy03750a02010-10-18 14:06:08 -07002401 int alpha;
2402 SkXfermode::Mode mode;
2403 getAlphaAndMode(paint, &alpha, &mode);
2404
Romain Guyd21b6e12011-11-30 20:21:23 -08002405 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2406
Romain Guy886b2752013-01-04 12:26:18 -08002407 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2408 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002409
Romain Guy886b2752013-01-04 12:26:18 -08002410 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2411 // Apply a scale transform on the canvas only when a shader is in use
2412 // Skia handles the ratio between the dst and src rects as a scale factor
2413 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002414 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002415 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002416
Romain Guy3b753822013-03-05 10:27:35 -08002417 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2418 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2419 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002420
2421 dstRight = x + (dstRight - dstLeft);
2422 dstBottom = y + (dstBottom - dstTop);
2423
2424 dstLeft = x;
2425 dstTop = y;
2426
2427 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2428 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002429 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002430 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002431 }
2432
2433 if (CC_UNLIKELY(useScaleTransform)) {
2434 save(SkCanvas::kMatrix_SaveFlag);
2435 translate(dstLeft, dstTop);
2436 scale(scaleX, scaleY);
2437
2438 dstLeft = 0.0f;
2439 dstTop = 0.0f;
2440
2441 dstRight = srcRight - srcLeft;
2442 dstBottom = srcBottom - srcTop;
2443 }
2444
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002445 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002446 int color = paint ? paint->getColor() : 0;
2447 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2448 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002449 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002450 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2451 } else {
2452 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2453 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002454 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002455 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2456 }
2457
2458 if (CC_UNLIKELY(useScaleTransform)) {
2459 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002460 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002461
2462 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002463
2464 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002465}
2466
Romain Guy3b748a42013-04-17 18:54:38 -07002467status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002468 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002469 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002470 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002471 }
2472
Romain Guy4c2547f2013-06-11 16:19:24 -07002473 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002474 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2475 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002476
Romain Guy03c00b52013-06-20 18:30:28 -07002477 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002478}
2479
Romain Guy03c00b52013-06-20 18:30:28 -07002480status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2481 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002482 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002483 return DrawGlInfo::kStatusDone;
2484 }
2485
Romain Guy211370f2012-02-01 16:10:55 -08002486 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002487 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002488 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002489 if (!texture) return DrawGlInfo::kStatusDone;
2490 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002491
Romain Guya4adcf02013-02-28 12:15:35 -08002492 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2493 texture->setFilter(GL_LINEAR, true);
2494
Romain Guy03c00b52013-06-20 18:30:28 -07002495 int alpha;
2496 SkXfermode::Mode mode;
2497 getAlphaAndMode(paint, &alpha, &mode);
2498
Romain Guy3b753822013-03-05 10:27:35 -08002499 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002500 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002501 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002502 const float offsetX = left + currentTransform().getTranslateX();
2503 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002504 const size_t count = mesh->quads.size();
2505 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002506 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002507 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002508 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2509 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2510 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002511 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002512 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002513 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002514 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002515 }
2516 }
2517
Chris Craik4063a0e2013-11-15 16:06:56 -08002518 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002519 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002520 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2521 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002522
Romain Guy3b748a42013-04-17 18:54:38 -07002523 right = x + right - left;
2524 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002525 left = x;
2526 top = y;
2527 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002528 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002529 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2530 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2531 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2532 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002533 }
Chet Haase48659092012-05-31 15:21:51 -07002534
2535 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002536}
2537
Romain Guy03c00b52013-06-20 18:30:28 -07002538/**
2539 * Important note: this method is intended to draw batches of 9-patch objects and
2540 * will not set the scissor enable or dirty the current layer, if any.
2541 * The caller is responsible for properly dirtying the current layer.
2542 */
2543status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2544 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2545 mCaches.activeTexture(0);
2546 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2547 if (!texture) return DrawGlInfo::kStatusDone;
2548 const AutoTexture autoCleanup(texture);
2549
2550 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2551 texture->setFilter(GL_LINEAR, true);
2552
2553 int alpha;
2554 SkXfermode::Mode mode;
2555 getAlphaAndMode(paint, &alpha, &mode);
2556
2557 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002558 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002559 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002560
2561 return DrawGlInfo::kStatusDrew;
2562}
2563
Chris Craik65cd6122012-12-10 17:56:27 -08002564status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2565 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002566 // not missing call to quickReject/dirtyLayer, always done at a higher level
2567
Chris Craik6d29c8d2013-05-08 18:35:44 -07002568 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002569 // no vertices to draw
2570 return DrawGlInfo::kStatusDone;
2571 }
2572
Chris Craikcb4d6002012-09-25 12:00:29 -07002573 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002574 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2575 bool isAA = paint->isAntiAlias();
2576
Chris Craik710f46d2012-09-17 17:25:49 -07002577 setupDraw();
2578 setupDrawNoTexture();
2579 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002580 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2581 setupDrawColorFilter();
2582 setupDrawShader();
2583 setupDrawBlending(isAA, mode);
2584 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002585 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002586 setupDrawColorUniforms();
2587 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002588 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002589
ztenghui55bfb4e2013-12-03 10:38:55 -08002590 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002591 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002592 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002593 mCaches.resetTexCoordsVertexPointer();
2594 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002595
Chris Craik710f46d2012-09-17 17:25:49 -07002596 int alphaSlot = -1;
2597 if (isAA) {
2598 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2599 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002600
Chris Craik710f46d2012-09-17 17:25:49 -07002601 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002602 glEnableVertexAttribArray(alphaSlot);
2603 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002604 }
Romain Guy04299382012-07-18 17:15:41 -07002605
Chris Craik6d29c8d2013-05-08 18:35:44 -07002606 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002607
Chris Craik710f46d2012-09-17 17:25:49 -07002608 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002609 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002610 }
Chris Craik65cd6122012-12-10 17:56:27 -08002611
2612 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002613}
2614
2615/**
Chris Craik65cd6122012-12-10 17:56:27 -08002616 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2617 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2618 * screen space in all directions. However, instead of using a fragment shader to compute the
2619 * translucency of the color from its position, we simply use a varying parameter to define how far
2620 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2621 *
2622 * Doesn't yet support joins, caps, or path effects.
2623 */
2624status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2625 VertexBuffer vertexBuffer;
2626 // TODO: try clipping large paths to viewport
2627 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2628
Romain Guyc46d07a2013-03-15 19:06:39 -07002629 if (hasLayer()) {
2630 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002631 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002632 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2633 }
Chris Craik65cd6122012-12-10 17:56:27 -08002634
2635 return drawVertexBuffer(vertexBuffer, paint);
2636}
2637
2638/**
2639 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2640 * and additional geometry for defining an alpha slope perimeter.
2641 *
2642 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2643 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2644 * in-shader alpha region, but found it to be taxing on some GPUs.
2645 *
2646 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2647 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002648 */
Chet Haase48659092012-05-31 15:21:51 -07002649status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002650 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002651
Chris Craik65cd6122012-12-10 17:56:27 -08002652 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002653
Chris Craik65cd6122012-12-10 17:56:27 -08002654 VertexBuffer buffer;
2655 SkRect bounds;
2656 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002657
Chris Craikf0a59072013-11-19 18:00:46 -08002658 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2659 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002660 return DrawGlInfo::kStatusDone;
2661 }
2662
Romain Guy3b753822013-03-05 10:27:35 -08002663 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002664
Chris Craik65cd6122012-12-10 17:56:27 -08002665 bool useOffset = !paint->isAntiAlias();
2666 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002667}
2668
Chet Haase48659092012-05-31 15:21:51 -07002669status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002670 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002671
Chris Craik6d29c8d2013-05-08 18:35:44 -07002672 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002673
Chris Craik6d29c8d2013-05-08 18:35:44 -07002674 VertexBuffer buffer;
2675 SkRect bounds;
2676 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002677
Chris Craikf0a59072013-11-19 18:00:46 -08002678 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2679 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002680 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002681 }
2682
Chris Craik6d29c8d2013-05-08 18:35:44 -07002683 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002684
Chris Craik6d29c8d2013-05-08 18:35:44 -07002685 bool useOffset = !paint->isAntiAlias();
2686 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002687}
2688
Chet Haase48659092012-05-31 15:21:51 -07002689status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002690 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002691 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002692
Romain Guyae88e5e2010-10-22 17:49:18 -07002693 Rect& clip(*mSnapshot->clipRect);
2694 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002695
Romain Guy3d58c032010-07-14 16:34:53 -07002696 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002697
2698 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002699}
Romain Guy9d5316e2010-06-24 19:30:36 -07002700
Chet Haase48659092012-05-31 15:21:51 -07002701status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2702 SkPaint* paint) {
2703 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002704 const AutoTexture autoCleanup(texture);
2705
2706 const float x = left + texture->left - texture->offset;
2707 const float y = top + texture->top - texture->offset;
2708
2709 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002710
2711 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002712}
2713
Chet Haase48659092012-05-31 15:21:51 -07002714status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002715 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002716 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002717 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002718 return DrawGlInfo::kStatusDone;
2719 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002720
Chris Craikcb4d6002012-09-25 12:00:29 -07002721 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002722 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002723 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002724 right - left, bottom - top, rx, ry, p);
2725 return drawShape(left, top, texture, p);
2726 }
2727
2728 SkPath path;
2729 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002730 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2731 float outset = p->getStrokeWidth() / 2;
2732 rect.outset(outset, outset);
2733 rx += outset;
2734 ry += outset;
2735 }
Chris Craik710f46d2012-09-17 17:25:49 -07002736 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002737 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002738}
2739
Chris Craik710f46d2012-09-17 17:25:49 -07002740status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002741 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002742 x + radius, y + radius, p) ||
2743 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002744 return DrawGlInfo::kStatusDone;
2745 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002746 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002747 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002748 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002749 return drawShape(x - radius, y - radius, texture, p);
2750 }
2751
2752 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002753 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2754 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2755 } else {
2756 path.addCircle(x, y, radius);
2757 }
Chris Craik65cd6122012-12-10 17:56:27 -08002758 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002759}
Romain Guy01d58e42011-01-19 21:54:02 -08002760
Chet Haase48659092012-05-31 15:21:51 -07002761status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002762 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002763 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002764 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002765 return DrawGlInfo::kStatusDone;
2766 }
Romain Guy01d58e42011-01-19 21:54:02 -08002767
Chris Craikcb4d6002012-09-25 12:00:29 -07002768 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002769 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002770 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002771 return drawShape(left, top, texture, p);
2772 }
2773
2774 SkPath path;
2775 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002776 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2777 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2778 }
Chris Craik710f46d2012-09-17 17:25:49 -07002779 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002780 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002781}
2782
Chet Haase48659092012-05-31 15:21:51 -07002783status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002784 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002785 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002786 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002787 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002788 }
2789
Chris Craik780c1282012-10-04 14:10:49 -07002790 if (fabs(sweepAngle) >= 360.0f) {
2791 return drawOval(left, top, right, bottom, p);
2792 }
2793
2794 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002795 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002796 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002797 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002798 startAngle, sweepAngle, useCenter, p);
2799 return drawShape(left, top, texture, p);
2800 }
2801
2802 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2803 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2804 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2805 }
2806
2807 SkPath path;
2808 if (useCenter) {
2809 path.moveTo(rect.centerX(), rect.centerY());
2810 }
2811 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2812 if (useCenter) {
2813 path.close();
2814 }
Chris Craik65cd6122012-12-10 17:56:27 -08002815 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002816}
2817
Romain Guycf8675e2012-10-02 12:32:25 -07002818// See SkPaintDefaults.h
2819#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2820
Chet Haase48659092012-05-31 15:21:51 -07002821status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002822 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002823 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002824 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002825 }
2826
Chris Craik710f46d2012-09-17 17:25:49 -07002827 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002828 // only fill style is supported by drawConvexPath, since others have to handle joins
2829 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2830 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2831 mCaches.activeTexture(0);
2832 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002833 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002834 return drawShape(left, top, texture, p);
2835 }
2836
2837 SkPath path;
2838 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2839 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2840 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2841 }
2842 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002843 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002844 }
2845
Romain Guy3b753822013-03-05 10:27:35 -08002846 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002847 SkPath path;
2848 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002849 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002850 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002851 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002852 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002853 }
Romain Guyc7d53492010-06-25 13:41:57 -07002854}
Romain Guy9d5316e2010-06-24 19:30:36 -07002855
Raph Levien416a8472012-07-19 22:48:17 -07002856void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2857 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2858 float x, float y) {
2859 mCaches.activeTexture(0);
2860
2861 // NOTE: The drop shadow will not perform gamma correction
2862 // if shader-based correction is enabled
2863 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2864 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002865 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002866 // If the drop shadow exceeds the max texture size or couldn't be
2867 // allocated, skip drawing
2868 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002869 const AutoTexture autoCleanup(shadow);
2870
Chris Craikc3566d02013-02-04 16:16:33 -08002871 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2872 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002873
Chris Craikc3566d02013-02-04 16:16:33 -08002874 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2875 int shadowColor = mDrawModifiers.mShadowColor;
2876 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002877 shadowColor = 0xffffffff;
2878 }
2879
2880 setupDraw();
2881 setupDrawWithTexture(true);
2882 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2883 setupDrawColorFilter();
2884 setupDrawShader();
2885 setupDrawBlending(true, mode);
2886 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002887 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2888 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002889 setupDrawTexture(shadow->id);
2890 setupDrawPureColorUniforms();
2891 setupDrawColorFilterUniforms();
2892 setupDrawShaderUniforms();
2893 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2894
2895 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2896}
2897
Romain Guy768bffc2013-02-27 13:50:45 -08002898bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2899 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2900 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2901}
2902
Chet Haase48659092012-05-31 15:21:51 -07002903status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002904 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002905 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002906 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002907 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002908
Romain Guy671d6cf2012-01-18 12:39:17 -08002909 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002910 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002911 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002912 }
2913
Chris Craik39a908c2013-06-13 14:39:01 -07002914 mCaches.enableScissor();
2915
Romain Guy671d6cf2012-01-18 12:39:17 -08002916 float x = 0.0f;
2917 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002918 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002919 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002920 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2921 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002922 }
2923
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002924 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002925 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002926
2927 int alpha;
2928 SkXfermode::Mode mode;
2929 getAlphaAndMode(paint, &alpha, &mode);
2930
Chris Craikc3566d02013-02-04 16:16:33 -08002931 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002932 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2933 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002934 }
2935
Romain Guy671d6cf2012-01-18 12:39:17 -08002936 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002937 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002938 if (pureTranslate && !linearFilter) {
2939 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2940 }
Romain Guy257ae352013-03-20 16:31:12 -07002941 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002942
2943 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2944 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2945
Romain Guy211370f2012-02-01 16:10:55 -08002946 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002947
Victoria Lease1e546812013-06-25 14:25:17 -07002948 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002949 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002950 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002951 if (hasActiveLayer) {
2952 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002953 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002954 }
2955 dirtyLayerUnchecked(bounds, getRegion());
2956 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002957 }
Chet Haase48659092012-05-31 15:21:51 -07002958
2959 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002960}
2961
Romain Guy624234f2013-03-05 16:43:31 -08002962mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2963 mat4 fontTransform;
2964 if (CC_LIKELY(transform.isPureTranslate())) {
2965 fontTransform = mat4::identity();
2966 } else {
2967 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002968 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002969 } else {
2970 float sx, sy;
2971 currentTransform().decomposeScale(sx, sy);
2972 fontTransform.loadScale(sx, sy, 1.0f);
2973 }
2974 }
2975 return fontTransform;
2976}
2977
Chris Craik41541822013-05-03 16:35:54 -07002978status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2979 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002980 DrawOpMode drawOpMode) {
2981
Chris Craik527a3aa2013-03-04 10:19:31 -08002982 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002983 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2984 // drawing as ops from DeferredDisplayList are already filtered for these
2985 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002986 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002987 return DrawGlInfo::kStatusDone;
2988 }
Romain Guycac5fd32011-12-01 20:08:50 -08002989 }
2990
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002991 const float oldX = x;
2992 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002993
2994 const mat4& transform = currentTransform();
2995 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002996
Romain Guy211370f2012-02-01 16:10:55 -08002997 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002998 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2999 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003000 }
3001
Romain Guy86568192010-12-14 15:55:39 -08003002 int alpha;
3003 SkXfermode::Mode mode;
3004 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07003005
Romain Guyc74f45a2013-02-26 19:10:14 -08003006 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
3007
Chris Craikc3566d02013-02-04 16:16:33 -08003008 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08003009 fontRenderer.setFont(paint, mat4::identity());
3010 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
3011 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07003012 }
3013
Romain Guy19d4dd82013-03-04 11:14:26 -08003014 const bool hasActiveLayer = hasLayer();
3015
Romain Guy624234f2013-03-05 16:43:31 -08003016 // We only pass a partial transform to the font renderer. That partial
3017 // matrix defines how glyphs are rasterized. Typically we want glyphs
3018 // to be rasterized at their final size on screen, which means the partial
3019 // matrix needs to take the scale factor into account.
3020 // When a partial matrix is used to transform glyphs during rasterization,
3021 // the mesh is generated with the inverse transform (in the case of scale,
3022 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3023 // apply the full transform matrix at draw time in the vertex shader.
3024 // Applying the full matrix in the shader is the easiest way to handle
3025 // rotation and perspective and allows us to always generated quads in the
3026 // font renderer which greatly simplifies the code, clipping in particular.
3027 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003028 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003029
Romain Guy6620c6d2010-12-06 18:07:02 -08003030 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003031 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003032 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003033
Romain Guy624234f2013-03-05 16:43:31 -08003034 // TODO: Implement better clipping for scaled/rotated text
3035 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003036 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 -07003037
Raph Levien996e57c2012-07-23 15:22:52 -07003038 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003039 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003040
3041 // don't call issuedrawcommand, do it at end of batch
3042 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003043 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003044 SkPaint paintCopy(*paint);
3045 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3046 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003047 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003048 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003049 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003050 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003051 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003052
Chris Craik527a3aa2013-03-04 10:19:31 -08003053 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003054 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003055 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003056 }
Chris Craik41541822013-05-03 16:35:54 -07003057 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003058 }
Romain Guy694b5192010-07-21 21:33:20 -07003059
Chris Craike63f7c622013-10-17 10:30:55 -07003060 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003061
3062 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003063}
3064
Chet Haase48659092012-05-31 15:21:51 -07003065status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003066 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003067 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003068 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003069 }
3070
Chris Craik39a908c2013-06-13 14:39:01 -07003071 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3072 mCaches.enableScissor();
3073
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003074 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003075 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003076 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003077
3078 int alpha;
3079 SkXfermode::Mode mode;
3080 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003081 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003082
Romain Guy97771732012-02-28 18:17:02 -08003083 const Rect* clip = &mSnapshot->getLocalClip();
3084 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 -08003085
Romain Guy97771732012-02-28 18:17:02 -08003086 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003087
3088 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003089 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003090 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003091 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003092 dirtyLayerUnchecked(bounds, getRegion());
3093 }
Romain Guy97771732012-02-28 18:17:02 -08003094 }
Chet Haase48659092012-05-31 15:21:51 -07003095
3096 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003097}
3098
Chet Haase48659092012-05-31 15:21:51 -07003099status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3100 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003101
Romain Guya1d3c912011-12-13 14:55:06 -08003102 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003103
Romain Guyfb8b7632010-08-23 21:05:08 -07003104 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003105 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003106 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003107
Romain Guy8b55f372010-08-18 17:10:07 -07003108 const float x = texture->left - texture->offset;
3109 const float y = texture->top - texture->offset;
3110
Romain Guy01d58e42011-01-19 21:54:02 -08003111 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003112
3113 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003114}
3115
Chris Craika08f95c2013-03-15 17:24:33 -07003116status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003117 if (!layer) {
3118 return DrawGlInfo::kStatusDone;
3119 }
3120
Romain Guyb2e2f242012-10-17 18:18:35 -07003121 mat4* transform = NULL;
3122 if (layer->isTextureLayer()) {
3123 transform = &layer->getTransform();
3124 if (!transform->isIdentity()) {
3125 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003126 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003127 }
3128 }
3129
Chris Craik39a908c2013-06-13 14:39:01 -07003130 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08003131 const bool rejected = calculateQuickRejectForScissor(x, y,
3132 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003133
3134 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003135 if (transform && !transform->isIdentity()) {
3136 restore();
3137 }
Chet Haase48659092012-05-31 15:21:51 -07003138 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003139 }
3140
Romain Guy5bb3c732012-11-29 17:52:58 -08003141 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003142
Chris Craik39a908c2013-06-13 14:39:01 -07003143 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003144 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003145
Romain Guy211370f2012-02-01 16:10:55 -08003146 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003147 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3148 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003149
Romain Guyc88e3572011-01-22 00:32:12 -08003150 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003151 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3152 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003153 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003154 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003155 setupDraw();
3156 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003157 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003158 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003159 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003160 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003161 setupDrawPureColorUniforms();
3162 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003163 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003164 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3165 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3166 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003167
Romain Guyd21b6e12011-11-30 20:21:23 -08003168 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003169 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003170 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003171 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003172 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003173 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003174 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3175 }
Romain Guyf219da52011-01-16 12:54:25 -08003176
Romain Guy448455f2013-07-22 13:57:50 -07003177 TextureVertex* mesh = &layer->mesh[0];
3178 GLsizei elementsCount = layer->meshElementCount;
3179
3180 while (elementsCount > 0) {
3181 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3182
Romain Guy3380cfd2013-08-15 16:57:57 -07003183 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003184 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3185 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3186
3187 elementsCount -= drawCount;
3188 // Though there are 4 vertices in a quad, we use 6 indices per
3189 // quad to draw with GL_TRIANGLES
3190 mesh += (drawCount / 6) * 4;
3191 }
Romain Guyf219da52011-01-16 12:54:25 -08003192
Romain Guy3a3133d2011-02-01 22:59:58 -08003193#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003194 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003195#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003196 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003197
Chris Craikc3566d02013-02-04 16:16:33 -08003198 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003199
Romain Guy5bb3c732012-11-29 17:52:58 -08003200 if (layer->debugDrawUpdate) {
3201 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003202 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3203 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3204 }
Romain Guyf219da52011-01-16 12:54:25 -08003205 }
Chris Craik34416ea2013-04-15 16:08:28 -07003206 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003207
Romain Guyb2e2f242012-10-17 18:18:35 -07003208 if (transform && !transform->isIdentity()) {
3209 restore();
3210 }
3211
Chet Haase48659092012-05-31 15:21:51 -07003212 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003213}
3214
Romain Guy6926c722010-07-12 20:20:03 -07003215///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003216// Shaders
3217///////////////////////////////////////////////////////////////////////////////
3218
3219void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003220 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003221}
3222
Romain Guy06f96e22010-07-30 19:18:16 -07003223void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003224 mDrawModifiers.mShader = shader;
3225 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003226 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003227 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003228}
3229
Romain Guyd27977d2010-07-14 19:18:51 -07003230///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003231// Color filters
3232///////////////////////////////////////////////////////////////////////////////
3233
3234void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003235 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003236}
3237
3238void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003239 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003240}
3241
3242///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003243// Drop shadow
3244///////////////////////////////////////////////////////////////////////////////
3245
3246void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003247 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003248}
3249
3250void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003251 mDrawModifiers.mHasShadow = true;
3252 mDrawModifiers.mShadowRadius = radius;
3253 mDrawModifiers.mShadowDx = dx;
3254 mDrawModifiers.mShadowDy = dy;
3255 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003256}
3257
3258///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003259// Draw filters
3260///////////////////////////////////////////////////////////////////////////////
3261
3262void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003263 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3264 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003265 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003266 mDrawModifiers.mPaintFilterClearBits = 0;
3267 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003268}
3269
3270void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003271 mDrawModifiers.mHasDrawFilter = true;
3272 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3273 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003274}
3275
Chris Craika08f95c2013-03-15 17:24:33 -07003276SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003277 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003278 return paint;
3279 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003280
3281 uint32_t flags = paint->getFlags();
3282
3283 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003284 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3285 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003286
3287 return &mFilteredPaint;
3288}
3289
3290///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003291// Drawing implementation
3292///////////////////////////////////////////////////////////////////////////////
3293
Romain Guy3b748a42013-04-17 18:54:38 -07003294Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3295 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3296 if (!texture) {
3297 return mCaches.textureCache.get(bitmap);
3298 }
3299 return texture;
3300}
3301
Romain Guy01d58e42011-01-19 21:54:02 -08003302void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3303 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003304 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003305 return;
3306 }
3307
3308 int alpha;
3309 SkXfermode::Mode mode;
3310 getAlphaAndMode(paint, &alpha, &mode);
3311
3312 setupDraw();
3313 setupDrawWithTexture(true);
3314 setupDrawAlpha8Color(paint->getColor(), alpha);
3315 setupDrawColorFilter();
3316 setupDrawShader();
3317 setupDrawBlending(true, mode);
3318 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003319 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3320 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003321 setupDrawTexture(texture->id);
3322 setupDrawPureColorUniforms();
3323 setupDrawColorFilterUniforms();
3324 setupDrawShaderUniforms();
3325 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3326
3327 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003328}
3329
Romain Guyf607bdc2010-09-10 19:20:06 -07003330// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003331#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3332#define kStdUnderline_Offset (1.0f / 9.0f)
3333#define kStdUnderline_Thickness (1.0f / 18.0f)
3334
Chris Craike63f7c622013-10-17 10:30:55 -07003335void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003336 // Handle underline and strike-through
3337 uint32_t flags = paint->getFlags();
3338 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003339 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003340
Romain Guy211370f2012-02-01 16:10:55 -08003341 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003342 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003343 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003344
Raph Levien8b4072d2012-07-30 15:50:00 -07003345 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003346 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003347
Romain Guyf6834472011-01-23 13:32:12 -08003348 int linesCount = 0;
3349 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3350 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3351
3352 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003353 float points[pointsCount];
3354 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003355
3356 if (flags & SkPaint::kUnderlineText_Flag) {
3357 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003358 points[currentPoint++] = left;
3359 points[currentPoint++] = top;
3360 points[currentPoint++] = left + underlineWidth;
3361 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003362 }
3363
3364 if (flags & SkPaint::kStrikeThruText_Flag) {
3365 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003366 points[currentPoint++] = left;
3367 points[currentPoint++] = top;
3368 points[currentPoint++] = left + underlineWidth;
3369 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003370 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003371
Romain Guy726aeba2011-06-01 14:52:00 -07003372 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003373
Romain Guy726aeba2011-06-01 14:52:00 -07003374 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003375 }
3376 }
3377}
3378
Romain Guy672433d2013-01-04 19:05:13 -08003379status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3380 if (mSnapshot->isIgnored()) {
3381 return DrawGlInfo::kStatusDone;
3382 }
3383
Romain Guy735738c2012-12-03 12:34:51 -08003384 int color = paint->getColor();
3385 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003386 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003387 color |= 0x00ffffff;
3388 }
3389 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3390
3391 return drawColorRects(rects, count, color, mode);
3392}
3393
Chris Craikf57776b2013-10-25 18:30:17 -07003394status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3395 float width, float height) {
3396 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3397
3398 // For now, always and scissor
3399 // TODO: use quickReject
3400 mCaches.enableScissor();
3401
3402 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003403 paint.setColor(mCaches.propertyShadowStrength << 24);
3404 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003405
ztenghui55bfb4e2013-12-03 10:38:55 -08003406 VertexBuffer shadowVertexBuffer;
3407 ShadowTessellator::tessellateAmbientShadow(width, height, casterTransform,
3408 shadowVertexBuffer);
3409 return drawVertexBuffer(shadowVertexBuffer, &paint);
Chris Craikf57776b2013-10-25 18:30:17 -07003410}
3411
Romain Guy735738c2012-12-03 12:34:51 -08003412status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003413 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003414 if (count == 0) {
3415 return DrawGlInfo::kStatusDone;
3416 }
Romain Guy735738c2012-12-03 12:34:51 -08003417
Romain Guy672433d2013-01-04 19:05:13 -08003418 float left = FLT_MAX;
3419 float top = FLT_MAX;
3420 float right = FLT_MIN;
3421 float bottom = FLT_MIN;
3422
Romain Guy448455f2013-07-22 13:57:50 -07003423 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003424 Vertex* vertex = mesh;
3425
Chris Craik2af46352012-11-26 18:30:17 -08003426 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003427 float l = rects[index + 0];
3428 float t = rects[index + 1];
3429 float r = rects[index + 2];
3430 float b = rects[index + 3];
3431
Romain Guy3b753822013-03-05 10:27:35 -08003432 Vertex::set(vertex++, l, t);
3433 Vertex::set(vertex++, r, t);
3434 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003435 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003436
Romain Guy3b753822013-03-05 10:27:35 -08003437 left = fminf(left, l);
3438 top = fminf(top, t);
3439 right = fmaxf(right, r);
3440 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003441 }
3442
Chris Craikf0a59072013-11-19 18:00:46 -08003443 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003444 return DrawGlInfo::kStatusDone;
3445 }
Romain Guy672433d2013-01-04 19:05:13 -08003446
Romain Guy672433d2013-01-04 19:05:13 -08003447 setupDraw();
3448 setupDrawNoTexture();
3449 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3450 setupDrawShader();
3451 setupDrawColorFilter();
3452 setupDrawBlending(mode);
3453 setupDrawProgram();
3454 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003455 setupDrawModelView(kModelViewMode_Translate, false,
3456 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003457 setupDrawColorUniforms();
3458 setupDrawShaderUniforms();
3459 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003460
Romain Guy8ce00302013-01-15 18:51:42 -08003461 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003462 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003463 }
3464
Chris Craik4063a0e2013-11-15 16:06:56 -08003465 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003466
3467 return DrawGlInfo::kStatusDrew;
3468}
3469
Romain Guy026c5e162010-06-28 17:12:22 -07003470void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003471 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003472 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003473 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003474 color |= 0x00ffffff;
3475 }
3476
Romain Guy70ca14e2010-12-13 18:24:33 -08003477 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003478 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003479 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003480 setupDrawShader();
3481 setupDrawColorFilter();
3482 setupDrawBlending(mode);
3483 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003484 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3485 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003486 setupDrawColorUniforms();
3487 setupDrawShaderUniforms(ignoreTransform);
3488 setupDrawColorFilterUniforms();
3489 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003490
Romain Guyc95c8d62010-09-17 15:31:32 -07003491 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3492}
3493
Romain Guy82ba8142010-07-09 13:25:56 -07003494void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003495 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003496 int alpha;
3497 SkXfermode::Mode mode;
3498 getAlphaAndMode(paint, &alpha, &mode);
3499
Romain Guyd21b6e12011-11-30 20:21:23 -08003500 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003501
Romain Guy3b748a42013-04-17 18:54:38 -07003502 GLvoid* vertices = (GLvoid*) NULL;
3503 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3504
3505 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003506 vertices = &mMeshVertices[0].x;
3507 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003508
3509 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3510 texture->uvMapper->map(uvs);
3511
3512 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3513 }
3514
Romain Guy3b753822013-03-05 10:27:35 -08003515 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3516 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3517 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003518
Romain Guyd21b6e12011-11-30 20:21:23 -08003519 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003520 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003521 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3522 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003523 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003524 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003525 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003526 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3527 }
3528
3529 if (texture->uvMapper) {
3530 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003531 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003532}
3533
Romain Guybd6b79b2010-06-26 00:13:53 -07003534void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003535 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3536 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003537 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003538}
3539
3540void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003541 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003542 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003543 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3544 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003545
Romain Guy746b7402010-10-26 16:27:31 -07003546 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003547 setupDrawWithTexture();
3548 setupDrawColor(alpha, alpha, alpha, alpha);
3549 setupDrawColorFilter();
3550 setupDrawBlending(blend, mode, swapSrcDst);
3551 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003552 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003553 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003554 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003555 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003556 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003557 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003558
Romain Guy6820ac82010-09-15 18:11:50 -07003559 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003560}
3561
Romain Guy3b748a42013-04-17 18:54:38 -07003562void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3563 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3564 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003565 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3566 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003567
3568 setupDraw();
3569 setupDrawWithTexture();
3570 setupDrawColor(alpha, alpha, alpha, alpha);
3571 setupDrawColorFilter();
3572 setupDrawBlending(blend, mode, swapSrcDst);
3573 setupDrawProgram();
3574 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003575 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003576 setupDrawTexture(texture);
3577 setupDrawPureColorUniforms();
3578 setupDrawColorFilterUniforms();
3579 setupDrawMeshIndices(vertices, texCoords, vbo);
3580
3581 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003582}
3583
Romain Guy886b2752013-01-04 12:26:18 -08003584void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3585 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3586 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003587 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003588
3589 setupDraw();
3590 setupDrawWithTexture(true);
3591 if (hasColor) {
3592 setupDrawAlpha8Color(color, alpha);
3593 }
3594 setupDrawColorFilter();
3595 setupDrawShader();
3596 setupDrawBlending(true, mode);
3597 setupDrawProgram();
3598 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003599 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003600 setupDrawTexture(texture);
3601 setupDrawPureColorUniforms();
3602 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003603 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003604 setupDrawMesh(vertices, texCoords);
3605
3606 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003607}
3608
Romain Guya5aed0d2010-09-09 14:42:43 -07003609void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003610 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003611 if (mCountOverdraw) {
3612 if (!mCaches.blend) glEnable(GL_BLEND);
3613 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3614 glBlendFunc(GL_ONE, GL_ONE);
3615 }
3616
3617 mCaches.blend = true;
3618 mCaches.lastSrcMode = GL_ONE;
3619 mCaches.lastDstMode = GL_ONE;
3620
3621 return;
3622 }
3623
Romain Guy82ba8142010-07-09 13:25:56 -07003624 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003625
Romain Guy82ba8142010-07-09 13:25:56 -07003626 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003627 // These blend modes are not supported by OpenGL directly and have
3628 // to be implemented using shaders. Since the shader will perform
3629 // the blending, turn blending off here
3630 // If the blend mode cannot be implemented using shaders, fall
3631 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003632 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003633 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003634 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003635 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003636
Romain Guy82bc7a72012-01-03 14:13:39 -08003637 if (mCaches.blend) {
3638 glDisable(GL_BLEND);
3639 mCaches.blend = false;
3640 }
3641
3642 return;
3643 } else {
3644 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003645 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003646 }
3647
3648 if (!mCaches.blend) {
3649 glEnable(GL_BLEND);
3650 }
3651
3652 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3653 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3654
3655 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3656 glBlendFunc(sourceMode, destMode);
3657 mCaches.lastSrcMode = sourceMode;
3658 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003659 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003660 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003661 glDisable(GL_BLEND);
3662 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003663 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003664}
3665
Romain Guy889f8d12010-07-29 14:37:42 -07003666bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003667 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003668 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003669 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003670 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003671 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003672 }
Romain Guy6926c722010-07-12 20:20:03 -07003673 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003674}
3675
Romain Guy026c5e162010-06-28 17:12:22 -07003676void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003677 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003678 TextureVertex::setUV(v++, u1, v1);
3679 TextureVertex::setUV(v++, u2, v1);
3680 TextureVertex::setUV(v++, u1, v2);
3681 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003682}
3683
Chris Craik16ecda52013-03-29 10:59:59 -07003684void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003685 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003686 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3687 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003688 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003689 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003690 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003691}
3692
Chris Craik16ecda52013-03-29 10:59:59 -07003693float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3694 float alpha;
3695 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3696 alpha = mDrawModifiers.mOverrideLayerAlpha;
3697 } else {
3698 alpha = layer->getAlpha() / 255.0f;
3699 }
3700 return alpha * mSnapshot->alpha;
3701}
3702
Romain Guy9d5316e2010-06-24 19:30:36 -07003703}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003704}; // namespace android