blob: 578a2517105dd4bc05fd981c9b2be6f817817146 [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 Craikf57776b2013-10-25 18:30:17 -0700185 float dist = std::max(width, height) * 1.5;
186
187 if (DEBUG_ENABLE_3D) {
188 // TODO: make view proj app configurable
189 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
Romain Guy0fe478e2010-11-08 12:08:41 -08002084 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2085 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002086 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07002087 // compute 3d ordering
2088 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07002089 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002090 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002091 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2092 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002093 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002094 }
2095
Chris Craik28ce94a2013-05-31 11:38:03 -07002096 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2097 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002098 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2099 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002100
2101 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002102 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002103
Chris Craikf57776b2013-10-25 18:30:17 -07002104 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002105 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002106
Romain Guy65549432012-03-26 16:45:05 -07002107 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002108}
2109
Romain Guya168d732011-03-18 16:50:13 -07002110void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2111 int alpha;
2112 SkXfermode::Mode mode;
2113 getAlphaAndMode(paint, &alpha, &mode);
2114
Romain Guy886b2752013-01-04 12:26:18 -08002115 int color = paint != NULL ? paint->getColor() : 0;
2116
Romain Guya168d732011-03-18 16:50:13 -07002117 float x = left;
2118 float y = top;
2119
Romain Guy886b2752013-01-04 12:26:18 -08002120 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2121
Romain Guya168d732011-03-18 16:50:13 -07002122 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002123 if (currentTransform().isPureTranslate()) {
2124 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2125 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002126 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002127
2128 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002129 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002130 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002131 }
2132
Romain Guy3b748a42013-04-17 18:54:38 -07002133 // No need to check for a UV mapper on the texture object, only ARGB_8888
2134 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002135 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002136 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2137 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002138}
2139
Romain Guy03c00b52013-06-20 18:30:28 -07002140/**
2141 * Important note: this method is intended to draw batches of bitmaps and
2142 * will not set the scissor enable or dirty the current layer, if any.
2143 * The caller is responsible for properly dirtying the current layer.
2144 */
Romain Guy55b6f952013-06-27 15:27:09 -07002145status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002146 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002147 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002148 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002149 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002150
Chris Craik527a3aa2013-03-04 10:19:31 -08002151 const AutoTexture autoCleanup(texture);
2152
2153 int alpha;
2154 SkXfermode::Mode mode;
2155 getAlphaAndMode(paint, &alpha, &mode);
2156
2157 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002158 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002159
2160 const float x = (int) floorf(bounds.left + 0.5f);
2161 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002162 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002163 int color = paint != NULL ? paint->getColor() : 0;
2164 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2165 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002166 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002167 GL_TRIANGLES, bitmapCount * 6, true,
2168 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002169 } else {
2170 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2171 texture->id, alpha / 255.0f, mode, texture->blend,
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, false, true, 0,
2174 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002175 }
2176
2177 return DrawGlInfo::kStatusDrew;
2178}
2179
Chet Haase48659092012-05-31 15:21:51 -07002180status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002181 const float right = left + bitmap->width();
2182 const float bottom = top + bitmap->height();
2183
Chris Craikf0a59072013-11-19 18:00:46 -08002184 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002185 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002186 }
2187
Romain Guya1d3c912011-12-13 14:55:06 -08002188 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002189 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002190 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002191 const AutoTexture autoCleanup(texture);
2192
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002193 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002194 drawAlphaBitmap(texture, left, top, paint);
2195 } else {
2196 drawTextureRect(left, top, right, bottom, texture, paint);
2197 }
Chet Haase48659092012-05-31 15:21:51 -07002198
2199 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002200}
2201
Chet Haase48659092012-05-31 15:21:51 -07002202status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002203 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2204 const mat4 transform(*matrix);
2205 transform.mapRect(r);
2206
Chris Craikf0a59072013-11-19 18:00:46 -08002207 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002208 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002209 }
2210
Romain Guya1d3c912011-12-13 14:55:06 -08002211 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002212 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002213 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002214 const AutoTexture autoCleanup(texture);
2215
Romain Guy5b3b3522010-10-27 18:57:51 -07002216 // This could be done in a cheaper way, all we need is pass the matrix
2217 // to the vertex shader. The save/restore is a bit overkill.
2218 save(SkCanvas::kMatrix_SaveFlag);
2219 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002220 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002221 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2222 } else {
2223 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2224 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002225 restore();
Chet Haase48659092012-05-31 15:21:51 -07002226
2227 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002228}
2229
Chet Haase48659092012-05-31 15:21:51 -07002230status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002231 const float right = left + bitmap->width();
2232 const float bottom = top + bitmap->height();
2233
Chris Craikf0a59072013-11-19 18:00:46 -08002234 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002235 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002236 }
2237
2238 mCaches.activeTexture(0);
2239 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2240 const AutoTexture autoCleanup(texture);
2241
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002242 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002243 drawAlphaBitmap(texture, left, top, paint);
2244 } else {
2245 drawTextureRect(left, top, right, bottom, texture, paint);
2246 }
Chet Haase48659092012-05-31 15:21:51 -07002247
2248 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002249}
2250
Chet Haase48659092012-05-31 15:21:51 -07002251status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002252 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002253 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002254 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002255 }
2256
Chris Craik39a908c2013-06-13 14:39:01 -07002257 // TODO: use quickReject on bounds from vertices
2258 mCaches.enableScissor();
2259
Romain Guyb18d2d02011-02-10 15:52:54 -08002260 float left = FLT_MAX;
2261 float top = FLT_MAX;
2262 float right = FLT_MIN;
2263 float bottom = FLT_MIN;
2264
Romain Guya92bb4d2012-10-16 11:08:44 -07002265 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002266
Romain Guyff316ec2013-02-13 18:39:43 -08002267 ColorTextureVertex mesh[count];
2268 ColorTextureVertex* vertex = mesh;
2269
2270 bool cleanupColors = false;
2271 if (!colors) {
2272 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2273 colors = new int[colorsCount];
2274 memset(colors, 0xff, colorsCount * sizeof(int));
2275 cleanupColors = true;
2276 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002277
Romain Guy3b748a42013-04-17 18:54:38 -07002278 mCaches.activeTexture(0);
2279 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2280 const UvMapper& mapper(getMapper(texture));
2281
Romain Guy5a7b4662011-01-20 19:09:30 -08002282 for (int32_t y = 0; y < meshHeight; y++) {
2283 for (int32_t x = 0; x < meshWidth; x++) {
2284 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2285
2286 float u1 = float(x) / meshWidth;
2287 float u2 = float(x + 1) / meshWidth;
2288 float v1 = float(y) / meshHeight;
2289 float v2 = float(y + 1) / meshHeight;
2290
Romain Guy3b748a42013-04-17 18:54:38 -07002291 mapper.map(u1, v1, u2, v2);
2292
Romain Guy5a7b4662011-01-20 19:09:30 -08002293 int ax = i + (meshWidth + 1) * 2;
2294 int ay = ax + 1;
2295 int bx = i;
2296 int by = bx + 1;
2297 int cx = i + 2;
2298 int cy = cx + 1;
2299 int dx = i + (meshWidth + 1) * 2 + 2;
2300 int dy = dx + 1;
2301
Romain Guyff316ec2013-02-13 18:39:43 -08002302 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2303 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2304 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002305
Romain Guyff316ec2013-02-13 18:39:43 -08002306 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2307 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2308 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002309
Romain Guya92bb4d2012-10-16 11:08:44 -07002310 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2311 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2312 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2313 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002314 }
2315 }
2316
Chris Craikf0a59072013-11-19 18:00:46 -08002317 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002318 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002319 return DrawGlInfo::kStatusDone;
2320 }
2321
Romain Guyff316ec2013-02-13 18:39:43 -08002322 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002323 texture = mCaches.textureCache.get(bitmap);
2324 if (!texture) {
2325 if (cleanupColors) delete[] colors;
2326 return DrawGlInfo::kStatusDone;
2327 }
Romain Guyff316ec2013-02-13 18:39:43 -08002328 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002329 const AutoTexture autoCleanup(texture);
2330
2331 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2332 texture->setFilter(FILTER(paint), true);
2333
2334 int alpha;
2335 SkXfermode::Mode mode;
2336 getAlphaAndMode(paint, &alpha, &mode);
2337
Romain Guyff316ec2013-02-13 18:39:43 -08002338 float a = alpha / 255.0f;
2339
Romain Guya92bb4d2012-10-16 11:08:44 -07002340 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002341 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002342 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002343
Romain Guyff316ec2013-02-13 18:39:43 -08002344 setupDraw();
2345 setupDrawWithTextureAndColor();
2346 setupDrawColor(a, a, a, a);
2347 setupDrawColorFilter();
2348 setupDrawBlending(true, mode, false);
2349 setupDrawProgram();
2350 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002351 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002352 setupDrawTexture(texture->id);
2353 setupDrawPureColorUniforms();
2354 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002355 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002356
2357 glDrawArrays(GL_TRIANGLES, 0, count);
2358
Romain Guyff316ec2013-02-13 18:39:43 -08002359 int slot = mCaches.currentProgram->getAttrib("colors");
2360 if (slot >= 0) {
2361 glDisableVertexAttribArray(slot);
2362 }
2363
2364 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002365
2366 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002367}
2368
Chet Haase48659092012-05-31 15:21:51 -07002369status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002370 float srcLeft, float srcTop, float srcRight, float srcBottom,
2371 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002372 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002373 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002374 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002375 }
2376
Romain Guya1d3c912011-12-13 14:55:06 -08002377 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002378 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002379 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002380 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002381
Romain Guy8ba548f2010-06-30 19:21:21 -07002382 const float width = texture->width;
2383 const float height = texture->height;
2384
Romain Guy3b748a42013-04-17 18:54:38 -07002385 float u1 = fmax(0.0f, srcLeft / width);
2386 float v1 = fmax(0.0f, srcTop / height);
2387 float u2 = fmin(1.0f, srcRight / width);
2388 float v2 = fmin(1.0f, srcBottom / height);
2389
2390 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002391
Romain Guy03750a02010-10-18 14:06:08 -07002392 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002393 resetDrawTextureTexCoords(u1, v1, u2, v2);
2394
Romain Guy03750a02010-10-18 14:06:08 -07002395 int alpha;
2396 SkXfermode::Mode mode;
2397 getAlphaAndMode(paint, &alpha, &mode);
2398
Romain Guyd21b6e12011-11-30 20:21:23 -08002399 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2400
Romain Guy886b2752013-01-04 12:26:18 -08002401 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2402 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002403
Romain Guy886b2752013-01-04 12:26:18 -08002404 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2405 // Apply a scale transform on the canvas only when a shader is in use
2406 // Skia handles the ratio between the dst and src rects as a scale factor
2407 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002408 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002409 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002410
Romain Guy3b753822013-03-05 10:27:35 -08002411 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2412 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2413 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002414
2415 dstRight = x + (dstRight - dstLeft);
2416 dstBottom = y + (dstBottom - dstTop);
2417
2418 dstLeft = x;
2419 dstTop = y;
2420
2421 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2422 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002423 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002424 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002425 }
2426
2427 if (CC_UNLIKELY(useScaleTransform)) {
2428 save(SkCanvas::kMatrix_SaveFlag);
2429 translate(dstLeft, dstTop);
2430 scale(scaleX, scaleY);
2431
2432 dstLeft = 0.0f;
2433 dstTop = 0.0f;
2434
2435 dstRight = srcRight - srcLeft;
2436 dstBottom = srcBottom - srcTop;
2437 }
2438
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002439 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002440 int color = paint ? paint->getColor() : 0;
2441 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2442 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002443 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002444 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2445 } else {
2446 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2447 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002448 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002449 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2450 }
2451
2452 if (CC_UNLIKELY(useScaleTransform)) {
2453 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002454 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002455
2456 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002457
2458 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002459}
2460
Romain Guy3b748a42013-04-17 18:54:38 -07002461status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002462 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002463 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002464 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002465 }
2466
Romain Guy4c2547f2013-06-11 16:19:24 -07002467 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002468 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2469 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002470
Romain Guy03c00b52013-06-20 18:30:28 -07002471 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002472}
2473
Romain Guy03c00b52013-06-20 18:30:28 -07002474status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2475 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002476 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002477 return DrawGlInfo::kStatusDone;
2478 }
2479
Romain Guy211370f2012-02-01 16:10:55 -08002480 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002481 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002482 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002483 if (!texture) return DrawGlInfo::kStatusDone;
2484 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002485
Romain Guya4adcf02013-02-28 12:15:35 -08002486 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2487 texture->setFilter(GL_LINEAR, true);
2488
Romain Guy03c00b52013-06-20 18:30:28 -07002489 int alpha;
2490 SkXfermode::Mode mode;
2491 getAlphaAndMode(paint, &alpha, &mode);
2492
Romain Guy3b753822013-03-05 10:27:35 -08002493 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002494 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002495 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002496 const float offsetX = left + currentTransform().getTranslateX();
2497 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002498 const size_t count = mesh->quads.size();
2499 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002500 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002501 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002502 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2503 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2504 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002505 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002506 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002507 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002508 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002509 }
2510 }
2511
Chris Craik4063a0e2013-11-15 16:06:56 -08002512 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002513 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002514 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2515 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002516
Romain Guy3b748a42013-04-17 18:54:38 -07002517 right = x + right - left;
2518 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002519 left = x;
2520 top = y;
2521 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002522 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002523 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2524 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2525 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2526 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002527 }
Chet Haase48659092012-05-31 15:21:51 -07002528
2529 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002530}
2531
Romain Guy03c00b52013-06-20 18:30:28 -07002532/**
2533 * Important note: this method is intended to draw batches of 9-patch objects and
2534 * will not set the scissor enable or dirty the current layer, if any.
2535 * The caller is responsible for properly dirtying the current layer.
2536 */
2537status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2538 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2539 mCaches.activeTexture(0);
2540 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2541 if (!texture) return DrawGlInfo::kStatusDone;
2542 const AutoTexture autoCleanup(texture);
2543
2544 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2545 texture->setFilter(GL_LINEAR, true);
2546
2547 int alpha;
2548 SkXfermode::Mode mode;
2549 getAlphaAndMode(paint, &alpha, &mode);
2550
2551 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002552 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002553 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002554
2555 return DrawGlInfo::kStatusDrew;
2556}
2557
Chris Craik65cd6122012-12-10 17:56:27 -08002558status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2559 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002560 // not missing call to quickReject/dirtyLayer, always done at a higher level
2561
Chris Craik6d29c8d2013-05-08 18:35:44 -07002562 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002563 // no vertices to draw
2564 return DrawGlInfo::kStatusDone;
2565 }
2566
Chris Craikcb4d6002012-09-25 12:00:29 -07002567 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002568 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2569 bool isAA = paint->isAntiAlias();
2570
Chris Craik710f46d2012-09-17 17:25:49 -07002571 setupDraw();
2572 setupDrawNoTexture();
2573 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002574 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2575 setupDrawColorFilter();
2576 setupDrawShader();
2577 setupDrawBlending(isAA, mode);
2578 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002579 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002580 setupDrawColorUniforms();
2581 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002582 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002583
ztenghui55bfb4e2013-12-03 10:38:55 -08002584 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002585 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002586 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002587 mCaches.resetTexCoordsVertexPointer();
2588 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002589
Chris Craik710f46d2012-09-17 17:25:49 -07002590 int alphaSlot = -1;
2591 if (isAA) {
2592 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2593 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002594
Chris Craik710f46d2012-09-17 17:25:49 -07002595 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002596 glEnableVertexAttribArray(alphaSlot);
2597 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002598 }
Romain Guy04299382012-07-18 17:15:41 -07002599
Chris Craik6d29c8d2013-05-08 18:35:44 -07002600 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002601
Chris Craik710f46d2012-09-17 17:25:49 -07002602 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002603 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002604 }
Chris Craik65cd6122012-12-10 17:56:27 -08002605
2606 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002607}
2608
2609/**
Chris Craik65cd6122012-12-10 17:56:27 -08002610 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2611 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2612 * screen space in all directions. However, instead of using a fragment shader to compute the
2613 * translucency of the color from its position, we simply use a varying parameter to define how far
2614 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2615 *
2616 * Doesn't yet support joins, caps, or path effects.
2617 */
2618status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2619 VertexBuffer vertexBuffer;
2620 // TODO: try clipping large paths to viewport
2621 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2622
Romain Guyc46d07a2013-03-15 19:06:39 -07002623 if (hasLayer()) {
2624 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002625 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002626 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2627 }
Chris Craik65cd6122012-12-10 17:56:27 -08002628
2629 return drawVertexBuffer(vertexBuffer, paint);
2630}
2631
2632/**
2633 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2634 * and additional geometry for defining an alpha slope perimeter.
2635 *
2636 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2637 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2638 * in-shader alpha region, but found it to be taxing on some GPUs.
2639 *
2640 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2641 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002642 */
Chet Haase48659092012-05-31 15:21:51 -07002643status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002644 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002645
Chris Craik65cd6122012-12-10 17:56:27 -08002646 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002647
Chris Craik65cd6122012-12-10 17:56:27 -08002648 VertexBuffer buffer;
2649 SkRect bounds;
2650 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002651
Chris Craikf0a59072013-11-19 18:00:46 -08002652 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2653 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002654 return DrawGlInfo::kStatusDone;
2655 }
2656
Romain Guy3b753822013-03-05 10:27:35 -08002657 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002658
Chris Craik65cd6122012-12-10 17:56:27 -08002659 bool useOffset = !paint->isAntiAlias();
2660 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002661}
2662
Chet Haase48659092012-05-31 15:21:51 -07002663status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002664 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002665
Chris Craik6d29c8d2013-05-08 18:35:44 -07002666 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002667
Chris Craik6d29c8d2013-05-08 18:35:44 -07002668 VertexBuffer buffer;
2669 SkRect bounds;
2670 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002671
Chris Craikf0a59072013-11-19 18:00:46 -08002672 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2673 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002674 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002675 }
2676
Chris Craik6d29c8d2013-05-08 18:35:44 -07002677 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002678
Chris Craik6d29c8d2013-05-08 18:35:44 -07002679 bool useOffset = !paint->isAntiAlias();
2680 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002681}
2682
Chet Haase48659092012-05-31 15:21:51 -07002683status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002684 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002685 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002686
Romain Guyae88e5e2010-10-22 17:49:18 -07002687 Rect& clip(*mSnapshot->clipRect);
2688 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002689
Romain Guy3d58c032010-07-14 16:34:53 -07002690 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002691
2692 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002693}
Romain Guy9d5316e2010-06-24 19:30:36 -07002694
Chet Haase48659092012-05-31 15:21:51 -07002695status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2696 SkPaint* paint) {
2697 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002698 const AutoTexture autoCleanup(texture);
2699
2700 const float x = left + texture->left - texture->offset;
2701 const float y = top + texture->top - texture->offset;
2702
2703 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002704
2705 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002706}
2707
Chet Haase48659092012-05-31 15:21:51 -07002708status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002709 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002710 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002711 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002712 return DrawGlInfo::kStatusDone;
2713 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002714
Chris Craikcb4d6002012-09-25 12:00:29 -07002715 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002716 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002717 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002718 right - left, bottom - top, rx, ry, p);
2719 return drawShape(left, top, texture, p);
2720 }
2721
2722 SkPath path;
2723 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002724 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2725 float outset = p->getStrokeWidth() / 2;
2726 rect.outset(outset, outset);
2727 rx += outset;
2728 ry += outset;
2729 }
Chris Craik710f46d2012-09-17 17:25:49 -07002730 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002731 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002732}
2733
Chris Craik710f46d2012-09-17 17:25:49 -07002734status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002735 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002736 x + radius, y + radius, p) ||
2737 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002738 return DrawGlInfo::kStatusDone;
2739 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002740 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002741 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002742 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002743 return drawShape(x - radius, y - radius, texture, p);
2744 }
2745
2746 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002747 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2748 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2749 } else {
2750 path.addCircle(x, y, radius);
2751 }
Chris Craik65cd6122012-12-10 17:56:27 -08002752 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002753}
Romain Guy01d58e42011-01-19 21:54:02 -08002754
Chet Haase48659092012-05-31 15:21:51 -07002755status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002756 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002757 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002758 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002759 return DrawGlInfo::kStatusDone;
2760 }
Romain Guy01d58e42011-01-19 21:54:02 -08002761
Chris Craikcb4d6002012-09-25 12:00:29 -07002762 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002763 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002764 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002765 return drawShape(left, top, texture, p);
2766 }
2767
2768 SkPath path;
2769 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002770 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2771 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2772 }
Chris Craik710f46d2012-09-17 17:25:49 -07002773 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002774 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002775}
2776
Chet Haase48659092012-05-31 15:21:51 -07002777status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002778 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002779 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002780 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002781 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002782 }
2783
Chris Craik780c1282012-10-04 14:10:49 -07002784 if (fabs(sweepAngle) >= 360.0f) {
2785 return drawOval(left, top, right, bottom, p);
2786 }
2787
2788 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002789 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002790 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002791 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002792 startAngle, sweepAngle, useCenter, p);
2793 return drawShape(left, top, texture, p);
2794 }
2795
2796 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2797 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2798 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2799 }
2800
2801 SkPath path;
2802 if (useCenter) {
2803 path.moveTo(rect.centerX(), rect.centerY());
2804 }
2805 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2806 if (useCenter) {
2807 path.close();
2808 }
Chris Craik65cd6122012-12-10 17:56:27 -08002809 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002810}
2811
Romain Guycf8675e2012-10-02 12:32:25 -07002812// See SkPaintDefaults.h
2813#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2814
Chet Haase48659092012-05-31 15:21:51 -07002815status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002816 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002817 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002818 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002819 }
2820
Chris Craik710f46d2012-09-17 17:25:49 -07002821 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002822 // only fill style is supported by drawConvexPath, since others have to handle joins
2823 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2824 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2825 mCaches.activeTexture(0);
2826 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002827 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002828 return drawShape(left, top, texture, p);
2829 }
2830
2831 SkPath path;
2832 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2833 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2834 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2835 }
2836 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002837 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002838 }
2839
Romain Guy3b753822013-03-05 10:27:35 -08002840 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002841 SkPath path;
2842 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002843 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002844 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002845 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002846 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002847 }
Romain Guyc7d53492010-06-25 13:41:57 -07002848}
Romain Guy9d5316e2010-06-24 19:30:36 -07002849
Raph Levien416a8472012-07-19 22:48:17 -07002850void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2851 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2852 float x, float y) {
2853 mCaches.activeTexture(0);
2854
2855 // NOTE: The drop shadow will not perform gamma correction
2856 // if shader-based correction is enabled
2857 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2858 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002859 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002860 // If the drop shadow exceeds the max texture size or couldn't be
2861 // allocated, skip drawing
2862 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002863 const AutoTexture autoCleanup(shadow);
2864
Chris Craikc3566d02013-02-04 16:16:33 -08002865 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2866 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002867
Chris Craikc3566d02013-02-04 16:16:33 -08002868 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2869 int shadowColor = mDrawModifiers.mShadowColor;
2870 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002871 shadowColor = 0xffffffff;
2872 }
2873
2874 setupDraw();
2875 setupDrawWithTexture(true);
2876 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2877 setupDrawColorFilter();
2878 setupDrawShader();
2879 setupDrawBlending(true, mode);
2880 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002881 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2882 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002883 setupDrawTexture(shadow->id);
2884 setupDrawPureColorUniforms();
2885 setupDrawColorFilterUniforms();
2886 setupDrawShaderUniforms();
2887 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2888
2889 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2890}
2891
Romain Guy768bffc2013-02-27 13:50:45 -08002892bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2893 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2894 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2895}
2896
Chet Haase48659092012-05-31 15:21:51 -07002897status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002898 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002899 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002900 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002901 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002902
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002904 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002905 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002906 }
2907
Chris Craik39a908c2013-06-13 14:39:01 -07002908 mCaches.enableScissor();
2909
Romain Guy671d6cf2012-01-18 12:39:17 -08002910 float x = 0.0f;
2911 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002912 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002913 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002914 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2915 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002916 }
2917
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002918 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002919 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002920
2921 int alpha;
2922 SkXfermode::Mode mode;
2923 getAlphaAndMode(paint, &alpha, &mode);
2924
Chris Craikc3566d02013-02-04 16:16:33 -08002925 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002926 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2927 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002928 }
2929
Romain Guy671d6cf2012-01-18 12:39:17 -08002930 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002931 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002932 if (pureTranslate && !linearFilter) {
2933 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2934 }
Romain Guy257ae352013-03-20 16:31:12 -07002935 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002936
2937 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2938 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2939
Romain Guy211370f2012-02-01 16:10:55 -08002940 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002941
Victoria Lease1e546812013-06-25 14:25:17 -07002942 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002943 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002944 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002945 if (hasActiveLayer) {
2946 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002947 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002948 }
2949 dirtyLayerUnchecked(bounds, getRegion());
2950 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002951 }
Chet Haase48659092012-05-31 15:21:51 -07002952
2953 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002954}
2955
Romain Guy624234f2013-03-05 16:43:31 -08002956mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2957 mat4 fontTransform;
2958 if (CC_LIKELY(transform.isPureTranslate())) {
2959 fontTransform = mat4::identity();
2960 } else {
2961 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002962 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002963 } else {
2964 float sx, sy;
2965 currentTransform().decomposeScale(sx, sy);
2966 fontTransform.loadScale(sx, sy, 1.0f);
2967 }
2968 }
2969 return fontTransform;
2970}
2971
Chris Craik41541822013-05-03 16:35:54 -07002972status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2973 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002974 DrawOpMode drawOpMode) {
2975
Chris Craik527a3aa2013-03-04 10:19:31 -08002976 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002977 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2978 // drawing as ops from DeferredDisplayList are already filtered for these
2979 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002980 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002981 return DrawGlInfo::kStatusDone;
2982 }
Romain Guycac5fd32011-12-01 20:08:50 -08002983 }
2984
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002985 const float oldX = x;
2986 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002987
2988 const mat4& transform = currentTransform();
2989 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002990
Romain Guy211370f2012-02-01 16:10:55 -08002991 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002992 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2993 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002994 }
2995
Romain Guy86568192010-12-14 15:55:39 -08002996 int alpha;
2997 SkXfermode::Mode mode;
2998 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002999
Romain Guyc74f45a2013-02-26 19:10:14 -08003000 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
3001
Chris Craikc3566d02013-02-04 16:16:33 -08003002 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08003003 fontRenderer.setFont(paint, mat4::identity());
3004 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
3005 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07003006 }
3007
Romain Guy19d4dd82013-03-04 11:14:26 -08003008 const bool hasActiveLayer = hasLayer();
3009
Romain Guy624234f2013-03-05 16:43:31 -08003010 // We only pass a partial transform to the font renderer. That partial
3011 // matrix defines how glyphs are rasterized. Typically we want glyphs
3012 // to be rasterized at their final size on screen, which means the partial
3013 // matrix needs to take the scale factor into account.
3014 // When a partial matrix is used to transform glyphs during rasterization,
3015 // the mesh is generated with the inverse transform (in the case of scale,
3016 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3017 // apply the full transform matrix at draw time in the vertex shader.
3018 // Applying the full matrix in the shader is the easiest way to handle
3019 // rotation and perspective and allows us to always generated quads in the
3020 // font renderer which greatly simplifies the code, clipping in particular.
3021 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003022 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003023
Romain Guy6620c6d2010-12-06 18:07:02 -08003024 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003025 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003026 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003027
Romain Guy624234f2013-03-05 16:43:31 -08003028 // TODO: Implement better clipping for scaled/rotated text
3029 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003030 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 -07003031
Raph Levien996e57c2012-07-23 15:22:52 -07003032 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003033 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003034
3035 // don't call issuedrawcommand, do it at end of batch
3036 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003037 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003038 SkPaint paintCopy(*paint);
3039 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3040 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003041 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003042 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003043 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003044 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003045 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003046
Chris Craik527a3aa2013-03-04 10:19:31 -08003047 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003048 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003049 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003050 }
Chris Craik41541822013-05-03 16:35:54 -07003051 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003052 }
Romain Guy694b5192010-07-21 21:33:20 -07003053
Chris Craike63f7c622013-10-17 10:30:55 -07003054 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003055
3056 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003057}
3058
Chet Haase48659092012-05-31 15:21:51 -07003059status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003060 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003061 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003062 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003063 }
3064
Chris Craik39a908c2013-06-13 14:39:01 -07003065 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3066 mCaches.enableScissor();
3067
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003068 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003069 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003070 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003071
3072 int alpha;
3073 SkXfermode::Mode mode;
3074 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003075 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003076
Romain Guy97771732012-02-28 18:17:02 -08003077 const Rect* clip = &mSnapshot->getLocalClip();
3078 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 -08003079
Romain Guy97771732012-02-28 18:17:02 -08003080 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003081
3082 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003083 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003084 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003085 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003086 dirtyLayerUnchecked(bounds, getRegion());
3087 }
Romain Guy97771732012-02-28 18:17:02 -08003088 }
Chet Haase48659092012-05-31 15:21:51 -07003089
3090 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003091}
3092
Chet Haase48659092012-05-31 15:21:51 -07003093status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3094 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003095
Romain Guya1d3c912011-12-13 14:55:06 -08003096 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003097
Romain Guyfb8b7632010-08-23 21:05:08 -07003098 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003099 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003100 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003101
Romain Guy8b55f372010-08-18 17:10:07 -07003102 const float x = texture->left - texture->offset;
3103 const float y = texture->top - texture->offset;
3104
Romain Guy01d58e42011-01-19 21:54:02 -08003105 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003106
3107 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003108}
3109
Chris Craika08f95c2013-03-15 17:24:33 -07003110status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003111 if (!layer) {
3112 return DrawGlInfo::kStatusDone;
3113 }
3114
Romain Guyb2e2f242012-10-17 18:18:35 -07003115 mat4* transform = NULL;
3116 if (layer->isTextureLayer()) {
3117 transform = &layer->getTransform();
3118 if (!transform->isIdentity()) {
3119 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003120 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003121 }
3122 }
3123
Chris Craik39a908c2013-06-13 14:39:01 -07003124 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08003125 const bool rejected = calculateQuickRejectForScissor(x, y,
3126 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003127
3128 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003129 if (transform && !transform->isIdentity()) {
3130 restore();
3131 }
Chet Haase48659092012-05-31 15:21:51 -07003132 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003133 }
3134
Romain Guy5bb3c732012-11-29 17:52:58 -08003135 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003136
Chris Craik39a908c2013-06-13 14:39:01 -07003137 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003138 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003139
Romain Guy211370f2012-02-01 16:10:55 -08003140 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003141 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3142 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003143
Romain Guyc88e3572011-01-22 00:32:12 -08003144 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003145 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3146 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003147 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003148 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003149 setupDraw();
3150 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003151 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003152 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003153 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003154 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003155 setupDrawPureColorUniforms();
3156 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003157 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003158 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3159 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3160 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003161
Romain Guyd21b6e12011-11-30 20:21:23 -08003162 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003163 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003164 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003165 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003166 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003167 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003168 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3169 }
Romain Guyf219da52011-01-16 12:54:25 -08003170
Romain Guy448455f2013-07-22 13:57:50 -07003171 TextureVertex* mesh = &layer->mesh[0];
3172 GLsizei elementsCount = layer->meshElementCount;
3173
3174 while (elementsCount > 0) {
3175 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3176
Romain Guy3380cfd2013-08-15 16:57:57 -07003177 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003178 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3179 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3180
3181 elementsCount -= drawCount;
3182 // Though there are 4 vertices in a quad, we use 6 indices per
3183 // quad to draw with GL_TRIANGLES
3184 mesh += (drawCount / 6) * 4;
3185 }
Romain Guyf219da52011-01-16 12:54:25 -08003186
Romain Guy3a3133d2011-02-01 22:59:58 -08003187#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003188 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003189#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003190 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003191
Chris Craikc3566d02013-02-04 16:16:33 -08003192 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003193
Romain Guy5bb3c732012-11-29 17:52:58 -08003194 if (layer->debugDrawUpdate) {
3195 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003196 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3197 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3198 }
Romain Guyf219da52011-01-16 12:54:25 -08003199 }
Chris Craik34416ea2013-04-15 16:08:28 -07003200 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003201
Romain Guyb2e2f242012-10-17 18:18:35 -07003202 if (transform && !transform->isIdentity()) {
3203 restore();
3204 }
3205
Chet Haase48659092012-05-31 15:21:51 -07003206 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003207}
3208
Romain Guy6926c722010-07-12 20:20:03 -07003209///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003210// Shaders
3211///////////////////////////////////////////////////////////////////////////////
3212
3213void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003214 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003215}
3216
Romain Guy06f96e22010-07-30 19:18:16 -07003217void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003218 mDrawModifiers.mShader = shader;
3219 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003220 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003221 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003222}
3223
Romain Guyd27977d2010-07-14 19:18:51 -07003224///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003225// Color filters
3226///////////////////////////////////////////////////////////////////////////////
3227
3228void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003229 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003230}
3231
3232void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003233 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003234}
3235
3236///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003237// Drop shadow
3238///////////////////////////////////////////////////////////////////////////////
3239
3240void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003241 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003242}
3243
3244void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003245 mDrawModifiers.mHasShadow = true;
3246 mDrawModifiers.mShadowRadius = radius;
3247 mDrawModifiers.mShadowDx = dx;
3248 mDrawModifiers.mShadowDy = dy;
3249 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003250}
3251
3252///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003253// Draw filters
3254///////////////////////////////////////////////////////////////////////////////
3255
3256void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003257 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3258 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003259 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003260 mDrawModifiers.mPaintFilterClearBits = 0;
3261 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003262}
3263
3264void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003265 mDrawModifiers.mHasDrawFilter = true;
3266 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3267 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003268}
3269
Chris Craika08f95c2013-03-15 17:24:33 -07003270SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003271 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003272 return paint;
3273 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003274
3275 uint32_t flags = paint->getFlags();
3276
3277 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003278 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3279 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003280
3281 return &mFilteredPaint;
3282}
3283
3284///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003285// Drawing implementation
3286///////////////////////////////////////////////////////////////////////////////
3287
Romain Guy3b748a42013-04-17 18:54:38 -07003288Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3289 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3290 if (!texture) {
3291 return mCaches.textureCache.get(bitmap);
3292 }
3293 return texture;
3294}
3295
Romain Guy01d58e42011-01-19 21:54:02 -08003296void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3297 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003298 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003299 return;
3300 }
3301
3302 int alpha;
3303 SkXfermode::Mode mode;
3304 getAlphaAndMode(paint, &alpha, &mode);
3305
3306 setupDraw();
3307 setupDrawWithTexture(true);
3308 setupDrawAlpha8Color(paint->getColor(), alpha);
3309 setupDrawColorFilter();
3310 setupDrawShader();
3311 setupDrawBlending(true, mode);
3312 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003313 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3314 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003315 setupDrawTexture(texture->id);
3316 setupDrawPureColorUniforms();
3317 setupDrawColorFilterUniforms();
3318 setupDrawShaderUniforms();
3319 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3320
3321 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003322}
3323
Romain Guyf607bdc2010-09-10 19:20:06 -07003324// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003325#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3326#define kStdUnderline_Offset (1.0f / 9.0f)
3327#define kStdUnderline_Thickness (1.0f / 18.0f)
3328
Chris Craike63f7c622013-10-17 10:30:55 -07003329void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003330 // Handle underline and strike-through
3331 uint32_t flags = paint->getFlags();
3332 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003333 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003334
Romain Guy211370f2012-02-01 16:10:55 -08003335 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003336 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003337 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003338
Raph Levien8b4072d2012-07-30 15:50:00 -07003339 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003340 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003341
Romain Guyf6834472011-01-23 13:32:12 -08003342 int linesCount = 0;
3343 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3344 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3345
3346 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003347 float points[pointsCount];
3348 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003349
3350 if (flags & SkPaint::kUnderlineText_Flag) {
3351 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003352 points[currentPoint++] = left;
3353 points[currentPoint++] = top;
3354 points[currentPoint++] = left + underlineWidth;
3355 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003356 }
3357
3358 if (flags & SkPaint::kStrikeThruText_Flag) {
3359 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003360 points[currentPoint++] = left;
3361 points[currentPoint++] = top;
3362 points[currentPoint++] = left + underlineWidth;
3363 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003364 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003365
Romain Guy726aeba2011-06-01 14:52:00 -07003366 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003367
Romain Guy726aeba2011-06-01 14:52:00 -07003368 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003369 }
3370 }
3371}
3372
Romain Guy672433d2013-01-04 19:05:13 -08003373status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3374 if (mSnapshot->isIgnored()) {
3375 return DrawGlInfo::kStatusDone;
3376 }
3377
Romain Guy735738c2012-12-03 12:34:51 -08003378 int color = paint->getColor();
3379 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003380 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003381 color |= 0x00ffffff;
3382 }
3383 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3384
3385 return drawColorRects(rects, count, color, mode);
3386}
3387
Chris Craikf57776b2013-10-25 18:30:17 -07003388status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3389 float width, float height) {
3390 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3391
3392 // For now, always and scissor
3393 // TODO: use quickReject
3394 mCaches.enableScissor();
3395
3396 SkPaint paint;
3397 paint.setColor(0x3f000000);
ztenghui55bfb4e2013-12-03 10:38:55 -08003398 // Force the draw to use alpha values.
Chris Craikf57776b2013-10-25 18:30:17 -07003399 paint.setAntiAlias(true);
Chris Craikf57776b2013-10-25 18:30:17 -07003400
ztenghui55bfb4e2013-12-03 10:38:55 -08003401 VertexBuffer shadowVertexBuffer;
3402 ShadowTessellator::tessellateAmbientShadow(width, height, casterTransform,
3403 shadowVertexBuffer);
3404 return drawVertexBuffer(shadowVertexBuffer, &paint);
Chris Craikf57776b2013-10-25 18:30:17 -07003405}
3406
Romain Guy735738c2012-12-03 12:34:51 -08003407status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003408 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003409 if (count == 0) {
3410 return DrawGlInfo::kStatusDone;
3411 }
Romain Guy735738c2012-12-03 12:34:51 -08003412
Romain Guy672433d2013-01-04 19:05:13 -08003413 float left = FLT_MAX;
3414 float top = FLT_MAX;
3415 float right = FLT_MIN;
3416 float bottom = FLT_MIN;
3417
Romain Guy448455f2013-07-22 13:57:50 -07003418 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003419 Vertex* vertex = mesh;
3420
Chris Craik2af46352012-11-26 18:30:17 -08003421 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003422 float l = rects[index + 0];
3423 float t = rects[index + 1];
3424 float r = rects[index + 2];
3425 float b = rects[index + 3];
3426
Romain Guy3b753822013-03-05 10:27:35 -08003427 Vertex::set(vertex++, l, t);
3428 Vertex::set(vertex++, r, t);
3429 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003430 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003431
Romain Guy3b753822013-03-05 10:27:35 -08003432 left = fminf(left, l);
3433 top = fminf(top, t);
3434 right = fmaxf(right, r);
3435 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003436 }
3437
Chris Craikf0a59072013-11-19 18:00:46 -08003438 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003439 return DrawGlInfo::kStatusDone;
3440 }
Romain Guy672433d2013-01-04 19:05:13 -08003441
Romain Guy672433d2013-01-04 19:05:13 -08003442 setupDraw();
3443 setupDrawNoTexture();
3444 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3445 setupDrawShader();
3446 setupDrawColorFilter();
3447 setupDrawBlending(mode);
3448 setupDrawProgram();
3449 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003450 setupDrawModelView(kModelViewMode_Translate, false,
3451 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003452 setupDrawColorUniforms();
3453 setupDrawShaderUniforms();
3454 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003455
Romain Guy8ce00302013-01-15 18:51:42 -08003456 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003457 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003458 }
3459
Chris Craik4063a0e2013-11-15 16:06:56 -08003460 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003461
3462 return DrawGlInfo::kStatusDrew;
3463}
3464
Romain Guy026c5e162010-06-28 17:12:22 -07003465void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003466 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003467 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003468 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003469 color |= 0x00ffffff;
3470 }
3471
Romain Guy70ca14e2010-12-13 18:24:33 -08003472 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003473 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003474 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003475 setupDrawShader();
3476 setupDrawColorFilter();
3477 setupDrawBlending(mode);
3478 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003479 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3480 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003481 setupDrawColorUniforms();
3482 setupDrawShaderUniforms(ignoreTransform);
3483 setupDrawColorFilterUniforms();
3484 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003485
Romain Guyc95c8d62010-09-17 15:31:32 -07003486 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3487}
3488
Romain Guy82ba8142010-07-09 13:25:56 -07003489void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003490 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003491 int alpha;
3492 SkXfermode::Mode mode;
3493 getAlphaAndMode(paint, &alpha, &mode);
3494
Romain Guyd21b6e12011-11-30 20:21:23 -08003495 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003496
Romain Guy3b748a42013-04-17 18:54:38 -07003497 GLvoid* vertices = (GLvoid*) NULL;
3498 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3499
3500 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003501 vertices = &mMeshVertices[0].x;
3502 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003503
3504 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3505 texture->uvMapper->map(uvs);
3506
3507 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3508 }
3509
Romain Guy3b753822013-03-05 10:27:35 -08003510 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3511 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3512 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003513
Romain Guyd21b6e12011-11-30 20:21:23 -08003514 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003515 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003516 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3517 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003518 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003519 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003520 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003521 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3522 }
3523
3524 if (texture->uvMapper) {
3525 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003526 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003527}
3528
Romain Guybd6b79b2010-06-26 00:13:53 -07003529void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003530 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3531 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003532 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003533}
3534
3535void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003536 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003537 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003538 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3539 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003540
Romain Guy746b7402010-10-26 16:27:31 -07003541 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003542 setupDrawWithTexture();
3543 setupDrawColor(alpha, alpha, alpha, alpha);
3544 setupDrawColorFilter();
3545 setupDrawBlending(blend, mode, swapSrcDst);
3546 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003547 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003548 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003549 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003550 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003551 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003552 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003553
Romain Guy6820ac82010-09-15 18:11:50 -07003554 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003555}
3556
Romain Guy3b748a42013-04-17 18:54:38 -07003557void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3558 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3559 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003560 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3561 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003562
3563 setupDraw();
3564 setupDrawWithTexture();
3565 setupDrawColor(alpha, alpha, alpha, alpha);
3566 setupDrawColorFilter();
3567 setupDrawBlending(blend, mode, swapSrcDst);
3568 setupDrawProgram();
3569 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003570 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003571 setupDrawTexture(texture);
3572 setupDrawPureColorUniforms();
3573 setupDrawColorFilterUniforms();
3574 setupDrawMeshIndices(vertices, texCoords, vbo);
3575
3576 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003577}
3578
Romain Guy886b2752013-01-04 12:26:18 -08003579void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3580 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3581 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003582 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003583
3584 setupDraw();
3585 setupDrawWithTexture(true);
3586 if (hasColor) {
3587 setupDrawAlpha8Color(color, alpha);
3588 }
3589 setupDrawColorFilter();
3590 setupDrawShader();
3591 setupDrawBlending(true, mode);
3592 setupDrawProgram();
3593 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003594 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003595 setupDrawTexture(texture);
3596 setupDrawPureColorUniforms();
3597 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003598 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003599 setupDrawMesh(vertices, texCoords);
3600
3601 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003602}
3603
Romain Guya5aed0d2010-09-09 14:42:43 -07003604void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003605 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003606 if (mCountOverdraw) {
3607 if (!mCaches.blend) glEnable(GL_BLEND);
3608 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3609 glBlendFunc(GL_ONE, GL_ONE);
3610 }
3611
3612 mCaches.blend = true;
3613 mCaches.lastSrcMode = GL_ONE;
3614 mCaches.lastDstMode = GL_ONE;
3615
3616 return;
3617 }
3618
Romain Guy82ba8142010-07-09 13:25:56 -07003619 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003620
Romain Guy82ba8142010-07-09 13:25:56 -07003621 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003622 // These blend modes are not supported by OpenGL directly and have
3623 // to be implemented using shaders. Since the shader will perform
3624 // the blending, turn blending off here
3625 // If the blend mode cannot be implemented using shaders, fall
3626 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003627 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003628 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003629 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003630 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003631
Romain Guy82bc7a72012-01-03 14:13:39 -08003632 if (mCaches.blend) {
3633 glDisable(GL_BLEND);
3634 mCaches.blend = false;
3635 }
3636
3637 return;
3638 } else {
3639 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003640 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003641 }
3642
3643 if (!mCaches.blend) {
3644 glEnable(GL_BLEND);
3645 }
3646
3647 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3648 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3649
3650 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3651 glBlendFunc(sourceMode, destMode);
3652 mCaches.lastSrcMode = sourceMode;
3653 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003654 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003655 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003656 glDisable(GL_BLEND);
3657 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003658 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003659}
3660
Romain Guy889f8d12010-07-29 14:37:42 -07003661bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003662 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003663 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003664 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003665 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003666 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003667 }
Romain Guy6926c722010-07-12 20:20:03 -07003668 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003669}
3670
Romain Guy026c5e162010-06-28 17:12:22 -07003671void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003672 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003673 TextureVertex::setUV(v++, u1, v1);
3674 TextureVertex::setUV(v++, u2, v1);
3675 TextureVertex::setUV(v++, u1, v2);
3676 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003677}
3678
Chris Craik16ecda52013-03-29 10:59:59 -07003679void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003680 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003681 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3682 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003683 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003684 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003685 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003686}
3687
Chris Craik16ecda52013-03-29 10:59:59 -07003688float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3689 float alpha;
3690 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3691 alpha = mDrawModifiers.mOverrideLayerAlpha;
3692 } else {
3693 alpha = layer->getAlpha() / 255.0f;
3694 }
3695 return alpha * mSnapshot->alpha;
3696}
3697
Romain Guy9d5316e2010-06-24 19:30:36 -07003698}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003699}; // namespace android