blob: c30d86fbd773f7eb9c91062ef80a58fd67215c8c [file] [log] [blame]
Romain Guye4d01122010-06-16 18:44:05 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Romain Guy85bf02f2010-06-22 13:11:24 -070017#define LOG_TAG "OpenGLRenderer"
Romain Guye4d01122010-06-16 18:44:05 -070018
19#include <stdlib.h>
20#include <stdint.h>
21#include <sys/types.h>
22
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <SkCanvas.h>
Romain Guy694b5192010-07-21 21:33:20 -070024#include <SkTypeface.h>
Romain Guy5cbbce52010-06-27 22:59:20 -070025
Romain Guye4d01122010-06-16 18:44:05 -070026#include <utils/Log.h>
Romain Guye2d345e2010-09-24 18:39:22 -070027#include <utils/StopWatch.h>
Romain Guye4d01122010-06-16 18:44:05 -070028
Romain Guy08aa2cb2011-03-17 11:06:57 -070029#include <private/hwui/DrawGlInfo.h>
30
Romain Guy5b3b3522010-10-27 18:57:51 -070031#include <ui/Rect.h>
32
Romain Guy85bf02f2010-06-22 13:11:24 -070033#include "OpenGLRenderer.h"
Chris Craikc3566d02013-02-04 16:16:33 -080034#include "DeferredDisplayList.h"
Romain Guy0fe478e2010-11-08 12:08:41 -080035#include "DisplayListRenderer.h"
Romain Guy40543602013-06-12 15:31:28 -070036#include "Fence.h"
Chris Craik65cd6122012-12-10 17:56:27 -080037#include "PathTessellator.h"
Romain Guy87e2f7572012-09-24 11:37:12 -070038#include "Properties.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080039#include "ShadowTessellator.h"
Romain Guya957eea2010-12-08 18:34:42 -080040#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080041#include "VertexBuffer.h"
Romain Guye4d01122010-06-16 18:44:05 -070042
43namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070044namespace uirenderer {
45
46///////////////////////////////////////////////////////////////////////////////
47// Defines
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy759ea802010-09-16 20:49:46 -070050#define RAD_TO_DEG (180.0f / 3.14159265f)
51#define MIN_ANGLE 0.001f
52
Romain Guyf8773082012-07-12 18:01:00 -070053#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070054
Romain Guy713e1bb2012-10-16 18:44:09 -070055#define FILTER(paint) (!paint || paint->isFilterBitmap() ? GL_LINEAR : GL_NEAREST)
Romain Guyd21b6e12011-11-30 20:21:23 -080056
Romain Guy9d5316e2010-06-24 19:30:36 -070057///////////////////////////////////////////////////////////////////////////////
58// Globals
59///////////////////////////////////////////////////////////////////////////////
60
Romain Guy889f8d12010-07-29 14:37:42 -070061/**
62 * Structure mapping Skia xfermodes to OpenGL blending factors.
63 */
64struct Blender {
65 SkXfermode::Mode mode;
66 GLenum src;
67 GLenum dst;
68}; // struct Blender
69
Romain Guy026c5e162010-06-28 17:12:22 -070070// In this array, the index of each Blender equals the value of the first
71// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
72static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070073 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
74 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
75 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
76 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
77 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
78 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
79 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
80 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
81 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
82 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
84 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
85 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050086 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070087 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070088};
Romain Guye4d01122010-06-16 18:44:05 -070089
Romain Guy87a76572010-09-13 18:11:21 -070090// This array contains the swapped version of each SkXfermode. For instance
91// this array's SrcOver blending mode is actually DstOver. You can refer to
92// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070093static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070094 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
95 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
96 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
97 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
98 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
99 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
100 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
102 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
103 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
104 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
105 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
106 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500107 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700108 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700109};
110
Romain Guyf6a11b82010-06-23 17:47:49 -0700111///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700112// Functions
113///////////////////////////////////////////////////////////////////////////////
114
115template<typename T>
116static inline T min(T a, T b) {
117 return a < b ? a : b;
118}
119
120///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700121// Constructors/destructor
122///////////////////////////////////////////////////////////////////////////////
123
Romain Guy3bbacf22013-02-06 16:51:04 -0800124OpenGLRenderer::OpenGLRenderer():
125 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800126 // *set* draw modifiers to be 0
127 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700128 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700129
Romain Guyac670c02010-07-27 17:39:27 -0700130 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
131
Romain Guyae5575b2010-07-29 18:48:04 -0700132 mFirstSnapshot = new Snapshot;
Romain Guy96885eb2013-03-26 15:05:58 -0700133 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700134 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700135
136 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700137}
138
Romain Guy85bf02f2010-06-22 13:11:24 -0700139OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700140 // The context has already been destroyed at this point, do not call
141 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700142}
143
Romain Guy87e2f7572012-09-24 11:37:12 -0700144void OpenGLRenderer::initProperties() {
145 char property[PROPERTY_VALUE_MAX];
146 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
147 mScissorOptimizationDisabled = !strcasecmp(property, "true");
148 INIT_LOGD(" Scissor optimization %s",
149 mScissorOptimizationDisabled ? "disabled" : "enabled");
150 } else {
151 INIT_LOGD(" Scissor optimization enabled");
152 }
Romain Guy13631f32012-01-30 17:41:55 -0800153}
154
155///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700156// Setup
157///////////////////////////////////////////////////////////////////////////////
158
Romain Guy85bf02f2010-06-22 13:11:24 -0700159void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700160 initViewport(width, height);
161
162 glDisable(GL_DITHER);
163 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
164
165 glEnableVertexAttribArray(Program::kBindingPosition);
166}
167
168void OpenGLRenderer::initViewport(int width, int height) {
Chris Craikba9b6132013-12-15 17:10:19 -0800169 if (mCaches.propertyEnable3d) {
Chris Craikf57776b2013-10-25 18:30:17 -0700170 // TODO: make view proj app configurable
Chris Craikba9b6132013-12-15 17:10:19 -0800171 float dist = std::max(width, height) * 1.5;
172 dist *= mCaches.propertyCameraDistance;
Chris Craikf57776b2013-10-25 18:30:17 -0700173 Matrix4 projection;
174 projection.loadFrustum(-width / 2, -height / 2, width / 2, height / 2, dist, 0);
175 Matrix4 view;
176 view.loadLookAt(0, 0, dist,
177 0, 0, 0,
178 0, 1, 0);
179 mViewProjMatrix.loadMultiply(projection, view);
180 mViewProjMatrix.translate(-width/2, -height/2);
181 } else {
182 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
183 }
Romain Guybb9524b2010-06-22 18:56:38 -0700184
185 mWidth = width;
186 mHeight = height;
Romain Guyeb993562010-10-05 18:14:38 -0700187
188 mFirstSnapshot->height = height;
189 mFirstSnapshot->viewport.set(0, 0, width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700190}
191
Romain Guy96885eb2013-03-26 15:05:58 -0700192void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800193 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800194 mCaches.clearGarbage();
195
Romain Guy96885eb2013-03-26 15:05:58 -0700196 mOpaque = opaque;
Romain Guy8aef54f2010-09-01 15:13:49 -0700197 mSnapshot = new Snapshot(mFirstSnapshot,
198 SkCanvas::kMatrix_SaveFlag | SkCanvas::kClip_SaveFlag);
Romain Guy84962f22011-03-02 15:43:44 -0800199 mSnapshot->fbo = getTargetFbo();
Romain Guy8fb95422010-08-17 18:38:51 -0700200 mSaveCount = 1;
Romain Guyf6a11b82010-06-23 17:47:49 -0700201
Romain Guy7d7b5492011-01-24 16:33:45 -0800202 mSnapshot->setClip(left, top, right, bottom);
Chris Craik5f803622013-03-21 14:39:04 -0700203 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700204}
205
206status_t OpenGLRenderer::startFrame() {
207 if (mFrameStarted) return DrawGlInfo::kStatusDone;
208 mFrameStarted = true;
209
Romain Guy41308e22012-10-22 20:02:43 -0700210 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700211
Romain Guy96885eb2013-03-26 15:05:58 -0700212 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 glViewport(0, 0, mWidth, mHeight);
Romain Guy7d7b5492011-01-24 16:33:45 -0800215
Romain Guy54c1a642012-09-27 17:55:46 -0700216 // Functors break the tiling extension in pretty spectacular ways
217 // This ensures we don't use tiling when a functor is going to be
218 // invoked during the frame
219 mSuppressTiling = mCaches.hasRegisteredFunctors();
220
Chris Craik5f803622013-03-21 14:39:04 -0700221 startTiling(mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700222
Romain Guy7c450aa2012-09-21 19:15:00 -0700223 debugOverdraw(true, true);
224
Romain Guy96885eb2013-03-26 15:05:58 -0700225 return clear(mTilingClip.left, mTilingClip.top,
226 mTilingClip.right, mTilingClip.bottom, mOpaque);
227}
228
229status_t OpenGLRenderer::prepare(bool opaque) {
230 return prepareDirty(0.0f, 0.0f, mWidth, mHeight, opaque);
231}
232
233status_t OpenGLRenderer::prepareDirty(float left, float top,
234 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700235
Romain Guy96885eb2013-03-26 15:05:58 -0700236 setupFrameState(left, top, right, bottom, opaque);
237
238 // Layer renderers will start the frame immediately
239 // The framebuffer renderer will first defer the display list
240 // for each layer and wait until the first drawing command
241 // to start the frame
242 if (mSnapshot->fbo == 0) {
243 syncState();
244 updateLayers();
245 } else {
246 return startFrame();
247 }
248
249 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700250}
251
Romain Guydcfc8362013-01-03 13:08:57 -0800252void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
253 // If we know that we are going to redraw the entire framebuffer,
254 // perform a discard to let the driver know we don't need to preserve
255 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800256 if (mExtensions.hasDiscardFramebuffer() &&
Romain Guydcfc8362013-01-03 13:08:57 -0800257 left <= 0.0f && top <= 0.0f && right >= mWidth && bottom >= mHeight) {
Romain Guyf1581982013-01-31 17:20:30 -0800258 const bool isFbo = getTargetFbo() == 0;
259 const GLenum attachments[] = {
260 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
261 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800262 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
263 }
264}
265
Romain Guy7c25aab2012-10-18 15:05:02 -0700266status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700267 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700268 mCaches.enableScissor();
Romain Guyddf74372012-05-22 14:07:07 -0700269 mCaches.setScissor(left, mSnapshot->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700270 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700271 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700272 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700273
Romain Guy7c25aab2012-10-18 15:05:02 -0700274 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700275 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700276}
277
278void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700279 if (mCaches.blend) {
280 glEnable(GL_BLEND);
281 } else {
282 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700283 }
Romain Guybb9524b2010-06-22 18:56:38 -0700284}
285
Romain Guy57b52682012-09-20 17:38:46 -0700286void OpenGLRenderer::startTiling(const sp<Snapshot>& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700287 if (!mSuppressTiling) {
Chris Craik5f803622013-03-21 14:39:04 -0700288 Rect* clip = &mTilingClip;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800289 if (s->flags & Snapshot::kFlagFboTarget) {
Chris Craik5f803622013-03-21 14:39:04 -0700290 clip = &(s->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700291 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700292
Romain Guyc3fedaf2013-01-29 17:26:25 -0800293 startTiling(*clip, s->height, opaque);
294 }
295}
296
297void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
298 if (!mSuppressTiling) {
299 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800300 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700301 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700302}
303
304void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700305 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700306}
307
Romain Guyb025b9c2010-09-16 14:16:48 -0700308void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700309 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700310 endTiling();
311
Romain Guyca89e2a2013-03-08 17:44:20 -0800312 // When finish() is invoked on FBO 0 we've reached the end
313 // of the current frame
314 if (getTargetFbo() == 0) {
315 mCaches.pathCache.trim();
316 }
317
Romain Guy11cb6422012-09-21 00:39:43 -0700318 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700319#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700320 GLenum status = GL_NO_ERROR;
321 while ((status = glGetError()) != GL_NO_ERROR) {
322 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
323 switch (status) {
324 case GL_INVALID_ENUM:
325 ALOGE(" GL_INVALID_ENUM");
326 break;
327 case GL_INVALID_VALUE:
328 ALOGE(" GL_INVALID_VALUE");
329 break;
330 case GL_INVALID_OPERATION:
331 ALOGE(" GL_INVALID_OPERATION");
332 break;
333 case GL_OUT_OF_MEMORY:
334 ALOGE(" Out of memory!");
335 break;
336 }
Romain Guya07105b2011-01-10 21:14:18 -0800337 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700338#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700339
Romain Guyc15008e2010-11-10 11:59:15 -0800340#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800341 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700342#else
343 if (mCaches.getDebugLevel() & kDebugMemory) {
344 mCaches.dumpMemoryUsage();
345 }
Romain Guyc15008e2010-11-10 11:59:15 -0800346#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700347 }
Romain Guy96885eb2013-03-26 15:05:58 -0700348
Romain Guy78dd96d2013-05-03 14:24:16 -0700349 if (mCountOverdraw) {
350 countOverdraw();
351 }
352
Romain Guy96885eb2013-03-26 15:05:58 -0700353 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700354}
355
Romain Guy6c319ca2011-01-11 14:29:25 -0800356void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700357 if (mCaches.currentProgram) {
358 if (mCaches.currentProgram->isInUse()) {
359 mCaches.currentProgram->remove();
360 mCaches.currentProgram = NULL;
361 }
362 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700363 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700364 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800365 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800366 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800367 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700368 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700369}
370
Romain Guy6c319ca2011-01-11 14:29:25 -0800371void OpenGLRenderer::resume() {
Chet Haase08837c22011-11-28 11:53:21 -0800372 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
Chet Haase08837c22011-11-28 11:53:21 -0800373 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
Romain Guy35643dd2012-09-18 15:40:58 -0700374 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700375 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700376
Romain Guy3e263fa2011-12-12 16:47:48 -0800377 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
378
Chet Haase80250612012-08-15 13:46:54 -0700379 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700380 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800381 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700382 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700383
Romain Guya1d3c912011-12-13 14:55:06 -0800384 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700385 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700386
Romain Guy50c0f092010-10-19 11:42:22 -0700387 mCaches.blend = true;
388 glEnable(GL_BLEND);
389 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
390 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700391}
392
Romain Guy35643dd2012-09-18 15:40:58 -0700393void OpenGLRenderer::resumeAfterLayer() {
394 sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;
395 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
396 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700397 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700398
399 mCaches.resetScissor();
400 dirtyClip();
401}
402
Romain Guyba6be8a2012-04-23 18:22:09 -0700403void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700404 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700405}
406
407void OpenGLRenderer::attachFunctor(Functor* functor) {
408 mFunctors.add(functor);
409}
410
Romain Guy8f3b8e32012-03-27 16:33:45 -0700411status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
412 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700413 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700414
Romain Guyba6be8a2012-04-23 18:22:09 -0700415 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800416 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700417 SortedVector<Functor*> functors(mFunctors);
418 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700419
Romain Guyba6be8a2012-04-23 18:22:09 -0700420 DrawGlInfo info;
421 info.clipLeft = 0;
422 info.clipTop = 0;
423 info.clipRight = 0;
424 info.clipBottom = 0;
425 info.isLayer = false;
426 info.width = 0;
427 info.height = 0;
428 memset(info.transform, 0, sizeof(float) * 16);
429
430 for (size_t i = 0; i < count; i++) {
431 Functor* f = functors.itemAt(i);
432 result |= (*f)(DrawGlInfo::kModeProcess, &info);
433
Chris Craikc2c95432012-04-25 15:13:52 -0700434 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700435 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
436 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700437 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700438
Chris Craikc2c95432012-04-25 15:13:52 -0700439 if (result & DrawGlInfo::kStatusInvoke) {
440 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700441 }
442 }
Chris Craikd15321b2012-11-28 14:45:04 -0800443 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700444 }
445
446 return result;
447}
448
449status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik408eb122013-03-26 18:55:15 -0700450 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
451
Chris Craik932b7f62012-06-06 13:59:33 -0700452 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700453
Romain Guyd643bb52011-03-01 14:55:21 -0800454
Romain Guy80911b82011-03-16 15:30:12 -0700455 Rect clip(*mSnapshot->clipRect);
456 clip.snapToPixelBoundaries();
457
Romain Guyd643bb52011-03-01 14:55:21 -0800458 // Since we don't know what the functor will draw, let's dirty
459 // tne entire clip region
460 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800461 dirtyLayerUnchecked(clip, getRegion());
462 }
Romain Guyd643bb52011-03-01 14:55:21 -0800463
Romain Guy08aa2cb2011-03-17 11:06:57 -0700464 DrawGlInfo info;
465 info.clipLeft = clip.left;
466 info.clipTop = clip.top;
467 info.clipRight = clip.right;
468 info.clipBottom = clip.bottom;
469 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700470 info.width = getSnapshot()->viewport.getWidth();
471 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700472 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700473
John Reck25d2f7b2013-09-10 13:12:09 -0700474 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700475 // setup GL state for functor
476 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700477 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
478 }
John Reck25d2f7b2013-09-10 13:12:09 -0700479 if (mCaches.enableScissor() || dirtyClip) {
480 setScissorFromClip();
481 }
Chris Craik54f574a2013-08-26 11:23:46 -0700482 interrupt();
483
484 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700485 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800486
Romain Guy8f3b8e32012-03-27 16:33:45 -0700487 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700488 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800489 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700490
Chris Craik65924a32012-04-05 17:52:11 -0700491 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700492 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700493 }
Romain Guycabfcc12011-03-07 18:06:46 -0800494 }
495
Chet Haasedaf98e92011-01-10 14:10:36 -0800496 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700497 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800498}
499
Romain Guyf6a11b82010-06-23 17:47:49 -0700500///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700501// Debug
502///////////////////////////////////////////////////////////////////////////////
503
Romain Guy0f6675332013-03-01 14:31:04 -0800504void OpenGLRenderer::eventMark(const char* name) const {
505 mCaches.eventMark(0, name);
506}
507
Romain Guy87e2f7572012-09-24 11:37:12 -0700508void OpenGLRenderer::startMark(const char* name) const {
509 mCaches.startMark(0, name);
510}
511
512void OpenGLRenderer::endMark() const {
513 mCaches.endMark();
514}
515
516void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
517 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
518 if (clear) {
519 mCaches.disableScissor();
520 mCaches.stencil.clear();
521 }
522 if (enable) {
523 mCaches.stencil.enableDebugWrite();
524 } else {
525 mCaches.stencil.disable();
526 }
527 }
528}
529
530void OpenGLRenderer::renderOverdraw() {
531 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700532 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700533
534 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700535 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700536 clip->right - clip->left, clip->bottom - clip->top);
537
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700539 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700540 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
541
542 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700543 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700544 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
545
546 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700547 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700548 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
549
550 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700551 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700552 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
553
Romain Guy87e2f7572012-09-24 11:37:12 -0700554 mCaches.stencil.disable();
555 }
556}
557
Romain Guy78dd96d2013-05-03 14:24:16 -0700558void OpenGLRenderer::countOverdraw() {
559 size_t count = mWidth * mHeight;
560 uint32_t* buffer = new uint32_t[count];
561 glReadPixels(0, 0, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
562
563 size_t total = 0;
564 for (size_t i = 0; i < count; i++) {
565 total += buffer[i] & 0xff;
566 }
567
568 mOverdraw = total / float(count);
569
570 delete[] buffer;
571}
572
Romain Guy87e2f7572012-09-24 11:37:12 -0700573///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700574// Layers
575///////////////////////////////////////////////////////////////////////////////
576
577bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700578 if (layer->deferredUpdateScheduled && layer->renderer &&
579 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700580 ATRACE_CALL();
581
Romain Guy11cb6422012-09-21 00:39:43 -0700582 Rect& dirty = layer->dirtyRect;
583
Romain Guy7c450aa2012-09-21 19:15:00 -0700584 if (inFrame) {
585 endTiling();
586 debugOverdraw(false, false);
587 }
Romain Guy11cb6422012-09-21 00:39:43 -0700588
Romain Guy96885eb2013-03-26 15:05:58 -0700589 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700590 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700591 } else {
592 layer->defer();
593 }
Romain Guy11cb6422012-09-21 00:39:43 -0700594
595 if (inFrame) {
596 resumeAfterLayer();
597 startTiling(mSnapshot);
598 }
599
Romain Guy5bb3c732012-11-29 17:52:58 -0800600 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700601 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700602
603 return true;
604 }
605
606 return false;
607}
608
609void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700610 // If draw deferring is enabled this method will simply defer
611 // the display list of each individual layer. The layers remain
612 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700613 int count = mLayerUpdates.size();
614 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700615 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
616 startMark("Layer Updates");
617 } else {
618 startMark("Defer Layer Updates");
619 }
Romain Guy11cb6422012-09-21 00:39:43 -0700620
Chris Craik1206b9b2013-04-04 14:46:24 -0700621 // Note: it is very important to update the layers in order
622 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700623 Layer* layer = mLayerUpdates.itemAt(i);
624 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700625 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
626 mCaches.resourceCache.decrementRefcount(layer);
627 }
Romain Guy11cb6422012-09-21 00:39:43 -0700628 }
Romain Guy11cb6422012-09-21 00:39:43 -0700629
Romain Guy96885eb2013-03-26 15:05:58 -0700630 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
631 mLayerUpdates.clear();
632 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
633 }
634 endMark();
635 }
636}
637
638void OpenGLRenderer::flushLayers() {
639 int count = mLayerUpdates.size();
640 if (count > 0) {
641 startMark("Apply Layer Updates");
642 char layerName[12];
643
Chris Craik1206b9b2013-04-04 14:46:24 -0700644 // Note: it is very important to update the layers in order
645 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700646 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700647 startMark(layerName);
648
Romain Guy40543602013-06-12 15:31:28 -0700649 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700650 Layer* layer = mLayerUpdates.itemAt(i);
651 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700652 ATRACE_END();
653
Romain Guy02b49b72013-03-29 12:37:16 -0700654 mCaches.resourceCache.decrementRefcount(layer);
655
Romain Guy96885eb2013-03-26 15:05:58 -0700656 endMark();
657 }
658
659 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700660 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700661
Romain Guy11cb6422012-09-21 00:39:43 -0700662 endMark();
663 }
664}
665
666void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
667 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700668 // Make sure we don't introduce duplicates.
669 // SortedVector would do this automatically but we need to respect
670 // the insertion order. The linear search is not an issue since
671 // this list is usually very short (typically one item, at most a few)
672 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
673 if (mLayerUpdates.itemAt(i) == layer) {
674 return;
675 }
676 }
Romain Guy11cb6422012-09-21 00:39:43 -0700677 mLayerUpdates.push_back(layer);
678 mCaches.resourceCache.incrementRefcount(layer);
679 }
680}
681
Romain Guye93482f2013-06-17 13:14:51 -0700682void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
683 if (layer) {
684 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
685 if (mLayerUpdates.itemAt(i) == layer) {
686 mLayerUpdates.removeAt(i);
687 mCaches.resourceCache.decrementRefcount(layer);
688 break;
689 }
690 }
691 }
692}
693
Romain Guy11cb6422012-09-21 00:39:43 -0700694void OpenGLRenderer::clearLayerUpdates() {
695 size_t count = mLayerUpdates.size();
696 if (count > 0) {
697 mCaches.resourceCache.lock();
698 for (size_t i = 0; i < count; i++) {
699 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
700 }
701 mCaches.resourceCache.unlock();
702 mLayerUpdates.clear();
703 }
704}
705
Romain Guy40543602013-06-12 15:31:28 -0700706void OpenGLRenderer::flushLayerUpdates() {
707 syncState();
708 updateLayers();
709 flushLayers();
710 // Wait for all the layer updates to be executed
711 AutoFence fence;
712}
713
Romain Guy11cb6422012-09-21 00:39:43 -0700714///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700715// State management
716///////////////////////////////////////////////////////////////////////////////
717
Romain Guybb9524b2010-06-22 18:56:38 -0700718int OpenGLRenderer::getSaveCount() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700719 return mSaveCount;
Romain Guybb9524b2010-06-22 18:56:38 -0700720}
721
722int OpenGLRenderer::save(int flags) {
Romain Guy8aef54f2010-09-01 15:13:49 -0700723 return saveSnapshot(flags);
Romain Guybb9524b2010-06-22 18:56:38 -0700724}
725
726void OpenGLRenderer::restore() {
Romain Guy2542d192010-08-18 11:47:12 -0700727 if (mSaveCount > 1) {
728 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700729 }
Romain Guybb9524b2010-06-22 18:56:38 -0700730}
731
732void OpenGLRenderer::restoreToCount(int saveCount) {
Romain Guy8fb95422010-08-17 18:38:51 -0700733 if (saveCount < 1) saveCount = 1;
Romain Guybb9524b2010-06-22 18:56:38 -0700734
Romain Guy8fb95422010-08-17 18:38:51 -0700735 while (mSaveCount > saveCount) {
Romain Guy2542d192010-08-18 11:47:12 -0700736 restoreSnapshot();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700737 }
Romain Guybb9524b2010-06-22 18:56:38 -0700738}
739
Romain Guy8aef54f2010-09-01 15:13:49 -0700740int OpenGLRenderer::saveSnapshot(int flags) {
741 mSnapshot = new Snapshot(mSnapshot, flags);
Romain Guy8fb95422010-08-17 18:38:51 -0700742 return mSaveCount++;
Romain Guybb9524b2010-06-22 18:56:38 -0700743}
744
745bool OpenGLRenderer::restoreSnapshot() {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700746 bool restoreClip = mSnapshot->flags & Snapshot::kFlagClipSet;
Romain Guybd6b79b2010-06-26 00:13:53 -0700747 bool restoreLayer = mSnapshot->flags & Snapshot::kFlagIsLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700748 bool restoreOrtho = mSnapshot->flags & Snapshot::kFlagDirtyOrtho;
Romain Guybb9524b2010-06-22 18:56:38 -0700749
Romain Guybd6b79b2010-06-26 00:13:53 -0700750 sp<Snapshot> current = mSnapshot;
751 sp<Snapshot> previous = mSnapshot->previous;
752
Romain Guyeb993562010-10-05 18:14:38 -0700753 if (restoreOrtho) {
754 Rect& r = previous->viewport;
755 glViewport(r.left, r.top, r.right, r.bottom);
Chris Craikf57776b2013-10-25 18:30:17 -0700756 mViewProjMatrix.load(current->orthoMatrix);
Romain Guyeb993562010-10-05 18:14:38 -0700757 }
758
Romain Guy8b55f372010-08-18 17:10:07 -0700759 mSaveCount--;
760 mSnapshot = previous;
761
Romain Guy2542d192010-08-18 11:47:12 -0700762 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700763 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700764 }
Romain Guy2542d192010-08-18 11:47:12 -0700765
Romain Guy5ec99242010-11-03 16:19:08 -0700766 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700767 endMark(); // Savelayer
768 startMark("ComposeLayer");
Romain Guy5ec99242010-11-03 16:19:08 -0700769 composeLayer(current, previous);
Chris Craik7273daa2013-03-28 11:25:24 -0700770 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700771 }
772
Romain Guy2542d192010-08-18 11:47:12 -0700773 return restoreClip;
Romain Guybb9524b2010-06-22 18:56:38 -0700774}
775
Romain Guyf6a11b82010-06-23 17:47:49 -0700776///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700777// Layers
778///////////////////////////////////////////////////////////////////////////////
779
780int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800781 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700782 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700783
Romain Guyaf636eb2010-12-09 17:47:21 -0800784 if (!mSnapshot->isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700785 createLayer(left, top, right, bottom, alpha, mode, flags);
Romain Guydbc26d22010-10-11 17:58:29 -0700786 }
Romain Guyd55a8612010-06-28 17:42:46 -0700787
788 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700789}
790
Chris Craikd90144d2013-03-19 15:03:48 -0700791void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
792 const Rect untransformedBounds(bounds);
793
794 currentTransform().mapRect(bounds);
795
796 // Layers only make sense if they are in the framebuffer's bounds
797 if (bounds.intersect(*mSnapshot->clipRect)) {
798 // We cannot work with sub-pixels in this case
799 bounds.snapToPixelBoundaries();
800
801 // When the layer is not an FBO, we may use glCopyTexImage so we
802 // need to make sure the layer does not extend outside the bounds
803 // of the framebuffer
804 if (!bounds.intersect(mSnapshot->previous->viewport)) {
805 bounds.setEmpty();
806 } else if (fboLayer) {
807 clip.set(bounds);
808 mat4 inverse;
809 inverse.loadInverse(currentTransform());
810 inverse.mapRect(clip);
811 clip.snapToPixelBoundaries();
812 if (clip.intersect(untransformedBounds)) {
813 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
814 bounds.set(untransformedBounds);
815 } else {
816 clip.setEmpty();
817 }
818 }
819 } else {
820 bounds.setEmpty();
821 }
822}
823
Chris Craik408eb122013-03-26 18:55:15 -0700824void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
825 bool fboLayer, int alpha) {
826 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
827 bounds.getHeight() > mCaches.maxTextureSize ||
828 (fboLayer && clip.isEmpty())) {
829 mSnapshot->empty = fboLayer;
830 } else {
831 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
832 }
833}
834
Chris Craikd90144d2013-03-19 15:03:48 -0700835int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
836 int alpha, SkXfermode::Mode mode, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700837 const int count = saveSnapshot(flags);
838
839 if (!mSnapshot->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
840 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
841 // operations will be able to store and restore the current clip and transform info, and
842 // quick rejection will be correct (for display lists)
843
844 Rect bounds(left, top, right, bottom);
845 Rect clip;
846 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700847 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700848
Chris Craik408eb122013-03-26 18:55:15 -0700849 if (!mSnapshot->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700850 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
851 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700852 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700853 }
854 }
855
856 return count;
857}
858
859
Romain Guy1c740bc2010-09-13 18:00:09 -0700860/**
861 * Layers are viewed by Skia are slightly different than layers in image editing
862 * programs (for instance.) When a layer is created, previously created layers
863 * and the frame buffer still receive every drawing command. For instance, if a
864 * layer is created and a shape intersecting the bounds of the layers and the
865 * framebuffer is draw, the shape will be drawn on both (unless the layer was
866 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
867 *
868 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
869 * texture. Unfortunately, this is inefficient as it requires every primitive to
870 * be drawn n + 1 times, where n is the number of active layers. In practice this
871 * means, for every primitive:
872 * - Switch active frame buffer
873 * - Change viewport, clip and projection matrix
874 * - Issue the drawing
875 *
876 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700877 * To avoid this, layers are implemented in a different way here, at least in the
878 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
879 * is set. When this flag is set we can redirect all drawing operations into a
880 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700881 *
882 * This implementation relies on the frame buffer being at least RGBA 8888. When
883 * a layer is created, only a texture is created, not an FBO. The content of the
884 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700885 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700886 * buffer and drawing continues as normal. This technique therefore treats the
887 * frame buffer as a scratch buffer for the layers.
888 *
889 * To compose the layers back onto the frame buffer, each layer texture
890 * (containing the original frame buffer data) is drawn as a simple quad over
891 * the frame buffer. The trick is that the quad is set as the composition
892 * destination in the blending equation, and the frame buffer becomes the source
893 * of the composition.
894 *
895 * Drawing layers with an alpha value requires an extra step before composition.
896 * An empty quad is drawn over the layer's region in the frame buffer. This quad
897 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
898 * quad is used to multiply the colors in the frame buffer. This is achieved by
899 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
900 * GL_ZERO, GL_SRC_ALPHA.
901 *
902 * Because glCopyTexImage2D() can be slow, an alternative implementation might
903 * be use to draw a single clipped layer. The implementation described above
904 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700905 *
906 * (1) The frame buffer is actually not cleared right away. To allow the GPU
907 * to potentially optimize series of calls to glCopyTexImage2D, the frame
908 * buffer is left untouched until the first drawing operation. Only when
909 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700910 */
Chet Haased48885a2012-08-28 17:43:28 -0700911bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700912 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700913 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700914 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700915
Romain Guyeb993562010-10-05 18:14:38 -0700916 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
917
Romain Guyf607bdc2010-09-10 19:20:06 -0700918 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700919 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700920 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700921 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700922 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700923
924 // Bail out if we won't draw in this snapshot
Chris Craik408eb122013-03-26 18:55:15 -0700925 if (mSnapshot->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700926 return false;
927 }
Romain Guyf18fd992010-07-08 11:45:51 -0700928
Romain Guya1d3c912011-12-13 14:55:06 -0800929 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700930 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700931 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700932 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700933 }
934
Romain Guy9ace8f52011-07-07 20:50:11 -0700935 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700936 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700937 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
938 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800939 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700940 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700941 layer->setDirty(false);
Romain Guydda570202010-07-06 11:39:32 -0700942
Romain Guy8fb95422010-08-17 18:38:51 -0700943 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700944 mSnapshot->flags |= Snapshot::kFlagIsLayer;
945 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700946
Chris Craik7273daa2013-03-28 11:25:24 -0700947 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700948 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700949 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700950 } else {
951 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700952 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800953 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700954 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700955 // Workaround for some GL drivers. When reading pixels lying outside
956 // of the window we should get undefined values for those pixels.
957 // Unfortunately some drivers will turn the entire target texture black
958 // when reading outside of the window.
959 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
960 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700961 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800962 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700963
Romain Guyb254c242013-06-27 17:15:24 -0700964 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
965 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
966
Romain Guy54be1cd2011-06-13 19:04:27 -0700967 // Enqueue the buffer coordinates to clear the corresponding region later
968 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700969 }
Romain Guyeb993562010-10-05 18:14:38 -0700970 }
Romain Guyf86ef572010-07-01 11:05:42 -0700971
Romain Guyd55a8612010-06-28 17:42:46 -0700972 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700973}
974
Chris Craike63f7c622013-10-17 10:30:55 -0700975bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800976 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700977 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700978
Chet Haased48885a2012-08-28 17:43:28 -0700979 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800980 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
981 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700982 mSnapshot->fbo = layer->getFbo();
983 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
984 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
985 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
986 mSnapshot->height = bounds.getHeight();
Chris Craikf57776b2013-10-25 18:30:17 -0700987 mSnapshot->orthoMatrix.load(mViewProjMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700988
Romain Guy2b7028e2012-09-19 17:25:38 -0700989 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700990 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700991 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700992 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
993 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700994
995 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700996 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700997 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700998 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700999 }
1000
1001 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -07001002 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -07001003
Romain Guyf735c8e2013-01-31 17:45:55 -08001004 startTiling(mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001005
1006 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -07001007 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -08001008 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -07001009 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 glClear(GL_COLOR_BUFFER_BIT);
1011
1012 dirtyClip();
1013
1014 // Change the ortho projection
1015 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craikf57776b2013-10-25 18:30:17 -07001016
1017 // TODO: determine best way to support 3d drawing within HW layers
1018 mViewProjMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -07001019
1020 return true;
1021}
1022
Romain Guy1c740bc2010-09-13 18:00:09 -07001023/**
1024 * Read the documentation of createLayer() before doing anything in this method.
1025 */
Romain Guy1d83e192010-08-17 11:37:00 -07001026void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
1027 if (!current->layer) {
Steve Block3762c312012-01-06 19:20:56 +00001028 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -07001029 return;
1030 }
1031
Romain Guy8ce00302013-01-15 18:51:42 -08001032 Layer* layer = current->layer;
1033 const Rect& rect = layer->layer;
Romain Guy5b3b3522010-10-27 18:57:51 -07001034 const bool fboLayer = current->flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -07001035
Chris Craik39a908c2013-06-13 14:39:01 -07001036 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001037 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
1038 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -07001039 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
1040
Romain Guyeb993562010-10-05 18:14:38 -07001041 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -07001042 endTiling();
1043
Romain Guye0aa84b2012-04-03 19:30:26 -07001044 // Detach the texture from the FBO
1045 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -08001046
1047 layer->removeFbo(false);
1048
Romain Guyeb993562010-10-05 18:14:38 -07001049 // Unbind current FBO and restore previous one
1050 glBindFramebuffer(GL_FRAMEBUFFER, previous->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001051 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001052
1053 startTiling(previous);
Romain Guyeb993562010-10-05 18:14:38 -07001054 }
1055
Romain Guy9ace8f52011-07-07 20:50:11 -07001056 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001057 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001058 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001059 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001060 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001061 }
1062
Romain Guy03750a02010-10-18 14:06:08 -07001063 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001064
Romain Guya1d3c912011-12-13 14:55:06 -08001065 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001066
Romain Guy5b3b3522010-10-27 18:57:51 -07001067 // When the layer is stored in an FBO, we can save a bit of fillrate by
1068 // drawing only the dirty region
1069 if (fboLayer) {
1070 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *previous->transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001071 if (layer->getColorFilter()) {
1072 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001073 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001074 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001075 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001076 resetColorFilter();
1077 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001078 } else if (!rect.isEmpty()) {
1079 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301080
1081 save(0);
1082 // the layer contains screen buffer content that shouldn't be alpha modulated
1083 // (and any necessary alpha modulation was handled drawing into the layer)
1084 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001085 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301086 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001087 }
Romain Guy8b55f372010-08-18 17:10:07 -07001088
Romain Guy746b7402010-10-26 16:27:31 -07001089 dirtyClip();
1090
Romain Guyeb993562010-10-05 18:14:38 -07001091 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001092 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001093 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001094 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001095 }
1096}
1097
Romain Guyaa6c24c2011-04-28 18:40:04 -07001098void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001099 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001100
1101 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001102 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001103 setupDrawWithTexture();
1104 } else {
1105 setupDrawWithExternalTexture();
1106 }
1107 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001108 setupDrawColor(alpha, alpha, alpha, alpha);
1109 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001110 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001111 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001112 setupDrawPureColorUniforms();
1113 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001114 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1115 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001116 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001117 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001118 }
Romain Guy3b753822013-03-05 10:27:35 -08001119 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001120 layer->getWidth() == (uint32_t) rect.getWidth() &&
1121 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001122 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1123 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001124
Romain Guyd21b6e12011-11-30 20:21:23 -08001125 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001126 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1127 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001128 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001129 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001130 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1131 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001132 }
1133 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001134 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001135
1136 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001137}
1138
Romain Guy5b3b3522010-10-27 18:57:51 -07001139void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001140 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001141 const Rect& texCoords = layer->texCoords;
1142 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1143 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001144
Romain Guy9ace8f52011-07-07 20:50:11 -07001145 float x = rect.left;
1146 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001147 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001148 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001149 layer->getHeight() == (uint32_t) rect.getHeight();
1150
1151 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001152 // When we're swapping, the layer is already in screen coordinates
1153 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001154 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1155 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001156 }
1157
Romain Guyd21b6e12011-11-30 20:21:23 -08001158 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001159 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001160 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001161 }
1162
Chris Craik16ecda52013-03-29 10:59:59 -07001163 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001164 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001165 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001166 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001167 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001168 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001169
Romain Guyaa6c24c2011-04-28 18:40:04 -07001170 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1171 } else {
1172 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1173 drawTextureLayer(layer, rect);
1174 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1175 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001176}
1177
Chris Craik34416ea2013-04-15 16:08:28 -07001178/**
1179 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1180 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1181 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1182 * by saveLayer's restore
1183 */
1184#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1185 DRAW_COMMAND; \
1186 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1187 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1188 DRAW_COMMAND; \
1189 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1190 } \
1191 }
1192
1193#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1194
Romain Guy5b3b3522010-10-27 18:57:51 -07001195void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001196 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001197 layer->setRegionAsRect();
1198
Chris Craik34416ea2013-04-15 16:08:28 -07001199 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001200
Romain Guy5b3b3522010-10-27 18:57:51 -07001201 layer->region.clear();
1202 return;
1203 }
1204
Romain Guy211370f2012-02-01 16:10:55 -08001205 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001206 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001207 const android::Rect* rects;
1208 Region safeRegion;
1209 if (CC_LIKELY(hasRectToRectTransform())) {
1210 rects = layer->region.getArray(&count);
1211 } else {
1212 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1213 rects = safeRegion.getArray(&count);
1214 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001215
Chris Craik16ecda52013-03-29 10:59:59 -07001216 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001217 const float texX = 1.0f / float(layer->getWidth());
1218 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001219 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001220
Romain Guy8ce00302013-01-15 18:51:42 -08001221 setupDraw();
1222
1223 // We must get (and therefore bind) the region mesh buffer
1224 // after we setup drawing in case we need to mess with the
1225 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001226 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001227 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001228
Romain Guy7230a742011-01-10 22:26:16 -08001229 setupDrawWithTexture();
1230 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001231 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001232 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001233 setupDrawProgram();
1234 setupDrawDirtyRegionsDisabled();
1235 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001236 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001237 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001238 if (currentTransform().isPureTranslate()) {
1239 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1240 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001241
Romain Guyd21b6e12011-11-30 20:21:23 -08001242 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001243 setupDrawModelView(kModelViewMode_Translate, false,
1244 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001245 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001246 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001247 setupDrawModelView(kModelViewMode_Translate, false,
1248 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001249 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001250 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001251
1252 for (size_t i = 0; i < count; i++) {
1253 const android::Rect* r = &rects[i];
1254
1255 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001256 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001257 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001258 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001259
1260 // TODO: Reject quads outside of the clip
1261 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1262 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1263 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1264 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1265
1266 numQuads++;
1267
Romain Guy31e08e92013-06-18 15:53:53 -07001268 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001269 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1270 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001271 numQuads = 0;
1272 mesh = mCaches.getRegionMesh();
1273 }
1274 }
1275
1276 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001277 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1278 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001279 }
1280
Romain Guy5b3b3522010-10-27 18:57:51 -07001281#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001282 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001283#endif
1284
1285 layer->region.clear();
1286 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001287}
1288
Romain Guy3a3133d2011-02-01 22:59:58 -08001289#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001290void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001291 size_t count;
1292 const android::Rect* rects = region.getArray(&count);
1293
1294 uint32_t colors[] = {
1295 0x7fff0000, 0x7f00ff00,
1296 0x7f0000ff, 0x7fff00ff,
1297 };
1298
1299 int offset = 0;
1300 int32_t top = rects[0].top;
1301
1302 for (size_t i = 0; i < count; i++) {
1303 if (top != rects[i].top) {
1304 offset ^= 0x2;
1305 top = rects[i].top;
1306 }
1307
1308 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1309 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1310 SkXfermode::kSrcOver_Mode);
1311 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001312}
Chris Craike63f7c622013-10-17 10:30:55 -07001313#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001314
Romain Guy8ce00302013-01-15 18:51:42 -08001315void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1316 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001317 Vector<float> rects;
1318
1319 SkRegion::Iterator it(region);
1320 while (!it.done()) {
1321 const SkIRect& r = it.rect();
1322 rects.push(r.fLeft);
1323 rects.push(r.fTop);
1324 rects.push(r.fRight);
1325 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001326 it.next();
1327 }
1328
Romain Guy448455f2013-07-22 13:57:50 -07001329 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001330}
1331
Romain Guy5b3b3522010-10-27 18:57:51 -07001332void OpenGLRenderer::dirtyLayer(const float left, const float top,
1333 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001334 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001335 Rect bounds(left, top, right, bottom);
1336 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001337 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001338 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001339}
1340
1341void OpenGLRenderer::dirtyLayer(const float left, const float top,
1342 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001343 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001344 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001345 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001346 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001347}
1348
1349void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001350 if (bounds.intersect(*mSnapshot->clipRect)) {
1351 bounds.snapToPixelBoundaries();
1352 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1353 if (!dirty.isEmpty()) {
1354 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001355 }
1356 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001357}
1358
Chris Craik4063a0e2013-11-15 16:06:56 -08001359void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001360 GLsizei elementsCount = quadsCount * 6;
1361 while (elementsCount > 0) {
1362 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1363
Romain Guy3380cfd2013-08-15 16:57:57 -07001364 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001365 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1366
1367 elementsCount -= drawCount;
1368 // Though there are 4 vertices in a quad, we use 6 indices per
1369 // quad to draw with GL_TRIANGLES
1370 mesh += (drawCount / 6) * 4;
1371 }
1372}
1373
Romain Guy54be1cd2011-06-13 19:04:27 -07001374void OpenGLRenderer::clearLayerRegions() {
1375 const size_t count = mLayers.size();
1376 if (count == 0) return;
1377
1378 if (!mSnapshot->isIgnored()) {
1379 // Doing several glScissor/glClear here can negatively impact
1380 // GPUs with a tiler architecture, instead we draw quads with
1381 // the Clear blending mode
1382
1383 // The list contains bounds that have already been clipped
1384 // against their initial clip rect, and the current clip
1385 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001386 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001387
Romain Guy448455f2013-07-22 13:57:50 -07001388 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001389 Vertex* vertex = mesh;
1390
1391 for (uint32_t i = 0; i < count; i++) {
1392 Rect* bounds = mLayers.itemAt(i);
1393
Romain Guy54be1cd2011-06-13 19:04:27 -07001394 Vertex::set(vertex++, bounds->left, bounds->top);
1395 Vertex::set(vertex++, bounds->right, bounds->top);
1396 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001397 Vertex::set(vertex++, bounds->right, bounds->bottom);
1398
1399 delete bounds;
1400 }
Romain Guye67307c2013-02-11 18:01:20 -08001401 // We must clear the list of dirty rects before we
1402 // call setupDraw() to prevent stencil setup to do
1403 // the same thing again
1404 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001405
1406 setupDraw(false);
1407 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1408 setupDrawBlending(true, SkXfermode::kClear_Mode);
1409 setupDrawProgram();
1410 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001411 setupDrawModelView(kModelViewMode_Translate, false,
1412 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001413
Chris Craik4063a0e2013-11-15 16:06:56 -08001414 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001415
1416 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001417 } else {
1418 for (uint32_t i = 0; i < count; i++) {
1419 delete mLayers.itemAt(i);
1420 }
Romain Guye67307c2013-02-11 18:01:20 -08001421 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001422 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001423}
1424
Romain Guybd6b79b2010-06-26 00:13:53 -07001425///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001426// State Deferral
1427///////////////////////////////////////////////////////////////////////////////
1428
Chris Craikff785832013-03-08 13:12:16 -08001429bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikc3566d02013-02-04 16:16:33 -08001430 const Rect& currentClip = *(mSnapshot->clipRect);
1431 const mat4& currentMatrix = *(mSnapshot->transform);
1432
Chris Craikff785832013-03-08 13:12:16 -08001433 if (stateDeferFlags & kStateDeferFlag_Draw) {
1434 // state has bounds initialized in local coordinates
1435 if (!state.mBounds.isEmpty()) {
1436 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001437 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001438 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1439 // is used, it should more closely duplicate the quickReject logic (in how it uses
1440 // snapToPixelBoundaries)
1441
Chris Craik28ce94a2013-05-31 11:38:03 -07001442 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001443 // quick rejected
1444 return true;
1445 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001446
Chris Craika02c4ed2013-06-14 13:43:58 -07001447 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001448 if (!currentClip.contains(state.mBounds)) {
1449 int& flags = state.mClipSideFlags;
1450 // op partially clipped, so record which sides are clipped for clip-aware merging
1451 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1452 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1453 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1454 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1455 }
1456 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001457 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001458 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1459 // overdraw avoidance (since we don't know what it overlaps)
1460 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001461 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001462 }
1463 }
1464
Chris Craik527a3aa2013-03-04 10:19:31 -08001465 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1466 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001467 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001468 }
1469
Chris Craik7273daa2013-03-28 11:25:24 -07001470 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1471 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001472 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001473 state.mDrawModifiers = mDrawModifiers;
1474 state.mAlpha = mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001475 return false;
1476}
1477
Chris Craik527a3aa2013-03-04 10:19:31 -08001478void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Romain Guy3b753822013-03-05 10:27:35 -08001479 currentTransform().load(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001480 mDrawModifiers = state.mDrawModifiers;
1481 mSnapshot->alpha = state.mAlpha;
Chris Craikff785832013-03-08 13:12:16 -08001482
Chris Craik527a3aa2013-03-04 10:19:31 -08001483 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001484 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1485 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001486 dirtyClip();
1487 }
Chris Craikc3566d02013-02-04 16:16:33 -08001488}
1489
Chris Craik28ce94a2013-05-31 11:38:03 -07001490/**
1491 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1492 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1493 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1494 *
1495 * This method should be called when restoreDisplayState() won't be restoring the clip
1496 */
1497void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1498 if (clipRect != NULL) {
1499 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1500 } else {
1501 mSnapshot->setClip(0, 0, mWidth, mHeight);
1502 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001503 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001504 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001505}
1506
Chris Craikc3566d02013-02-04 16:16:33 -08001507///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001508// Transforms
1509///////////////////////////////////////////////////////////////////////////////
1510
Chris Craikb4589422013-12-26 15:13:13 -08001511void OpenGLRenderer::translate(float dx, float dy, float dz) {
1512 currentTransform().translate(dx, dy, dz);
Romain Guyf6a11b82010-06-23 17:47:49 -07001513}
1514
1515void OpenGLRenderer::rotate(float degrees) {
Romain Guy3b753822013-03-05 10:27:35 -08001516 currentTransform().rotate(degrees, 0.0f, 0.0f, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001517}
1518
1519void OpenGLRenderer::scale(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001520 currentTransform().scale(sx, sy, 1.0f);
Romain Guyf6a11b82010-06-23 17:47:49 -07001521}
1522
Romain Guy807daf72011-01-18 11:19:19 -08001523void OpenGLRenderer::skew(float sx, float sy) {
Romain Guy3b753822013-03-05 10:27:35 -08001524 currentTransform().skew(sx, sy);
Romain Guy807daf72011-01-18 11:19:19 -08001525}
1526
Romain Guyf6a11b82010-06-23 17:47:49 -07001527void OpenGLRenderer::setMatrix(SkMatrix* matrix) {
Romain Guye7078592011-10-28 14:32:20 -07001528 if (matrix) {
Romain Guy3b753822013-03-05 10:27:35 -08001529 currentTransform().load(*matrix);
Romain Guye7078592011-10-28 14:32:20 -07001530 } else {
Romain Guy3b753822013-03-05 10:27:35 -08001531 currentTransform().loadIdentity();
Romain Guye7078592011-10-28 14:32:20 -07001532 }
Romain Guyf6a11b82010-06-23 17:47:49 -07001533}
1534
Chris Craikb98a0162013-02-21 11:30:22 -08001535bool OpenGLRenderer::hasRectToRectTransform() {
Romain Guy3b753822013-03-05 10:27:35 -08001536 return CC_LIKELY(currentTransform().rectToRect());
Chris Craikb98a0162013-02-21 11:30:22 -08001537}
1538
Chris Craikb4589422013-12-26 15:13:13 -08001539void OpenGLRenderer::getMatrix(SkMatrix* matrix) const {
Romain Guy3b753822013-03-05 10:27:35 -08001540 currentTransform().copyTo(*matrix);
Romain Guyf6a11b82010-06-23 17:47:49 -07001541}
1542
1543void OpenGLRenderer::concatMatrix(SkMatrix* matrix) {
Chris Craikf57776b2013-10-25 18:30:17 -07001544 mat4 transform(*matrix);
1545 currentTransform().multiply(transform);
Romain Guyf6a11b82010-06-23 17:47:49 -07001546}
1547
1548///////////////////////////////////////////////////////////////////////////////
1549// Clipping
1550///////////////////////////////////////////////////////////////////////////////
1551
Romain Guybb9524b2010-06-22 18:56:38 -07001552void OpenGLRenderer::setScissorFromClip() {
Romain Guye5ebcb02010-10-15 13:57:28 -07001553 Rect clip(*mSnapshot->clipRect);
1554 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001555
Romain Guy8a4ac612012-07-17 17:32:48 -07001556 if (mCaches.setScissor(clip.left, mSnapshot->height - clip.bottom,
1557 clip.getWidth(), clip.getHeight())) {
1558 mDirtyClip = false;
1559 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001560}
1561
Romain Guy8ce00302013-01-15 18:51:42 -08001562void OpenGLRenderer::ensureStencilBuffer() {
1563 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1564 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1565 // just hope we have one when hasLayer() returns false.
1566 if (hasLayer()) {
1567 attachStencilBufferToLayer(mSnapshot->layer);
1568 }
1569}
1570
1571void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1572 // The layer's FBO is already bound when we reach this stage
1573 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001574 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1575 // is attached after we initiated tiling. We must turn it off,
1576 // attach the new render buffer then turn tiling back on
1577 endTiling();
1578
Romain Guy8d4aeb72013-02-12 16:08:55 -08001579 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001580 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001581 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001582
Romain Guyf735c8e2013-01-31 17:45:55 -08001583 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001584 }
1585}
1586
1587void OpenGLRenderer::setStencilFromClip() {
1588 if (!mCaches.debugOverdraw) {
1589 if (!mSnapshot->clipRegion->isEmpty()) {
1590 // NOTE: The order here is important, we must set dirtyClip to false
1591 // before any draw call to avoid calling back into this method
1592 mDirtyClip = false;
1593
1594 ensureStencilBuffer();
1595
1596 mCaches.stencil.enableWrite();
1597
1598 // Clear the stencil but first make sure we restrict drawing
1599 // to the region's bounds
1600 bool resetScissor = mCaches.enableScissor();
1601 if (resetScissor) {
1602 // The scissor was not set so we now need to update it
1603 setScissorFromClip();
1604 }
1605 mCaches.stencil.clear();
1606 if (resetScissor) mCaches.disableScissor();
1607
1608 // NOTE: We could use the region contour path to generate a smaller mesh
1609 // Since we are using the stencil we could use the red book path
1610 // drawing technique. It might increase bandwidth usage though.
1611
1612 // The last parameter is important: we are not drawing in the color buffer
1613 // so we don't want to dirty the current layer, if any
1614 drawRegionRects(*mSnapshot->clipRegion, 0xff000000, SkXfermode::kSrc_Mode, false);
1615
1616 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001617
1618 // Draw the region used to generate the stencil if the appropriate debug
1619 // mode is enabled
1620 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
1621 drawRegionRects(*mSnapshot->clipRegion, 0x7f0000ff, SkXfermode::kSrcOver_Mode);
1622 }
Romain Guy8ce00302013-01-15 18:51:42 -08001623 } else {
1624 mCaches.stencil.disable();
1625 }
1626 }
1627}
1628
Chris Craikb4589422013-12-26 15:13:13 -08001629const Rect& OpenGLRenderer::getClipBounds() const {
Romain Guy079ba2c2010-07-16 14:12:24 -07001630 return mSnapshot->getLocalClip();
Romain Guybb9524b2010-06-22 18:56:38 -07001631}
1632
Chris Craikf0a59072013-11-19 18:00:46 -08001633/**
1634 * Calculates whether content drawn within the passed bounds would be outside of, or intersect with
1635 * the clipRect. Does not modify the scissor.
1636 *
1637 * @param clipRequired if not null, will be set to true if element intersects clip
1638 * (and wasn't rejected)
1639 *
1640 * @param snapOut if set, the geometry will be treated as having an AA ramp.
1641 * See Rect::snapGeometryToPixelBoundaries()
1642 */
1643bool OpenGLRenderer::calculateQuickRejectForScissor(float left, float top,
1644 float right, float bottom, bool* clipRequired, bool snapOut) const {
Chris Craik39a908c2013-06-13 14:39:01 -07001645 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
Romain Guy8a4ac612012-07-17 17:32:48 -07001646 return true;
1647 }
1648
1649 Rect r(left, top, right, bottom);
Romain Guy3b753822013-03-05 10:27:35 -08001650 currentTransform().mapRect(r);
Chris Craik32f05e32013-09-17 16:20:29 -07001651 r.snapGeometryToPixelBoundaries(snapOut);
Romain Guy8a4ac612012-07-17 17:32:48 -07001652
1653 Rect clipRect(*mSnapshot->clipRect);
1654 clipRect.snapToPixelBoundaries();
1655
Chris Craik39a908c2013-06-13 14:39:01 -07001656 if (!clipRect.intersects(r)) return true;
Romain Guy8a4ac612012-07-17 17:32:48 -07001657
Chris Craikf0a59072013-11-19 18:00:46 -08001658 // clip is required if geometry intersects clip rect
Chris Craik39a908c2013-06-13 14:39:01 -07001659 if (clipRequired) *clipRequired = !clipRect.contains(r);
1660 return false;
Romain Guy35643dd2012-09-18 15:40:58 -07001661}
1662
Chris Craikf0a59072013-11-19 18:00:46 -08001663/**
1664 * Returns false if drawing won't be clipped out.
1665 *
1666 * Makes the decision conservatively, by rounding out the mapped rect before comparing with the
1667 * clipRect. To be used when perfect, pixel accuracy is not possible (esp. with tessellation) but
1668 * rejection is still desired.
1669 *
1670 * This function, unlike quickRejectSetupScissor, should be used where precise geometry information
1671 * isn't known (esp. when geometry adjusts based on scale). Generally, this will be first pass
1672 * rejection where precise rejection isn't important, or precise information isn't available.
1673 */
1674bool OpenGLRenderer::quickRejectConservative(float left, float top,
1675 float right, float bottom) const {
1676 if (mSnapshot->isIgnored() || bottom <= top || right <= left) {
1677 return true;
Chris Craikcb4d6002012-09-25 12:00:29 -07001678 }
Chris Craikf0a59072013-11-19 18:00:46 -08001679
1680 Rect r(left, top, right, bottom);
1681 currentTransform().mapRect(r);
1682 r.roundOut(); // rounded out to be conservative
1683
1684 Rect clipRect(*mSnapshot->clipRect);
1685 clipRect.snapToPixelBoundaries();
1686
1687 if (!clipRect.intersects(r)) return true;
1688
1689 return false;
Chris Craikcb4d6002012-09-25 12:00:29 -07001690}
1691
Chris Craikf0a59072013-11-19 18:00:46 -08001692/**
1693 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1694 *
1695 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1696 * style, and tessellated AA ramp
1697 */
1698bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
1699 SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001700 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001701 bool snapOut = paint && paint->isAntiAlias();
1702
1703 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1704 float outset = paint->getStrokeWidth() * 0.5f;
1705 left -= outset;
1706 top -= outset;
1707 right += outset;
1708 bottom += outset;
1709 }
1710
1711 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001712 return true;
1713 }
1714
Chris Craikb4589422013-12-26 15:13:13 -08001715 if (!isRecording()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001716 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001717 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001718 }
Chris Craik39a908c2013-06-13 14:39:01 -07001719 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001720}
1721
Romain Guy8ce00302013-01-15 18:51:42 -08001722void OpenGLRenderer::debugClip() {
1723#if DEBUG_CLIP_REGIONS
Chris Craikb4589422013-12-26 15:13:13 -08001724 if (!isRecording() && !mSnapshot->clipRegion->isEmpty()) {
Romain Guy8ce00302013-01-15 18:51:42 -08001725 drawRegionRects(*mSnapshot->clipRegion, 0x7f00ff00, SkXfermode::kSrcOver_Mode);
1726 }
1727#endif
1728}
1729
Romain Guy079ba2c2010-07-16 14:12:24 -07001730bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001731 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001732 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1733 if (clipped) {
1734 dirtyClip();
1735 }
1736 return !mSnapshot->clipRect->isEmpty();
1737 }
1738
1739 SkPath path;
1740 path.addRect(left, top, right, bottom);
1741
Romain Guyb7b93e02013-08-01 15:29:25 -07001742 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001743}
1744
1745bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1746 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001747 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001748
1749 SkPath transformed;
1750 path->transform(transform, &transformed);
1751
1752 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001753 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1754 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001755 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001756 if (mSnapshot->previous == mFirstSnapshot) {
1757 clip.setRect(0, 0, mWidth, mHeight);
1758 } else {
1759 Rect* bounds = mSnapshot->previous->clipRect;
1760 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1761 }
Romain Guy8ce00302013-01-15 18:51:42 -08001762 }
1763
1764 SkRegion region;
1765 region.setPath(transformed, clip);
1766
1767 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001768 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001769 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001770 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001771 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001772}
1773
Romain Guy735738c2012-12-03 12:34:51 -08001774bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001775 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1776 if (clipped) {
1777 dirtyClip();
1778 }
1779 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001780}
1781
Chet Haasea23eed82012-04-12 15:19:04 -07001782Rect* OpenGLRenderer::getClipRect() {
1783 return mSnapshot->clipRect;
1784}
1785
Romain Guyf6a11b82010-06-23 17:47:49 -07001786///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001787// Drawing commands
1788///////////////////////////////////////////////////////////////////////////////
1789
Romain Guy54be1cd2011-06-13 19:04:27 -07001790void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001791 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001792 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001793 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001794 // Make sure setScissor & setStencil happen at the beginning of
1795 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001796 if (mDirtyClip) {
1797 if (mCaches.scissorEnabled) {
1798 setScissorFromClip();
1799 }
Romain Guy8ce00302013-01-15 18:51:42 -08001800 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001801 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001802
Romain Guy70ca14e2010-12-13 18:24:33 -08001803 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001804
Romain Guy70ca14e2010-12-13 18:24:33 -08001805 mSetShaderColor = false;
1806 mColorSet = false;
1807 mColorA = mColorR = mColorG = mColorB = 0.0f;
1808 mTextureUnit = 0;
1809 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001810
1811 // Enable debug highlight when what we're about to draw is tested against
1812 // the stencil buffer and if stencil highlight debugging is on
1813 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1814 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1815 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001816
1817 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001818}
1819
1820void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1821 mDescription.hasTexture = true;
1822 mDescription.hasAlpha8Texture = isAlpha8;
1823}
1824
Romain Guyff316ec2013-02-13 18:39:43 -08001825void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1826 mDescription.hasTexture = true;
1827 mDescription.hasColors = true;
1828 mDescription.hasAlpha8Texture = isAlpha8;
1829}
1830
Romain Guyaa6c24c2011-04-28 18:40:04 -07001831void OpenGLRenderer::setupDrawWithExternalTexture() {
1832 mDescription.hasExternalTexture = true;
1833}
1834
Romain Guy15bc6432011-12-13 13:11:32 -08001835void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001836 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001837}
1838
Chris Craik710f46d2012-09-17 17:25:49 -07001839void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001840 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001841}
1842
Romain Guy8d0d4782010-12-14 20:13:35 -08001843void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1844 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001845 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1846 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1847 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001848 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001849 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001850}
1851
Romain Guy86568192010-12-14 15:55:39 -08001852void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1853 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001854 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1855 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1856 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001857 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001858 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001859}
1860
Romain Guy41210632012-07-16 17:04:24 -07001861void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1862 mCaches.fontRenderer->describe(mDescription, paint);
1863}
1864
Romain Guy70ca14e2010-12-13 18:24:33 -08001865void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1866 mColorA = a;
1867 mColorR = r;
1868 mColorG = g;
1869 mColorB = b;
1870 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001871 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001872}
1873
1874void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001875 if (mDrawModifiers.mShader) {
1876 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001877 }
1878}
1879
1880void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001881 if (mDrawModifiers.mColorFilter) {
1882 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001883 }
1884}
1885
Romain Guyf09ef512011-05-27 11:43:46 -07001886void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1887 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1888 mColorA = 1.0f;
1889 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001890 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001891 }
1892}
1893
Romain Guy70ca14e2010-12-13 18:24:33 -08001894void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001895 // When the blending mode is kClear_Mode, we need to use a modulate color
1896 // argb=1,0,0,0
1897 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001898 bool blend = (mColorSet && mColorA < 1.0f) ||
1899 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1900 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001901}
1902
1903void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001904 // When the blending mode is kClear_Mode, we need to use a modulate color
1905 // argb=1,0,0,0
1906 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001907 blend |= (mColorSet && mColorA < 1.0f) ||
1908 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1909 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1910 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001911}
1912
1913void OpenGLRenderer::setupDrawProgram() {
1914 useProgram(mCaches.programCache.get(mDescription));
1915}
1916
1917void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1918 mTrackDirtyRegions = false;
1919}
1920
Chris Craik4063a0e2013-11-15 16:06:56 -08001921void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1922 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001923 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001924 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001925 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001926 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001927
Romain Guy86568192010-12-14 15:55:39 -08001928 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1929 if (!ignoreTransform) {
Chris Craikf57776b2013-10-25 18:30:17 -07001930 mCaches.currentProgram->set(mViewProjMatrix, mModelView, currentTransform(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001931 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001932 } else {
Chris Craikf57776b2013-10-25 18:30:17 -07001933 mCaches.currentProgram->set(mViewProjMatrix, mModelView, mat4::identity(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001934 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001935 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001936}
1937
1938void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001939 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001940 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1941 }
1942}
1943
Romain Guy86568192010-12-14 15:55:39 -08001944void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001945 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001946 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001947 }
1948}
1949
Romain Guy70ca14e2010-12-13 18:24:33 -08001950void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001951 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001952 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001953 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1954 // because it was built into modelView / the geometry, and the SkiaShader needs to
1955 // compensate.
1956 mat4 modelViewWithoutTransform;
1957 modelViewWithoutTransform.loadInverse(currentTransform());
1958 modelViewWithoutTransform.multiply(mModelView);
1959 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001960 }
Chris Craikc3566d02013-02-04 16:16:33 -08001961 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1962 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001963 }
1964}
1965
1966void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001967 if (mDrawModifiers.mColorFilter) {
1968 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001969 }
1970}
1971
Romain Guy41210632012-07-16 17:04:24 -07001972void OpenGLRenderer::setupDrawTextGammaUniforms() {
1973 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1974}
1975
Romain Guy70ca14e2010-12-13 18:24:33 -08001976void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001977 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001978 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001979 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001980}
1981
1982void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001983 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001984 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001985 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001986}
1987
Romain Guyaa6c24c2011-04-28 18:40:04 -07001988void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1989 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001990 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001991 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001992}
1993
Romain Guy8f0095c2011-05-02 17:24:22 -07001994void OpenGLRenderer::setupDrawTextureTransform() {
1995 mDescription.hasTextureTransform = true;
1996}
1997
1998void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001999 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
2000 GL_FALSE, &transform.data[0]);
2001}
2002
Romain Guy70ca14e2010-12-13 18:24:33 -08002003void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002004 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07002005 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002006 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08002007 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08002008 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08002009 }
Romain Guyd71dd362011-12-12 19:03:35 -08002010
Chris Craikcb4d6002012-09-25 12:00:29 -07002011 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002012 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002013 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08002014 }
2015
2016 mCaches.unbindIndicesBuffer();
2017}
2018
Romain Guyff316ec2013-02-13 18:39:43 -08002019void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
2020 bool force = mCaches.unbindMeshBuffer();
2021 GLsizei stride = sizeof(ColorTextureVertex);
2022
2023 mCaches.bindPositionVertexPointer(force, vertices, stride);
2024 if (mCaches.currentProgram->texCoords >= 0) {
2025 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
2026 }
2027 int slot = mCaches.currentProgram->getAttrib("colors");
2028 if (slot >= 0) {
2029 glEnableVertexAttribArray(slot);
2030 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
2031 }
2032
2033 mCaches.unbindIndicesBuffer();
2034}
2035
Romain Guy3b748a42013-04-17 18:54:38 -07002036void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
2037 bool force = false;
2038 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
2039 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
2040 // use the default VBO found in Caches
2041 if (!vertices || vbo) {
2042 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
2043 } else {
2044 force = mCaches.unbindMeshBuffer();
2045 }
2046 mCaches.bindIndicesBuffer();
2047
Chris Craikcb4d6002012-09-25 12:00:29 -07002048 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08002049 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07002050 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08002051 }
Romain Guy70ca14e2010-12-13 18:24:33 -08002052}
2053
Romain Guy448455f2013-07-22 13:57:50 -07002054void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08002055 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07002056 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002057 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07002058}
2059
Romain Guy70ca14e2010-12-13 18:24:33 -08002060///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07002061// Drawing
2062///////////////////////////////////////////////////////////////////////////////
2063
Chris Craikff785832013-03-08 13:12:16 -08002064status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
2065 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07002066 status_t status;
Chris Craikf57776b2013-10-25 18:30:17 -07002067
Chris Craikba9b6132013-12-15 17:10:19 -08002068 if (mCaches.propertyDirtyViewport) {
2069 // force recalc of view/proj matrices
2070 setViewport(mWidth, mHeight);
2071 mCaches.propertyDirtyViewport = false;
2072 }
2073
Romain Guy0fe478e2010-11-08 12:08:41 -08002074 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
2075 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07002076 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07002077 // compute 3d ordering
2078 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07002079 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07002080 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08002081 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
2082 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07002083 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08002084 }
2085
Chris Craik28ce94a2013-05-31 11:38:03 -07002086 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
2087 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08002088 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
2089 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07002090
2091 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07002092 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07002093
Chris Craikf57776b2013-10-25 18:30:17 -07002094 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08002095 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07002096
Romain Guy65549432012-03-26 16:45:05 -07002097 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08002098}
2099
Romain Guya168d732011-03-18 16:50:13 -07002100void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
2101 int alpha;
2102 SkXfermode::Mode mode;
2103 getAlphaAndMode(paint, &alpha, &mode);
2104
Romain Guy886b2752013-01-04 12:26:18 -08002105 int color = paint != NULL ? paint->getColor() : 0;
2106
Romain Guya168d732011-03-18 16:50:13 -07002107 float x = left;
2108 float y = top;
2109
Romain Guy886b2752013-01-04 12:26:18 -08002110 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2111
Romain Guya168d732011-03-18 16:50:13 -07002112 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08002113 if (currentTransform().isPureTranslate()) {
2114 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2115 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07002116 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08002117
2118 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08002119 } else {
Romain Guy886b2752013-01-04 12:26:18 -08002120 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07002121 }
2122
Romain Guy3b748a42013-04-17 18:54:38 -07002123 // No need to check for a UV mapper on the texture object, only ARGB_8888
2124 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08002125 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07002126 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
2127 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07002128}
2129
Romain Guy03c00b52013-06-20 18:30:28 -07002130/**
2131 * Important note: this method is intended to draw batches of bitmaps and
2132 * will not set the scissor enable or dirty the current layer, if any.
2133 * The caller is responsible for properly dirtying the current layer.
2134 */
Romain Guy55b6f952013-06-27 15:27:09 -07002135status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07002136 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002137 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07002138 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08002139 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07002140
Chris Craik527a3aa2013-03-04 10:19:31 -08002141 const AutoTexture autoCleanup(texture);
2142
2143 int alpha;
2144 SkXfermode::Mode mode;
2145 getAlphaAndMode(paint, &alpha, &mode);
2146
2147 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07002148 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08002149
2150 const float x = (int) floorf(bounds.left + 0.5f);
2151 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002152 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08002153 int color = paint != NULL ? paint->getColor() : 0;
2154 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2155 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002156 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002157 GL_TRIANGLES, bitmapCount * 6, true,
2158 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002159 } else {
2160 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2161 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002162 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002163 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2164 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002165 }
2166
2167 return DrawGlInfo::kStatusDrew;
2168}
2169
Chet Haase48659092012-05-31 15:21:51 -07002170status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002171 const float right = left + bitmap->width();
2172 const float bottom = top + bitmap->height();
2173
Chris Craikf0a59072013-11-19 18:00:46 -08002174 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002175 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002176 }
2177
Romain Guya1d3c912011-12-13 14:55:06 -08002178 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002179 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002180 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002181 const AutoTexture autoCleanup(texture);
2182
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002183 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002184 drawAlphaBitmap(texture, left, top, paint);
2185 } else {
2186 drawTextureRect(left, top, right, bottom, texture, paint);
2187 }
Chet Haase48659092012-05-31 15:21:51 -07002188
2189 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002190}
2191
Chet Haase48659092012-05-31 15:21:51 -07002192status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002193 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2194 const mat4 transform(*matrix);
2195 transform.mapRect(r);
2196
Chris Craikf0a59072013-11-19 18:00:46 -08002197 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002198 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002199 }
2200
Romain Guya1d3c912011-12-13 14:55:06 -08002201 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002202 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002203 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002204 const AutoTexture autoCleanup(texture);
2205
Romain Guy5b3b3522010-10-27 18:57:51 -07002206 // This could be done in a cheaper way, all we need is pass the matrix
2207 // to the vertex shader. The save/restore is a bit overkill.
2208 save(SkCanvas::kMatrix_SaveFlag);
2209 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002210 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002211 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2212 } else {
2213 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2214 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002215 restore();
Chet Haase48659092012-05-31 15:21:51 -07002216
2217 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002218}
2219
Chet Haase48659092012-05-31 15:21:51 -07002220status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002221 const float right = left + bitmap->width();
2222 const float bottom = top + bitmap->height();
2223
Chris Craikf0a59072013-11-19 18:00:46 -08002224 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002225 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002226 }
2227
2228 mCaches.activeTexture(0);
2229 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2230 const AutoTexture autoCleanup(texture);
2231
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002232 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002233 drawAlphaBitmap(texture, left, top, paint);
2234 } else {
2235 drawTextureRect(left, top, right, bottom, texture, paint);
2236 }
Chet Haase48659092012-05-31 15:21:51 -07002237
2238 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002239}
2240
Chet Haase48659092012-05-31 15:21:51 -07002241status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002242 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002243 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002244 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002245 }
2246
Chris Craik39a908c2013-06-13 14:39:01 -07002247 // TODO: use quickReject on bounds from vertices
2248 mCaches.enableScissor();
2249
Romain Guyb18d2d02011-02-10 15:52:54 -08002250 float left = FLT_MAX;
2251 float top = FLT_MAX;
2252 float right = FLT_MIN;
2253 float bottom = FLT_MIN;
2254
Romain Guya92bb4d2012-10-16 11:08:44 -07002255 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002256
Romain Guyff316ec2013-02-13 18:39:43 -08002257 ColorTextureVertex mesh[count];
2258 ColorTextureVertex* vertex = mesh;
2259
2260 bool cleanupColors = false;
2261 if (!colors) {
2262 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2263 colors = new int[colorsCount];
2264 memset(colors, 0xff, colorsCount * sizeof(int));
2265 cleanupColors = true;
2266 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002267
Romain Guy3b748a42013-04-17 18:54:38 -07002268 mCaches.activeTexture(0);
2269 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2270 const UvMapper& mapper(getMapper(texture));
2271
Romain Guy5a7b4662011-01-20 19:09:30 -08002272 for (int32_t y = 0; y < meshHeight; y++) {
2273 for (int32_t x = 0; x < meshWidth; x++) {
2274 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2275
2276 float u1 = float(x) / meshWidth;
2277 float u2 = float(x + 1) / meshWidth;
2278 float v1 = float(y) / meshHeight;
2279 float v2 = float(y + 1) / meshHeight;
2280
Romain Guy3b748a42013-04-17 18:54:38 -07002281 mapper.map(u1, v1, u2, v2);
2282
Romain Guy5a7b4662011-01-20 19:09:30 -08002283 int ax = i + (meshWidth + 1) * 2;
2284 int ay = ax + 1;
2285 int bx = i;
2286 int by = bx + 1;
2287 int cx = i + 2;
2288 int cy = cx + 1;
2289 int dx = i + (meshWidth + 1) * 2 + 2;
2290 int dy = dx + 1;
2291
Romain Guyff316ec2013-02-13 18:39:43 -08002292 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2293 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2294 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002295
Romain Guyff316ec2013-02-13 18:39:43 -08002296 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2297 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2298 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002299
Romain Guya92bb4d2012-10-16 11:08:44 -07002300 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2301 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2302 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2303 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002304 }
2305 }
2306
Chris Craikf0a59072013-11-19 18:00:46 -08002307 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002308 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002309 return DrawGlInfo::kStatusDone;
2310 }
2311
Romain Guyff316ec2013-02-13 18:39:43 -08002312 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002313 texture = mCaches.textureCache.get(bitmap);
2314 if (!texture) {
2315 if (cleanupColors) delete[] colors;
2316 return DrawGlInfo::kStatusDone;
2317 }
Romain Guyff316ec2013-02-13 18:39:43 -08002318 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002319 const AutoTexture autoCleanup(texture);
2320
2321 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2322 texture->setFilter(FILTER(paint), true);
2323
2324 int alpha;
2325 SkXfermode::Mode mode;
2326 getAlphaAndMode(paint, &alpha, &mode);
2327
Romain Guyff316ec2013-02-13 18:39:43 -08002328 float a = alpha / 255.0f;
2329
Romain Guya92bb4d2012-10-16 11:08:44 -07002330 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002331 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002332 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002333
Romain Guyff316ec2013-02-13 18:39:43 -08002334 setupDraw();
2335 setupDrawWithTextureAndColor();
2336 setupDrawColor(a, a, a, a);
2337 setupDrawColorFilter();
2338 setupDrawBlending(true, mode, false);
2339 setupDrawProgram();
2340 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002341 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002342 setupDrawTexture(texture->id);
2343 setupDrawPureColorUniforms();
2344 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002345 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002346
2347 glDrawArrays(GL_TRIANGLES, 0, count);
2348
Romain Guyff316ec2013-02-13 18:39:43 -08002349 int slot = mCaches.currentProgram->getAttrib("colors");
2350 if (slot >= 0) {
2351 glDisableVertexAttribArray(slot);
2352 }
2353
2354 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002355
2356 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002357}
2358
Chet Haase48659092012-05-31 15:21:51 -07002359status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002360 float srcLeft, float srcTop, float srcRight, float srcBottom,
2361 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002362 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002363 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002364 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002365 }
2366
Romain Guya1d3c912011-12-13 14:55:06 -08002367 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002368 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002369 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002370 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002371
Romain Guy8ba548f2010-06-30 19:21:21 -07002372 const float width = texture->width;
2373 const float height = texture->height;
2374
Romain Guy3b748a42013-04-17 18:54:38 -07002375 float u1 = fmax(0.0f, srcLeft / width);
2376 float v1 = fmax(0.0f, srcTop / height);
2377 float u2 = fmin(1.0f, srcRight / width);
2378 float v2 = fmin(1.0f, srcBottom / height);
2379
2380 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002381
Romain Guy03750a02010-10-18 14:06:08 -07002382 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002383 resetDrawTextureTexCoords(u1, v1, u2, v2);
2384
Romain Guy03750a02010-10-18 14:06:08 -07002385 int alpha;
2386 SkXfermode::Mode mode;
2387 getAlphaAndMode(paint, &alpha, &mode);
2388
Romain Guyd21b6e12011-11-30 20:21:23 -08002389 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2390
Romain Guy886b2752013-01-04 12:26:18 -08002391 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2392 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002393
Romain Guy886b2752013-01-04 12:26:18 -08002394 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2395 // Apply a scale transform on the canvas only when a shader is in use
2396 // Skia handles the ratio between the dst and src rects as a scale factor
2397 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002398 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002399 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002400
Romain Guy3b753822013-03-05 10:27:35 -08002401 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2402 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2403 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002404
2405 dstRight = x + (dstRight - dstLeft);
2406 dstBottom = y + (dstBottom - dstTop);
2407
2408 dstLeft = x;
2409 dstTop = y;
2410
2411 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2412 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002413 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002414 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002415 }
2416
2417 if (CC_UNLIKELY(useScaleTransform)) {
2418 save(SkCanvas::kMatrix_SaveFlag);
2419 translate(dstLeft, dstTop);
2420 scale(scaleX, scaleY);
2421
2422 dstLeft = 0.0f;
2423 dstTop = 0.0f;
2424
2425 dstRight = srcRight - srcLeft;
2426 dstBottom = srcBottom - srcTop;
2427 }
2428
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002429 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002430 int color = paint ? paint->getColor() : 0;
2431 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2432 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002433 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002434 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2435 } else {
2436 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2437 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002438 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002439 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2440 }
2441
2442 if (CC_UNLIKELY(useScaleTransform)) {
2443 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002444 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002445
2446 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002447
2448 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002449}
2450
Romain Guy3b748a42013-04-17 18:54:38 -07002451status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002452 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002453 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002454 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002455 }
2456
Romain Guy4c2547f2013-06-11 16:19:24 -07002457 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002458 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2459 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002460
Romain Guy03c00b52013-06-20 18:30:28 -07002461 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002462}
2463
Romain Guy03c00b52013-06-20 18:30:28 -07002464status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2465 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002466 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002467 return DrawGlInfo::kStatusDone;
2468 }
2469
Romain Guy211370f2012-02-01 16:10:55 -08002470 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002471 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002472 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002473 if (!texture) return DrawGlInfo::kStatusDone;
2474 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002475
Romain Guya4adcf02013-02-28 12:15:35 -08002476 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2477 texture->setFilter(GL_LINEAR, true);
2478
Romain Guy03c00b52013-06-20 18:30:28 -07002479 int alpha;
2480 SkXfermode::Mode mode;
2481 getAlphaAndMode(paint, &alpha, &mode);
2482
Romain Guy3b753822013-03-05 10:27:35 -08002483 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002484 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002485 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002486 const float offsetX = left + currentTransform().getTranslateX();
2487 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002488 const size_t count = mesh->quads.size();
2489 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002490 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002491 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002492 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2493 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2494 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002495 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002496 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002497 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002498 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002499 }
2500 }
2501
Chris Craik4063a0e2013-11-15 16:06:56 -08002502 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002503 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002504 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2505 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002506
Romain Guy3b748a42013-04-17 18:54:38 -07002507 right = x + right - left;
2508 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002509 left = x;
2510 top = y;
2511 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002512 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002513 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2514 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2515 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2516 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002517 }
Chet Haase48659092012-05-31 15:21:51 -07002518
2519 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002520}
2521
Romain Guy03c00b52013-06-20 18:30:28 -07002522/**
2523 * Important note: this method is intended to draw batches of 9-patch objects and
2524 * will not set the scissor enable or dirty the current layer, if any.
2525 * The caller is responsible for properly dirtying the current layer.
2526 */
2527status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2528 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2529 mCaches.activeTexture(0);
2530 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2531 if (!texture) return DrawGlInfo::kStatusDone;
2532 const AutoTexture autoCleanup(texture);
2533
2534 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2535 texture->setFilter(GL_LINEAR, true);
2536
2537 int alpha;
2538 SkXfermode::Mode mode;
2539 getAlphaAndMode(paint, &alpha, &mode);
2540
2541 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002542 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002543 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002544
2545 return DrawGlInfo::kStatusDrew;
2546}
2547
Chris Craik65cd6122012-12-10 17:56:27 -08002548status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2549 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002550 // not missing call to quickReject/dirtyLayer, always done at a higher level
2551
Chris Craik6d29c8d2013-05-08 18:35:44 -07002552 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002553 // no vertices to draw
2554 return DrawGlInfo::kStatusDone;
2555 }
2556
Chris Craikcb4d6002012-09-25 12:00:29 -07002557 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002558 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2559 bool isAA = paint->isAntiAlias();
2560
Chris Craik710f46d2012-09-17 17:25:49 -07002561 setupDraw();
2562 setupDrawNoTexture();
2563 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002564 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2565 setupDrawColorFilter();
2566 setupDrawShader();
2567 setupDrawBlending(isAA, mode);
2568 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002569 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002570 setupDrawColorUniforms();
2571 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002572 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002573
ztenghui55bfb4e2013-12-03 10:38:55 -08002574 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002575 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002576 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002577 mCaches.resetTexCoordsVertexPointer();
2578 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002579
Chris Craik710f46d2012-09-17 17:25:49 -07002580 int alphaSlot = -1;
2581 if (isAA) {
2582 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2583 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002584
Chris Craik710f46d2012-09-17 17:25:49 -07002585 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002586 glEnableVertexAttribArray(alphaSlot);
2587 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002588 }
Romain Guy04299382012-07-18 17:15:41 -07002589
Chris Craik6d29c8d2013-05-08 18:35:44 -07002590 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002591
Chris Craik710f46d2012-09-17 17:25:49 -07002592 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002593 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002594 }
Chris Craik65cd6122012-12-10 17:56:27 -08002595
2596 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002597}
2598
2599/**
Chris Craik65cd6122012-12-10 17:56:27 -08002600 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2601 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2602 * screen space in all directions. However, instead of using a fragment shader to compute the
2603 * translucency of the color from its position, we simply use a varying parameter to define how far
2604 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2605 *
2606 * Doesn't yet support joins, caps, or path effects.
2607 */
2608status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2609 VertexBuffer vertexBuffer;
2610 // TODO: try clipping large paths to viewport
2611 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2612
Romain Guyc46d07a2013-03-15 19:06:39 -07002613 if (hasLayer()) {
2614 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002615 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002616 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2617 }
Chris Craik65cd6122012-12-10 17:56:27 -08002618
2619 return drawVertexBuffer(vertexBuffer, paint);
2620}
2621
2622/**
2623 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2624 * and additional geometry for defining an alpha slope perimeter.
2625 *
2626 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2627 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2628 * in-shader alpha region, but found it to be taxing on some GPUs.
2629 *
2630 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2631 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002632 */
Chet Haase48659092012-05-31 15:21:51 -07002633status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002634 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002635
Chris Craik65cd6122012-12-10 17:56:27 -08002636 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002637
Chris Craik65cd6122012-12-10 17:56:27 -08002638 VertexBuffer buffer;
2639 SkRect bounds;
2640 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002641
Chris Craikf0a59072013-11-19 18:00:46 -08002642 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2643 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002644 return DrawGlInfo::kStatusDone;
2645 }
2646
Romain Guy3b753822013-03-05 10:27:35 -08002647 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002648
Chris Craik65cd6122012-12-10 17:56:27 -08002649 bool useOffset = !paint->isAntiAlias();
2650 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002651}
2652
Chet Haase48659092012-05-31 15:21:51 -07002653status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002654 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002655
Chris Craik6d29c8d2013-05-08 18:35:44 -07002656 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002657
Chris Craik6d29c8d2013-05-08 18:35:44 -07002658 VertexBuffer buffer;
2659 SkRect bounds;
2660 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002661
Chris Craikf0a59072013-11-19 18:00:46 -08002662 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2663 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002664 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002665 }
2666
Chris Craik6d29c8d2013-05-08 18:35:44 -07002667 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002668
Chris Craik6d29c8d2013-05-08 18:35:44 -07002669 bool useOffset = !paint->isAntiAlias();
2670 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002671}
2672
Chet Haase48659092012-05-31 15:21:51 -07002673status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002674 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002675 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002676
Romain Guyae88e5e2010-10-22 17:49:18 -07002677 Rect& clip(*mSnapshot->clipRect);
2678 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002679
Romain Guy3d58c032010-07-14 16:34:53 -07002680 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002681
2682 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002683}
Romain Guy9d5316e2010-06-24 19:30:36 -07002684
Chet Haase48659092012-05-31 15:21:51 -07002685status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2686 SkPaint* paint) {
2687 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002688 const AutoTexture autoCleanup(texture);
2689
2690 const float x = left + texture->left - texture->offset;
2691 const float y = top + texture->top - texture->offset;
2692
2693 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002694
2695 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002696}
2697
Chet Haase48659092012-05-31 15:21:51 -07002698status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002699 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002700 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002701 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002702 return DrawGlInfo::kStatusDone;
2703 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002704
Chris Craikcb4d6002012-09-25 12:00:29 -07002705 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002706 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002707 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002708 right - left, bottom - top, rx, ry, p);
2709 return drawShape(left, top, texture, p);
2710 }
2711
2712 SkPath path;
2713 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002714 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2715 float outset = p->getStrokeWidth() / 2;
2716 rect.outset(outset, outset);
2717 rx += outset;
2718 ry += outset;
2719 }
Chris Craik710f46d2012-09-17 17:25:49 -07002720 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002721 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002722}
2723
Chris Craik710f46d2012-09-17 17:25:49 -07002724status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002725 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002726 x + radius, y + radius, p) ||
2727 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002728 return DrawGlInfo::kStatusDone;
2729 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002730 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002731 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002732 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002733 return drawShape(x - radius, y - radius, texture, p);
2734 }
2735
2736 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002737 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2738 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2739 } else {
2740 path.addCircle(x, y, radius);
2741 }
Chris Craik65cd6122012-12-10 17:56:27 -08002742 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002743}
Romain Guy01d58e42011-01-19 21:54:02 -08002744
Chet Haase48659092012-05-31 15:21:51 -07002745status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002746 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002747 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002748 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002749 return DrawGlInfo::kStatusDone;
2750 }
Romain Guy01d58e42011-01-19 21:54:02 -08002751
Chris Craikcb4d6002012-09-25 12:00:29 -07002752 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002753 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002754 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002755 return drawShape(left, top, texture, p);
2756 }
2757
2758 SkPath path;
2759 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002760 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2761 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2762 }
Chris Craik710f46d2012-09-17 17:25:49 -07002763 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002764 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002765}
2766
Chet Haase48659092012-05-31 15:21:51 -07002767status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002768 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002769 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002770 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002771 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002772 }
2773
Chris Craik780c1282012-10-04 14:10:49 -07002774 if (fabs(sweepAngle) >= 360.0f) {
2775 return drawOval(left, top, right, bottom, p);
2776 }
2777
2778 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002779 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002780 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002781 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002782 startAngle, sweepAngle, useCenter, p);
2783 return drawShape(left, top, texture, p);
2784 }
2785
2786 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2787 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2788 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2789 }
2790
2791 SkPath path;
2792 if (useCenter) {
2793 path.moveTo(rect.centerX(), rect.centerY());
2794 }
2795 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2796 if (useCenter) {
2797 path.close();
2798 }
Chris Craik65cd6122012-12-10 17:56:27 -08002799 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002800}
2801
Romain Guycf8675e2012-10-02 12:32:25 -07002802// See SkPaintDefaults.h
2803#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2804
Chet Haase48659092012-05-31 15:21:51 -07002805status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002806 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002807 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002808 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002809 }
2810
Chris Craik710f46d2012-09-17 17:25:49 -07002811 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002812 // only fill style is supported by drawConvexPath, since others have to handle joins
2813 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2814 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2815 mCaches.activeTexture(0);
2816 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002817 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002818 return drawShape(left, top, texture, p);
2819 }
2820
2821 SkPath path;
2822 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2823 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2824 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2825 }
2826 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002827 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002828 }
2829
Romain Guy3b753822013-03-05 10:27:35 -08002830 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002831 SkPath path;
2832 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002833 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002834 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002835 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002836 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002837 }
Romain Guyc7d53492010-06-25 13:41:57 -07002838}
Romain Guy9d5316e2010-06-24 19:30:36 -07002839
Raph Levien416a8472012-07-19 22:48:17 -07002840void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2841 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2842 float x, float y) {
2843 mCaches.activeTexture(0);
2844
2845 // NOTE: The drop shadow will not perform gamma correction
2846 // if shader-based correction is enabled
2847 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2848 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002849 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002850 // If the drop shadow exceeds the max texture size or couldn't be
2851 // allocated, skip drawing
2852 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002853 const AutoTexture autoCleanup(shadow);
2854
Chris Craikc3566d02013-02-04 16:16:33 -08002855 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2856 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002857
Chris Craikc3566d02013-02-04 16:16:33 -08002858 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2859 int shadowColor = mDrawModifiers.mShadowColor;
2860 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002861 shadowColor = 0xffffffff;
2862 }
2863
2864 setupDraw();
2865 setupDrawWithTexture(true);
2866 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2867 setupDrawColorFilter();
2868 setupDrawShader();
2869 setupDrawBlending(true, mode);
2870 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002871 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2872 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002873 setupDrawTexture(shadow->id);
2874 setupDrawPureColorUniforms();
2875 setupDrawColorFilterUniforms();
2876 setupDrawShaderUniforms();
2877 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2878
2879 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2880}
2881
Romain Guy768bffc2013-02-27 13:50:45 -08002882bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2883 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2884 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2885}
2886
Chet Haase48659092012-05-31 15:21:51 -07002887status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002888 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002889 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002890 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002891 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002892
Romain Guy671d6cf2012-01-18 12:39:17 -08002893 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002894 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002895 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002896 }
2897
Chris Craik39a908c2013-06-13 14:39:01 -07002898 mCaches.enableScissor();
2899
Romain Guy671d6cf2012-01-18 12:39:17 -08002900 float x = 0.0f;
2901 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002902 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002903 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002904 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2905 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002906 }
2907
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002908 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002909 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002910
2911 int alpha;
2912 SkXfermode::Mode mode;
2913 getAlphaAndMode(paint, &alpha, &mode);
2914
Chris Craikc3566d02013-02-04 16:16:33 -08002915 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002916 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2917 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002918 }
2919
Romain Guy671d6cf2012-01-18 12:39:17 -08002920 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002921 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002922 if (pureTranslate && !linearFilter) {
2923 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2924 }
Romain Guy257ae352013-03-20 16:31:12 -07002925 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002926
2927 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2928 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2929
Romain Guy211370f2012-02-01 16:10:55 -08002930 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002931
Victoria Lease1e546812013-06-25 14:25:17 -07002932 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002933 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002934 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002935 if (hasActiveLayer) {
2936 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002937 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002938 }
2939 dirtyLayerUnchecked(bounds, getRegion());
2940 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002941 }
Chet Haase48659092012-05-31 15:21:51 -07002942
2943 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002944}
2945
Romain Guy624234f2013-03-05 16:43:31 -08002946mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2947 mat4 fontTransform;
2948 if (CC_LIKELY(transform.isPureTranslate())) {
2949 fontTransform = mat4::identity();
2950 } else {
2951 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002952 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002953 } else {
2954 float sx, sy;
2955 currentTransform().decomposeScale(sx, sy);
2956 fontTransform.loadScale(sx, sy, 1.0f);
2957 }
2958 }
2959 return fontTransform;
2960}
2961
Chris Craik41541822013-05-03 16:35:54 -07002962status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2963 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002964 DrawOpMode drawOpMode) {
2965
Chris Craik527a3aa2013-03-04 10:19:31 -08002966 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002967 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2968 // drawing as ops from DeferredDisplayList are already filtered for these
2969 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002970 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002971 return DrawGlInfo::kStatusDone;
2972 }
Romain Guycac5fd32011-12-01 20:08:50 -08002973 }
2974
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002975 const float oldX = x;
2976 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002977
2978 const mat4& transform = currentTransform();
2979 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002980
Romain Guy211370f2012-02-01 16:10:55 -08002981 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002982 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2983 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002984 }
2985
Romain Guy86568192010-12-14 15:55:39 -08002986 int alpha;
2987 SkXfermode::Mode mode;
2988 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002989
Romain Guyc74f45a2013-02-26 19:10:14 -08002990 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2991
Chris Craikc3566d02013-02-04 16:16:33 -08002992 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002993 fontRenderer.setFont(paint, mat4::identity());
2994 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2995 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002996 }
2997
Romain Guy19d4dd82013-03-04 11:14:26 -08002998 const bool hasActiveLayer = hasLayer();
2999
Romain Guy624234f2013-03-05 16:43:31 -08003000 // We only pass a partial transform to the font renderer. That partial
3001 // matrix defines how glyphs are rasterized. Typically we want glyphs
3002 // to be rasterized at their final size on screen, which means the partial
3003 // matrix needs to take the scale factor into account.
3004 // When a partial matrix is used to transform glyphs during rasterization,
3005 // the mesh is generated with the inverse transform (in the case of scale,
3006 // the mesh is generated at 1.0 / scale for instance.) This allows us to
3007 // apply the full transform matrix at draw time in the vertex shader.
3008 // Applying the full matrix in the shader is the easiest way to handle
3009 // rotation and perspective and allows us to always generated quads in the
3010 // font renderer which greatly simplifies the code, clipping in particular.
3011 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08003012 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08003013
Romain Guy6620c6d2010-12-06 18:07:02 -08003014 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08003015 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07003016 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07003017
Romain Guy624234f2013-03-05 16:43:31 -08003018 // TODO: Implement better clipping for scaled/rotated text
3019 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07003020 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 -07003021
Raph Levien996e57c2012-07-23 15:22:52 -07003022 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07003023 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08003024
3025 // don't call issuedrawcommand, do it at end of batch
3026 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07003027 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07003028 SkPaint paintCopy(*paint);
3029 paintCopy.setTextAlign(SkPaint::kLeft_Align);
3030 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003031 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003032 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07003033 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07003034 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07003035 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003036
Chris Craik527a3aa2013-03-04 10:19:31 -08003037 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08003038 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07003039 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08003040 }
Chris Craik41541822013-05-03 16:35:54 -07003041 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07003042 }
Romain Guy694b5192010-07-21 21:33:20 -07003043
Chris Craike63f7c622013-10-17 10:30:55 -07003044 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07003045
3046 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07003047}
3048
Chet Haase48659092012-05-31 15:21:51 -07003049status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08003050 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08003051 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07003052 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08003053 }
3054
Chris Craik39a908c2013-06-13 14:39:01 -07003055 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
3056 mCaches.enableScissor();
3057
Romain Guyb1d0a4e2012-07-13 18:25:35 -07003058 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08003059 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07003060 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08003061
3062 int alpha;
3063 SkXfermode::Mode mode;
3064 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07003065 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08003066
Romain Guy97771732012-02-28 18:17:02 -08003067 const Rect* clip = &mSnapshot->getLocalClip();
3068 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 -08003069
Romain Guy97771732012-02-28 18:17:02 -08003070 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08003071
3072 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07003073 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08003074 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08003075 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08003076 dirtyLayerUnchecked(bounds, getRegion());
3077 }
Romain Guy97771732012-02-28 18:17:02 -08003078 }
Chet Haase48659092012-05-31 15:21:51 -07003079
3080 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08003081}
3082
Chet Haase48659092012-05-31 15:21:51 -07003083status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
3084 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07003085
Romain Guya1d3c912011-12-13 14:55:06 -08003086 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07003087
Romain Guyfb8b7632010-08-23 21:05:08 -07003088 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07003089 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07003090 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07003091
Romain Guy8b55f372010-08-18 17:10:07 -07003092 const float x = texture->left - texture->offset;
3093 const float y = texture->top - texture->offset;
3094
Romain Guy01d58e42011-01-19 21:54:02 -08003095 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07003096
3097 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07003098}
3099
Chris Craika08f95c2013-03-15 17:24:33 -07003100status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07003101 if (!layer) {
3102 return DrawGlInfo::kStatusDone;
3103 }
3104
Romain Guyb2e2f242012-10-17 18:18:35 -07003105 mat4* transform = NULL;
3106 if (layer->isTextureLayer()) {
3107 transform = &layer->getTransform();
3108 if (!transform->isIdentity()) {
3109 save(0);
Romain Guy3b753822013-03-05 10:27:35 -08003110 currentTransform().multiply(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07003111 }
3112 }
3113
Chris Craik39a908c2013-06-13 14:39:01 -07003114 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08003115 const bool rejected = calculateQuickRejectForScissor(x, y,
3116 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07003117
3118 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07003119 if (transform && !transform->isIdentity()) {
3120 restore();
3121 }
Chet Haase48659092012-05-31 15:21:51 -07003122 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08003123 }
3124
Romain Guy5bb3c732012-11-29 17:52:58 -08003125 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08003126
Chris Craik39a908c2013-06-13 14:39:01 -07003127 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08003128 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08003129
Romain Guy211370f2012-02-01 16:10:55 -08003130 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08003131 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
3132 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07003133
Romain Guyc88e3572011-01-22 00:32:12 -08003134 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07003135 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3136 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08003137 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07003138 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08003139 setupDraw();
3140 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08003141 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08003142 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07003143 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08003144 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08003145 setupDrawPureColorUniforms();
3146 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07003147 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08003148 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3149 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
3150 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07003151
Romain Guyd21b6e12011-11-30 20:21:23 -08003152 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08003153 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07003154 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003155 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003156 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003157 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003158 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3159 }
Romain Guyf219da52011-01-16 12:54:25 -08003160
Romain Guy448455f2013-07-22 13:57:50 -07003161 TextureVertex* mesh = &layer->mesh[0];
3162 GLsizei elementsCount = layer->meshElementCount;
3163
3164 while (elementsCount > 0) {
3165 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3166
Romain Guy3380cfd2013-08-15 16:57:57 -07003167 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003168 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3169 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3170
3171 elementsCount -= drawCount;
3172 // Though there are 4 vertices in a quad, we use 6 indices per
3173 // quad to draw with GL_TRIANGLES
3174 mesh += (drawCount / 6) * 4;
3175 }
Romain Guyf219da52011-01-16 12:54:25 -08003176
Romain Guy3a3133d2011-02-01 22:59:58 -08003177#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003178 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003179#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003180 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003181
Chris Craikc3566d02013-02-04 16:16:33 -08003182 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003183
Romain Guy5bb3c732012-11-29 17:52:58 -08003184 if (layer->debugDrawUpdate) {
3185 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003186 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3187 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3188 }
Romain Guyf219da52011-01-16 12:54:25 -08003189 }
Chris Craik34416ea2013-04-15 16:08:28 -07003190 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003191
Romain Guyb2e2f242012-10-17 18:18:35 -07003192 if (transform && !transform->isIdentity()) {
3193 restore();
3194 }
3195
Chet Haase48659092012-05-31 15:21:51 -07003196 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003197}
3198
Romain Guy6926c722010-07-12 20:20:03 -07003199///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003200// Shaders
3201///////////////////////////////////////////////////////////////////////////////
3202
3203void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003204 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003205}
3206
Romain Guy06f96e22010-07-30 19:18:16 -07003207void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003208 mDrawModifiers.mShader = shader;
3209 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003210 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003211 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003212}
3213
Romain Guyd27977d2010-07-14 19:18:51 -07003214///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003215// Color filters
3216///////////////////////////////////////////////////////////////////////////////
3217
3218void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003219 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003220}
3221
3222void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003223 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003224}
3225
3226///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003227// Drop shadow
3228///////////////////////////////////////////////////////////////////////////////
3229
3230void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003231 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003232}
3233
3234void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003235 mDrawModifiers.mHasShadow = true;
3236 mDrawModifiers.mShadowRadius = radius;
3237 mDrawModifiers.mShadowDx = dx;
3238 mDrawModifiers.mShadowDy = dy;
3239 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003240}
3241
3242///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003243// Draw filters
3244///////////////////////////////////////////////////////////////////////////////
3245
3246void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003247 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3248 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003249 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003250 mDrawModifiers.mPaintFilterClearBits = 0;
3251 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003252}
3253
3254void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003255 mDrawModifiers.mHasDrawFilter = true;
3256 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3257 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003258}
3259
Chris Craika08f95c2013-03-15 17:24:33 -07003260SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003261 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003262 return paint;
3263 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003264
3265 uint32_t flags = paint->getFlags();
3266
3267 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003268 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3269 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003270
3271 return &mFilteredPaint;
3272}
3273
3274///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003275// Drawing implementation
3276///////////////////////////////////////////////////////////////////////////////
3277
Romain Guy3b748a42013-04-17 18:54:38 -07003278Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3279 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3280 if (!texture) {
3281 return mCaches.textureCache.get(bitmap);
3282 }
3283 return texture;
3284}
3285
Romain Guy01d58e42011-01-19 21:54:02 -08003286void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3287 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003288 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003289 return;
3290 }
3291
3292 int alpha;
3293 SkXfermode::Mode mode;
3294 getAlphaAndMode(paint, &alpha, &mode);
3295
3296 setupDraw();
3297 setupDrawWithTexture(true);
3298 setupDrawAlpha8Color(paint->getColor(), alpha);
3299 setupDrawColorFilter();
3300 setupDrawShader();
3301 setupDrawBlending(true, mode);
3302 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003303 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3304 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003305 setupDrawTexture(texture->id);
3306 setupDrawPureColorUniforms();
3307 setupDrawColorFilterUniforms();
3308 setupDrawShaderUniforms();
3309 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3310
3311 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003312}
3313
Romain Guyf607bdc2010-09-10 19:20:06 -07003314// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003315#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3316#define kStdUnderline_Offset (1.0f / 9.0f)
3317#define kStdUnderline_Thickness (1.0f / 18.0f)
3318
Chris Craike63f7c622013-10-17 10:30:55 -07003319void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003320 // Handle underline and strike-through
3321 uint32_t flags = paint->getFlags();
3322 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003323 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003324
Romain Guy211370f2012-02-01 16:10:55 -08003325 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003326 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003327 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003328
Raph Levien8b4072d2012-07-30 15:50:00 -07003329 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003330 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003331
Romain Guyf6834472011-01-23 13:32:12 -08003332 int linesCount = 0;
3333 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3334 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3335
3336 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003337 float points[pointsCount];
3338 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003339
3340 if (flags & SkPaint::kUnderlineText_Flag) {
3341 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003342 points[currentPoint++] = left;
3343 points[currentPoint++] = top;
3344 points[currentPoint++] = left + underlineWidth;
3345 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003346 }
3347
3348 if (flags & SkPaint::kStrikeThruText_Flag) {
3349 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003350 points[currentPoint++] = left;
3351 points[currentPoint++] = top;
3352 points[currentPoint++] = left + underlineWidth;
3353 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003354 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003355
Romain Guy726aeba2011-06-01 14:52:00 -07003356 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003357
Romain Guy726aeba2011-06-01 14:52:00 -07003358 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003359 }
3360 }
3361}
3362
Romain Guy672433d2013-01-04 19:05:13 -08003363status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3364 if (mSnapshot->isIgnored()) {
3365 return DrawGlInfo::kStatusDone;
3366 }
3367
Romain Guy735738c2012-12-03 12:34:51 -08003368 int color = paint->getColor();
3369 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003370 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003371 color |= 0x00ffffff;
3372 }
3373 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3374
3375 return drawColorRects(rects, count, color, mode);
3376}
3377
Chris Craikf57776b2013-10-25 18:30:17 -07003378status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3379 float width, float height) {
3380 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3381
3382 // For now, always and scissor
3383 // TODO: use quickReject
3384 mCaches.enableScissor();
3385
3386 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003387 paint.setColor(mCaches.propertyShadowStrength << 24);
3388 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003389
ztenghui55bfb4e2013-12-03 10:38:55 -08003390 VertexBuffer shadowVertexBuffer;
3391 ShadowTessellator::tessellateAmbientShadow(width, height, casterTransform,
3392 shadowVertexBuffer);
3393 return drawVertexBuffer(shadowVertexBuffer, &paint);
Chris Craikf57776b2013-10-25 18:30:17 -07003394}
3395
Romain Guy735738c2012-12-03 12:34:51 -08003396status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003397 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003398 if (count == 0) {
3399 return DrawGlInfo::kStatusDone;
3400 }
Romain Guy735738c2012-12-03 12:34:51 -08003401
Romain Guy672433d2013-01-04 19:05:13 -08003402 float left = FLT_MAX;
3403 float top = FLT_MAX;
3404 float right = FLT_MIN;
3405 float bottom = FLT_MIN;
3406
Romain Guy448455f2013-07-22 13:57:50 -07003407 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003408 Vertex* vertex = mesh;
3409
Chris Craik2af46352012-11-26 18:30:17 -08003410 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003411 float l = rects[index + 0];
3412 float t = rects[index + 1];
3413 float r = rects[index + 2];
3414 float b = rects[index + 3];
3415
Romain Guy3b753822013-03-05 10:27:35 -08003416 Vertex::set(vertex++, l, t);
3417 Vertex::set(vertex++, r, t);
3418 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003419 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003420
Romain Guy3b753822013-03-05 10:27:35 -08003421 left = fminf(left, l);
3422 top = fminf(top, t);
3423 right = fmaxf(right, r);
3424 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003425 }
3426
Chris Craikf0a59072013-11-19 18:00:46 -08003427 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003428 return DrawGlInfo::kStatusDone;
3429 }
Romain Guy672433d2013-01-04 19:05:13 -08003430
Romain Guy672433d2013-01-04 19:05:13 -08003431 setupDraw();
3432 setupDrawNoTexture();
3433 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3434 setupDrawShader();
3435 setupDrawColorFilter();
3436 setupDrawBlending(mode);
3437 setupDrawProgram();
3438 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003439 setupDrawModelView(kModelViewMode_Translate, false,
3440 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003441 setupDrawColorUniforms();
3442 setupDrawShaderUniforms();
3443 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003444
Romain Guy8ce00302013-01-15 18:51:42 -08003445 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003446 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003447 }
3448
Chris Craik4063a0e2013-11-15 16:06:56 -08003449 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003450
3451 return DrawGlInfo::kStatusDrew;
3452}
3453
Romain Guy026c5e162010-06-28 17:12:22 -07003454void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003455 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003456 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003457 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003458 color |= 0x00ffffff;
3459 }
3460
Romain Guy70ca14e2010-12-13 18:24:33 -08003461 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003462 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003463 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003464 setupDrawShader();
3465 setupDrawColorFilter();
3466 setupDrawBlending(mode);
3467 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003468 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3469 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003470 setupDrawColorUniforms();
3471 setupDrawShaderUniforms(ignoreTransform);
3472 setupDrawColorFilterUniforms();
3473 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003474
Romain Guyc95c8d62010-09-17 15:31:32 -07003475 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3476}
3477
Romain Guy82ba8142010-07-09 13:25:56 -07003478void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003479 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003480 int alpha;
3481 SkXfermode::Mode mode;
3482 getAlphaAndMode(paint, &alpha, &mode);
3483
Romain Guyd21b6e12011-11-30 20:21:23 -08003484 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003485
Romain Guy3b748a42013-04-17 18:54:38 -07003486 GLvoid* vertices = (GLvoid*) NULL;
3487 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3488
3489 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003490 vertices = &mMeshVertices[0].x;
3491 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003492
3493 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3494 texture->uvMapper->map(uvs);
3495
3496 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3497 }
3498
Romain Guy3b753822013-03-05 10:27:35 -08003499 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3500 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3501 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003502
Romain Guyd21b6e12011-11-30 20:21:23 -08003503 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003504 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003505 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3506 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003507 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003508 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003509 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003510 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3511 }
3512
3513 if (texture->uvMapper) {
3514 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003515 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003516}
3517
Romain Guybd6b79b2010-06-26 00:13:53 -07003518void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003519 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3520 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003521 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003522}
3523
3524void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003525 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003526 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003527 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3528 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003529
Romain Guy746b7402010-10-26 16:27:31 -07003530 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003531 setupDrawWithTexture();
3532 setupDrawColor(alpha, alpha, alpha, alpha);
3533 setupDrawColorFilter();
3534 setupDrawBlending(blend, mode, swapSrcDst);
3535 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003536 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003537 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003538 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003539 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003540 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003541 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003542
Romain Guy6820ac82010-09-15 18:11:50 -07003543 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003544}
3545
Romain Guy3b748a42013-04-17 18:54:38 -07003546void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3547 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3548 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003549 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3550 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003551
3552 setupDraw();
3553 setupDrawWithTexture();
3554 setupDrawColor(alpha, alpha, alpha, alpha);
3555 setupDrawColorFilter();
3556 setupDrawBlending(blend, mode, swapSrcDst);
3557 setupDrawProgram();
3558 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003559 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003560 setupDrawTexture(texture);
3561 setupDrawPureColorUniforms();
3562 setupDrawColorFilterUniforms();
3563 setupDrawMeshIndices(vertices, texCoords, vbo);
3564
3565 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003566}
3567
Romain Guy886b2752013-01-04 12:26:18 -08003568void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3569 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3570 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003571 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003572
3573 setupDraw();
3574 setupDrawWithTexture(true);
3575 if (hasColor) {
3576 setupDrawAlpha8Color(color, alpha);
3577 }
3578 setupDrawColorFilter();
3579 setupDrawShader();
3580 setupDrawBlending(true, mode);
3581 setupDrawProgram();
3582 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003583 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003584 setupDrawTexture(texture);
3585 setupDrawPureColorUniforms();
3586 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003587 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003588 setupDrawMesh(vertices, texCoords);
3589
3590 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003591}
3592
Romain Guya5aed0d2010-09-09 14:42:43 -07003593void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003594 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003595 if (mCountOverdraw) {
3596 if (!mCaches.blend) glEnable(GL_BLEND);
3597 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3598 glBlendFunc(GL_ONE, GL_ONE);
3599 }
3600
3601 mCaches.blend = true;
3602 mCaches.lastSrcMode = GL_ONE;
3603 mCaches.lastDstMode = GL_ONE;
3604
3605 return;
3606 }
3607
Romain Guy82ba8142010-07-09 13:25:56 -07003608 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003609
Romain Guy82ba8142010-07-09 13:25:56 -07003610 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003611 // These blend modes are not supported by OpenGL directly and have
3612 // to be implemented using shaders. Since the shader will perform
3613 // the blending, turn blending off here
3614 // If the blend mode cannot be implemented using shaders, fall
3615 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003616 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003617 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003618 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003619 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003620
Romain Guy82bc7a72012-01-03 14:13:39 -08003621 if (mCaches.blend) {
3622 glDisable(GL_BLEND);
3623 mCaches.blend = false;
3624 }
3625
3626 return;
3627 } else {
3628 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003629 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003630 }
3631
3632 if (!mCaches.blend) {
3633 glEnable(GL_BLEND);
3634 }
3635
3636 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3637 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3638
3639 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3640 glBlendFunc(sourceMode, destMode);
3641 mCaches.lastSrcMode = sourceMode;
3642 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003643 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003644 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003645 glDisable(GL_BLEND);
3646 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003647 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003648}
3649
Romain Guy889f8d12010-07-29 14:37:42 -07003650bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003651 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003652 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003653 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003654 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003655 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003656 }
Romain Guy6926c722010-07-12 20:20:03 -07003657 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003658}
3659
Romain Guy026c5e162010-06-28 17:12:22 -07003660void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003661 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003662 TextureVertex::setUV(v++, u1, v1);
3663 TextureVertex::setUV(v++, u2, v1);
3664 TextureVertex::setUV(v++, u1, v2);
3665 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003666}
3667
Chris Craik16ecda52013-03-29 10:59:59 -07003668void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003669 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003670 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3671 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003672 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003673 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003674 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003675}
3676
Chris Craik16ecda52013-03-29 10:59:59 -07003677float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3678 float alpha;
3679 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3680 alpha = mDrawModifiers.mOverrideLayerAlpha;
3681 } else {
3682 alpha = layer->getAlpha() / 255.0f;
3683 }
3684 return alpha * mSnapshot->alpha;
3685}
3686
Romain Guy9d5316e2010-06-24 19:30:36 -07003687}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003688}; // namespace android