blob: 79df5201f62c7aeec8fa13dac312370f02c4c3b3 [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
Romain Guya957eea2010-12-08 18:34:42 -080039#include "Vector.h"
Romain Guye4d01122010-06-16 18:44:05 -070040
41namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070042namespace uirenderer {
43
44///////////////////////////////////////////////////////////////////////////////
45// Defines
46///////////////////////////////////////////////////////////////////////////////
47
Romain Guy759ea802010-09-16 20:49:46 -070048#define RAD_TO_DEG (180.0f / 3.14159265f)
49#define MIN_ANGLE 0.001f
50
Romain Guyf8773082012-07-12 18:01:00 -070051#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070052
Romain Guy713e1bb2012-10-16 18:44:09 -070053#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080054
Romain Guy9d5316e2010-06-24 19:30:36 -070055///////////////////////////////////////////////////////////////////////////////
56// Globals
57///////////////////////////////////////////////////////////////////////////////
58
Romain Guy889f8d12010-07-29 14:37:42 -070059/**
60 * Structure mapping Skia xfermodes to OpenGL blending factors.
61 */
62struct Blender {
63 SkXfermode::Mode mode;
64 GLenum src;
65 GLenum dst;
66}; // struct Blender
67
Romain Guy026c5e162010-06-28 17:12:22 -070068// In this array, the index of each Blender equals the value of the first
69// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
70static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070071 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
72 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
73 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
74 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
75 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
76 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
77 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
78 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
81 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
82 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050084 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070085 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070086};
Romain Guye4d01122010-06-16 18:44:05 -070087
Romain Guy87a76572010-09-13 18:11:21 -070088// This array contains the swapped version of each SkXfermode. For instance
89// this array's SrcOver blending mode is actually DstOver. You can refer to
90// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070091static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070092 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
93 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
94 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
95 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
96 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
97 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
98 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
99 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
100 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
102 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
103 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
104 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500105 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700106 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700107};
108
Romain Guyf6a11b82010-06-23 17:47:49 -0700109///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700110// Functions
111///////////////////////////////////////////////////////////////////////////////
112
113template<typename T>
114static inline T min(T a, T b) {
115 return a < b ? a : b;
116}
117
118///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700119// Constructors/destructor
120///////////////////////////////////////////////////////////////////////////////
121
Romain Guy3bbacf22013-02-06 16:51:04 -0800122OpenGLRenderer::OpenGLRenderer():
123 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800124 // *set* draw modifiers to be 0
125 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700126 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700127
Romain Guyac670c02010-07-27 17:39:27 -0700128 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
129
Romain Guyae5575b2010-07-29 18:48:04 -0700130 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700131 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700132 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700133
134 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700135}
136
Romain Guy85bf02f2010-06-22 13:11:24 -0700137OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700138 // The context has already been destroyed at this point, do not call
139 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700140}
141
Romain Guy87e2f7572012-09-24 11:37:12 -0700142void OpenGLRenderer::initProperties() {
143 char property[PROPERTY_VALUE_MAX];
144 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
145 mScissorOptimizationDisabled = !strcasecmp(property, "true");
146 INIT_LOGD(" Scissor optimization %s",
147 mScissorOptimizationDisabled ? "disabled" : "enabled");
148 } else {
149 INIT_LOGD(" Scissor optimization enabled");
150 }
Romain Guy13631f32012-01-30 17:41:55 -0800151}
152
153///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700154// Setup
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guyef359272013-01-31 19:07:29 -0800157void OpenGLRenderer::setName(const char* name) {
158 if (name) {
159 mName.setTo(name);
160 } else {
161 mName.clear();
162 }
163}
164
165const char* OpenGLRenderer::getName() const {
166 return mName.string();
167}
168
Romain Guy49c5fc02012-05-15 11:10:01 -0700169bool OpenGLRenderer::isDeferred() {
170 return false;
171}
172
Romain Guy85bf02f2010-06-22 13:11:24 -0700173void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700174 initViewport(width, height);
175
176 glDisable(GL_DITHER);
177 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
178
179 glEnableVertexAttribArray(Program::kBindingPosition);
180}
181
182void OpenGLRenderer::initViewport(int width, int height) {
Chris Craikf57776b2013-10-25 18:30:17 -0700183 float dist = std::max(width, height) * 1.5;
184
185 if (DEBUG_ENABLE_3D) {
186 // TODO: make view proj app configurable
187 Matrix4 projection;
188 projection.loadFrustum(-width / 2, -height / 2, width / 2, height / 2, dist, 0);
189 Matrix4 view;
190 view.loadLookAt(0, 0, dist,
191 0, 0, 0,
192 0, 1, 0);
193 mViewProjMatrix.loadMultiply(projection, view);
194 mViewProjMatrix.translate(-width/2, -height/2);
195 } else {
196 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
197 }
Romain Guybb9524b2010-06-22 18:56:38 -0700198
199 mWidth = width;
200 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700201
202 mFirstSnapshot->height = height;
203 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700204}
205
Romain Guy96885eb2013-03-26 15:05:58 -0700206void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800207 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800208 mCaches.clearGarbage();
209
Romain Guy96885eb2013-03-26 15:05:58 -0700210 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700211 mSnapshot = new Snapshot(mFirstSnapshot,
212 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800213 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700214 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700215
Romain Guy7d7b5492011-01-24 16:33:45 -0800216 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700217 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700218}
219
220status_t OpenGLRenderer::startFrame() {
221 if (mFrameStarted) return DrawGlInfo::kStatusDone;
222 mFrameStarted = true;
223
Romain Guy41308e22012-10-22 20:02:43 -0700224 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700225
Romain Guy96885eb2013-03-26 15:05:58 -0700226 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700227
Romain Guy96885eb2013-03-26 15:05:58 -0700228 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800229
Romain Guy54c1a642012-09-27 17:55:46 -0700230 // Functors break the tiling extension in pretty spectacular ways
231 // This ensures we don't use tiling when a functor is going to be
232 // invoked during the frame
233 mSuppressTiling = mCaches.hasRegisteredFunctors();
234
Chris Craik5f803622013-03-21 14:39:04 -0700235 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700236
Romain Guy7c450aa2012-09-21 19:15:00 -0700237 debugOverdraw(true, true);
238
Romain Guy96885eb2013-03-26 15:05:58 -0700239 return clear(mTilingClip.left, mTilingClip.top,
240 mTilingClip.right, mTilingClip.bottom, mOpaque);
241}
242
243status_t OpenGLRenderer::prepare(bool opaque) {
244 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
245}
246
247status_t OpenGLRenderer::prepareDirty(float left, float top,
248 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700249
Romain Guy96885eb2013-03-26 15:05:58 -0700250 setupFrameState(left, top, right, bottom, opaque);
251
252 // Layer renderers will start the frame immediately
253 // The framebuffer renderer will first defer the display list
254 // for each layer and wait until the first drawing command
255 // to start the frame
256 if (mSnapshot->fbo == 0) {
257 syncState();
258 updateLayers();
259 } else {
260 return startFrame();
261 }
262
263 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700264}
265
Romain Guydcfc8362013-01-03 13:08:57 -0800266void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
267 // If we know that we are going to redraw the entire framebuffer,
268 // perform a discard to let the driver know we don't need to preserve
269 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800270 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800271 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800272 const bool isFbo = getTargetFbo() == 0;
273 const GLenum attachments[] = {
274 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
275 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800276 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
277 }
278}
279
Romain Guy7c25aab2012-10-18 15:05:02 -0700280status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700281 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700282 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700283 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700284 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700285 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700286 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700287
Romain Guy7c25aab2012-10-18 15:05:02 -0700288 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700289 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700290}
291
292void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700293 if (mCaches.blend) {
294 glEnable(GL_BLEND);
295 } else {
296 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700297 }
Romain Guybb9524b2010-06-22 18:56:38 -0700298}
299
Romain Guy57b52682012-09-20 17:38:46 -0700300void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700301 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700302 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800303 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700304 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700305 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700306
Romain Guyc3fedaf2013-01-29 17:26:25 -0800307 startTiling(*clip, s->height, opaque);
308 }
309}
310
311void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
312 if (!mSuppressTiling) {
313 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800314 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700315 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700316}
317
318void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700319 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700320}
321
Romain Guyb025b9c2010-09-16 14:16:48 -0700322void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700323 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700324 endTiling();
325
Romain Guyca89e2a2013-03-08 17:44:20 -0800326 // When finish() is invoked on FBO 0 we've reached the end
327 // of the current frame
328 if (getTargetFbo() == 0) {
329 mCaches.pathCache.trim();
330 }
331
Romain Guy11cb6422012-09-21 00:39:43 -0700332 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700333#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700334 GLenum status = GL_NO_ERROR;
335 while ((status = glGetError()) != GL_NO_ERROR) {
336 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
337 switch (status) {
338 case GL_INVALID_ENUM:
339 ALOGE(" GL_INVALID_ENUM");
340 break;
341 case GL_INVALID_VALUE:
342 ALOGE(" GL_INVALID_VALUE");
343 break;
344 case GL_INVALID_OPERATION:
345 ALOGE(" GL_INVALID_OPERATION");
346 break;
347 case GL_OUT_OF_MEMORY:
348 ALOGE(" Out of memory!");
349 break;
350 }
Romain Guya07105b2011-01-10 21:14:18 -0800351 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700352#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700353
Romain Guyc15008e2010-11-10 11:59:15 -0800354#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800355 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700356#else
357 if (mCaches.getDebugLevel() & kDebugMemory) {
358 mCaches.dumpMemoryUsage();
359 }
Romain Guyc15008e2010-11-10 11:59:15 -0800360#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700361 }
Romain Guy96885eb2013-03-26 15:05:58 -0700362
Romain Guy78dd96d2013-05-03 14:24:16 -0700363 if (mCountOverdraw) {
364 countOverdraw();
365 }
366
Romain Guy96885eb2013-03-26 15:05:58 -0700367 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700368}
369
Romain Guy6c319ca2011-01-11 14:29:25 -0800370void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700371 if (mCaches.currentProgram) {
372 if (mCaches.currentProgram->isInUse()) {
373 mCaches.currentProgram->remove();
374 mCaches.currentProgram = NULL;
375 }
376 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700377 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700378 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800379 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800380 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800381 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700382 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700383}
384
Romain Guy6c319ca2011-01-11 14:29:25 -0800385void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800386 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800387 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700388 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700389 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700390
Romain Guy3e263fa2011-12-12 16:47:48 -0800391 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
392
Chet Haase80250612012-08-15 13:46:54 -0700393 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700394 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800395 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700396 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700397
Romain Guya1d3c912011-12-13 14:55:06 -0800398 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700399 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700400
Romain Guy50c0f092010-10-19 11:42:22 -0700401 mCaches.blend = true;
402 glEnable(GL_BLEND);
403 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
404 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700405}
406
Romain Guy35643dd2012-09-18 15:40:58 -0700407void OpenGLRenderer::resumeAfterLayer() {
408 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
409 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
410 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700411 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700412
413 mCaches.resetScissor();
414 dirtyClip();
415}
416
Romain Guyba6be8a2012-04-23 18:22:09 -0700417void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700418 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700419}
420
421void OpenGLRenderer::attachFunctor(Functor* functor) {
422 mFunctors.add(functor);
423}
424
Romain Guy8f3b8e32012-03-27 16:33:45 -0700425status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
426 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700427 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700428
Romain Guyba6be8a2012-04-23 18:22:09 -0700429 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800430 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700431 SortedVector<Functor*> functors(mFunctors);
432 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700433
Romain Guyba6be8a2012-04-23 18:22:09 -0700434 DrawGlInfo info;
435 info.clipLeft = 0;
436 info.clipTop = 0;
437 info.clipRight = 0;
438 info.clipBottom = 0;
439 info.isLayer = false;
440 info.width = 0;
441 info.height = 0;
442 memset(info.transform, 0, sizeof(float) * 16);
443
444 for (size_t i = 0; i < count; i++) {
445 Functor* f = functors.itemAt(i);
446 result |= (*f)(DrawGlInfo::kModeProcess, &info);
447
Chris Craikc2c95432012-04-25 15:13:52 -0700448 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700449 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
450 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700451 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700452
Chris Craikc2c95432012-04-25 15:13:52 -0700453 if (result & DrawGlInfo::kStatusInvoke) {
454 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700455 }
456 }
Chris Craikd15321b2012-11-28 14:45:04 -0800457 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700458 }
459
460 return result;
461}
462
463status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700464 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
465
Chris Craik932b7f62012-06-06 13:59:33 -0700466 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700467
Romain Guyd643bb52011-03-01 14:55:21 -0800468
Romain Guy80911b82011-03-16 15:30:12 -0700469 Rect clip(*mSnapshot->clipRect);
470 clip.snapToPixelBoundaries();
471
Romain Guyd643bb52011-03-01 14:55:21 -0800472 // Since we don't know what the functor will draw, let's dirty
473 // tne entire clip region
474 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800475 dirtyLayerUnchecked(clip, getRegion());
476 }
Romain Guyd643bb52011-03-01 14:55:21 -0800477
Romain Guy08aa2cb2011-03-17 11:06:57 -0700478 DrawGlInfo info;
479 info.clipLeft = clip.left;
480 info.clipTop = clip.top;
481 info.clipRight = clip.right;
482 info.clipBottom = clip.bottom;
483 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700484 info.width = getSnapshot()->viewport.getWidth();
485 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700486 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700487
John Reck25d2f7b2013-09-10 13:12:09 -0700488 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700489 // setup GL state for functor
490 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700491 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
492 }
John Reck25d2f7b2013-09-10 13:12:09 -0700493 if (mCaches.enableScissor() || dirtyClip) {
494 setScissorFromClip();
495 }
Chris Craik54f574a2013-08-26 11:23:46 -0700496 interrupt();
497
498 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700499 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800500
Romain Guy8f3b8e32012-03-27 16:33:45 -0700501 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700502 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800503 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700504
Chris Craik65924a32012-04-05 17:52:11 -0700505 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700506 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700507 }
Romain Guycabfcc12011-03-07 18:06:46 -0800508 }
509
Chet Haasedaf98e92011-01-10 14:10:36 -0800510 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700511 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800512}
513
Romain Guyf6a11b82010-06-23 17:47:49 -0700514///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700515// Debug
516///////////////////////////////////////////////////////////////////////////////
517
Romain Guy0f6675332013-03-01 14:31:04 -0800518void OpenGLRenderer::eventMark(const char* name) const {
519 mCaches.eventMark(0, name);
520}
521
Romain Guy87e2f7572012-09-24 11:37:12 -0700522void OpenGLRenderer::startMark(const char* name) const {
523 mCaches.startMark(0, name);
524}
525
526void OpenGLRenderer::endMark() const {
527 mCaches.endMark();
528}
529
530void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
531 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
532 if (clear) {
533 mCaches.disableScissor();
534 mCaches.stencil.clear();
535 }
536 if (enable) {
537 mCaches.stencil.enableDebugWrite();
538 } else {
539 mCaches.stencil.disable();
540 }
541 }
542}
543
544void OpenGLRenderer::renderOverdraw() {
545 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700546 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700547
548 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700549 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700550 clip->right - clip->left, clip->bottom - clip->top);
551
Romain Guy627c6fd2013-08-21 11:53:18 -0700552 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700553 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700554 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
555
556 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700557 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700558 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
559
560 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700561 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700562 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
563
564 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700565 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700566 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
567
Romain Guy87e2f7572012-09-24 11:37:12 -0700568 mCaches.stencil.disable();
569 }
570}
571
Romain Guy78dd96d2013-05-03 14:24:16 -0700572void OpenGLRenderer::countOverdraw() {
573 size_t count = mWidth * mHeight;
574 uint32_t* buffer = new uint32_t[count];
575 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
576
577 size_t total = 0;
578 for (size_t i = 0; i < count; i++) {
579 total += buffer[i] & 0xff;
580 }
581
582 mOverdraw = total / float(count);
583
584 delete[] buffer;
585}
586
Romain Guy87e2f7572012-09-24 11:37:12 -0700587///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700588// Layers
589///////////////////////////////////////////////////////////////////////////////
590
591bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700592 if (layer->deferredUpdateScheduled && layer->renderer &&
593 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700594 ATRACE_CALL();
595
Romain Guy11cb6422012-09-21 00:39:43 -0700596 Rect& dirty = layer->dirtyRect;
597
Romain Guy7c450aa2012-09-21 19:15:00 -0700598 if (inFrame) {
599 endTiling();
600 debugOverdraw(false, false);
601 }
Romain Guy11cb6422012-09-21 00:39:43 -0700602
Romain Guy96885eb2013-03-26 15:05:58 -0700603 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700604 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700605 } else {
606 layer->defer();
607 }
Romain Guy11cb6422012-09-21 00:39:43 -0700608
609 if (inFrame) {
610 resumeAfterLayer();
611 startTiling(mSnapshot);
612 }
613
Romain Guy5bb3c732012-11-29 17:52:58 -0800614 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700615 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700616
617 return true;
618 }
619
620 return false;
621}
622
623void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700624 // If draw deferring is enabled this method will simply defer
625 // the display list of each individual layer. The layers remain
626 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700627 int count = mLayerUpdates.size();
628 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700629 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
630 startMark("Layer Updates");
631 } else {
632 startMark("Defer Layer Updates");
633 }
Romain Guy11cb6422012-09-21 00:39:43 -0700634
Chris Craik1206b9b2013-04-04 14:46:24 -0700635 // Note: it is very important to update the layers in order
636 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700637 Layer* layer = mLayerUpdates.itemAt(i);
638 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700639 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
640 mCaches.resourceCache.decrementRefcount(layer);
641 }
Romain Guy11cb6422012-09-21 00:39:43 -0700642 }
Romain Guy11cb6422012-09-21 00:39:43 -0700643
Romain Guy96885eb2013-03-26 15:05:58 -0700644 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
645 mLayerUpdates.clear();
646 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
647 }
648 endMark();
649 }
650}
651
652void OpenGLRenderer::flushLayers() {
653 int count = mLayerUpdates.size();
654 if (count > 0) {
655 startMark("Apply Layer Updates");
656 char layerName[12];
657
Chris Craik1206b9b2013-04-04 14:46:24 -0700658 // Note: it is very important to update the layers in order
659 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700660 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700661 startMark(layerName);
662
Romain Guy40543602013-06-12 15:31:28 -0700663 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700664 Layer* layer = mLayerUpdates.itemAt(i);
665 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700666 ATRACE_END();
667
Romain Guy02b49b72013-03-29 12:37:16 -0700668 mCaches.resourceCache.decrementRefcount(layer);
669
Romain Guy96885eb2013-03-26 15:05:58 -0700670 endMark();
671 }
672
673 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700674 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700675
Romain Guy11cb6422012-09-21 00:39:43 -0700676 endMark();
677 }
678}
679
680void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
681 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700682 // Make sure we don't introduce duplicates.
683 // SortedVector would do this automatically but we need to respect
684 // the insertion order. The linear search is not an issue since
685 // this list is usually very short (typically one item, at most a few)
686 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
687 if (mLayerUpdates.itemAt(i) == layer) {
688 return;
689 }
690 }
Romain Guy11cb6422012-09-21 00:39:43 -0700691 mLayerUpdates.push_back(layer);
692 mCaches.resourceCache.incrementRefcount(layer);
693 }
694}
695
Romain Guye93482f2013-06-17 13:14:51 -0700696void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
697 if (layer) {
698 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
699 if (mLayerUpdates.itemAt(i) == layer) {
700 mLayerUpdates.removeAt(i);
701 mCaches.resourceCache.decrementRefcount(layer);
702 break;
703 }
704 }
705 }
706}
707
Romain Guy11cb6422012-09-21 00:39:43 -0700708void OpenGLRenderer::clearLayerUpdates() {
709 size_t count = mLayerUpdates.size();
710 if (count > 0) {
711 mCaches.resourceCache.lock();
712 for (size_t i = 0; i < count; i++) {
713 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
714 }
715 mCaches.resourceCache.unlock();
716 mLayerUpdates.clear();
717 }
718}
719
Romain Guy40543602013-06-12 15:31:28 -0700720void OpenGLRenderer::flushLayerUpdates() {
721 syncState();
722 updateLayers();
723 flushLayers();
724 // Wait for all the layer updates to be executed
725 AutoFence fence;
726}
727
Romain Guy11cb6422012-09-21 00:39:43 -0700728///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700729// State management
730///////////////////////////////////////////////////////////////////////////////
731
Romain Guybb9524b2010-06-22 18:56:38 -0700732int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700733 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700734}
735
736int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700737 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
740void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700741 if (mSaveCount > 1) {
742 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700743 }
Romain Guybb9524b2010-06-22 18:56:38 -0700744}
745
746void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700747 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700748
Romain Guy8fb95422010-08-17 18:38:51 -0700749 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700750 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700751 }
Romain Guybb9524b2010-06-22 18:56:38 -0700752}
753
Romain Guy8aef54f2010-09-01 15:13:49 -0700754int OpenGLRenderer::saveSnapshot(int flags) {
755 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700756 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700757}
758
759bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700760 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700761 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700762 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700763
Romain Guybd6b79b2010-06-26 00:13:53 -0700764 sp<Snapshot> current = mSnapshot;
765 sp<Snapshot> previous = mSnapshot->previous;
766
Romain Guyeb993562010-10-05 18:14:38 -0700767 if (restoreOrtho) {
768 Rect& r = previous->viewport;
769 glViewport(r.left, r.top, r.right, r.bottom);
Chris Craikf57776b2013-10-25 18:30:17 -0700770 mViewProjMatrix.load(current->orthoMatrix);
Romain Guyeb993562010-10-05 18:14:38 -0700771 }
772
Romain Guy8b55f372010-08-18 17:10:07 -0700773 mSaveCount--;
774 mSnapshot = previous;
775
Romain Guy2542d192010-08-18 11:47:12 -0700776 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700777 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700778 }
Romain Guy2542d192010-08-18 11:47:12 -0700779
Romain Guy5ec99242010-11-03 16:19:08 -0700780 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700781 endMark(); // Savelayer
782 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700783 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700784 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700785 }
786
Romain Guy2542d192010-08-18 11:47:12 -0700787 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700788}
789
Romain Guyf6a11b82010-06-23 17:47:49 -0700790///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700791// Layers
792///////////////////////////////////////////////////////////////////////////////
793
794int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800795 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700796 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700797
Romain Guyaf636eb2010-12-09 17:47:21 -0800798 if (!mSnapshot->isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700799 createLayer(left, top, right, bottom, alpha, mode, flags);
Romain Guydbc26d22010-10-11 17:58:29 -0700800 }
Romain Guyd55a8612010-06-28 17:42:46 -0700801
802 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700803}
804
Chris Craikd90144d2013-03-19 15:03:48 -0700805void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
806 const Rect untransformedBounds(bounds);
807
808 currentTransform().mapRect(bounds);
809
810 // Layers only make sense if they are in the framebuffer's bounds
811 if (bounds.intersect(*mSnapshot->clipRect)) {
812 // We cannot work with sub-pixels in this case
813 bounds.snapToPixelBoundaries();
814
815 // When the layer is not an FBO, we may use glCopyTexImage so we
816 // need to make sure the layer does not extend outside the bounds
817 // of the framebuffer
818 if (!bounds.intersect(mSnapshot->previous->viewport)) {
819 bounds.setEmpty();
820 } else if (fboLayer) {
821 clip.set(bounds);
822 mat4 inverse;
823 inverse.loadInverse(currentTransform());
824 inverse.mapRect(clip);
825 clip.snapToPixelBoundaries();
826 if (clip.intersect(untransformedBounds)) {
827 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
828 bounds.set(untransformedBounds);
829 } else {
830 clip.setEmpty();
831 }
832 }
833 } else {
834 bounds.setEmpty();
835 }
836}
837
Chris Craik408eb122013-03-26 18:55:15 -0700838void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
839 bool fboLayer, int alpha) {
840 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
841 bounds.getHeight() > mCaches.maxTextureSize ||
842 (fboLayer && clip.isEmpty())) {
843 mSnapshot->empty = fboLayer;
844 } else {
845 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
846 }
847}
848
Chris Craikd90144d2013-03-19 15:03:48 -0700849int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
850 int alpha, SkXfermode::Mode mode, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700851 const int count = saveSnapshot(flags);
852
853 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
854 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
855 // operations will be able to store and restore the current clip and transform info, and
856 // quick rejection will be correct (for display lists)
857
858 Rect bounds(left, top, right, bottom);
859 Rect clip;
860 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700861 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700862
Chris Craik408eb122013-03-26 18:55:15 -0700863 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700864 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
865 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700866 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700867 }
868 }
869
870 return count;
871}
872
873
Romain Guy1c740bc2010-09-13 18:00:09 -0700874/**
875 * Layers are viewed by Skia are slightly different than layers in image editing
876 * programs (for instance.) When a layer is created, previously created layers
877 * and the frame buffer still receive every drawing command. For instance, if a
878 * layer is created and a shape intersecting the bounds of the layers and the
879 * framebuffer is draw, the shape will be drawn on both (unless the layer was
880 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
881 *
882 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
883 * texture. Unfortunately, this is inefficient as it requires every primitive to
884 * be drawn n + 1 times, where n is the number of active layers. In practice this
885 * means, for every primitive:
886 * - Switch active frame buffer
887 * - Change viewport, clip and projection matrix
888 * - Issue the drawing
889 *
890 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700891 * To avoid this, layers are implemented in a different way here, at least in the
892 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
893 * is set. When this flag is set we can redirect all drawing operations into a
894 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700895 *
896 * This implementation relies on the frame buffer being at least RGBA 8888. When
897 * a layer is created, only a texture is created, not an FBO. The content of the
898 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700899 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700900 * buffer and drawing continues as normal. This technique therefore treats the
901 * frame buffer as a scratch buffer for the layers.
902 *
903 * To compose the layers back onto the frame buffer, each layer texture
904 * (containing the original frame buffer data) is drawn as a simple quad over
905 * the frame buffer. The trick is that the quad is set as the composition
906 * destination in the blending equation, and the frame buffer becomes the source
907 * of the composition.
908 *
909 * Drawing layers with an alpha value requires an extra step before composition.
910 * An empty quad is drawn over the layer's region in the frame buffer. This quad
911 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
912 * quad is used to multiply the colors in the frame buffer. This is achieved by
913 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
914 * GL_ZERO, GL_SRC_ALPHA.
915 *
916 * Because glCopyTexImage2D() can be slow, an alternative implementation might
917 * be use to draw a single clipped layer. The implementation described above
918 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700919 *
920 * (1) The frame buffer is actually not cleared right away. To allow the GPU
921 * to potentially optimize series of calls to glCopyTexImage2D, the frame
922 * buffer is left untouched until the first drawing operation. Only when
923 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700924 */
Chet Haased48885a2012-08-28 17:43:28 -0700925bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700926 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700927 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700928 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700929
Romain Guyeb993562010-10-05 18:14:38 -0700930 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
931
Romain Guyf607bdc2010-09-10 19:20:06 -0700932 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700933 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700934 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700935 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700936 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700937
938 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700939 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700940 return false;
941 }
Romain Guyf18fd992010-07-08 11:45:51 -0700942
Romain Guya1d3c912011-12-13 14:55:06 -0800943 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700944 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700945 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700946 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700947 }
948
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700950 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700951 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
952 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800953 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700954 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700955 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700956
Romain Guy8fb95422010-08-17 18:38:51 -0700957 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700958 mSnapshot->flags |= Snapshot::kFlagIsLayer;
959 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700960
Chris Craik7273daa2013-03-28 11:25:24 -0700961 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700962 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700963 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700964 } else {
965 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700966 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800967 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700968 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700969 // Workaround for some GL drivers. When reading pixels lying outside
970 // of the window we should get undefined values for those pixels.
971 // Unfortunately some drivers will turn the entire target texture black
972 // when reading outside of the window.
973 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
974 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700975 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800976 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700977
Romain Guyb254c242013-06-27 17:15:24 -0700978 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
979 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
980
Romain Guy54be1cd2011-06-13 19:04:27 -0700981 // Enqueue the buffer coordinates to clear the corresponding region later
982 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700983 }
Romain Guyeb993562010-10-05 18:14:38 -0700984 }
Romain Guyf86ef572010-07-01 11:05:42 -0700985
Romain Guyd55a8612010-06-28 17:42:46 -0700986 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700987}
988
Chris Craike63f7c622013-10-17 10:30:55 -0700989bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800990 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700991 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700992
Chet Haased48885a2012-08-28 17:43:28 -0700993 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800994 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
995 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700996 mSnapshot->fbo = layer->getFbo();
997 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
998 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
999 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
1000 mSnapshot->height = bounds.getHeight();
Chris Craikf57776b2013-10-25 18:30:17 -07001001 mSnapshot->orthoMatrix.load(mViewProjMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -07001002
Romain Guy2b7028e2012-09-19 17:25:38 -07001003 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -07001004 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -07001006 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
1007 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -07001008
1009 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -07001011 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -07001012 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -07001013 }
1014
1015 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001016 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001017
Romain Guyf735c8e2013-01-31 17:45:55 -08001018 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001019
1020 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001021 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001022 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001023 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001024 glClear(GL_COLOR_BUFFER_BIT);
1025
1026 dirtyClip();
1027
1028 // Change the ortho projection
1029 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craikf57776b2013-10-25 18:30:17 -07001030
1031 // TODO: determine best way to support 3d drawing within HW layers
1032 mViewProjMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001033
1034 return true;
1035}
1036
Romain Guy1c740bc2010-09-13 18:00:09 -07001037/**
1038 * Read the documentation of createLayer() before doing anything in this method.
1039 */
Romain Guy1d83e192010-08-17 11:37:00 -07001040void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1041 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001042 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001043 return;
1044 }
1045
Romain Guy8ce00302013-01-15 18:51:42 -08001046 Layer* layer = current->layer;
1047 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001048 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001049
Chris Craik39a908c2013-06-13 14:39:01 -07001050 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001051 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
1052 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -07001053 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1054
Romain Guyeb993562010-10-05 18:14:38 -07001055 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001056 endTiling();
1057
Romain Guye0aa84b2012-04-03 19:30:26 -07001058 // Detach the texture from the FBO
1059 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001060
1061 layer->removeFbo(false);
1062
Romain Guyeb993562010-10-05 18:14:38 -07001063 // Unbind current FBO and restore previous one
1064 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001065 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001066
1067 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001068 }
1069
Romain Guy9ace8f52011-07-07 20:50:11 -07001070 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001071 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001072 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001073 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001074 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001075 }
1076
Romain Guy03750a02010-10-18 14:06:08 -07001077 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001078
Romain Guya1d3c912011-12-13 14:55:06 -08001079 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001080
Romain Guy5b3b3522010-10-27 18:57:51 -07001081 // When the layer is stored in an FBO, we can save a bit of fillrate by
1082 // drawing only the dirty region
1083 if (fboLayer) {
1084 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001085 if (layer->getColorFilter()) {
1086 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001087 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001088 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001089 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001090 resetColorFilter();
1091 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001092 } else if (!rect.isEmpty()) {
1093 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301094
1095 save(0);
1096 // the layer contains screen buffer content that shouldn't be alpha modulated
1097 // (and any necessary alpha modulation was handled drawing into the layer)
1098 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001099 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301100 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001101 }
Romain Guy8b55f372010-08-18 17:10:07 -07001102
Romain Guy746b7402010-10-26 16:27:31 -07001103 dirtyClip();
1104
Romain Guyeb993562010-10-05 18:14:38 -07001105 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001106 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001107 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001108 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001109 }
1110}
1111
Romain Guyaa6c24c2011-04-28 18:40:04 -07001112void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001113 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001114
1115 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001116 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001117 setupDrawWithTexture();
1118 } else {
1119 setupDrawWithExternalTexture();
1120 }
1121 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001122 setupDrawColor(alpha, alpha, alpha, alpha);
1123 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001124 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001125 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001126 setupDrawPureColorUniforms();
1127 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001128 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1129 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001130 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001131 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001132 }
Romain Guy3b753822013-03-05 10:27:35 -08001133 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001134 layer->getWidth() == (uint32_t) rect.getWidth() &&
1135 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001136 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1137 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001138
Romain Guyd21b6e12011-11-30 20:21:23 -08001139 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001140 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1141 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001142 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001143 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001144 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1145 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001146 }
1147 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001148 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001149
1150 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001151}
1152
Romain Guy5b3b3522010-10-27 18:57:51 -07001153void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001154 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001155 const Rect& texCoords = layer->texCoords;
1156 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1157 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001158
Romain Guy9ace8f52011-07-07 20:50:11 -07001159 float x = rect.left;
1160 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001161 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001162 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001163 layer->getHeight() == (uint32_t) rect.getHeight();
1164
1165 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001166 // When we're swapping, the layer is already in screen coordinates
1167 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001168 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1169 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001170 }
1171
Romain Guyd21b6e12011-11-30 20:21:23 -08001172 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001173 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001174 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001175 }
1176
Chris Craik16ecda52013-03-29 10:59:59 -07001177 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001178 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001179 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001180 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001181 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001182 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001183
Romain Guyaa6c24c2011-04-28 18:40:04 -07001184 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1185 } else {
1186 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1187 drawTextureLayer(layer, rect);
1188 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1189 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001190}
1191
Chris Craik34416ea2013-04-15 16:08:28 -07001192/**
1193 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1194 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1195 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1196 * by saveLayer's restore
1197 */
1198#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1199 DRAW_COMMAND; \
1200 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1201 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1202 DRAW_COMMAND; \
1203 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1204 } \
1205 }
1206
1207#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1208
Romain Guy5b3b3522010-10-27 18:57:51 -07001209void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001210 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001211 layer->setRegionAsRect();
1212
Chris Craik34416ea2013-04-15 16:08:28 -07001213 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001214
Romain Guy5b3b3522010-10-27 18:57:51 -07001215 layer->region.clear();
1216 return;
1217 }
1218
Romain Guy211370f2012-02-01 16:10:55 -08001219 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001220 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001221 const android::Rect* rects;
1222 Region safeRegion;
1223 if (CC_LIKELY(hasRectToRectTransform())) {
1224 rects = layer->region.getArray(&count);
1225 } else {
1226 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1227 rects = safeRegion.getArray(&count);
1228 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001229
Chris Craik16ecda52013-03-29 10:59:59 -07001230 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001231 const float texX = 1.0f / float(layer->getWidth());
1232 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001233 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001234
Romain Guy8ce00302013-01-15 18:51:42 -08001235 setupDraw();
1236
1237 // We must get (and therefore bind) the region mesh buffer
1238 // after we setup drawing in case we need to mess with the
1239 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001240 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001241 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001242
Romain Guy7230a742011-01-10 22:26:16 -08001243 setupDrawWithTexture();
1244 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001245 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001246 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001247 setupDrawProgram();
1248 setupDrawDirtyRegionsDisabled();
1249 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001250 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001251 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001252 if (currentTransform().isPureTranslate()) {
1253 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1254 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001255
Romain Guyd21b6e12011-11-30 20:21:23 -08001256 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001257 setupDrawModelView(kModelViewMode_Translate, false,
1258 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001259 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001260 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001261 setupDrawModelView(kModelViewMode_Translate, false,
1262 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001263 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001264 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001265
1266 for (size_t i = 0; i < count; i++) {
1267 const android::Rect* r = &rects[i];
1268
1269 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001270 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001271 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001272 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001273
1274 // TODO: Reject quads outside of the clip
1275 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1276 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1277 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1278 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1279
1280 numQuads++;
1281
Romain Guy31e08e92013-06-18 15:53:53 -07001282 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001283 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1284 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001285 numQuads = 0;
1286 mesh = mCaches.getRegionMesh();
1287 }
1288 }
1289
1290 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001291 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1292 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001293 }
1294
Romain Guy5b3b3522010-10-27 18:57:51 -07001295#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001296 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001297#endif
1298
1299 layer->region.clear();
1300 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001301}
1302
Romain Guy3a3133d2011-02-01 22:59:58 -08001303#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001304void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001305 size_t count;
1306 const android::Rect* rects = region.getArray(&count);
1307
1308 uint32_t colors[] = {
1309 0x7fff0000, 0x7f00ff00,
1310 0x7f0000ff, 0x7fff00ff,
1311 };
1312
1313 int offset = 0;
1314 int32_t top = rects[0].top;
1315
1316 for (size_t i = 0; i < count; i++) {
1317 if (top != rects[i].top) {
1318 offset ^= 0x2;
1319 top = rects[i].top;
1320 }
1321
1322 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1323 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1324 SkXfermode::kSrcOver_Mode);
1325 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001326}
Chris Craike63f7c622013-10-17 10:30:55 -07001327#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001328
Romain Guy8ce00302013-01-15 18:51:42 -08001329void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1330 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001331 Vector<float> rects;
1332
1333 SkRegion::Iterator it(region);
1334 while (!it.done()) {
1335 const SkIRect& r = it.rect();
1336 rects.push(r.fLeft);
1337 rects.push(r.fTop);
1338 rects.push(r.fRight);
1339 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001340 it.next();
1341 }
1342
Romain Guy448455f2013-07-22 13:57:50 -07001343 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001344}
1345
Romain Guy5b3b3522010-10-27 18:57:51 -07001346void OpenGLRenderer::dirtyLayer(const float left, const float top,
1347 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001348 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001349 Rect bounds(left, top, right, bottom);
1350 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001351 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001352 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001353}
1354
1355void OpenGLRenderer::dirtyLayer(const float left, const float top,
1356 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001357 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001358 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001359 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001360 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001361}
1362
1363void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001364 if (bounds.intersect(*mSnapshot->clipRect)) {
1365 bounds.snapToPixelBoundaries();
1366 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1367 if (!dirty.isEmpty()) {
1368 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001369 }
1370 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001371}
1372
Chris Craik4063a0e2013-11-15 16:06:56 -08001373void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001374 GLsizei elementsCount = quadsCount * 6;
1375 while (elementsCount > 0) {
1376 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1377
Romain Guy3380cfd2013-08-15 16:57:57 -07001378 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001379 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1380
1381 elementsCount -= drawCount;
1382 // Though there are 4 vertices in a quad, we use 6 indices per
1383 // quad to draw with GL_TRIANGLES
1384 mesh += (drawCount / 6) * 4;
1385 }
1386}
1387
Romain Guy54be1cd2011-06-13 19:04:27 -07001388void OpenGLRenderer::clearLayerRegions() {
1389 const size_t count = mLayers.size();
1390 if (count == 0) return;
1391
1392 if (!mSnapshot->isIgnored()) {
1393 // Doing several glScissor/glClear here can negatively impact
1394 // GPUs with a tiler architecture, instead we draw quads with
1395 // the Clear blending mode
1396
1397 // The list contains bounds that have already been clipped
1398 // against their initial clip rect, and the current clip
1399 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001400 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001401
Romain Guy448455f2013-07-22 13:57:50 -07001402 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001403 Vertex* vertex = mesh;
1404
1405 for (uint32_t i = 0; i < count; i++) {
1406 Rect* bounds = mLayers.itemAt(i);
1407
Romain Guy54be1cd2011-06-13 19:04:27 -07001408 Vertex::set(vertex++, bounds->left, bounds->top);
1409 Vertex::set(vertex++, bounds->right, bounds->top);
1410 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001411 Vertex::set(vertex++, bounds->right, bounds->bottom);
1412
1413 delete bounds;
1414 }
Romain Guye67307c2013-02-11 18:01:20 -08001415 // We must clear the list of dirty rects before we
1416 // call setupDraw() to prevent stencil setup to do
1417 // the same thing again
1418 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001419
1420 setupDraw(false);
1421 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1422 setupDrawBlending(true, SkXfermode::kClear_Mode);
1423 setupDrawProgram();
1424 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001425 setupDrawModelView(kModelViewMode_Translate, false,
1426 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001427
Chris Craik4063a0e2013-11-15 16:06:56 -08001428 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001429
1430 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001431 } else {
1432 for (uint32_t i = 0; i < count; i++) {
1433 delete mLayers.itemAt(i);
1434 }
Romain Guye67307c2013-02-11 18:01:20 -08001435 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001436 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001437}
1438
Romain Guybd6b79b2010-06-26 00:13:53 -07001439///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001440// State Deferral
1441///////////////////////////////////////////////////////////////////////////////
1442
Chris Craikff785832013-03-08 13:12:16 -08001443bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001444 const Rect& currentClip = *(mSnapshot->clipRect);
1445 const mat4& currentMatrix = *(mSnapshot->transform);
1446
Chris Craikff785832013-03-08 13:12:16 -08001447 if (stateDeferFlags & kStateDeferFlag_Draw) {
1448 // state has bounds initialized in local coordinates
1449 if (!state.mBounds.isEmpty()) {
1450 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001451 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001452 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1453 // is used, it should more closely duplicate the quickReject logic (in how it uses
1454 // snapToPixelBoundaries)
1455
Chris Craik28ce94a2013-05-31 11:38:03 -07001456 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001457 // quick rejected
1458 return true;
1459 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001460
Chris Craika02c4ed2013-06-14 13:43:58 -07001461 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001462 if (!currentClip.contains(state.mBounds)) {
1463 int& flags = state.mClipSideFlags;
1464 // op partially clipped, so record which sides are clipped for clip-aware merging
1465 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1466 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1467 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1468 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1469 }
1470 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001471 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001472 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1473 // overdraw avoidance (since we don't know what it overlaps)
1474 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001475 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001476 }
1477 }
1478
Chris Craik527a3aa2013-03-04 10:19:31 -08001479 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1480 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001481 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001482 }
1483
Chris Craik7273daa2013-03-28 11:25:24 -07001484 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1485 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001486 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001487 state.mDrawModifiers = mDrawModifiers;
1488 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001489 return false;
1490}
1491
Chris Craik527a3aa2013-03-04 10:19:31 -08001492void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001493 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001494 mDrawModifiers = state.mDrawModifiers;
1495 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001496
Chris Craik527a3aa2013-03-04 10:19:31 -08001497 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001498 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1499 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001500 dirtyClip();
1501 }
Chris Craikc3566d02013-02-04 16:16:33 -08001502}
1503
Chris Craik28ce94a2013-05-31 11:38:03 -07001504/**
1505 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1506 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1507 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1508 *
1509 * This method should be called when restoreDisplayState() won't be restoring the clip
1510 */
1511void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1512 if (clipRect != NULL) {
1513 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1514 } else {
1515 mSnapshot->setClip(0, 0, mWidth, mHeight);
1516 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001517 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001518 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001519}
1520
Chris Craikc3566d02013-02-04 16:16:33 -08001521///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001522// Transforms
1523///////////////////////////////////////////////////////////////////////////////
1524
1525void OpenGLRenderer::translate(float dx, float dy) {
Romain Guy4c2547f2013-06-11 16:19:24 -07001526 currentTransform().translate(dx, dy);
Romain Guyf6a11b82010-06-23 17:47:49 -07001527}
1528
1529void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001530 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001531}
1532
1533void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001534 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001535}
1536
Romain Guy807daf72011-01-18 11:19:19 -08001537void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001538 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001539}
1540
Romain Guyf6a11b82010-06-23 17:47:49 -07001541void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001542 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001543 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001544 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001545 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001546 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001547}
1548
Chris Craikb98a0162013-02-21 11:30:22 -08001549bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001550 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001551}
1552
Romain Guyf6a11b82010-06-23 17:47:49 -07001553void OpenGLRenderer::getMatrix(SkMatrix* matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001554 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001555}
1556
1557void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Chris Craikf57776b2013-10-25 18:30:17 -07001558 mat4 transform(*matrix);
1559 currentTransform().multiply(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001560}
1561
1562///////////////////////////////////////////////////////////////////////////////
1563// Clipping
1564///////////////////////////////////////////////////////////////////////////////
1565
Romain Guybb9524b2010-06-22 18:56:38 -07001566void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001567 Rect clip(*mSnapshot->clipRect);
1568 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001569
Romain Guy8a4ac612012-07-17 17:32:48 -07001570 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1571 clip.getWidth(), clip.getHeight())) {
1572 mDirtyClip = false;
1573 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001574}
1575
Romain Guy8ce00302013-01-15 18:51:42 -08001576void OpenGLRenderer::ensureStencilBuffer() {
1577 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1578 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1579 // just hope we have one when hasLayer() returns false.
1580 if (hasLayer()) {
1581 attachStencilBufferToLayer(mSnapshot->layer);
1582 }
1583}
1584
1585void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1586 // The layer's FBO is already bound when we reach this stage
1587 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001588 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1589 // is attached after we initiated tiling. We must turn it off,
1590 // attach the new render buffer then turn tiling back on
1591 endTiling();
1592
Romain Guy8d4aeb72013-02-12 16:08:55 -08001593 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001594 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001595 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001596
Romain Guyf735c8e2013-01-31 17:45:55 -08001597 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001598 }
1599}
1600
1601void OpenGLRenderer::setStencilFromClip() {
1602 if (!mCaches.debugOverdraw) {
1603 if (!mSnapshot->clipRegion->isEmpty()) {
1604 // NOTE: The order here is important, we must set dirtyClip to false
1605 // before any draw call to avoid calling back into this method
1606 mDirtyClip = false;
1607
1608 ensureStencilBuffer();
1609
1610 mCaches.stencil.enableWrite();
1611
1612 // Clear the stencil but first make sure we restrict drawing
1613 // to the region's bounds
1614 bool resetScissor = mCaches.enableScissor();
1615 if (resetScissor) {
1616 // The scissor was not set so we now need to update it
1617 setScissorFromClip();
1618 }
1619 mCaches.stencil.clear();
1620 if (resetScissor) mCaches.disableScissor();
1621
1622 // NOTE: We could use the region contour path to generate a smaller mesh
1623 // Since we are using the stencil we could use the red book path
1624 // drawing technique. It might increase bandwidth usage though.
1625
1626 // The last parameter is important: we are not drawing in the color buffer
1627 // so we don't want to dirty the current layer, if any
1628 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1629
1630 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001631
1632 // Draw the region used to generate the stencil if the appropriate debug
1633 // mode is enabled
1634 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1635 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1636 }
Romain Guy8ce00302013-01-15 18:51:42 -08001637 } else {
1638 mCaches.stencil.disable();
1639 }
1640 }
1641}
1642
Romain Guy9d5316e2010-06-24 19:30:36 -07001643const Rect& OpenGLRenderer::getClipBounds() {
Romain Guy079ba2c2010-07-16 14:12:24 -07001644 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001645}
1646
Chris Craikf0a59072013-11-19 18:00:46 -08001647/**
1648 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
1649 * the clipRect. Does not modify the scissor.
1650 *
1651 * @param clipRequired if not null, will be set to true if element intersects clip
1652 * (and wasn't rejected)
1653 *
1654 * @param snapOut if set, the geometry will be treated as having an AA ramp.
1655 * See Rect::snapGeometryToPixelBoundaries()
1656 */
1657bool OpenGLRenderer::calculateQuickRejectForScissor(float left, float top,
1658 float right, float bottom, bool* clipRequired, bool snapOut) const {
Chris Craik39a908c2013-06-13 14:39:01 -07001659 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001660 return true;
1661 }
1662
1663 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001664 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001665 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001666
1667 Rect clipRect(*mSnapshot->clipRect);
1668 clipRect.snapToPixelBoundaries();
1669
Chris Craik39a908c2013-06-13 14:39:01 -07001670 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001671
Chris Craikf0a59072013-11-19 18:00:46 -08001672 // clip is required if geometry intersects clip rect
Chris Craik39a908c2013-06-13 14:39:01 -07001673 if (clipRequired) *clipRequired = !clipRect.contains(r);
1674 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001675}
1676
Chris Craikf0a59072013-11-19 18:00:46 -08001677/**
1678 * Returns false if drawing won't be clipped out.
1679 *
1680 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
1681 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
1682 * rejection is still desired.
1683 *
1684 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
1685 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
1686 * rejection where precise rejection isn't important, or precise information isn't available.
1687 */
1688bool OpenGLRenderer::quickRejectConservative(float left, float top,
1689 float right, float bottom) const {
1690 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
1691 return true;
Chris Craikcb4d6002012-09-25 12:00:29 -07001692 }
Chris Craikf0a59072013-11-19 18:00:46 -08001693
1694 Rect r(left, top, right, bottom);
1695 currentTransform().mapRect(r);
1696 r.roundOut(); // rounded out to be conservative
1697
1698 Rect clipRect(*mSnapshot->clipRect);
1699 clipRect.snapToPixelBoundaries();
1700
1701 if (!clipRect.intersects(r)) return true;
1702
1703 return false;
Chris Craikcb4d6002012-09-25 12:00:29 -07001704}
1705
Chris Craikf0a59072013-11-19 18:00:46 -08001706/**
1707 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1708 *
1709 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1710 * style, and tessellated AA ramp
1711 */
1712bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
1713 SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001714 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001715 bool snapOut = paint && paint->isAntiAlias();
1716
1717 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1718 float outset = paint->getStrokeWidth() * 0.5f;
1719 left -= outset;
1720 top -= outset;
1721 right += outset;
1722 bottom += outset;
1723 }
1724
1725 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001726 return true;
1727 }
1728
Chris Craik39a908c2013-06-13 14:39:01 -07001729 if (!isDeferred()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001730 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001731 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001732 }
Chris Craik39a908c2013-06-13 14:39:01 -07001733 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001734}
1735
Romain Guy8ce00302013-01-15 18:51:42 -08001736void OpenGLRenderer::debugClip() {
1737#if DEBUG_CLIP_REGIONS
1738 if (!isDeferred() && !mSnapshot->clipRegion->isEmpty()) {
1739 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1740 }
1741#endif
1742}
1743
Romain Guy079ba2c2010-07-16 14:12:24 -07001744bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001745 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001746 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1747 if (clipped) {
1748 dirtyClip();
1749 }
1750 return !mSnapshot->clipRect->isEmpty();
1751 }
1752
1753 SkPath path;
1754 path.addRect(left, top, right, bottom);
1755
Romain Guyb7b93e02013-08-01 15:29:25 -07001756 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001757}
1758
1759bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1760 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001761 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001762
1763 SkPath transformed;
1764 path->transform(transform, &transformed);
1765
1766 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001767 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1768 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001769 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001770 if (mSnapshot->previous == mFirstSnapshot) {
1771 clip.setRect(0, 0, mWidth, mHeight);
1772 } else {
1773 Rect* bounds = mSnapshot->previous->clipRect;
1774 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1775 }
Romain Guy8ce00302013-01-15 18:51:42 -08001776 }
1777
1778 SkRegion region;
1779 region.setPath(transformed, clip);
1780
1781 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001782 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001783 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001784 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001785 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001786}
1787
Romain Guy735738c2012-12-03 12:34:51 -08001788bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001789 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1790 if (clipped) {
1791 dirtyClip();
1792 }
1793 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001794}
1795
Chet Haasea23eed82012-04-12 15:19:04 -07001796Rect* OpenGLRenderer::getClipRect() {
1797 return mSnapshot->clipRect;
1798}
1799
Romain Guyf6a11b82010-06-23 17:47:49 -07001800///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001801// Drawing commands
1802///////////////////////////////////////////////////////////////////////////////
1803
Romain Guy54be1cd2011-06-13 19:04:27 -07001804void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001805 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001806 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001807 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001808 // Make sure setScissor & setStencil happen at the beginning of
1809 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001810 if (mDirtyClip) {
1811 if (mCaches.scissorEnabled) {
1812 setScissorFromClip();
1813 }
Romain Guy8ce00302013-01-15 18:51:42 -08001814 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001815 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001816
Romain Guy70ca14e2010-12-13 18:24:33 -08001817 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001818
Romain Guy70ca14e2010-12-13 18:24:33 -08001819 mSetShaderColor = false;
1820 mColorSet = false;
1821 mColorA = mColorR = mColorG = mColorB = 0.0f;
1822 mTextureUnit = 0;
1823 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001824
1825 // Enable debug highlight when what we're about to draw is tested against
1826 // the stencil buffer and if stencil highlight debugging is on
1827 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1828 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1829 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001830
1831 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001832}
1833
1834void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1835 mDescription.hasTexture = true;
1836 mDescription.hasAlpha8Texture = isAlpha8;
1837}
1838
Romain Guyff316ec2013-02-13 18:39:43 -08001839void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1840 mDescription.hasTexture = true;
1841 mDescription.hasColors = true;
1842 mDescription.hasAlpha8Texture = isAlpha8;
1843}
1844
Romain Guyaa6c24c2011-04-28 18:40:04 -07001845void OpenGLRenderer::setupDrawWithExternalTexture() {
1846 mDescription.hasExternalTexture = true;
1847}
1848
Romain Guy15bc6432011-12-13 13:11:32 -08001849void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001850 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001851}
1852
Chris Craik710f46d2012-09-17 17:25:49 -07001853void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001854 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001855}
1856
Romain Guy8d0d4782010-12-14 20:13:35 -08001857void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1858 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001859 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1860 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1861 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001862 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001863 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001864}
1865
Romain Guy86568192010-12-14 15:55:39 -08001866void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1867 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001868 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1869 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1870 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001871 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001872 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001873}
1874
Romain Guy41210632012-07-16 17:04:24 -07001875void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1876 mCaches.fontRenderer->describe(mDescription, paint);
1877}
1878
Romain Guy70ca14e2010-12-13 18:24:33 -08001879void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1880 mColorA = a;
1881 mColorR = r;
1882 mColorG = g;
1883 mColorB = b;
1884 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001885 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001886}
1887
1888void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001889 if (mDrawModifiers.mShader) {
1890 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001891 }
1892}
1893
1894void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001895 if (mDrawModifiers.mColorFilter) {
1896 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001897 }
1898}
1899
Romain Guyf09ef512011-05-27 11:43:46 -07001900void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1901 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1902 mColorA = 1.0f;
1903 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001904 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001905 }
1906}
1907
Romain Guy70ca14e2010-12-13 18:24:33 -08001908void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001909 // When the blending mode is kClear_Mode, we need to use a modulate color
1910 // argb=1,0,0,0
1911 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001912 bool blend = (mColorSet && mColorA < 1.0f) ||
1913 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1914 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001915}
1916
1917void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001918 // When the blending mode is kClear_Mode, we need to use a modulate color
1919 // argb=1,0,0,0
1920 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001921 blend |= (mColorSet && mColorA < 1.0f) ||
1922 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1923 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1924 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001925}
1926
1927void OpenGLRenderer::setupDrawProgram() {
1928 useProgram(mCaches.programCache.get(mDescription));
1929}
1930
1931void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1932 mTrackDirtyRegions = false;
1933}
1934
Chris Craik4063a0e2013-11-15 16:06:56 -08001935void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1936 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001937 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001938 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001939 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001940 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001941
Romain Guy86568192010-12-14 15:55:39 -08001942 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1943 if (!ignoreTransform) {
Chris Craikf57776b2013-10-25 18:30:17 -07001944 mCaches.currentProgram->set(mViewProjMatrix, mModelView, currentTransform(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001945 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001946 } else {
Chris Craikf57776b2013-10-25 18:30:17 -07001947 mCaches.currentProgram->set(mViewProjMatrix, mModelView, mat4::identity(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001948 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001949 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001950}
1951
1952void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001953 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001954 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1955 }
1956}
1957
Romain Guy86568192010-12-14 15:55:39 -08001958void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001959 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001960 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001961 }
1962}
1963
Romain Guy70ca14e2010-12-13 18:24:33 -08001964void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001965 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001966 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001967 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1968 // because it was built into modelView / the geometry, and the SkiaShader needs to
1969 // compensate.
1970 mat4 modelViewWithoutTransform;
1971 modelViewWithoutTransform.loadInverse(currentTransform());
1972 modelViewWithoutTransform.multiply(mModelView);
1973 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001974 }
Chris Craikc3566d02013-02-04 16:16:33 -08001975 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1976 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001977 }
1978}
1979
1980void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001981 if (mDrawModifiers.mColorFilter) {
1982 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001983 }
1984}
1985
Romain Guy41210632012-07-16 17:04:24 -07001986void OpenGLRenderer::setupDrawTextGammaUniforms() {
1987 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1988}
1989
Romain Guy70ca14e2010-12-13 18:24:33 -08001990void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001991 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001992 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001993 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001994}
1995
1996void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001997 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001998 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001999 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08002000}
2001
Romain Guyaa6c24c2011-04-28 18:40:04 -07002002void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
2003 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08002004 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08002005 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07002006}
2007
Romain Guy8f0095c2011-05-02 17:24:22 -07002008void OpenGLRenderer::setupDrawTextureTransform() {
2009 mDescription.hasTextureTransform = true;
2010}
2011
2012void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07002013 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
2014 GL_FALSE, &transform.data[0]);
2015}
2016
Romain Guy70ca14e2010-12-13 18:24:33 -08002017void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002018 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07002019 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002020 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08002021 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08002022 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08002023 }
Romain Guyd71dd362011-12-12 19:03:35 -08002024
Chris Craikcb4d6002012-09-25 12:00:29 -07002025 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002026 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002027 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08002028 }
2029
2030 mCaches.unbindIndicesBuffer();
2031}
2032
Romain Guyff316ec2013-02-13 18:39:43 -08002033void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
2034 bool force = mCaches.unbindMeshBuffer();
2035 GLsizei stride = sizeof(ColorTextureVertex);
2036
2037 mCaches.bindPositionVertexPointer(force, vertices, stride);
2038 if (mCaches.currentProgram->texCoords >= 0) {
2039 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
2040 }
2041 int slot = mCaches.currentProgram->getAttrib("colors");
2042 if (slot >= 0) {
2043 glEnableVertexAttribArray(slot);
2044 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2045 }
2046
2047 mCaches.unbindIndicesBuffer();
2048}
2049
Romain Guy3b748a42013-04-17 18:54:38 -07002050void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2051 bool force = false;
2052 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2053 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2054 // use the default VBO found in Caches
2055 if (!vertices || vbo) {
2056 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2057 } else {
2058 force = mCaches.unbindMeshBuffer();
2059 }
2060 mCaches.bindIndicesBuffer();
2061
Chris Craikcb4d6002012-09-25 12:00:29 -07002062 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002063 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002064 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002065 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002066}
2067
Romain Guy448455f2013-07-22 13:57:50 -07002068void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002069 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002070 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002071 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002072}
2073
Romain Guy70ca14e2010-12-13 18:24:33 -08002074///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002075// Drawing
2076///////////////////////////////////////////////////////////////////////////////
2077
Chris Craikff785832013-03-08 13:12:16 -08002078status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2079 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002080 status_t status;
Chris Craikf57776b2013-10-25 18:30:17 -07002081
Romain Guy0fe478e2010-11-08 12:08:41 -08002082 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2083 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002084 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07002085 // compute 3d ordering
2086 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07002087 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002088 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002089 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2090 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002091 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002092 }
2093
Chris Craik28ce94a2013-05-31 11:38:03 -07002094 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2095 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002096 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2097 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002098
2099 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002100 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002101
Chris Craikf57776b2013-10-25 18:30:17 -07002102 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002103 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002104
Romain Guy65549432012-03-26 16:45:05 -07002105 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002106}
2107
Romain Guya168d732011-03-18 16:50:13 -07002108void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2109 int alpha;
2110 SkXfermode::Mode mode;
2111 getAlphaAndMode(paint, &alpha, &mode);
2112
Romain Guy886b2752013-01-04 12:26:18 -08002113 int color = paint != NULL ? paint->getColor() : 0;
2114
Romain Guya168d732011-03-18 16:50:13 -07002115 float x = left;
2116 float y = top;
2117
Romain Guy886b2752013-01-04 12:26:18 -08002118 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2119
Romain Guya168d732011-03-18 16:50:13 -07002120 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002121 if (currentTransform().isPureTranslate()) {
2122 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2123 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002124 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002125
2126 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002127 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002128 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002129 }
2130
Romain Guy3b748a42013-04-17 18:54:38 -07002131 // No need to check for a UV mapper on the texture object, only ARGB_8888
2132 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002133 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002134 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2135 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002136}
2137
Romain Guy03c00b52013-06-20 18:30:28 -07002138/**
2139 * Important note: this method is intended to draw batches of bitmaps and
2140 * will not set the scissor enable or dirty the current layer, if any.
2141 * The caller is responsible for properly dirtying the current layer.
2142 */
Romain Guy55b6f952013-06-27 15:27:09 -07002143status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002144 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002145 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002146 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002147 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002148
Chris Craik527a3aa2013-03-04 10:19:31 -08002149 const AutoTexture autoCleanup(texture);
2150
2151 int alpha;
2152 SkXfermode::Mode mode;
2153 getAlphaAndMode(paint, &alpha, &mode);
2154
2155 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002156 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002157
2158 const float x = (int) floorf(bounds.left + 0.5f);
2159 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002160 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002161 int color = paint != NULL ? paint->getColor() : 0;
2162 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2163 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002164 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002165 GL_TRIANGLES, bitmapCount * 6, true,
2166 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002167 } else {
2168 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2169 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002170 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002171 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2172 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002173 }
2174
2175 return DrawGlInfo::kStatusDrew;
2176}
2177
Chet Haase48659092012-05-31 15:21:51 -07002178status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002179 const float right = left + bitmap->width();
2180 const float bottom = top + bitmap->height();
2181
Chris Craikf0a59072013-11-19 18:00:46 -08002182 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002183 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002184 }
2185
Romain Guya1d3c912011-12-13 14:55:06 -08002186 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002187 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002188 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002189 const AutoTexture autoCleanup(texture);
2190
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002191 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002192 drawAlphaBitmap(texture, left, top, paint);
2193 } else {
2194 drawTextureRect(left, top, right, bottom, texture, paint);
2195 }
Chet Haase48659092012-05-31 15:21:51 -07002196
2197 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002198}
2199
Chet Haase48659092012-05-31 15:21:51 -07002200status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002201 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2202 const mat4 transform(*matrix);
2203 transform.mapRect(r);
2204
Chris Craikf0a59072013-11-19 18:00:46 -08002205 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002206 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002207 }
2208
Romain Guya1d3c912011-12-13 14:55:06 -08002209 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002210 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002211 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002212 const AutoTexture autoCleanup(texture);
2213
Romain Guy5b3b3522010-10-27 18:57:51 -07002214 // This could be done in a cheaper way, all we need is pass the matrix
2215 // to the vertex shader. The save/restore is a bit overkill.
2216 save(SkCanvas::kMatrix_SaveFlag);
2217 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002218 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002219 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2220 } else {
2221 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2222 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002223 restore();
Chet Haase48659092012-05-31 15:21:51 -07002224
2225 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002226}
2227
Chet Haase48659092012-05-31 15:21:51 -07002228status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002229 const float right = left + bitmap->width();
2230 const float bottom = top + bitmap->height();
2231
Chris Craikf0a59072013-11-19 18:00:46 -08002232 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002233 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002234 }
2235
2236 mCaches.activeTexture(0);
2237 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2238 const AutoTexture autoCleanup(texture);
2239
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002240 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002241 drawAlphaBitmap(texture, left, top, paint);
2242 } else {
2243 drawTextureRect(left, top, right, bottom, texture, paint);
2244 }
Chet Haase48659092012-05-31 15:21:51 -07002245
2246 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002247}
2248
Chet Haase48659092012-05-31 15:21:51 -07002249status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002250 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002251 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002252 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002253 }
2254
Chris Craik39a908c2013-06-13 14:39:01 -07002255 // TODO: use quickReject on bounds from vertices
2256 mCaches.enableScissor();
2257
Romain Guyb18d2d02011-02-10 15:52:54 -08002258 float left = FLT_MAX;
2259 float top = FLT_MAX;
2260 float right = FLT_MIN;
2261 float bottom = FLT_MIN;
2262
Romain Guya92bb4d2012-10-16 11:08:44 -07002263 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002264
Romain Guyff316ec2013-02-13 18:39:43 -08002265 ColorTextureVertex mesh[count];
2266 ColorTextureVertex* vertex = mesh;
2267
2268 bool cleanupColors = false;
2269 if (!colors) {
2270 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2271 colors = new int[colorsCount];
2272 memset(colors, 0xff, colorsCount * sizeof(int));
2273 cleanupColors = true;
2274 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002275
Romain Guy3b748a42013-04-17 18:54:38 -07002276 mCaches.activeTexture(0);
2277 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2278 const UvMapper& mapper(getMapper(texture));
2279
Romain Guy5a7b4662011-01-20 19:09:30 -08002280 for (int32_t y = 0; y < meshHeight; y++) {
2281 for (int32_t x = 0; x < meshWidth; x++) {
2282 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2283
2284 float u1 = float(x) / meshWidth;
2285 float u2 = float(x + 1) / meshWidth;
2286 float v1 = float(y) / meshHeight;
2287 float v2 = float(y + 1) / meshHeight;
2288
Romain Guy3b748a42013-04-17 18:54:38 -07002289 mapper.map(u1, v1, u2, v2);
2290
Romain Guy5a7b4662011-01-20 19:09:30 -08002291 int ax = i + (meshWidth + 1) * 2;
2292 int ay = ax + 1;
2293 int bx = i;
2294 int by = bx + 1;
2295 int cx = i + 2;
2296 int cy = cx + 1;
2297 int dx = i + (meshWidth + 1) * 2 + 2;
2298 int dy = dx + 1;
2299
Romain Guyff316ec2013-02-13 18:39:43 -08002300 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2301 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2302 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002303
Romain Guyff316ec2013-02-13 18:39:43 -08002304 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2305 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2306 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002307
Romain Guya92bb4d2012-10-16 11:08:44 -07002308 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2309 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2310 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2311 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002312 }
2313 }
2314
Chris Craikf0a59072013-11-19 18:00:46 -08002315 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002316 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002317 return DrawGlInfo::kStatusDone;
2318 }
2319
Romain Guyff316ec2013-02-13 18:39:43 -08002320 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002321 texture = mCaches.textureCache.get(bitmap);
2322 if (!texture) {
2323 if (cleanupColors) delete[] colors;
2324 return DrawGlInfo::kStatusDone;
2325 }
Romain Guyff316ec2013-02-13 18:39:43 -08002326 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002327 const AutoTexture autoCleanup(texture);
2328
2329 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2330 texture->setFilter(FILTER(paint), true);
2331
2332 int alpha;
2333 SkXfermode::Mode mode;
2334 getAlphaAndMode(paint, &alpha, &mode);
2335
Romain Guyff316ec2013-02-13 18:39:43 -08002336 float a = alpha / 255.0f;
2337
Romain Guya92bb4d2012-10-16 11:08:44 -07002338 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002339 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002340 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002341
Romain Guyff316ec2013-02-13 18:39:43 -08002342 setupDraw();
2343 setupDrawWithTextureAndColor();
2344 setupDrawColor(a, a, a, a);
2345 setupDrawColorFilter();
2346 setupDrawBlending(true, mode, false);
2347 setupDrawProgram();
2348 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002349 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002350 setupDrawTexture(texture->id);
2351 setupDrawPureColorUniforms();
2352 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002353 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002354
2355 glDrawArrays(GL_TRIANGLES, 0, count);
2356
Romain Guyff316ec2013-02-13 18:39:43 -08002357 int slot = mCaches.currentProgram->getAttrib("colors");
2358 if (slot >= 0) {
2359 glDisableVertexAttribArray(slot);
2360 }
2361
2362 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002363
2364 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002365}
2366
Chet Haase48659092012-05-31 15:21:51 -07002367status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002368 float srcLeft, float srcTop, float srcRight, float srcBottom,
2369 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002370 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002371 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002372 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002373 }
2374
Romain Guya1d3c912011-12-13 14:55:06 -08002375 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002376 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002377 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002378 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002379
Romain Guy8ba548f2010-06-30 19:21:21 -07002380 const float width = texture->width;
2381 const float height = texture->height;
2382
Romain Guy3b748a42013-04-17 18:54:38 -07002383 float u1 = fmax(0.0f, srcLeft / width);
2384 float v1 = fmax(0.0f, srcTop / height);
2385 float u2 = fmin(1.0f, srcRight / width);
2386 float v2 = fmin(1.0f, srcBottom / height);
2387
2388 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002389
Romain Guy03750a02010-10-18 14:06:08 -07002390 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002391 resetDrawTextureTexCoords(u1, v1, u2, v2);
2392
Romain Guy03750a02010-10-18 14:06:08 -07002393 int alpha;
2394 SkXfermode::Mode mode;
2395 getAlphaAndMode(paint, &alpha, &mode);
2396
Romain Guyd21b6e12011-11-30 20:21:23 -08002397 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2398
Romain Guy886b2752013-01-04 12:26:18 -08002399 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2400 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002401
Romain Guy886b2752013-01-04 12:26:18 -08002402 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2403 // Apply a scale transform on the canvas only when a shader is in use
2404 // Skia handles the ratio between the dst and src rects as a scale factor
2405 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002406 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002407 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002408
Romain Guy3b753822013-03-05 10:27:35 -08002409 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2410 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2411 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002412
2413 dstRight = x + (dstRight - dstLeft);
2414 dstBottom = y + (dstBottom - dstTop);
2415
2416 dstLeft = x;
2417 dstTop = y;
2418
2419 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2420 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002421 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002422 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002423 }
2424
2425 if (CC_UNLIKELY(useScaleTransform)) {
2426 save(SkCanvas::kMatrix_SaveFlag);
2427 translate(dstLeft, dstTop);
2428 scale(scaleX, scaleY);
2429
2430 dstLeft = 0.0f;
2431 dstTop = 0.0f;
2432
2433 dstRight = srcRight - srcLeft;
2434 dstBottom = srcBottom - srcTop;
2435 }
2436
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002437 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002438 int color = paint ? paint->getColor() : 0;
2439 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2440 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002441 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002442 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2443 } else {
2444 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2445 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002446 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002447 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2448 }
2449
2450 if (CC_UNLIKELY(useScaleTransform)) {
2451 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002452 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002453
2454 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002455
2456 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002457}
2458
Romain Guy3b748a42013-04-17 18:54:38 -07002459status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002460 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002461 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002462 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002463 }
2464
Romain Guy4c2547f2013-06-11 16:19:24 -07002465 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002466 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2467 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002468
Romain Guy03c00b52013-06-20 18:30:28 -07002469 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002470}
2471
Romain Guy03c00b52013-06-20 18:30:28 -07002472status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2473 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002474 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002475 return DrawGlInfo::kStatusDone;
2476 }
2477
Romain Guy211370f2012-02-01 16:10:55 -08002478 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002479 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002480 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002481 if (!texture) return DrawGlInfo::kStatusDone;
2482 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002483
Romain Guya4adcf02013-02-28 12:15:35 -08002484 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2485 texture->setFilter(GL_LINEAR, true);
2486
Romain Guy03c00b52013-06-20 18:30:28 -07002487 int alpha;
2488 SkXfermode::Mode mode;
2489 getAlphaAndMode(paint, &alpha, &mode);
2490
Romain Guy3b753822013-03-05 10:27:35 -08002491 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002492 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002493 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002494 const float offsetX = left + currentTransform().getTranslateX();
2495 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002496 const size_t count = mesh->quads.size();
2497 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002498 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002499 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002500 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2501 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2502 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002503 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002504 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002505 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002506 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002507 }
2508 }
2509
Chris Craik4063a0e2013-11-15 16:06:56 -08002510 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002511 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002512 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2513 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002514
Romain Guy3b748a42013-04-17 18:54:38 -07002515 right = x + right - left;
2516 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002517 left = x;
2518 top = y;
2519 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002520 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002521 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2522 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2523 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2524 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002525 }
Chet Haase48659092012-05-31 15:21:51 -07002526
2527 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002528}
2529
Romain Guy03c00b52013-06-20 18:30:28 -07002530/**
2531 * Important note: this method is intended to draw batches of 9-patch objects and
2532 * will not set the scissor enable or dirty the current layer, if any.
2533 * The caller is responsible for properly dirtying the current layer.
2534 */
2535status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2536 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2537 mCaches.activeTexture(0);
2538 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2539 if (!texture) return DrawGlInfo::kStatusDone;
2540 const AutoTexture autoCleanup(texture);
2541
2542 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2543 texture->setFilter(GL_LINEAR, true);
2544
2545 int alpha;
2546 SkXfermode::Mode mode;
2547 getAlphaAndMode(paint, &alpha, &mode);
2548
2549 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002550 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002551 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002552
2553 return DrawGlInfo::kStatusDrew;
2554}
2555
Chris Craik65cd6122012-12-10 17:56:27 -08002556status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2557 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002558 // not missing call to quickReject/dirtyLayer, always done at a higher level
2559
Chris Craik6d29c8d2013-05-08 18:35:44 -07002560 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002561 // no vertices to draw
2562 return DrawGlInfo::kStatusDone;
2563 }
2564
Chris Craikcb4d6002012-09-25 12:00:29 -07002565 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002566 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2567 bool isAA = paint->isAntiAlias();
2568
Chris Craik710f46d2012-09-17 17:25:49 -07002569 setupDraw();
2570 setupDrawNoTexture();
2571 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002572 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2573 setupDrawColorFilter();
2574 setupDrawShader();
2575 setupDrawBlending(isAA, mode);
2576 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002577 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002578 setupDrawColorUniforms();
2579 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002580 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002581
Chris Craik710f46d2012-09-17 17:25:49 -07002582 void* vertices = vertexBuffer.getBuffer();
2583 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002584 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002585 mCaches.resetTexCoordsVertexPointer();
2586 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002587
Chris Craik710f46d2012-09-17 17:25:49 -07002588 int alphaSlot = -1;
2589 if (isAA) {
2590 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2591 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002592
Chris Craik710f46d2012-09-17 17:25:49 -07002593 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002594 glEnableVertexAttribArray(alphaSlot);
2595 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002596 }
Romain Guy04299382012-07-18 17:15:41 -07002597
Chris Craik6d29c8d2013-05-08 18:35:44 -07002598 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002599
Chris Craik710f46d2012-09-17 17:25:49 -07002600 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002601 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002602 }
Chris Craik65cd6122012-12-10 17:56:27 -08002603
2604 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002605}
2606
2607/**
Chris Craik65cd6122012-12-10 17:56:27 -08002608 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2609 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2610 * screen space in all directions. However, instead of using a fragment shader to compute the
2611 * translucency of the color from its position, we simply use a varying parameter to define how far
2612 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2613 *
2614 * Doesn't yet support joins, caps, or path effects.
2615 */
2616status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2617 VertexBuffer vertexBuffer;
2618 // TODO: try clipping large paths to viewport
2619 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2620
Romain Guyc46d07a2013-03-15 19:06:39 -07002621 if (hasLayer()) {
2622 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002623 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002624 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2625 }
Chris Craik65cd6122012-12-10 17:56:27 -08002626
2627 return drawVertexBuffer(vertexBuffer, paint);
2628}
2629
2630/**
2631 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2632 * and additional geometry for defining an alpha slope perimeter.
2633 *
2634 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2635 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2636 * in-shader alpha region, but found it to be taxing on some GPUs.
2637 *
2638 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2639 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002640 */
Chet Haase48659092012-05-31 15:21:51 -07002641status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002642 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002643
Chris Craik65cd6122012-12-10 17:56:27 -08002644 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002645
Chris Craik65cd6122012-12-10 17:56:27 -08002646 VertexBuffer buffer;
2647 SkRect bounds;
2648 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002649
Chris Craikf0a59072013-11-19 18:00:46 -08002650 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2651 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002652 return DrawGlInfo::kStatusDone;
2653 }
2654
Romain Guy3b753822013-03-05 10:27:35 -08002655 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002656
Chris Craik65cd6122012-12-10 17:56:27 -08002657 bool useOffset = !paint->isAntiAlias();
2658 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002659}
2660
Chet Haase48659092012-05-31 15:21:51 -07002661status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002662 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002663
Chris Craik6d29c8d2013-05-08 18:35:44 -07002664 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002665
Chris Craik6d29c8d2013-05-08 18:35:44 -07002666 VertexBuffer buffer;
2667 SkRect bounds;
2668 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002669
Chris Craikf0a59072013-11-19 18:00:46 -08002670 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2671 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002672 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002673 }
2674
Chris Craik6d29c8d2013-05-08 18:35:44 -07002675 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002676
Chris Craik6d29c8d2013-05-08 18:35:44 -07002677 bool useOffset = !paint->isAntiAlias();
2678 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002679}
2680
Chet Haase48659092012-05-31 15:21:51 -07002681status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002682 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002683 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002684
Romain Guyae88e5e2010-10-22 17:49:18 -07002685 Rect& clip(*mSnapshot->clipRect);
2686 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002687
Romain Guy3d58c032010-07-14 16:34:53 -07002688 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002689
2690 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002691}
Romain Guy9d5316e2010-06-24 19:30:36 -07002692
Chet Haase48659092012-05-31 15:21:51 -07002693status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2694 SkPaint* paint) {
2695 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002696 const AutoTexture autoCleanup(texture);
2697
2698 const float x = left + texture->left - texture->offset;
2699 const float y = top + texture->top - texture->offset;
2700
2701 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002702
2703 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002704}
2705
Chet Haase48659092012-05-31 15:21:51 -07002706status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002707 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002708 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002709 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002710 return DrawGlInfo::kStatusDone;
2711 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002712
Chris Craikcb4d6002012-09-25 12:00:29 -07002713 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002714 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002715 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002716 right - left, bottom - top, rx, ry, p);
2717 return drawShape(left, top, texture, p);
2718 }
2719
2720 SkPath path;
2721 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002722 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2723 float outset = p->getStrokeWidth() / 2;
2724 rect.outset(outset, outset);
2725 rx += outset;
2726 ry += outset;
2727 }
Chris Craik710f46d2012-09-17 17:25:49 -07002728 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002729 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002730}
2731
Chris Craik710f46d2012-09-17 17:25:49 -07002732status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002733 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002734 x + radius, y + radius, p) ||
2735 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002736 return DrawGlInfo::kStatusDone;
2737 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002738 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002739 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002740 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002741 return drawShape(x - radius, y - radius, texture, p);
2742 }
2743
2744 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002745 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2746 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2747 } else {
2748 path.addCircle(x, y, radius);
2749 }
Chris Craik65cd6122012-12-10 17:56:27 -08002750 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002751}
Romain Guy01d58e42011-01-19 21:54:02 -08002752
Chet Haase48659092012-05-31 15:21:51 -07002753status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002754 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002755 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002756 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002757 return DrawGlInfo::kStatusDone;
2758 }
Romain Guy01d58e42011-01-19 21:54:02 -08002759
Chris Craikcb4d6002012-09-25 12:00:29 -07002760 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002761 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002762 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002763 return drawShape(left, top, texture, p);
2764 }
2765
2766 SkPath path;
2767 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002768 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2769 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2770 }
Chris Craik710f46d2012-09-17 17:25:49 -07002771 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002772 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002773}
2774
Chet Haase48659092012-05-31 15:21:51 -07002775status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002776 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002777 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002778 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002779 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002780 }
2781
Chris Craik780c1282012-10-04 14:10:49 -07002782 if (fabs(sweepAngle) >= 360.0f) {
2783 return drawOval(left, top, right, bottom, p);
2784 }
2785
2786 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002787 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002788 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002789 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002790 startAngle, sweepAngle, useCenter, p);
2791 return drawShape(left, top, texture, p);
2792 }
2793
2794 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2795 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2796 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2797 }
2798
2799 SkPath path;
2800 if (useCenter) {
2801 path.moveTo(rect.centerX(), rect.centerY());
2802 }
2803 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2804 if (useCenter) {
2805 path.close();
2806 }
Chris Craik65cd6122012-12-10 17:56:27 -08002807 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002808}
2809
Romain Guycf8675e2012-10-02 12:32:25 -07002810// See SkPaintDefaults.h
2811#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2812
Chet Haase48659092012-05-31 15:21:51 -07002813status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002814 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002815 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002816 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002817 }
2818
Chris Craik710f46d2012-09-17 17:25:49 -07002819 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002820 // only fill style is supported by drawConvexPath, since others have to handle joins
2821 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2822 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2823 mCaches.activeTexture(0);
2824 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002825 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002826 return drawShape(left, top, texture, p);
2827 }
2828
2829 SkPath path;
2830 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2831 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2832 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2833 }
2834 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002835 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002836 }
2837
Romain Guy3b753822013-03-05 10:27:35 -08002838 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002839 SkPath path;
2840 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002841 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002842 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002843 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002844 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002845 }
Romain Guyc7d53492010-06-25 13:41:57 -07002846}
Romain Guy9d5316e2010-06-24 19:30:36 -07002847
Raph Levien416a8472012-07-19 22:48:17 -07002848void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2849 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2850 float x, float y) {
2851 mCaches.activeTexture(0);
2852
2853 // NOTE: The drop shadow will not perform gamma correction
2854 // if shader-based correction is enabled
2855 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2856 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002857 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002858 // If the drop shadow exceeds the max texture size or couldn't be
2859 // allocated, skip drawing
2860 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002861 const AutoTexture autoCleanup(shadow);
2862
Chris Craikc3566d02013-02-04 16:16:33 -08002863 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2864 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002865
Chris Craikc3566d02013-02-04 16:16:33 -08002866 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2867 int shadowColor = mDrawModifiers.mShadowColor;
2868 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002869 shadowColor = 0xffffffff;
2870 }
2871
2872 setupDraw();
2873 setupDrawWithTexture(true);
2874 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2875 setupDrawColorFilter();
2876 setupDrawShader();
2877 setupDrawBlending(true, mode);
2878 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002879 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2880 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002881 setupDrawTexture(shadow->id);
2882 setupDrawPureColorUniforms();
2883 setupDrawColorFilterUniforms();
2884 setupDrawShaderUniforms();
2885 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2886
2887 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2888}
2889
Romain Guy768bffc2013-02-27 13:50:45 -08002890bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2891 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2892 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2893}
2894
Chet Haase48659092012-05-31 15:21:51 -07002895status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002896 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002897 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002898 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002899 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002900
Romain Guy671d6cf2012-01-18 12:39:17 -08002901 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002902 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002903 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002904 }
2905
Chris Craik39a908c2013-06-13 14:39:01 -07002906 mCaches.enableScissor();
2907
Romain Guy671d6cf2012-01-18 12:39:17 -08002908 float x = 0.0f;
2909 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002910 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002911 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002912 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2913 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002914 }
2915
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002916 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002917 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002918
2919 int alpha;
2920 SkXfermode::Mode mode;
2921 getAlphaAndMode(paint, &alpha, &mode);
2922
Chris Craikc3566d02013-02-04 16:16:33 -08002923 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002924 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2925 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002926 }
2927
Romain Guy671d6cf2012-01-18 12:39:17 -08002928 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002929 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002930 if (pureTranslate && !linearFilter) {
2931 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2932 }
Romain Guy257ae352013-03-20 16:31:12 -07002933 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002934
2935 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2936 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2937
Romain Guy211370f2012-02-01 16:10:55 -08002938 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002939
Victoria Lease1e546812013-06-25 14:25:17 -07002940 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002941 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002942 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002943 if (hasActiveLayer) {
2944 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002945 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002946 }
2947 dirtyLayerUnchecked(bounds, getRegion());
2948 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002949 }
Chet Haase48659092012-05-31 15:21:51 -07002950
2951 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002952}
2953
Romain Guy624234f2013-03-05 16:43:31 -08002954mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2955 mat4 fontTransform;
2956 if (CC_LIKELY(transform.isPureTranslate())) {
2957 fontTransform = mat4::identity();
2958 } else {
2959 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002960 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002961 } else {
2962 float sx, sy;
2963 currentTransform().decomposeScale(sx, sy);
2964 fontTransform.loadScale(sx, sy, 1.0f);
2965 }
2966 }
2967 return fontTransform;
2968}
2969
Chris Craik41541822013-05-03 16:35:54 -07002970status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2971 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002972 DrawOpMode drawOpMode) {
2973
Chris Craik527a3aa2013-03-04 10:19:31 -08002974 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002975 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2976 // drawing as ops from DeferredDisplayList are already filtered for these
2977 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002978 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002979 return DrawGlInfo::kStatusDone;
2980 }
Romain Guycac5fd32011-12-01 20:08:50 -08002981 }
2982
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002983 const float oldX = x;
2984 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002985
2986 const mat4& transform = currentTransform();
2987 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002988
Romain Guy211370f2012-02-01 16:10:55 -08002989 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002990 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2991 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002992 }
2993
Romain Guy86568192010-12-14 15:55:39 -08002994 int alpha;
2995 SkXfermode::Mode mode;
2996 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002997
Romain Guyc74f45a2013-02-26 19:10:14 -08002998 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2999
Chris Craikc3566d02013-02-04 16:16:33 -08003000 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08003001 fontRenderer.setFont(paint, mat4::identity());
3002 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
3003 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07003004 }
3005
Romain Guy19d4dd82013-03-04 11:14:26 -08003006 const bool hasActiveLayer = hasLayer();
3007
Romain Guy624234f2013-03-05 16:43:31 -08003008 // We only pass a partial transform to the font renderer. That partial
3009 // matrix defines how glyphs are rasterized. Typically we want glyphs
3010 // to be rasterized at their final size on screen, which means the partial
3011 // matrix needs to take the scale factor into account.
3012 // When a partial matrix is used to transform glyphs during rasterization,
3013 // the mesh is generated with the inverse transform (in the case of scale,
3014 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3015 // apply the full transform matrix at draw time in the vertex shader.
3016 // Applying the full matrix in the shader is the easiest way to handle
3017 // rotation and perspective and allows us to always generated quads in the
3018 // font renderer which greatly simplifies the code, clipping in particular.
3019 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003020 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003021
Romain Guy6620c6d2010-12-06 18:07:02 -08003022 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003023 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003024 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003025
Romain Guy624234f2013-03-05 16:43:31 -08003026 // TODO: Implement better clipping for scaled/rotated text
3027 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003028 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 -07003029
Raph Levien996e57c2012-07-23 15:22:52 -07003030 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003031 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003032
3033 // don't call issuedrawcommand, do it at end of batch
3034 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003035 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003036 SkPaint paintCopy(*paint);
3037 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3038 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003039 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003040 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003041 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003042 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003043 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003044
Chris Craik527a3aa2013-03-04 10:19:31 -08003045 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003046 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003047 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003048 }
Chris Craik41541822013-05-03 16:35:54 -07003049 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003050 }
Romain Guy694b5192010-07-21 21:33:20 -07003051
Chris Craike63f7c622013-10-17 10:30:55 -07003052 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003053
3054 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003055}
3056
Chet Haase48659092012-05-31 15:21:51 -07003057status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003058 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003059 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003060 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003061 }
3062
Chris Craik39a908c2013-06-13 14:39:01 -07003063 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3064 mCaches.enableScissor();
3065
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003066 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003067 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003068 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003069
3070 int alpha;
3071 SkXfermode::Mode mode;
3072 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003073 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003074
Romain Guy97771732012-02-28 18:17:02 -08003075 const Rect* clip = &mSnapshot->getLocalClip();
3076 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 -08003077
Romain Guy97771732012-02-28 18:17:02 -08003078 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003079
3080 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003081 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003082 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003083 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003084 dirtyLayerUnchecked(bounds, getRegion());
3085 }
Romain Guy97771732012-02-28 18:17:02 -08003086 }
Chet Haase48659092012-05-31 15:21:51 -07003087
3088 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003089}
3090
Chet Haase48659092012-05-31 15:21:51 -07003091status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3092 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003093
Romain Guya1d3c912011-12-13 14:55:06 -08003094 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003095
Romain Guyfb8b7632010-08-23 21:05:08 -07003096 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003097 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003098 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003099
Romain Guy8b55f372010-08-18 17:10:07 -07003100 const float x = texture->left - texture->offset;
3101 const float y = texture->top - texture->offset;
3102
Romain Guy01d58e42011-01-19 21:54:02 -08003103 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003104
3105 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003106}
3107
Chris Craika08f95c2013-03-15 17:24:33 -07003108status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003109 if (!layer) {
3110 return DrawGlInfo::kStatusDone;
3111 }
3112
Romain Guyb2e2f242012-10-17 18:18:35 -07003113 mat4* transform = NULL;
3114 if (layer->isTextureLayer()) {
3115 transform = &layer->getTransform();
3116 if (!transform->isIdentity()) {
3117 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003118 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003119 }
3120 }
3121
Chris Craik39a908c2013-06-13 14:39:01 -07003122 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08003123 const bool rejected = calculateQuickRejectForScissor(x, y,
3124 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003125
3126 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003127 if (transform && !transform->isIdentity()) {
3128 restore();
3129 }
Chet Haase48659092012-05-31 15:21:51 -07003130 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003131 }
3132
Romain Guy5bb3c732012-11-29 17:52:58 -08003133 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003134
Chris Craik39a908c2013-06-13 14:39:01 -07003135 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003136 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003137
Romain Guy211370f2012-02-01 16:10:55 -08003138 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003139 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3140 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003141
Romain Guyc88e3572011-01-22 00:32:12 -08003142 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003143 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3144 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003145 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003146 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003147 setupDraw();
3148 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003149 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003150 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003151 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003152 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003153 setupDrawPureColorUniforms();
3154 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003155 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003156 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3157 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3158 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003159
Romain Guyd21b6e12011-11-30 20:21:23 -08003160 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003161 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003162 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003163 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003164 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003165 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003166 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3167 }
Romain Guyf219da52011-01-16 12:54:25 -08003168
Romain Guy448455f2013-07-22 13:57:50 -07003169 TextureVertex* mesh = &layer->mesh[0];
3170 GLsizei elementsCount = layer->meshElementCount;
3171
3172 while (elementsCount > 0) {
3173 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3174
Romain Guy3380cfd2013-08-15 16:57:57 -07003175 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003176 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3177 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3178
3179 elementsCount -= drawCount;
3180 // Though there are 4 vertices in a quad, we use 6 indices per
3181 // quad to draw with GL_TRIANGLES
3182 mesh += (drawCount / 6) * 4;
3183 }
Romain Guyf219da52011-01-16 12:54:25 -08003184
Romain Guy3a3133d2011-02-01 22:59:58 -08003185#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003186 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003187#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003188 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003189
Chris Craikc3566d02013-02-04 16:16:33 -08003190 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003191
Romain Guy5bb3c732012-11-29 17:52:58 -08003192 if (layer->debugDrawUpdate) {
3193 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003194 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3195 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3196 }
Romain Guyf219da52011-01-16 12:54:25 -08003197 }
Chris Craik34416ea2013-04-15 16:08:28 -07003198 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003199
Romain Guyb2e2f242012-10-17 18:18:35 -07003200 if (transform && !transform->isIdentity()) {
3201 restore();
3202 }
3203
Chet Haase48659092012-05-31 15:21:51 -07003204 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003205}
3206
Romain Guy6926c722010-07-12 20:20:03 -07003207///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003208// Shaders
3209///////////////////////////////////////////////////////////////////////////////
3210
3211void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003212 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003213}
3214
Romain Guy06f96e22010-07-30 19:18:16 -07003215void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003216 mDrawModifiers.mShader = shader;
3217 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003218 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003219 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003220}
3221
Romain Guyd27977d2010-07-14 19:18:51 -07003222///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003223// Color filters
3224///////////////////////////////////////////////////////////////////////////////
3225
3226void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003227 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003228}
3229
3230void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003231 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003232}
3233
3234///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003235// Drop shadow
3236///////////////////////////////////////////////////////////////////////////////
3237
3238void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003239 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003240}
3241
3242void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003243 mDrawModifiers.mHasShadow = true;
3244 mDrawModifiers.mShadowRadius = radius;
3245 mDrawModifiers.mShadowDx = dx;
3246 mDrawModifiers.mShadowDy = dy;
3247 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003248}
3249
3250///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003251// Draw filters
3252///////////////////////////////////////////////////////////////////////////////
3253
3254void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003255 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3256 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003257 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003258 mDrawModifiers.mPaintFilterClearBits = 0;
3259 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003260}
3261
3262void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003263 mDrawModifiers.mHasDrawFilter = true;
3264 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3265 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003266}
3267
Chris Craika08f95c2013-03-15 17:24:33 -07003268SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003269 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003270 return paint;
3271 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003272
3273 uint32_t flags = paint->getFlags();
3274
3275 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003276 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3277 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003278
3279 return &mFilteredPaint;
3280}
3281
3282///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003283// Drawing implementation
3284///////////////////////////////////////////////////////////////////////////////
3285
Romain Guy3b748a42013-04-17 18:54:38 -07003286Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3287 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3288 if (!texture) {
3289 return mCaches.textureCache.get(bitmap);
3290 }
3291 return texture;
3292}
3293
Romain Guy01d58e42011-01-19 21:54:02 -08003294void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3295 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003296 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003297 return;
3298 }
3299
3300 int alpha;
3301 SkXfermode::Mode mode;
3302 getAlphaAndMode(paint, &alpha, &mode);
3303
3304 setupDraw();
3305 setupDrawWithTexture(true);
3306 setupDrawAlpha8Color(paint->getColor(), alpha);
3307 setupDrawColorFilter();
3308 setupDrawShader();
3309 setupDrawBlending(true, mode);
3310 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003311 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3312 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003313 setupDrawTexture(texture->id);
3314 setupDrawPureColorUniforms();
3315 setupDrawColorFilterUniforms();
3316 setupDrawShaderUniforms();
3317 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3318
3319 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003320}
3321
Romain Guyf607bdc2010-09-10 19:20:06 -07003322// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003323#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3324#define kStdUnderline_Offset (1.0f / 9.0f)
3325#define kStdUnderline_Thickness (1.0f / 18.0f)
3326
Chris Craike63f7c622013-10-17 10:30:55 -07003327void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003328 // Handle underline and strike-through
3329 uint32_t flags = paint->getFlags();
3330 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003331 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003332
Romain Guy211370f2012-02-01 16:10:55 -08003333 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003334 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003335 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003336
Raph Levien8b4072d2012-07-30 15:50:00 -07003337 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003338 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003339
Romain Guyf6834472011-01-23 13:32:12 -08003340 int linesCount = 0;
3341 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3342 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3343
3344 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003345 float points[pointsCount];
3346 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003347
3348 if (flags & SkPaint::kUnderlineText_Flag) {
3349 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003350 points[currentPoint++] = left;
3351 points[currentPoint++] = top;
3352 points[currentPoint++] = left + underlineWidth;
3353 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003354 }
3355
3356 if (flags & SkPaint::kStrikeThruText_Flag) {
3357 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003358 points[currentPoint++] = left;
3359 points[currentPoint++] = top;
3360 points[currentPoint++] = left + underlineWidth;
3361 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003362 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003363
Romain Guy726aeba2011-06-01 14:52:00 -07003364 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003365
Romain Guy726aeba2011-06-01 14:52:00 -07003366 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003367 }
3368 }
3369}
3370
Romain Guy672433d2013-01-04 19:05:13 -08003371status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3372 if (mSnapshot->isIgnored()) {
3373 return DrawGlInfo::kStatusDone;
3374 }
3375
Romain Guy735738c2012-12-03 12:34:51 -08003376 int color = paint->getColor();
3377 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003378 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003379 color |= 0x00ffffff;
3380 }
3381 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3382
3383 return drawColorRects(rects, count, color, mode);
3384}
3385
Chris Craikf57776b2013-10-25 18:30:17 -07003386status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3387 float width, float height) {
3388 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3389
3390 // For now, always and scissor
3391 // TODO: use quickReject
3392 mCaches.enableScissor();
3393
3394 SkPaint paint;
3395 paint.setColor(0x3f000000);
3396 paint.setAntiAlias(true);
3397 VertexBuffer vertexBuffer;
3398 {
3399 //TODO: populate vertex buffer with better shadow geometry.
3400 Vector3 pivot(width/2, height/2, 0.0f);
3401 casterTransform.mapPoint3d(pivot);
3402
3403 float zScaleFactor = 0.5 + 0.0005f * pivot.z;
3404
3405 SkPath path;
3406 path.addRect(pivot.x - width * zScaleFactor, pivot.y - height * zScaleFactor,
3407 pivot.x + width * zScaleFactor, pivot.y + height * zScaleFactor);
3408 PathTessellator::tessellatePath(path, &paint, mSnapshot->transform, vertexBuffer);
3409 }
3410
3411 return drawVertexBuffer(vertexBuffer, &paint);
3412}
3413
Romain Guy735738c2012-12-03 12:34:51 -08003414status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003415 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003416 if (count == 0) {
3417 return DrawGlInfo::kStatusDone;
3418 }
Romain Guy735738c2012-12-03 12:34:51 -08003419
Romain Guy672433d2013-01-04 19:05:13 -08003420 float left = FLT_MAX;
3421 float top = FLT_MAX;
3422 float right = FLT_MIN;
3423 float bottom = FLT_MIN;
3424
Romain Guy448455f2013-07-22 13:57:50 -07003425 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003426 Vertex* vertex = mesh;
3427
Chris Craik2af46352012-11-26 18:30:17 -08003428 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003429 float l = rects[index + 0];
3430 float t = rects[index + 1];
3431 float r = rects[index + 2];
3432 float b = rects[index + 3];
3433
Romain Guy3b753822013-03-05 10:27:35 -08003434 Vertex::set(vertex++, l, t);
3435 Vertex::set(vertex++, r, t);
3436 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003437 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003438
Romain Guy3b753822013-03-05 10:27:35 -08003439 left = fminf(left, l);
3440 top = fminf(top, t);
3441 right = fmaxf(right, r);
3442 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003443 }
3444
Chris Craikf0a59072013-11-19 18:00:46 -08003445 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003446 return DrawGlInfo::kStatusDone;
3447 }
Romain Guy672433d2013-01-04 19:05:13 -08003448
Romain Guy672433d2013-01-04 19:05:13 -08003449 setupDraw();
3450 setupDrawNoTexture();
3451 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3452 setupDrawShader();
3453 setupDrawColorFilter();
3454 setupDrawBlending(mode);
3455 setupDrawProgram();
3456 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003457 setupDrawModelView(kModelViewMode_Translate, false,
3458 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003459 setupDrawColorUniforms();
3460 setupDrawShaderUniforms();
3461 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003462
Romain Guy8ce00302013-01-15 18:51:42 -08003463 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003464 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003465 }
3466
Chris Craik4063a0e2013-11-15 16:06:56 -08003467 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003468
3469 return DrawGlInfo::kStatusDrew;
3470}
3471
Romain Guy026c5e162010-06-28 17:12:22 -07003472void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003473 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003474 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003475 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003476 color |= 0x00ffffff;
3477 }
3478
Romain Guy70ca14e2010-12-13 18:24:33 -08003479 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003480 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003481 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003482 setupDrawShader();
3483 setupDrawColorFilter();
3484 setupDrawBlending(mode);
3485 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003486 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3487 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003488 setupDrawColorUniforms();
3489 setupDrawShaderUniforms(ignoreTransform);
3490 setupDrawColorFilterUniforms();
3491 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003492
Romain Guyc95c8d62010-09-17 15:31:32 -07003493 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3494}
3495
Romain Guy82ba8142010-07-09 13:25:56 -07003496void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003497 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003498 int alpha;
3499 SkXfermode::Mode mode;
3500 getAlphaAndMode(paint, &alpha, &mode);
3501
Romain Guyd21b6e12011-11-30 20:21:23 -08003502 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003503
Romain Guy3b748a42013-04-17 18:54:38 -07003504 GLvoid* vertices = (GLvoid*) NULL;
3505 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3506
3507 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003508 vertices = &mMeshVertices[0].x;
3509 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003510
3511 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3512 texture->uvMapper->map(uvs);
3513
3514 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3515 }
3516
Romain Guy3b753822013-03-05 10:27:35 -08003517 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3518 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3519 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003520
Romain Guyd21b6e12011-11-30 20:21:23 -08003521 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003522 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003523 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3524 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003525 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003526 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003527 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003528 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3529 }
3530
3531 if (texture->uvMapper) {
3532 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003533 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003534}
3535
Romain Guybd6b79b2010-06-26 00:13:53 -07003536void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003537 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3538 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003539 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003540}
3541
3542void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003543 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003544 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003545 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3546 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003547
Romain Guy746b7402010-10-26 16:27:31 -07003548 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003549 setupDrawWithTexture();
3550 setupDrawColor(alpha, alpha, alpha, alpha);
3551 setupDrawColorFilter();
3552 setupDrawBlending(blend, mode, swapSrcDst);
3553 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003554 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003555 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003556 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003557 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003558 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003559 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003560
Romain Guy6820ac82010-09-15 18:11:50 -07003561 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003562}
3563
Romain Guy3b748a42013-04-17 18:54:38 -07003564void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3565 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3566 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003567 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3568 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003569
3570 setupDraw();
3571 setupDrawWithTexture();
3572 setupDrawColor(alpha, alpha, alpha, alpha);
3573 setupDrawColorFilter();
3574 setupDrawBlending(blend, mode, swapSrcDst);
3575 setupDrawProgram();
3576 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003577 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003578 setupDrawTexture(texture);
3579 setupDrawPureColorUniforms();
3580 setupDrawColorFilterUniforms();
3581 setupDrawMeshIndices(vertices, texCoords, vbo);
3582
3583 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003584}
3585
Romain Guy886b2752013-01-04 12:26:18 -08003586void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3587 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3588 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003589 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003590
3591 setupDraw();
3592 setupDrawWithTexture(true);
3593 if (hasColor) {
3594 setupDrawAlpha8Color(color, alpha);
3595 }
3596 setupDrawColorFilter();
3597 setupDrawShader();
3598 setupDrawBlending(true, mode);
3599 setupDrawProgram();
3600 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003601 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003602 setupDrawTexture(texture);
3603 setupDrawPureColorUniforms();
3604 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003605 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003606 setupDrawMesh(vertices, texCoords);
3607
3608 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003609}
3610
Romain Guya5aed0d2010-09-09 14:42:43 -07003611void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003612 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003613 if (mCountOverdraw) {
3614 if (!mCaches.blend) glEnable(GL_BLEND);
3615 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3616 glBlendFunc(GL_ONE, GL_ONE);
3617 }
3618
3619 mCaches.blend = true;
3620 mCaches.lastSrcMode = GL_ONE;
3621 mCaches.lastDstMode = GL_ONE;
3622
3623 return;
3624 }
3625
Romain Guy82ba8142010-07-09 13:25:56 -07003626 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003627
Romain Guy82ba8142010-07-09 13:25:56 -07003628 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003629 // These blend modes are not supported by OpenGL directly and have
3630 // to be implemented using shaders. Since the shader will perform
3631 // the blending, turn blending off here
3632 // If the blend mode cannot be implemented using shaders, fall
3633 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003634 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003635 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003636 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003637 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003638
Romain Guy82bc7a72012-01-03 14:13:39 -08003639 if (mCaches.blend) {
3640 glDisable(GL_BLEND);
3641 mCaches.blend = false;
3642 }
3643
3644 return;
3645 } else {
3646 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003647 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003648 }
3649
3650 if (!mCaches.blend) {
3651 glEnable(GL_BLEND);
3652 }
3653
3654 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3655 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3656
3657 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3658 glBlendFunc(sourceMode, destMode);
3659 mCaches.lastSrcMode = sourceMode;
3660 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003661 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003662 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003663 glDisable(GL_BLEND);
3664 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003665 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003666}
3667
Romain Guy889f8d12010-07-29 14:37:42 -07003668bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003669 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003670 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003671 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003672 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003673 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003674 }
Romain Guy6926c722010-07-12 20:20:03 -07003675 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003676}
3677
Romain Guy026c5e162010-06-28 17:12:22 -07003678void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003679 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003680 TextureVertex::setUV(v++, u1, v1);
3681 TextureVertex::setUV(v++, u2, v1);
3682 TextureVertex::setUV(v++, u1, v2);
3683 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003684}
3685
Chris Craik16ecda52013-03-29 10:59:59 -07003686void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003687 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003688 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3689 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003690 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003691 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003692 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003693}
3694
Chris Craik16ecda52013-03-29 10:59:59 -07003695float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3696 float alpha;
3697 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3698 alpha = mDrawModifiers.mOverrideLayerAlpha;
3699 } else {
3700 alpha = layer->getAlpha() / 255.0f;
3701 }
3702 return alpha * mSnapshot->alpha;
3703}
3704
Romain Guy9d5316e2010-06-24 19:30:36 -07003705}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003706}; // namespace android