blob: 6599b20ba23d9020265626fb12e727670a941199 [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 Guy0f667532013-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 Guydda57022010-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 Guydda57022010-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
Chris Craikc3566d02013-02-04 16:16:33 -08002108void OpenGLRenderer::outputDisplayList(DisplayList* displayList) {
Chet Haaseed30fd82011-04-22 16:18:45 -07002109 if (displayList) {
Romain Guy7031ff62013-02-22 11:48:16 -08002110 displayList->output(1);
Chet Haaseed30fd82011-04-22 16:18:45 -07002111 }
2112}
2113
Romain Guya168d732011-03-18 16:50:13 -07002114void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2115 int alpha;
2116 SkXfermode::Mode mode;
2117 getAlphaAndMode(paint, &alpha, &mode);
2118
Romain Guy886b2752013-01-04 12:26:18 -08002119 int color = paint != NULL ? paint->getColor() : 0;
2120
Romain Guya168d732011-03-18 16:50:13 -07002121 float x = left;
2122 float y = top;
2123
Romain Guy886b2752013-01-04 12:26:18 -08002124 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2125
Romain Guya168d732011-03-18 16:50:13 -07002126 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002127 if (currentTransform().isPureTranslate()) {
2128 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2129 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002130 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002131
2132 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002133 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002134 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002135 }
2136
Romain Guy3b748a42013-04-17 18:54:38 -07002137 // No need to check for a UV mapper on the texture object, only ARGB_8888
2138 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002139 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002140 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2141 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002142}
2143
Romain Guy03c00b52013-06-20 18:30:28 -07002144/**
2145 * Important note: this method is intended to draw batches of bitmaps and
2146 * will not set the scissor enable or dirty the current layer, if any.
2147 * The caller is responsible for properly dirtying the current layer.
2148 */
Romain Guy55b6f952013-06-27 15:27:09 -07002149status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002150 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002151 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002152 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002153 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002154
Chris Craik527a3aa2013-03-04 10:19:31 -08002155 const AutoTexture autoCleanup(texture);
2156
2157 int alpha;
2158 SkXfermode::Mode mode;
2159 getAlphaAndMode(paint, &alpha, &mode);
2160
2161 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002162 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002163
2164 const float x = (int) floorf(bounds.left + 0.5f);
2165 const float y = (int) floorf(bounds.top + 0.5f);
2166 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2167 int color = paint != NULL ? paint->getColor() : 0;
2168 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2169 texture->id, paint != NULL, color, alpha, mode,
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, true,
2172 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002173 } else {
2174 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2175 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002176 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002177 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2178 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002179 }
2180
2181 return DrawGlInfo::kStatusDrew;
2182}
2183
Chet Haase48659092012-05-31 15:21:51 -07002184status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002185 const float right = left + bitmap->width();
2186 const float bottom = top + bitmap->height();
2187
Chris Craikf0a59072013-11-19 18:00:46 -08002188 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002189 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002190 }
2191
Romain Guya1d3c912011-12-13 14:55:06 -08002192 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002193 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002194 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002195 const AutoTexture autoCleanup(texture);
2196
Romain Guy211370f2012-02-01 16:10:55 -08002197 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002198 drawAlphaBitmap(texture, left, top, paint);
2199 } else {
2200 drawTextureRect(left, top, right, bottom, texture, paint);
2201 }
Chet Haase48659092012-05-31 15:21:51 -07002202
2203 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002204}
2205
Chet Haase48659092012-05-31 15:21:51 -07002206status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002207 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2208 const mat4 transform(*matrix);
2209 transform.mapRect(r);
2210
Chris Craikf0a59072013-11-19 18:00:46 -08002211 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002212 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002213 }
2214
Romain Guya1d3c912011-12-13 14:55:06 -08002215 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002216 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002217 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002218 const AutoTexture autoCleanup(texture);
2219
Romain Guy5b3b3522010-10-27 18:57:51 -07002220 // This could be done in a cheaper way, all we need is pass the matrix
2221 // to the vertex shader. The save/restore is a bit overkill.
2222 save(SkCanvas::kMatrix_SaveFlag);
2223 concatMatrix(matrix);
Romain Guy886b2752013-01-04 12:26:18 -08002224 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2225 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2226 } else {
2227 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2228 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002229 restore();
Chet Haase48659092012-05-31 15:21:51 -07002230
2231 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002232}
2233
Chet Haase48659092012-05-31 15:21:51 -07002234status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002235 const float right = left + bitmap->width();
2236 const float bottom = top + bitmap->height();
2237
Chris Craikf0a59072013-11-19 18:00:46 -08002238 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002239 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002240 }
2241
2242 mCaches.activeTexture(0);
2243 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2244 const AutoTexture autoCleanup(texture);
2245
Romain Guy886b2752013-01-04 12:26:18 -08002246 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2247 drawAlphaBitmap(texture, left, top, paint);
2248 } else {
2249 drawTextureRect(left, top, right, bottom, texture, paint);
2250 }
Chet Haase48659092012-05-31 15:21:51 -07002251
2252 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002253}
2254
Chet Haase48659092012-05-31 15:21:51 -07002255status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002256 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002257 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002258 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002259 }
2260
Chris Craik39a908c2013-06-13 14:39:01 -07002261 // TODO: use quickReject on bounds from vertices
2262 mCaches.enableScissor();
2263
Romain Guyb18d2d02011-02-10 15:52:54 -08002264 float left = FLT_MAX;
2265 float top = FLT_MAX;
2266 float right = FLT_MIN;
2267 float bottom = FLT_MIN;
2268
Romain Guya92bb4d2012-10-16 11:08:44 -07002269 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002270
Romain Guyff316ec2013-02-13 18:39:43 -08002271 ColorTextureVertex mesh[count];
2272 ColorTextureVertex* vertex = mesh;
2273
2274 bool cleanupColors = false;
2275 if (!colors) {
2276 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2277 colors = new int[colorsCount];
2278 memset(colors, 0xff, colorsCount * sizeof(int));
2279 cleanupColors = true;
2280 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002281
Romain Guy3b748a42013-04-17 18:54:38 -07002282 mCaches.activeTexture(0);
2283 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2284 const UvMapper& mapper(getMapper(texture));
2285
Romain Guy5a7b4662011-01-20 19:09:30 -08002286 for (int32_t y = 0; y < meshHeight; y++) {
2287 for (int32_t x = 0; x < meshWidth; x++) {
2288 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2289
2290 float u1 = float(x) / meshWidth;
2291 float u2 = float(x + 1) / meshWidth;
2292 float v1 = float(y) / meshHeight;
2293 float v2 = float(y + 1) / meshHeight;
2294
Romain Guy3b748a42013-04-17 18:54:38 -07002295 mapper.map(u1, v1, u2, v2);
2296
Romain Guy5a7b4662011-01-20 19:09:30 -08002297 int ax = i + (meshWidth + 1) * 2;
2298 int ay = ax + 1;
2299 int bx = i;
2300 int by = bx + 1;
2301 int cx = i + 2;
2302 int cy = cx + 1;
2303 int dx = i + (meshWidth + 1) * 2 + 2;
2304 int dy = dx + 1;
2305
Romain Guyff316ec2013-02-13 18:39:43 -08002306 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2307 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2308 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002309
Romain Guyff316ec2013-02-13 18:39:43 -08002310 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2311 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2312 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002313
Romain Guya92bb4d2012-10-16 11:08:44 -07002314 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2315 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2316 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2317 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002318 }
2319 }
2320
Chris Craikf0a59072013-11-19 18:00:46 -08002321 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002322 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002323 return DrawGlInfo::kStatusDone;
2324 }
2325
Romain Guyff316ec2013-02-13 18:39:43 -08002326 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002327 texture = mCaches.textureCache.get(bitmap);
2328 if (!texture) {
2329 if (cleanupColors) delete[] colors;
2330 return DrawGlInfo::kStatusDone;
2331 }
Romain Guyff316ec2013-02-13 18:39:43 -08002332 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002333 const AutoTexture autoCleanup(texture);
2334
2335 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2336 texture->setFilter(FILTER(paint), true);
2337
2338 int alpha;
2339 SkXfermode::Mode mode;
2340 getAlphaAndMode(paint, &alpha, &mode);
2341
Romain Guyff316ec2013-02-13 18:39:43 -08002342 float a = alpha / 255.0f;
2343
Romain Guya92bb4d2012-10-16 11:08:44 -07002344 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002345 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002346 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002347
Romain Guyff316ec2013-02-13 18:39:43 -08002348 setupDraw();
2349 setupDrawWithTextureAndColor();
2350 setupDrawColor(a, a, a, a);
2351 setupDrawColorFilter();
2352 setupDrawBlending(true, mode, false);
2353 setupDrawProgram();
2354 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002355 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002356 setupDrawTexture(texture->id);
2357 setupDrawPureColorUniforms();
2358 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002359 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002360
2361 glDrawArrays(GL_TRIANGLES, 0, count);
2362
Romain Guyff316ec2013-02-13 18:39:43 -08002363 int slot = mCaches.currentProgram->getAttrib("colors");
2364 if (slot >= 0) {
2365 glDisableVertexAttribArray(slot);
2366 }
2367
2368 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002369
2370 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002371}
2372
Chet Haase48659092012-05-31 15:21:51 -07002373status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002374 float srcLeft, float srcTop, float srcRight, float srcBottom,
2375 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002376 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002377 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002378 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002379 }
2380
Romain Guya1d3c912011-12-13 14:55:06 -08002381 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002382 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002383 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002384 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002385
Romain Guy8ba548f2010-06-30 19:21:21 -07002386 const float width = texture->width;
2387 const float height = texture->height;
2388
Romain Guy3b748a42013-04-17 18:54:38 -07002389 float u1 = fmax(0.0f, srcLeft / width);
2390 float v1 = fmax(0.0f, srcTop / height);
2391 float u2 = fmin(1.0f, srcRight / width);
2392 float v2 = fmin(1.0f, srcBottom / height);
2393
2394 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002395
Romain Guy03750a02010-10-18 14:06:08 -07002396 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002397 resetDrawTextureTexCoords(u1, v1, u2, v2);
2398
Romain Guy03750a02010-10-18 14:06:08 -07002399 int alpha;
2400 SkXfermode::Mode mode;
2401 getAlphaAndMode(paint, &alpha, &mode);
2402
Romain Guyd21b6e12011-11-30 20:21:23 -08002403 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2404
Romain Guy886b2752013-01-04 12:26:18 -08002405 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2406 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002407
Romain Guy886b2752013-01-04 12:26:18 -08002408 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2409 // Apply a scale transform on the canvas only when a shader is in use
2410 // Skia handles the ratio between the dst and src rects as a scale factor
2411 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002412 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002413 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002414
Romain Guy3b753822013-03-05 10:27:35 -08002415 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2416 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2417 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002418
2419 dstRight = x + (dstRight - dstLeft);
2420 dstBottom = y + (dstBottom - dstTop);
2421
2422 dstLeft = x;
2423 dstTop = y;
2424
2425 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2426 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002427 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002428 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002429 }
2430
2431 if (CC_UNLIKELY(useScaleTransform)) {
2432 save(SkCanvas::kMatrix_SaveFlag);
2433 translate(dstLeft, dstTop);
2434 scale(scaleX, scaleY);
2435
2436 dstLeft = 0.0f;
2437 dstTop = 0.0f;
2438
2439 dstRight = srcRight - srcLeft;
2440 dstBottom = srcBottom - srcTop;
2441 }
2442
2443 if (CC_UNLIKELY(bitmap->getConfig() == SkBitmap::kA8_Config)) {
2444 int color = paint ? paint->getColor() : 0;
2445 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2446 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002447 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002448 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2449 } else {
2450 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2451 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002452 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002453 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2454 }
2455
2456 if (CC_UNLIKELY(useScaleTransform)) {
2457 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002458 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002459
2460 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002461
2462 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002463}
2464
Romain Guy3b748a42013-04-17 18:54:38 -07002465status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002466 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002467 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002468 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002469 }
2470
Romain Guy4c2547f2013-06-11 16:19:24 -07002471 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002472 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2473 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002474
Romain Guy03c00b52013-06-20 18:30:28 -07002475 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002476}
2477
Romain Guy03c00b52013-06-20 18:30:28 -07002478status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2479 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002480 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002481 return DrawGlInfo::kStatusDone;
2482 }
2483
Romain Guy211370f2012-02-01 16:10:55 -08002484 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002485 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002486 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002487 if (!texture) return DrawGlInfo::kStatusDone;
2488 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002489
Romain Guya4adcf02013-02-28 12:15:35 -08002490 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2491 texture->setFilter(GL_LINEAR, true);
2492
Romain Guy03c00b52013-06-20 18:30:28 -07002493 int alpha;
2494 SkXfermode::Mode mode;
2495 getAlphaAndMode(paint, &alpha, &mode);
2496
Romain Guy3b753822013-03-05 10:27:35 -08002497 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002498 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002499 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002500 const float offsetX = left + currentTransform().getTranslateX();
2501 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002502 const size_t count = mesh->quads.size();
2503 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002504 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002505 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002506 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2507 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2508 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002509 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002510 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002511 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002512 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002513 }
2514 }
2515
Chris Craik4063a0e2013-11-15 16:06:56 -08002516 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002517 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002518 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2519 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002520
Romain Guy3b748a42013-04-17 18:54:38 -07002521 right = x + right - left;
2522 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002523 left = x;
2524 top = y;
2525 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002526 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002527 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2528 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2529 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2530 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002531 }
Chet Haase48659092012-05-31 15:21:51 -07002532
2533 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002534}
2535
Romain Guy03c00b52013-06-20 18:30:28 -07002536/**
2537 * Important note: this method is intended to draw batches of 9-patch objects and
2538 * will not set the scissor enable or dirty the current layer, if any.
2539 * The caller is responsible for properly dirtying the current layer.
2540 */
2541status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2542 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2543 mCaches.activeTexture(0);
2544 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2545 if (!texture) return DrawGlInfo::kStatusDone;
2546 const AutoTexture autoCleanup(texture);
2547
2548 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2549 texture->setFilter(GL_LINEAR, true);
2550
2551 int alpha;
2552 SkXfermode::Mode mode;
2553 getAlphaAndMode(paint, &alpha, &mode);
2554
2555 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002556 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002557 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002558
2559 return DrawGlInfo::kStatusDrew;
2560}
2561
Chris Craik65cd6122012-12-10 17:56:27 -08002562status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2563 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002564 // not missing call to quickReject/dirtyLayer, always done at a higher level
2565
Chris Craik6d29c8d2013-05-08 18:35:44 -07002566 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002567 // no vertices to draw
2568 return DrawGlInfo::kStatusDone;
2569 }
2570
Chris Craikcb4d6002012-09-25 12:00:29 -07002571 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002572 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2573 bool isAA = paint->isAntiAlias();
2574
Chris Craik710f46d2012-09-17 17:25:49 -07002575 setupDraw();
2576 setupDrawNoTexture();
2577 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002578 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2579 setupDrawColorFilter();
2580 setupDrawShader();
2581 setupDrawBlending(isAA, mode);
2582 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002583 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002584 setupDrawColorUniforms();
2585 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002586 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002587
Chris Craik710f46d2012-09-17 17:25:49 -07002588 void* vertices = vertexBuffer.getBuffer();
2589 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002590 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002591 mCaches.resetTexCoordsVertexPointer();
2592 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002593
Chris Craik710f46d2012-09-17 17:25:49 -07002594 int alphaSlot = -1;
2595 if (isAA) {
2596 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2597 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002598
Chris Craik710f46d2012-09-17 17:25:49 -07002599 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002600 glEnableVertexAttribArray(alphaSlot);
2601 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002602 }
Romain Guy04299382012-07-18 17:15:41 -07002603
Chris Craik6d29c8d2013-05-08 18:35:44 -07002604 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002605
Chris Craik710f46d2012-09-17 17:25:49 -07002606 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002607 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002608 }
Chris Craik65cd6122012-12-10 17:56:27 -08002609
2610 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002611}
2612
2613/**
Chris Craik65cd6122012-12-10 17:56:27 -08002614 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2615 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2616 * screen space in all directions. However, instead of using a fragment shader to compute the
2617 * translucency of the color from its position, we simply use a varying parameter to define how far
2618 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2619 *
2620 * Doesn't yet support joins, caps, or path effects.
2621 */
2622status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2623 VertexBuffer vertexBuffer;
2624 // TODO: try clipping large paths to viewport
2625 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2626
Romain Guyc46d07a2013-03-15 19:06:39 -07002627 if (hasLayer()) {
2628 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002629 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002630 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2631 }
Chris Craik65cd6122012-12-10 17:56:27 -08002632
2633 return drawVertexBuffer(vertexBuffer, paint);
2634}
2635
2636/**
2637 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2638 * and additional geometry for defining an alpha slope perimeter.
2639 *
2640 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2641 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2642 * in-shader alpha region, but found it to be taxing on some GPUs.
2643 *
2644 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2645 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002646 */
Chet Haase48659092012-05-31 15:21:51 -07002647status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002648 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002649
Chris Craik65cd6122012-12-10 17:56:27 -08002650 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002651
Chris Craik65cd6122012-12-10 17:56:27 -08002652 VertexBuffer buffer;
2653 SkRect bounds;
2654 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002655
Chris Craikf0a59072013-11-19 18:00:46 -08002656 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2657 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002658 return DrawGlInfo::kStatusDone;
2659 }
2660
Romain Guy3b753822013-03-05 10:27:35 -08002661 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002662
Chris Craik65cd6122012-12-10 17:56:27 -08002663 bool useOffset = !paint->isAntiAlias();
2664 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002665}
2666
Chet Haase48659092012-05-31 15:21:51 -07002667status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002668 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002669
Chris Craik6d29c8d2013-05-08 18:35:44 -07002670 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002671
Chris Craik6d29c8d2013-05-08 18:35:44 -07002672 VertexBuffer buffer;
2673 SkRect bounds;
2674 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002675
Chris Craikf0a59072013-11-19 18:00:46 -08002676 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2677 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002678 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002679 }
2680
Chris Craik6d29c8d2013-05-08 18:35:44 -07002681 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002682
Chris Craik6d29c8d2013-05-08 18:35:44 -07002683 bool useOffset = !paint->isAntiAlias();
2684 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002685}
2686
Chet Haase48659092012-05-31 15:21:51 -07002687status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002688 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002689 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002690
Romain Guyae88e5e2010-10-22 17:49:18 -07002691 Rect& clip(*mSnapshot->clipRect);
2692 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002693
Romain Guy3d58c032010-07-14 16:34:53 -07002694 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002695
2696 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002697}
Romain Guy9d5316e2010-06-24 19:30:36 -07002698
Chet Haase48659092012-05-31 15:21:51 -07002699status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2700 SkPaint* paint) {
2701 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002702 const AutoTexture autoCleanup(texture);
2703
2704 const float x = left + texture->left - texture->offset;
2705 const float y = top + texture->top - texture->offset;
2706
2707 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002708
2709 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002710}
2711
Chet Haase48659092012-05-31 15:21:51 -07002712status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002713 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002714 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002715 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002716 return DrawGlInfo::kStatusDone;
2717 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002718
Chris Craikcb4d6002012-09-25 12:00:29 -07002719 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002720 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002721 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002722 right - left, bottom - top, rx, ry, p);
2723 return drawShape(left, top, texture, p);
2724 }
2725
2726 SkPath path;
2727 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002728 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2729 float outset = p->getStrokeWidth() / 2;
2730 rect.outset(outset, outset);
2731 rx += outset;
2732 ry += outset;
2733 }
Chris Craik710f46d2012-09-17 17:25:49 -07002734 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002735 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002736}
2737
Chris Craik710f46d2012-09-17 17:25:49 -07002738status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002739 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002740 x + radius, y + radius, p) ||
2741 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002742 return DrawGlInfo::kStatusDone;
2743 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002744 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002745 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002746 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002747 return drawShape(x - radius, y - radius, texture, p);
2748 }
2749
2750 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002751 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2752 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2753 } else {
2754 path.addCircle(x, y, radius);
2755 }
Chris Craik65cd6122012-12-10 17:56:27 -08002756 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002757}
Romain Guy01d58e42011-01-19 21:54:02 -08002758
Chet Haase48659092012-05-31 15:21:51 -07002759status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002760 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002761 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002762 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002763 return DrawGlInfo::kStatusDone;
2764 }
Romain Guy01d58e42011-01-19 21:54:02 -08002765
Chris Craikcb4d6002012-09-25 12:00:29 -07002766 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002767 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002768 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002769 return drawShape(left, top, texture, p);
2770 }
2771
2772 SkPath path;
2773 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002774 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2775 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2776 }
Chris Craik710f46d2012-09-17 17:25:49 -07002777 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002778 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002779}
2780
Chet Haase48659092012-05-31 15:21:51 -07002781status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002782 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002783 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002784 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002785 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002786 }
2787
Chris Craik780c1282012-10-04 14:10:49 -07002788 if (fabs(sweepAngle) >= 360.0f) {
2789 return drawOval(left, top, right, bottom, p);
2790 }
2791
2792 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002793 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002794 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002795 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002796 startAngle, sweepAngle, useCenter, p);
2797 return drawShape(left, top, texture, p);
2798 }
2799
2800 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2801 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2802 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2803 }
2804
2805 SkPath path;
2806 if (useCenter) {
2807 path.moveTo(rect.centerX(), rect.centerY());
2808 }
2809 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2810 if (useCenter) {
2811 path.close();
2812 }
Chris Craik65cd6122012-12-10 17:56:27 -08002813 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002814}
2815
Romain Guycf8675e2012-10-02 12:32:25 -07002816// See SkPaintDefaults.h
2817#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2818
Chet Haase48659092012-05-31 15:21:51 -07002819status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002820 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002821 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002822 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002823 }
2824
Chris Craik710f46d2012-09-17 17:25:49 -07002825 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002826 // only fill style is supported by drawConvexPath, since others have to handle joins
2827 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2828 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2829 mCaches.activeTexture(0);
2830 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002831 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002832 return drawShape(left, top, texture, p);
2833 }
2834
2835 SkPath path;
2836 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2837 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2838 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2839 }
2840 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002841 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002842 }
2843
Romain Guy3b753822013-03-05 10:27:35 -08002844 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002845 SkPath path;
2846 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002847 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002848 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002849 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002850 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002851 }
Romain Guyc7d53492010-06-25 13:41:57 -07002852}
Romain Guy9d5316e2010-06-24 19:30:36 -07002853
Raph Levien416a8472012-07-19 22:48:17 -07002854void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2855 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2856 float x, float y) {
2857 mCaches.activeTexture(0);
2858
2859 // NOTE: The drop shadow will not perform gamma correction
2860 // if shader-based correction is enabled
2861 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2862 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002863 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002864 // If the drop shadow exceeds the max texture size or couldn't be
2865 // allocated, skip drawing
2866 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002867 const AutoTexture autoCleanup(shadow);
2868
Chris Craikc3566d02013-02-04 16:16:33 -08002869 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2870 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002871
Chris Craikc3566d02013-02-04 16:16:33 -08002872 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2873 int shadowColor = mDrawModifiers.mShadowColor;
2874 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002875 shadowColor = 0xffffffff;
2876 }
2877
2878 setupDraw();
2879 setupDrawWithTexture(true);
2880 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2881 setupDrawColorFilter();
2882 setupDrawShader();
2883 setupDrawBlending(true, mode);
2884 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002885 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2886 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002887 setupDrawTexture(shadow->id);
2888 setupDrawPureColorUniforms();
2889 setupDrawColorFilterUniforms();
2890 setupDrawShaderUniforms();
2891 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2892
2893 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2894}
2895
Romain Guy768bffc2013-02-27 13:50:45 -08002896bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2897 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2898 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2899}
2900
Chet Haase48659092012-05-31 15:21:51 -07002901status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002902 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002903 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002904 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002905 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002906
Romain Guy671d6cf2012-01-18 12:39:17 -08002907 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002908 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002909 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002910 }
2911
Chris Craik39a908c2013-06-13 14:39:01 -07002912 mCaches.enableScissor();
2913
Romain Guy671d6cf2012-01-18 12:39:17 -08002914 float x = 0.0f;
2915 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002916 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002917 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002918 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2919 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002920 }
2921
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002922 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002923 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002924
2925 int alpha;
2926 SkXfermode::Mode mode;
2927 getAlphaAndMode(paint, &alpha, &mode);
2928
Chris Craikc3566d02013-02-04 16:16:33 -08002929 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002930 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2931 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002932 }
2933
Romain Guy671d6cf2012-01-18 12:39:17 -08002934 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002935 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002936 if (pureTranslate && !linearFilter) {
2937 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2938 }
Romain Guy257ae352013-03-20 16:31:12 -07002939 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002940
2941 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2942 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2943
Romain Guy211370f2012-02-01 16:10:55 -08002944 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002945
Victoria Lease1e546812013-06-25 14:25:17 -07002946 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002947 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002948 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002949 if (hasActiveLayer) {
2950 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002951 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002952 }
2953 dirtyLayerUnchecked(bounds, getRegion());
2954 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002955 }
Chet Haase48659092012-05-31 15:21:51 -07002956
2957 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002958}
2959
Romain Guy624234f2013-03-05 16:43:31 -08002960mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2961 mat4 fontTransform;
2962 if (CC_LIKELY(transform.isPureTranslate())) {
2963 fontTransform = mat4::identity();
2964 } else {
2965 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002966 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002967 } else {
2968 float sx, sy;
2969 currentTransform().decomposeScale(sx, sy);
2970 fontTransform.loadScale(sx, sy, 1.0f);
2971 }
2972 }
2973 return fontTransform;
2974}
2975
Chris Craik41541822013-05-03 16:35:54 -07002976status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2977 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002978 DrawOpMode drawOpMode) {
2979
Chris Craik527a3aa2013-03-04 10:19:31 -08002980 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002981 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2982 // drawing as ops from DeferredDisplayList are already filtered for these
2983 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002984 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002985 return DrawGlInfo::kStatusDone;
2986 }
Romain Guycac5fd32011-12-01 20:08:50 -08002987 }
2988
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002989 const float oldX = x;
2990 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002991
2992 const mat4& transform = currentTransform();
2993 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002994
Romain Guy211370f2012-02-01 16:10:55 -08002995 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002996 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2997 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002998 }
2999
Romain Guy86568192010-12-14 15:55:39 -08003000 int alpha;
3001 SkXfermode::Mode mode;
3002 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07003003
Romain Guyc74f45a2013-02-26 19:10:14 -08003004 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
3005
Chris Craikc3566d02013-02-04 16:16:33 -08003006 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08003007 fontRenderer.setFont(paint, mat4::identity());
3008 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
3009 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07003010 }
3011
Romain Guy19d4dd82013-03-04 11:14:26 -08003012 const bool hasActiveLayer = hasLayer();
3013
Romain Guy624234f2013-03-05 16:43:31 -08003014 // We only pass a partial transform to the font renderer. That partial
3015 // matrix defines how glyphs are rasterized. Typically we want glyphs
3016 // to be rasterized at their final size on screen, which means the partial
3017 // matrix needs to take the scale factor into account.
3018 // When a partial matrix is used to transform glyphs during rasterization,
3019 // the mesh is generated with the inverse transform (in the case of scale,
3020 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3021 // apply the full transform matrix at draw time in the vertex shader.
3022 // Applying the full matrix in the shader is the easiest way to handle
3023 // rotation and perspective and allows us to always generated quads in the
3024 // font renderer which greatly simplifies the code, clipping in particular.
3025 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003026 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003027
Romain Guy6620c6d2010-12-06 18:07:02 -08003028 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003029 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003030 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003031
Romain Guy624234f2013-03-05 16:43:31 -08003032 // TODO: Implement better clipping for scaled/rotated text
3033 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003034 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 -07003035
Raph Levien996e57c2012-07-23 15:22:52 -07003036 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003037 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003038
3039 // don't call issuedrawcommand, do it at end of batch
3040 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003041 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003042 SkPaint paintCopy(*paint);
3043 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3044 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003045 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003046 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003047 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003048 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003049 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003050
Chris Craik527a3aa2013-03-04 10:19:31 -08003051 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003052 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003053 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003054 }
Chris Craik41541822013-05-03 16:35:54 -07003055 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003056 }
Romain Guy694b5192010-07-21 21:33:20 -07003057
Chris Craike63f7c622013-10-17 10:30:55 -07003058 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003059
3060 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003061}
3062
Chet Haase48659092012-05-31 15:21:51 -07003063status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003064 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003065 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003066 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003067 }
3068
Chris Craik39a908c2013-06-13 14:39:01 -07003069 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3070 mCaches.enableScissor();
3071
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003072 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003073 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003074 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003075
3076 int alpha;
3077 SkXfermode::Mode mode;
3078 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003079 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003080
Romain Guy97771732012-02-28 18:17:02 -08003081 const Rect* clip = &mSnapshot->getLocalClip();
3082 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 -08003083
Romain Guy97771732012-02-28 18:17:02 -08003084 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003085
3086 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003087 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003088 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003089 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003090 dirtyLayerUnchecked(bounds, getRegion());
3091 }
Romain Guy97771732012-02-28 18:17:02 -08003092 }
Chet Haase48659092012-05-31 15:21:51 -07003093
3094 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003095}
3096
Chet Haase48659092012-05-31 15:21:51 -07003097status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3098 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003099
Romain Guya1d3c912011-12-13 14:55:06 -08003100 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003101
Romain Guyfb8b7632010-08-23 21:05:08 -07003102 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003103 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003104 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003105
Romain Guy8b55f372010-08-18 17:10:07 -07003106 const float x = texture->left - texture->offset;
3107 const float y = texture->top - texture->offset;
3108
Romain Guy01d58e42011-01-19 21:54:02 -08003109 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003110
3111 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003112}
3113
Chris Craika08f95c2013-03-15 17:24:33 -07003114status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003115 if (!layer) {
3116 return DrawGlInfo::kStatusDone;
3117 }
3118
Romain Guyb2e2f242012-10-17 18:18:35 -07003119 mat4* transform = NULL;
3120 if (layer->isTextureLayer()) {
3121 transform = &layer->getTransform();
3122 if (!transform->isIdentity()) {
3123 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003124 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003125 }
3126 }
3127
Chris Craik39a908c2013-06-13 14:39:01 -07003128 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08003129 const bool rejected = calculateQuickRejectForScissor(x, y,
3130 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003131
3132 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003133 if (transform && !transform->isIdentity()) {
3134 restore();
3135 }
Chet Haase48659092012-05-31 15:21:51 -07003136 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003137 }
3138
Romain Guy5bb3c732012-11-29 17:52:58 -08003139 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003140
Chris Craik39a908c2013-06-13 14:39:01 -07003141 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003142 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003143
Romain Guy211370f2012-02-01 16:10:55 -08003144 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003145 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3146 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003147
Romain Guyc88e3572011-01-22 00:32:12 -08003148 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003149 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3150 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003151 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003152 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003153 setupDraw();
3154 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003155 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003156 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003157 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003158 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003159 setupDrawPureColorUniforms();
3160 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003161 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003162 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3163 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3164 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003165
Romain Guyd21b6e12011-11-30 20:21:23 -08003166 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003167 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003168 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003169 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003170 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003171 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003172 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3173 }
Romain Guyf219da52011-01-16 12:54:25 -08003174
Romain Guy448455f2013-07-22 13:57:50 -07003175 TextureVertex* mesh = &layer->mesh[0];
3176 GLsizei elementsCount = layer->meshElementCount;
3177
3178 while (elementsCount > 0) {
3179 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3180
Romain Guy3380cfd2013-08-15 16:57:57 -07003181 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003182 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3183 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3184
3185 elementsCount -= drawCount;
3186 // Though there are 4 vertices in a quad, we use 6 indices per
3187 // quad to draw with GL_TRIANGLES
3188 mesh += (drawCount / 6) * 4;
3189 }
Romain Guyf219da52011-01-16 12:54:25 -08003190
Romain Guy3a3133d2011-02-01 22:59:58 -08003191#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003192 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003193#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003194 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003195
Chris Craikc3566d02013-02-04 16:16:33 -08003196 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003197
Romain Guy5bb3c732012-11-29 17:52:58 -08003198 if (layer->debugDrawUpdate) {
3199 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003200 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3201 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3202 }
Romain Guyf219da52011-01-16 12:54:25 -08003203 }
Chris Craik34416ea2013-04-15 16:08:28 -07003204 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003205
Romain Guyb2e2f242012-10-17 18:18:35 -07003206 if (transform && !transform->isIdentity()) {
3207 restore();
3208 }
3209
Chet Haase48659092012-05-31 15:21:51 -07003210 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003211}
3212
Romain Guy6926c722010-07-12 20:20:03 -07003213///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003214// Shaders
3215///////////////////////////////////////////////////////////////////////////////
3216
3217void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003218 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003219}
3220
Romain Guy06f96e22010-07-30 19:18:16 -07003221void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003222 mDrawModifiers.mShader = shader;
3223 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003224 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003225 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003226}
3227
Romain Guyd27977d2010-07-14 19:18:51 -07003228///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003229// Color filters
3230///////////////////////////////////////////////////////////////////////////////
3231
3232void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003233 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003234}
3235
3236void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003237 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003238}
3239
3240///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003241// Drop shadow
3242///////////////////////////////////////////////////////////////////////////////
3243
3244void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003245 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003246}
3247
3248void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003249 mDrawModifiers.mHasShadow = true;
3250 mDrawModifiers.mShadowRadius = radius;
3251 mDrawModifiers.mShadowDx = dx;
3252 mDrawModifiers.mShadowDy = dy;
3253 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003254}
3255
3256///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003257// Draw filters
3258///////////////////////////////////////////////////////////////////////////////
3259
3260void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003261 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3262 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003263 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003264 mDrawModifiers.mPaintFilterClearBits = 0;
3265 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003266}
3267
3268void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003269 mDrawModifiers.mHasDrawFilter = true;
3270 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3271 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003272}
3273
Chris Craika08f95c2013-03-15 17:24:33 -07003274SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003275 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003276 return paint;
3277 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003278
3279 uint32_t flags = paint->getFlags();
3280
3281 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003282 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3283 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003284
3285 return &mFilteredPaint;
3286}
3287
3288///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003289// Drawing implementation
3290///////////////////////////////////////////////////////////////////////////////
3291
Romain Guy3b748a42013-04-17 18:54:38 -07003292Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3293 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3294 if (!texture) {
3295 return mCaches.textureCache.get(bitmap);
3296 }
3297 return texture;
3298}
3299
Romain Guy01d58e42011-01-19 21:54:02 -08003300void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3301 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003302 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003303 return;
3304 }
3305
3306 int alpha;
3307 SkXfermode::Mode mode;
3308 getAlphaAndMode(paint, &alpha, &mode);
3309
3310 setupDraw();
3311 setupDrawWithTexture(true);
3312 setupDrawAlpha8Color(paint->getColor(), alpha);
3313 setupDrawColorFilter();
3314 setupDrawShader();
3315 setupDrawBlending(true, mode);
3316 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003317 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3318 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003319 setupDrawTexture(texture->id);
3320 setupDrawPureColorUniforms();
3321 setupDrawColorFilterUniforms();
3322 setupDrawShaderUniforms();
3323 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3324
3325 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003326}
3327
Romain Guyf607bdc2010-09-10 19:20:06 -07003328// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003329#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3330#define kStdUnderline_Offset (1.0f / 9.0f)
3331#define kStdUnderline_Thickness (1.0f / 18.0f)
3332
Chris Craike63f7c622013-10-17 10:30:55 -07003333void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003334 // Handle underline and strike-through
3335 uint32_t flags = paint->getFlags();
3336 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003337 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003338
Romain Guy211370f2012-02-01 16:10:55 -08003339 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003340 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003341 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003342
Raph Levien8b4072d2012-07-30 15:50:00 -07003343 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003344 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003345
Romain Guyf6834472011-01-23 13:32:12 -08003346 int linesCount = 0;
3347 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3348 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3349
3350 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003351 float points[pointsCount];
3352 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003353
3354 if (flags & SkPaint::kUnderlineText_Flag) {
3355 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003356 points[currentPoint++] = left;
3357 points[currentPoint++] = top;
3358 points[currentPoint++] = left + underlineWidth;
3359 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003360 }
3361
3362 if (flags & SkPaint::kStrikeThruText_Flag) {
3363 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003364 points[currentPoint++] = left;
3365 points[currentPoint++] = top;
3366 points[currentPoint++] = left + underlineWidth;
3367 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003368 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003369
Romain Guy726aeba2011-06-01 14:52:00 -07003370 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003371
Romain Guy726aeba2011-06-01 14:52:00 -07003372 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003373 }
3374 }
3375}
3376
Romain Guy672433d2013-01-04 19:05:13 -08003377status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3378 if (mSnapshot->isIgnored()) {
3379 return DrawGlInfo::kStatusDone;
3380 }
3381
Romain Guy735738c2012-12-03 12:34:51 -08003382 int color = paint->getColor();
3383 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003384 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003385 color |= 0x00ffffff;
3386 }
3387 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3388
3389 return drawColorRects(rects, count, color, mode);
3390}
3391
Chris Craikf57776b2013-10-25 18:30:17 -07003392status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3393 float width, float height) {
3394 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3395
3396 // For now, always and scissor
3397 // TODO: use quickReject
3398 mCaches.enableScissor();
3399
3400 SkPaint paint;
3401 paint.setColor(0x3f000000);
3402 paint.setAntiAlias(true);
3403 VertexBuffer vertexBuffer;
3404 {
3405 //TODO: populate vertex buffer with better shadow geometry.
3406 Vector3 pivot(width/2, height/2, 0.0f);
3407 casterTransform.mapPoint3d(pivot);
3408
3409 float zScaleFactor = 0.5 + 0.0005f * pivot.z;
3410
3411 SkPath path;
3412 path.addRect(pivot.x - width * zScaleFactor, pivot.y - height * zScaleFactor,
3413 pivot.x + width * zScaleFactor, pivot.y + height * zScaleFactor);
3414 PathTessellator::tessellatePath(path, &paint, mSnapshot->transform, vertexBuffer);
3415 }
3416
3417 return drawVertexBuffer(vertexBuffer, &paint);
3418}
3419
Romain Guy735738c2012-12-03 12:34:51 -08003420status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003421 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003422 if (count == 0) {
3423 return DrawGlInfo::kStatusDone;
3424 }
Romain Guy735738c2012-12-03 12:34:51 -08003425
Romain Guy672433d2013-01-04 19:05:13 -08003426 float left = FLT_MAX;
3427 float top = FLT_MAX;
3428 float right = FLT_MIN;
3429 float bottom = FLT_MIN;
3430
Romain Guy448455f2013-07-22 13:57:50 -07003431 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003432 Vertex* vertex = mesh;
3433
Chris Craik2af46352012-11-26 18:30:17 -08003434 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003435 float l = rects[index + 0];
3436 float t = rects[index + 1];
3437 float r = rects[index + 2];
3438 float b = rects[index + 3];
3439
Romain Guy3b753822013-03-05 10:27:35 -08003440 Vertex::set(vertex++, l, t);
3441 Vertex::set(vertex++, r, t);
3442 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003443 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003444
Romain Guy3b753822013-03-05 10:27:35 -08003445 left = fminf(left, l);
3446 top = fminf(top, t);
3447 right = fmaxf(right, r);
3448 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003449 }
3450
Chris Craikf0a59072013-11-19 18:00:46 -08003451 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003452 return DrawGlInfo::kStatusDone;
3453 }
Romain Guy672433d2013-01-04 19:05:13 -08003454
Romain Guy672433d2013-01-04 19:05:13 -08003455 setupDraw();
3456 setupDrawNoTexture();
3457 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3458 setupDrawShader();
3459 setupDrawColorFilter();
3460 setupDrawBlending(mode);
3461 setupDrawProgram();
3462 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003463 setupDrawModelView(kModelViewMode_Translate, false,
3464 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003465 setupDrawColorUniforms();
3466 setupDrawShaderUniforms();
3467 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003468
Romain Guy8ce00302013-01-15 18:51:42 -08003469 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003470 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003471 }
3472
Chris Craik4063a0e2013-11-15 16:06:56 -08003473 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003474
3475 return DrawGlInfo::kStatusDrew;
3476}
3477
Romain Guy026c5e162010-06-28 17:12:22 -07003478void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003479 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003480 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003481 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003482 color |= 0x00ffffff;
3483 }
3484
Romain Guy70ca14e2010-12-13 18:24:33 -08003485 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003486 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003487 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003488 setupDrawShader();
3489 setupDrawColorFilter();
3490 setupDrawBlending(mode);
3491 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003492 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3493 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003494 setupDrawColorUniforms();
3495 setupDrawShaderUniforms(ignoreTransform);
3496 setupDrawColorFilterUniforms();
3497 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003498
Romain Guyc95c8d62010-09-17 15:31:32 -07003499 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3500}
3501
Romain Guy82ba8142010-07-09 13:25:56 -07003502void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003503 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003504 int alpha;
3505 SkXfermode::Mode mode;
3506 getAlphaAndMode(paint, &alpha, &mode);
3507
Romain Guyd21b6e12011-11-30 20:21:23 -08003508 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003509
Romain Guy3b748a42013-04-17 18:54:38 -07003510 GLvoid* vertices = (GLvoid*) NULL;
3511 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3512
3513 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003514 vertices = &mMeshVertices[0].x;
3515 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003516
3517 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3518 texture->uvMapper->map(uvs);
3519
3520 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3521 }
3522
Romain Guy3b753822013-03-05 10:27:35 -08003523 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3524 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3525 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003526
Romain Guyd21b6e12011-11-30 20:21:23 -08003527 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003528 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003529 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3530 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003531 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003532 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003533 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003534 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3535 }
3536
3537 if (texture->uvMapper) {
3538 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003539 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003540}
3541
Romain Guybd6b79b2010-06-26 00:13:53 -07003542void OpenGLRenderer::drawTextureRect(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) {
3544 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003545 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003546}
3547
3548void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003549 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003550 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003551 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3552 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003553
Romain Guy746b7402010-10-26 16:27:31 -07003554 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003555 setupDrawWithTexture();
3556 setupDrawColor(alpha, alpha, alpha, alpha);
3557 setupDrawColorFilter();
3558 setupDrawBlending(blend, mode, swapSrcDst);
3559 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003560 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003561 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003562 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003563 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003564 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003565 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003566
Romain Guy6820ac82010-09-15 18:11:50 -07003567 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003568}
3569
Romain Guy3b748a42013-04-17 18:54:38 -07003570void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3571 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3572 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003573 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3574 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003575
3576 setupDraw();
3577 setupDrawWithTexture();
3578 setupDrawColor(alpha, alpha, alpha, alpha);
3579 setupDrawColorFilter();
3580 setupDrawBlending(blend, mode, swapSrcDst);
3581 setupDrawProgram();
3582 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003583 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003584 setupDrawTexture(texture);
3585 setupDrawPureColorUniforms();
3586 setupDrawColorFilterUniforms();
3587 setupDrawMeshIndices(vertices, texCoords, vbo);
3588
3589 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003590}
3591
Romain Guy886b2752013-01-04 12:26:18 -08003592void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3593 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3594 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003595 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003596
3597 setupDraw();
3598 setupDrawWithTexture(true);
3599 if (hasColor) {
3600 setupDrawAlpha8Color(color, alpha);
3601 }
3602 setupDrawColorFilter();
3603 setupDrawShader();
3604 setupDrawBlending(true, mode);
3605 setupDrawProgram();
3606 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003607 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003608 setupDrawTexture(texture);
3609 setupDrawPureColorUniforms();
3610 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003611 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003612 setupDrawMesh(vertices, texCoords);
3613
3614 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003615}
3616
Romain Guya5aed0d2010-09-09 14:42:43 -07003617void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003618 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003619 if (mCountOverdraw) {
3620 if (!mCaches.blend) glEnable(GL_BLEND);
3621 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3622 glBlendFunc(GL_ONE, GL_ONE);
3623 }
3624
3625 mCaches.blend = true;
3626 mCaches.lastSrcMode = GL_ONE;
3627 mCaches.lastDstMode = GL_ONE;
3628
3629 return;
3630 }
3631
Romain Guy82ba8142010-07-09 13:25:56 -07003632 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003633
Romain Guy82ba8142010-07-09 13:25:56 -07003634 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003635 // These blend modes are not supported by OpenGL directly and have
3636 // to be implemented using shaders. Since the shader will perform
3637 // the blending, turn blending off here
3638 // If the blend mode cannot be implemented using shaders, fall
3639 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003640 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003641 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003642 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003643 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003644
Romain Guy82bc7a72012-01-03 14:13:39 -08003645 if (mCaches.blend) {
3646 glDisable(GL_BLEND);
3647 mCaches.blend = false;
3648 }
3649
3650 return;
3651 } else {
3652 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003653 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003654 }
3655
3656 if (!mCaches.blend) {
3657 glEnable(GL_BLEND);
3658 }
3659
3660 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3661 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3662
3663 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3664 glBlendFunc(sourceMode, destMode);
3665 mCaches.lastSrcMode = sourceMode;
3666 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003667 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003668 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003669 glDisable(GL_BLEND);
3670 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003671 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003672}
3673
Romain Guy889f8d12010-07-29 14:37:42 -07003674bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003675 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003676 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003677 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003678 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003679 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003680 }
Romain Guy6926c722010-07-12 20:20:03 -07003681 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003682}
3683
Romain Guy026c5e162010-06-28 17:12:22 -07003684void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003685 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003686 TextureVertex::setUV(v++, u1, v1);
3687 TextureVertex::setUV(v++, u2, v1);
3688 TextureVertex::setUV(v++, u1, v2);
3689 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003690}
3691
Chris Craik16ecda52013-03-29 10:59:59 -07003692void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003693 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003694 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3695 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003696 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003697 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003698 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003699}
3700
Chris Craik16ecda52013-03-29 10:59:59 -07003701float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3702 float alpha;
3703 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3704 alpha = mDrawModifiers.mOverrideLayerAlpha;
3705 } else {
3706 alpha = layer->getAlpha() / 255.0f;
3707 }
3708 return alpha * mSnapshot->alpha;
3709}
3710
Romain Guy9d5316e2010-06-24 19:30:36 -07003711}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003712}; // namespace android