blob: a3a443200c4167228e9e79fb5f212e3ab2dc666f [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 Guy96885eb2013-03-26 15:05:58 -0700132 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700133 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700134
135 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700136}
137
Romain Guy85bf02f2010-06-22 13:11:24 -0700138OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700139 // The context has already been destroyed at this point, do not call
140 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700141}
142
Romain Guy87e2f7572012-09-24 11:37:12 -0700143void OpenGLRenderer::initProperties() {
144 char property[PROPERTY_VALUE_MAX];
145 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
146 mScissorOptimizationDisabled = !strcasecmp(property, "true");
147 INIT_LOGD(" Scissor optimization %s",
148 mScissorOptimizationDisabled ? "disabled" : "enabled");
149 } else {
150 INIT_LOGD(" Scissor optimization enabled");
151 }
Romain Guy13631f32012-01-30 17:41:55 -0800152}
153
154///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700155// Setup
156///////////////////////////////////////////////////////////////////////////////
157
Romain Guy85bf02f2010-06-22 13:11:24 -0700158void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700159 initViewport(width, height);
160
161 glDisable(GL_DITHER);
162 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
163
164 glEnableVertexAttribArray(Program::kBindingPosition);
165}
166
167void OpenGLRenderer::initViewport(int width, int height) {
Chris Craikba9b6132013-12-15 17:10:19 -0800168 if (mCaches.propertyEnable3d) {
Chris Craikf57776b2013-10-25 18:30:17 -0700169 // TODO: make view proj app configurable
Chris Craikba9b6132013-12-15 17:10:19 -0800170 float dist = std::max(width, height) * 1.5;
171 dist *= mCaches.propertyCameraDistance;
Chris Craikf57776b2013-10-25 18:30:17 -0700172 Matrix4 projection;
173 projection.loadFrustum(-width / 2, -height / 2, width / 2, height / 2, dist, 0);
174 Matrix4 view;
175 view.loadLookAt(0, 0, dist,
176 0, 0, 0,
177 0, 1, 0);
178 mViewProjMatrix.loadMultiply(projection, view);
179 mViewProjMatrix.translate(-width/2, -height/2);
180 } else {
181 mViewProjMatrix.loadOrtho(0, width, height, 0, -1, 1);
182 }
Romain Guybb9524b2010-06-22 18:56:38 -0700183
Chris Craik14e51302013-12-30 15:32:54 -0800184 initializeViewport(width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700185}
186
Romain Guy96885eb2013-03-26 15:05:58 -0700187void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800188 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800189 mCaches.clearGarbage();
190
Chris Craik14e51302013-12-30 15:32:54 -0800191 initializeSaveStack(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700192 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700193 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700194}
195
196status_t OpenGLRenderer::startFrame() {
197 if (mFrameStarted) return DrawGlInfo::kStatusDone;
198 mFrameStarted = true;
199
Romain Guy41308e22012-10-22 20:02:43 -0700200 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700201
Romain Guy96885eb2013-03-26 15:05:58 -0700202 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700203
Chris Craik14e51302013-12-30 15:32:54 -0800204 glViewport(0, 0, getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800205
Romain Guy54c1a642012-09-27 17:55:46 -0700206 // Functors break the tiling extension in pretty spectacular ways
207 // This ensures we don't use tiling when a functor is going to be
208 // invoked during the frame
209 mSuppressTiling = mCaches.hasRegisteredFunctors();
210
Chris Craik14e51302013-12-30 15:32:54 -0800211 startTiling(*mSnapshot, true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700212
Romain Guy7c450aa2012-09-21 19:15:00 -0700213 debugOverdraw(true, true);
214
Romain Guy96885eb2013-03-26 15:05:58 -0700215 return clear(mTilingClip.left, mTilingClip.top,
216 mTilingClip.right, mTilingClip.bottom, mOpaque);
217}
218
Romain Guy96885eb2013-03-26 15:05:58 -0700219status_t OpenGLRenderer::prepareDirty(float left, float top,
220 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700221
Romain Guy96885eb2013-03-26 15:05:58 -0700222 setupFrameState(left, top, right, bottom, opaque);
223
224 // Layer renderers will start the frame immediately
225 // The framebuffer renderer will first defer the display list
226 // for each layer and wait until the first drawing command
227 // to start the frame
Chris Craik14e51302013-12-30 15:32:54 -0800228 if (currentSnapshot().fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700229 syncState();
230 updateLayers();
231 } else {
232 return startFrame();
233 }
234
235 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700236}
237
Romain Guydcfc8362013-01-03 13:08:57 -0800238void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
239 // If we know that we are going to redraw the entire framebuffer,
240 // perform a discard to let the driver know we don't need to preserve
241 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800242 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800243 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800244 const bool isFbo = getTargetFbo() == 0;
245 const GLenum attachments[] = {
246 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
247 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800248 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
249 }
250}
251
Romain Guy7c25aab2012-10-18 15:05:02 -0700252status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700253 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700254 mCaches.enableScissor();
Chris Craik14e51302013-12-30 15:32:54 -0800255 mCaches.setScissor(left, currentSnapshot().height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700256 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700257 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700258 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700259
Romain Guy7c25aab2012-10-18 15:05:02 -0700260 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700261 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700262}
263
264void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700265 if (mCaches.blend) {
266 glEnable(GL_BLEND);
267 } else {
268 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700269 }
Romain Guybb9524b2010-06-22 18:56:38 -0700270}
271
Chris Craik14e51302013-12-30 15:32:54 -0800272void OpenGLRenderer::startTiling(const Snapshot& s, bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700273 if (!mSuppressTiling) {
Chris Craik14e51302013-12-30 15:32:54 -0800274 const Rect* clip = &mTilingClip;
275 if (s.flags & Snapshot::kFlagFboTarget) {
276 clip = &(s.layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700277 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700278
Chris Craik14e51302013-12-30 15:32:54 -0800279 startTiling(*clip, s.height, opaque);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800280 }
281}
282
283void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
284 if (!mSuppressTiling) {
285 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800286 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700287 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700288}
289
290void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700291 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700292}
293
Romain Guyb025b9c2010-09-16 14:16:48 -0700294void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700295 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700296 endTiling();
297
Romain Guyca89e2a2013-03-08 17:44:20 -0800298 // When finish() is invoked on FBO 0 we've reached the end
299 // of the current frame
300 if (getTargetFbo() == 0) {
301 mCaches.pathCache.trim();
302 }
303
Romain Guy11cb6422012-09-21 00:39:43 -0700304 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700305#if DEBUG_OPENGL
Romain Guy11cb6422012-09-21 00:39:43 -0700306 GLenum status = GL_NO_ERROR;
307 while ((status = glGetError()) != GL_NO_ERROR) {
308 ALOGD("GL error from OpenGLRenderer: 0x%x", status);
309 switch (status) {
310 case GL_INVALID_ENUM:
311 ALOGE(" GL_INVALID_ENUM");
312 break;
313 case GL_INVALID_VALUE:
314 ALOGE(" GL_INVALID_VALUE");
315 break;
316 case GL_INVALID_OPERATION:
317 ALOGE(" GL_INVALID_OPERATION");
318 break;
319 case GL_OUT_OF_MEMORY:
320 ALOGE(" Out of memory!");
321 break;
322 }
Romain Guya07105b2011-01-10 21:14:18 -0800323 }
Romain Guyb025b9c2010-09-16 14:16:48 -0700324#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700325
Romain Guyc15008e2010-11-10 11:59:15 -0800326#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800327 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700328#else
329 if (mCaches.getDebugLevel() & kDebugMemory) {
330 mCaches.dumpMemoryUsage();
331 }
Romain Guyc15008e2010-11-10 11:59:15 -0800332#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700333 }
Romain Guy96885eb2013-03-26 15:05:58 -0700334
Romain Guy78dd96d2013-05-03 14:24:16 -0700335 if (mCountOverdraw) {
336 countOverdraw();
337 }
338
Romain Guy96885eb2013-03-26 15:05:58 -0700339 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700340}
341
Romain Guy6c319ca2011-01-11 14:29:25 -0800342void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700343 if (mCaches.currentProgram) {
344 if (mCaches.currentProgram->isInUse()) {
345 mCaches.currentProgram->remove();
346 mCaches.currentProgram = NULL;
347 }
348 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700349 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700350 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800351 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800352 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800353 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700354 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700355}
356
Romain Guy6c319ca2011-01-11 14:29:25 -0800357void OpenGLRenderer::resume() {
Chris Craik14e51302013-12-30 15:32:54 -0800358 const Snapshot& snapshot = currentSnapshot();
359 glViewport(0, 0, snapshot.viewport.getWidth(), snapshot.viewport.getHeight());
360 glBindFramebuffer(GL_FRAMEBUFFER, snapshot.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700361 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700362
Romain Guy3e263fa2011-12-12 16:47:48 -0800363 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
364
Chet Haase80250612012-08-15 13:46:54 -0700365 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700366 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800367 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700368 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700369
Romain Guya1d3c912011-12-13 14:55:06 -0800370 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700371 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700372
Romain Guy50c0f092010-10-19 11:42:22 -0700373 mCaches.blend = true;
374 glEnable(GL_BLEND);
375 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
376 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700377}
378
Romain Guy35643dd2012-09-18 15:40:58 -0700379void OpenGLRenderer::resumeAfterLayer() {
Chris Craik14e51302013-12-30 15:32:54 -0800380 const Snapshot& snapshot = currentSnapshot();
381 glViewport(0, 0, snapshot.viewport.getWidth(), snapshot.viewport.getHeight());
382 glBindFramebuffer(GL_FRAMEBUFFER, snapshot.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700383 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700384
385 mCaches.resetScissor();
386 dirtyClip();
387}
388
Romain Guyba6be8a2012-04-23 18:22:09 -0700389void OpenGLRenderer::detachFunctor(Functor* functor) {
Chris Craik932b7f62012-06-06 13:59:33 -0700390 mFunctors.remove(functor);
Romain Guyba6be8a2012-04-23 18:22:09 -0700391}
392
393void OpenGLRenderer::attachFunctor(Functor* functor) {
394 mFunctors.add(functor);
395}
396
Romain Guy8f3b8e32012-03-27 16:33:45 -0700397status_t OpenGLRenderer::invokeFunctors(Rect& dirty) {
398 status_t result = DrawGlInfo::kStatusDone;
Romain Guy3d745c02012-04-23 20:36:17 -0700399 size_t count = mFunctors.size();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700400
Romain Guyba6be8a2012-04-23 18:22:09 -0700401 if (count > 0) {
Chris Craikd15321b2012-11-28 14:45:04 -0800402 interrupt();
Romain Guyba6be8a2012-04-23 18:22:09 -0700403 SortedVector<Functor*> functors(mFunctors);
404 mFunctors.clear();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700405
Romain Guyba6be8a2012-04-23 18:22:09 -0700406 DrawGlInfo info;
407 info.clipLeft = 0;
408 info.clipTop = 0;
409 info.clipRight = 0;
410 info.clipBottom = 0;
411 info.isLayer = false;
412 info.width = 0;
413 info.height = 0;
414 memset(info.transform, 0, sizeof(float) * 16);
415
416 for (size_t i = 0; i < count; i++) {
417 Functor* f = functors.itemAt(i);
418 result |= (*f)(DrawGlInfo::kModeProcess, &info);
419
Chris Craikc2c95432012-04-25 15:13:52 -0700420 if (result & DrawGlInfo::kStatusDraw) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700421 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
422 dirty.unionWith(localDirty);
Chris Craikc2c95432012-04-25 15:13:52 -0700423 }
Romain Guyba6be8a2012-04-23 18:22:09 -0700424
Chris Craikc2c95432012-04-25 15:13:52 -0700425 if (result & DrawGlInfo::kStatusInvoke) {
426 mFunctors.add(f);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700427 }
428 }
Chris Craikd15321b2012-11-28 14:45:04 -0800429 resume();
Romain Guy8f3b8e32012-03-27 16:33:45 -0700430 }
431
432 return result;
433}
434
435status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craik14e51302013-12-30 15:32:54 -0800436 if (currentSnapshot().isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craik408eb122013-03-26 18:55:15 -0700437
Chris Craik932b7f62012-06-06 13:59:33 -0700438 detachFunctor(functor);
Chris Craikc8538ade2012-05-22 11:54:06 -0700439
Romain Guyd643bb52011-03-01 14:55:21 -0800440
Chris Craik14e51302013-12-30 15:32:54 -0800441 Rect clip(currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700442 clip.snapToPixelBoundaries();
443
Romain Guyd643bb52011-03-01 14:55:21 -0800444 // Since we don't know what the functor will draw, let's dirty
445 // tne entire clip region
446 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800447 dirtyLayerUnchecked(clip, getRegion());
448 }
Romain Guyd643bb52011-03-01 14:55:21 -0800449
Romain Guy08aa2cb2011-03-17 11:06:57 -0700450 DrawGlInfo info;
451 info.clipLeft = clip.left;
452 info.clipTop = clip.top;
453 info.clipRight = clip.right;
454 info.clipBottom = clip.bottom;
455 info.isLayer = hasLayer();
Chet Haase7b6a7582012-04-11 14:32:02 -0700456 info.width = getSnapshot()->viewport.getWidth();
457 info.height = getSnapshot()->height;
Romain Guy08aa2cb2011-03-17 11:06:57 -0700458 getSnapshot()->transform->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700459
John Reck25d2f7b2013-09-10 13:12:09 -0700460 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700461 // setup GL state for functor
462 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700463 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
464 }
John Reck25d2f7b2013-09-10 13:12:09 -0700465 if (mCaches.enableScissor() || dirtyClip) {
466 setScissorFromClip();
467 }
Chris Craik54f574a2013-08-26 11:23:46 -0700468 interrupt();
469
470 // call functor immediately after GL state setup
Chris Craik4a2bff72013-04-16 13:50:16 -0700471 status_t result = (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800472
Romain Guy8f3b8e32012-03-27 16:33:45 -0700473 if (result != DrawGlInfo::kStatusDone) {
Romain Guy08aa2cb2011-03-17 11:06:57 -0700474 Rect localDirty(info.dirtyLeft, info.dirtyTop, info.dirtyRight, info.dirtyBottom);
Romain Guycabfcc12011-03-07 18:06:46 -0800475 dirty.unionWith(localDirty);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700476
Chris Craik65924a32012-04-05 17:52:11 -0700477 if (result & DrawGlInfo::kStatusInvoke) {
Romain Guyba6be8a2012-04-23 18:22:09 -0700478 mFunctors.add(functor);
Romain Guy8f3b8e32012-03-27 16:33:45 -0700479 }
Romain Guycabfcc12011-03-07 18:06:46 -0800480 }
481
Chet Haasedaf98e92011-01-10 14:10:36 -0800482 resume();
Chris Craik4a2bff72013-04-16 13:50:16 -0700483 return result | DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800484}
485
Romain Guyf6a11b82010-06-23 17:47:49 -0700486///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700487// Debug
488///////////////////////////////////////////////////////////////////////////////
489
Romain Guy0f667532013-03-01 14:31:04 -0800490void OpenGLRenderer::eventMark(const char* name) const {
491 mCaches.eventMark(0, name);
492}
493
Romain Guy87e2f7572012-09-24 11:37:12 -0700494void OpenGLRenderer::startMark(const char* name) const {
495 mCaches.startMark(0, name);
496}
497
498void OpenGLRenderer::endMark() const {
499 mCaches.endMark();
500}
501
502void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
503 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
504 if (clear) {
505 mCaches.disableScissor();
506 mCaches.stencil.clear();
507 }
508 if (enable) {
509 mCaches.stencil.enableDebugWrite();
510 } else {
511 mCaches.stencil.disable();
512 }
513 }
514}
515
516void OpenGLRenderer::renderOverdraw() {
517 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700518 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700519
520 mCaches.enableScissor();
Chris Craik5f803622013-03-21 14:39:04 -0700521 mCaches.setScissor(clip->left, mFirstSnapshot->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700522 clip->right - clip->left, clip->bottom - clip->top);
523
Romain Guy627c6fd2013-08-21 11:53:18 -0700524 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700525 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700526 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
527
528 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700529 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700530 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
531
532 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700533 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700534 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
535
536 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700537 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700538 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
539
Romain Guy87e2f7572012-09-24 11:37:12 -0700540 mCaches.stencil.disable();
541 }
542}
543
Romain Guy78dd96d2013-05-03 14:24:16 -0700544void OpenGLRenderer::countOverdraw() {
Chris Craik14e51302013-12-30 15:32:54 -0800545 size_t count = getWidth() * getHeight();
Romain Guy78dd96d2013-05-03 14:24:16 -0700546 uint32_t* buffer = new uint32_t[count];
Chris Craik14e51302013-12-30 15:32:54 -0800547 glReadPixels(0, 0, getWidth(), getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
Romain Guy78dd96d2013-05-03 14:24:16 -0700548
549 size_t total = 0;
550 for (size_t i = 0; i < count; i++) {
551 total += buffer[i] & 0xff;
552 }
553
554 mOverdraw = total / float(count);
555
556 delete[] buffer;
557}
558
Romain Guy87e2f7572012-09-24 11:37:12 -0700559///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700560// Layers
561///////////////////////////////////////////////////////////////////////////////
562
563bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700564 if (layer->deferredUpdateScheduled && layer->renderer &&
565 layer->displayList && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700566 ATRACE_CALL();
567
Romain Guy11cb6422012-09-21 00:39:43 -0700568 Rect& dirty = layer->dirtyRect;
569
Romain Guy7c450aa2012-09-21 19:15:00 -0700570 if (inFrame) {
571 endTiling();
572 debugOverdraw(false, false);
573 }
Romain Guy11cb6422012-09-21 00:39:43 -0700574
Romain Guy96885eb2013-03-26 15:05:58 -0700575 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700576 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700577 } else {
578 layer->defer();
579 }
Romain Guy11cb6422012-09-21 00:39:43 -0700580
581 if (inFrame) {
582 resumeAfterLayer();
Chris Craik14e51302013-12-30 15:32:54 -0800583 startTiling(*mSnapshot);
Romain Guy11cb6422012-09-21 00:39:43 -0700584 }
585
Romain Guy5bb3c732012-11-29 17:52:58 -0800586 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700587 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700588
589 return true;
590 }
591
592 return false;
593}
594
595void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700596 // If draw deferring is enabled this method will simply defer
597 // the display list of each individual layer. The layers remain
598 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700599 int count = mLayerUpdates.size();
600 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700601 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
602 startMark("Layer Updates");
603 } else {
604 startMark("Defer Layer Updates");
605 }
Romain Guy11cb6422012-09-21 00:39:43 -0700606
Chris Craik1206b9b2013-04-04 14:46:24 -0700607 // Note: it is very important to update the layers in order
608 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700609 Layer* layer = mLayerUpdates.itemAt(i);
610 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700611 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
612 mCaches.resourceCache.decrementRefcount(layer);
613 }
Romain Guy11cb6422012-09-21 00:39:43 -0700614 }
Romain Guy11cb6422012-09-21 00:39:43 -0700615
Romain Guy96885eb2013-03-26 15:05:58 -0700616 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
617 mLayerUpdates.clear();
618 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
619 }
620 endMark();
621 }
622}
623
624void OpenGLRenderer::flushLayers() {
625 int count = mLayerUpdates.size();
626 if (count > 0) {
627 startMark("Apply Layer Updates");
628 char layerName[12];
629
Chris Craik1206b9b2013-04-04 14:46:24 -0700630 // Note: it is very important to update the layers in order
631 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700632 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700633 startMark(layerName);
634
Romain Guy40543602013-06-12 15:31:28 -0700635 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700636 Layer* layer = mLayerUpdates.itemAt(i);
637 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700638 ATRACE_END();
639
Romain Guy02b49b72013-03-29 12:37:16 -0700640 mCaches.resourceCache.decrementRefcount(layer);
641
Romain Guy96885eb2013-03-26 15:05:58 -0700642 endMark();
643 }
644
645 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700646 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700647
Romain Guy11cb6422012-09-21 00:39:43 -0700648 endMark();
649 }
650}
651
652void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
653 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700654 // Make sure we don't introduce duplicates.
655 // SortedVector would do this automatically but we need to respect
656 // the insertion order. The linear search is not an issue since
657 // this list is usually very short (typically one item, at most a few)
658 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
659 if (mLayerUpdates.itemAt(i) == layer) {
660 return;
661 }
662 }
Romain Guy11cb6422012-09-21 00:39:43 -0700663 mLayerUpdates.push_back(layer);
664 mCaches.resourceCache.incrementRefcount(layer);
665 }
666}
667
Romain Guye93482f2013-06-17 13:14:51 -0700668void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
669 if (layer) {
670 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
671 if (mLayerUpdates.itemAt(i) == layer) {
672 mLayerUpdates.removeAt(i);
673 mCaches.resourceCache.decrementRefcount(layer);
674 break;
675 }
676 }
677 }
678}
679
Romain Guy11cb6422012-09-21 00:39:43 -0700680void OpenGLRenderer::clearLayerUpdates() {
681 size_t count = mLayerUpdates.size();
682 if (count > 0) {
683 mCaches.resourceCache.lock();
684 for (size_t i = 0; i < count; i++) {
685 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
686 }
687 mCaches.resourceCache.unlock();
688 mLayerUpdates.clear();
689 }
690}
691
Romain Guy40543602013-06-12 15:31:28 -0700692void OpenGLRenderer::flushLayerUpdates() {
693 syncState();
694 updateLayers();
695 flushLayers();
696 // Wait for all the layer updates to be executed
697 AutoFence fence;
698}
699
Romain Guy11cb6422012-09-21 00:39:43 -0700700///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700701// State management
702///////////////////////////////////////////////////////////////////////////////
703
Chris Craik14e51302013-12-30 15:32:54 -0800704void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
705 bool restoreOrtho = removed.flags & Snapshot::kFlagDirtyOrtho;
706 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
707 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700708
Romain Guyeb993562010-10-05 18:14:38 -0700709 if (restoreOrtho) {
Chris Craik14e51302013-12-30 15:32:54 -0800710 const Rect& r = restored.viewport;
Romain Guyeb993562010-10-05 18:14:38 -0700711 glViewport(r.left, r.top, r.right, r.bottom);
Chris Craik14e51302013-12-30 15:32:54 -0800712 mViewProjMatrix.load(removed.orthoMatrix); // todo: should ortho be stored in 'restored'?
Romain Guyeb993562010-10-05 18:14:38 -0700713 }
714
Romain Guy2542d192010-08-18 11:47:12 -0700715 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700716 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700717 }
Romain Guy2542d192010-08-18 11:47:12 -0700718
Romain Guy5ec99242010-11-03 16:19:08 -0700719 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700720 endMark(); // Savelayer
721 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800722 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700723 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700724 }
Romain Guybb9524b2010-06-22 18:56:38 -0700725}
726
Romain Guyf6a11b82010-06-23 17:47:49 -0700727///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700728// Layers
729///////////////////////////////////////////////////////////////////////////////
730
731int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craikff785832013-03-08 13:12:16 -0800732 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700733 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700734
Chris Craik14e51302013-12-30 15:32:54 -0800735 if (!currentSnapshot().isIgnored()) {
Chris Craike63f7c622013-10-17 10:30:55 -0700736 createLayer(left, top, right, bottom, alpha, mode, flags);
Romain Guydbc26d22010-10-11 17:58:29 -0700737 }
Romain Guyd55a8612010-06-28 17:42:46 -0700738
739 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700740}
741
Chris Craikd90144d2013-03-19 15:03:48 -0700742void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
743 const Rect untransformedBounds(bounds);
744
745 currentTransform().mapRect(bounds);
746
747 // Layers only make sense if they are in the framebuffer's bounds
Chris Craik14e51302013-12-30 15:32:54 -0800748 if (bounds.intersect(currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700749 // We cannot work with sub-pixels in this case
750 bounds.snapToPixelBoundaries();
751
752 // When the layer is not an FBO, we may use glCopyTexImage so we
753 // need to make sure the layer does not extend outside the bounds
754 // of the framebuffer
Chris Craik14e51302013-12-30 15:32:54 -0800755 if (!bounds.intersect(currentSnapshot().previous->viewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700756 bounds.setEmpty();
757 } else if (fboLayer) {
758 clip.set(bounds);
759 mat4 inverse;
760 inverse.loadInverse(currentTransform());
761 inverse.mapRect(clip);
762 clip.snapToPixelBoundaries();
763 if (clip.intersect(untransformedBounds)) {
764 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
765 bounds.set(untransformedBounds);
766 } else {
767 clip.setEmpty();
768 }
769 }
770 } else {
771 bounds.setEmpty();
772 }
773}
774
Chris Craik408eb122013-03-26 18:55:15 -0700775void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
776 bool fboLayer, int alpha) {
777 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
778 bounds.getHeight() > mCaches.maxTextureSize ||
779 (fboLayer && clip.isEmpty())) {
780 mSnapshot->empty = fboLayer;
781 } else {
782 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
783 }
784}
785
Chris Craikd90144d2013-03-19 15:03:48 -0700786int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
787 int alpha, SkXfermode::Mode mode, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700788 const int count = saveSnapshot(flags);
789
Chris Craik14e51302013-12-30 15:32:54 -0800790 if (!currentSnapshot().isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700791 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
792 // operations will be able to store and restore the current clip and transform info, and
793 // quick rejection will be correct (for display lists)
794
795 Rect bounds(left, top, right, bottom);
796 Rect clip;
797 calculateLayerBoundsAndClip(bounds, clip, true);
Chris Craik408eb122013-03-26 18:55:15 -0700798 updateSnapshotIgnoreForLayer(bounds, clip, true, alpha);
Chris Craikd90144d2013-03-19 15:03:48 -0700799
Chris Craik14e51302013-12-30 15:32:54 -0800800 if (!currentSnapshot().isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700801 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
802 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700803 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700804 }
805 }
806
807 return count;
808}
809
810
Romain Guy1c740bc2010-09-13 18:00:09 -0700811/**
812 * Layers are viewed by Skia are slightly different than layers in image editing
813 * programs (for instance.) When a layer is created, previously created layers
814 * and the frame buffer still receive every drawing command. For instance, if a
815 * layer is created and a shape intersecting the bounds of the layers and the
816 * framebuffer is draw, the shape will be drawn on both (unless the layer was
817 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
818 *
819 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
820 * texture. Unfortunately, this is inefficient as it requires every primitive to
821 * be drawn n + 1 times, where n is the number of active layers. In practice this
822 * means, for every primitive:
823 * - Switch active frame buffer
824 * - Change viewport, clip and projection matrix
825 * - Issue the drawing
826 *
827 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700828 * To avoid this, layers are implemented in a different way here, at least in the
829 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
830 * is set. When this flag is set we can redirect all drawing operations into a
831 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700832 *
833 * This implementation relies on the frame buffer being at least RGBA 8888. When
834 * a layer is created, only a texture is created, not an FBO. The content of the
835 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700836 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700837 * buffer and drawing continues as normal. This technique therefore treats the
838 * frame buffer as a scratch buffer for the layers.
839 *
840 * To compose the layers back onto the frame buffer, each layer texture
841 * (containing the original frame buffer data) is drawn as a simple quad over
842 * the frame buffer. The trick is that the quad is set as the composition
843 * destination in the blending equation, and the frame buffer becomes the source
844 * of the composition.
845 *
846 * Drawing layers with an alpha value requires an extra step before composition.
847 * An empty quad is drawn over the layer's region in the frame buffer. This quad
848 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
849 * quad is used to multiply the colors in the frame buffer. This is achieved by
850 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
851 * GL_ZERO, GL_SRC_ALPHA.
852 *
853 * Because glCopyTexImage2D() can be slow, an alternative implementation might
854 * be use to draw a single clipped layer. The implementation described above
855 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700856 *
857 * (1) The frame buffer is actually not cleared right away. To allow the GPU
858 * to potentially optimize series of calls to glCopyTexImage2D, the frame
859 * buffer is left untouched until the first drawing operation. Only when
860 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700861 */
Chet Haased48885a2012-08-28 17:43:28 -0700862bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craike63f7c622013-10-17 10:30:55 -0700863 int alpha, SkXfermode::Mode mode, int flags) {
Romain Guyeb993562010-10-05 18:14:38 -0700864 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700865 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700866
Romain Guyeb993562010-10-05 18:14:38 -0700867 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
868
Romain Guyf607bdc2010-09-10 19:20:06 -0700869 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700870 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700871 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700872 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Chris Craik408eb122013-03-26 18:55:15 -0700873 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, alpha);
Romain Guydbc26d22010-10-11 17:58:29 -0700874
875 // Bail out if we won't draw in this snapshot
Chris Craik14e51302013-12-30 15:32:54 -0800876 if (currentSnapshot().isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700877 return false;
878 }
Romain Guyf18fd992010-07-08 11:45:51 -0700879
Romain Guya1d3c912011-12-13 14:55:06 -0800880 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700881 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda57022010-07-06 11:39:32 -0700882 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700883 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700884 }
885
Romain Guy9ace8f52011-07-07 20:50:11 -0700886 layer->setAlpha(alpha, mode);
Romain Guy8aef54f2010-09-01 15:13:49 -0700887 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700888 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
889 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Chris Craikc3566d02013-02-04 16:16:33 -0800890 layer->setColorFilter(mDrawModifiers.mColorFilter);
Chet Haasea23eed82012-04-12 15:19:04 -0700891 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700892 layer->setDirty(false);
Romain Guydda57022010-07-06 11:39:32 -0700893
Romain Guy8fb95422010-08-17 18:38:51 -0700894 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700895 mSnapshot->flags |= Snapshot::kFlagIsLayer;
896 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700897
Chris Craik7273daa2013-03-28 11:25:24 -0700898 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700899 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700900 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700901 } else {
902 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700903 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800904 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700905 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700906 // Workaround for some GL drivers. When reading pixels lying outside
907 // of the window we should get undefined values for those pixels.
908 // Unfortunately some drivers will turn the entire target texture black
909 // when reading outside of the window.
910 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
911 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700912 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800913 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700914
Romain Guyb254c242013-06-27 17:15:24 -0700915 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
916 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
917
Romain Guy54be1cd2011-06-13 19:04:27 -0700918 // Enqueue the buffer coordinates to clear the corresponding region later
919 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700920 }
Romain Guyeb993562010-10-05 18:14:38 -0700921 }
Romain Guyf86ef572010-07-01 11:05:42 -0700922
Romain Guyd55a8612010-06-28 17:42:46 -0700923 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700924}
925
Chris Craike63f7c622013-10-17 10:30:55 -0700926bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800927 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700928 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700929
Chet Haased48885a2012-08-28 17:43:28 -0700930 mSnapshot->region = &mSnapshot->layer->region;
Romain Guyc3fedaf2013-01-29 17:26:25 -0800931 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer |
932 Snapshot::kFlagDirtyOrtho;
Chet Haased48885a2012-08-28 17:43:28 -0700933 mSnapshot->fbo = layer->getFbo();
934 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
935 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
936 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
937 mSnapshot->height = bounds.getHeight();
Chris Craikf57776b2013-10-25 18:30:17 -0700938 mSnapshot->orthoMatrix.load(mViewProjMatrix);
Romain Guy5b3b3522010-10-27 18:57:51 -0700939
Romain Guy2b7028e2012-09-19 17:25:38 -0700940 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700941 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700942 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700943 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
944 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700945
946 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700947 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700948 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700949 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700950 }
951
952 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700953 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700954
Chris Craik14e51302013-12-30 15:32:54 -0800955 startTiling(*mSnapshot, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700956
957 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700958 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800959 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700960 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700961 glClear(GL_COLOR_BUFFER_BIT);
962
963 dirtyClip();
964
965 // Change the ortho projection
966 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craikf57776b2013-10-25 18:30:17 -0700967
968 // TODO: determine best way to support 3d drawing within HW layers
969 mViewProjMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700970
971 return true;
972}
973
Romain Guy1c740bc2010-09-13 18:00:09 -0700974/**
975 * Read the documentation of createLayer() before doing anything in this method.
976 */
Chris Craik14e51302013-12-30 15:32:54 -0800977void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
978 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000979 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700980 return;
981 }
982
Chris Craik14e51302013-12-30 15:32:54 -0800983 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800984 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800985 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700986
Chris Craik39a908c2013-06-13 14:39:01 -0700987 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800988 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
989 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700990 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
991
Romain Guyeb993562010-10-05 18:14:38 -0700992 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700993 endTiling();
994
Romain Guye0aa84b2012-04-03 19:30:26 -0700995 // Detach the texture from the FBO
996 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800997
998 layer->removeFbo(false);
999
Romain Guyeb993562010-10-05 18:14:38 -07001000 // Unbind current FBO and restore previous one
Chris Craik14e51302013-12-30 15:32:54 -08001001 glBindFramebuffer(GL_FRAMEBUFFER, restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -07001002 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -07001003
Chris Craik14e51302013-12-30 15:32:54 -08001004 startTiling(restored);
Romain Guyeb993562010-10-05 18:14:38 -07001005 }
1006
Romain Guy9ace8f52011-07-07 20:50:11 -07001007 if (!fboLayer && layer->getAlpha() < 255) {
Romain Guyf607bdc2010-09-10 19:20:06 -07001008 drawColorRect(rect.left, rect.top, rect.right, rect.bottom,
Romain Guy9ace8f52011-07-07 20:50:11 -07001009 layer->getAlpha() << 24, SkXfermode::kDstIn_Mode, true);
Romain Guy5b3b3522010-10-27 18:57:51 -07001010 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -07001011 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -07001012 }
1013
Romain Guy03750a02010-10-18 14:06:08 -07001014 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -07001015
Romain Guya1d3c912011-12-13 14:55:06 -08001016 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -07001017
Romain Guy5b3b3522010-10-27 18:57:51 -07001018 // When the layer is stored in an FBO, we can save a bit of fillrate by
1019 // drawing only the dirty region
1020 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -08001021 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy9ace8f52011-07-07 20:50:11 -07001022 if (layer->getColorFilter()) {
1023 setupColorFilter(layer->getColorFilter());
Romain Guy171c5922011-01-06 10:04:23 -08001024 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001025 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 if (layer->getColorFilter()) {
Romain Guy171c5922011-01-06 10:04:23 -08001027 resetColorFilter();
1028 }
Romain Guy9ace8f52011-07-07 20:50:11 -07001029 } else if (!rect.isEmpty()) {
1030 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301031
1032 save(0);
1033 // the layer contains screen buffer content that shouldn't be alpha modulated
1034 // (and any necessary alpha modulation was handled drawing into the layer)
1035 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001036 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +05301037 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -07001038 }
Romain Guy8b55f372010-08-18 17:10:07 -07001039
Romain Guy746b7402010-10-26 16:27:31 -07001040 dirtyClip();
1041
Romain Guyeb993562010-10-05 18:14:38 -07001042 // Failing to add the layer to the cache should happen only if the layer is too large
Romain Guy8550c4c2010-10-08 15:49:53 -07001043 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -07001044 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -07001045 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -07001046 }
1047}
1048
Romain Guyaa6c24c2011-04-28 18:40:04 -07001049void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -07001050 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001051
1052 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -07001053 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -07001054 setupDrawWithTexture();
1055 } else {
1056 setupDrawWithExternalTexture();
1057 }
1058 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001059 setupDrawColor(alpha, alpha, alpha, alpha);
1060 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001061 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode());
Romain Guyaa6c24c2011-04-28 18:40:04 -07001062 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001063 setupDrawPureColorUniforms();
1064 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001065 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
1066 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001067 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -07001068 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -07001069 }
Romain Guy3b753822013-03-05 10:27:35 -08001070 if (currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001071 layer->getWidth() == (uint32_t) rect.getWidth() &&
1072 layer->getHeight() == (uint32_t) rect.getHeight()) {
Romain Guy3b753822013-03-05 10:27:35 -08001073 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1074 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001075
Romain Guyd21b6e12011-11-30 20:21:23 -08001076 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001077 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1078 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001079 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001080 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001081 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
1082 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001083 }
1084 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -07001085 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001086
1087 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001088}
1089
Romain Guy5b3b3522010-10-27 18:57:51 -07001090void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001091 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001092 const Rect& texCoords = layer->texCoords;
1093 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1094 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001095
Romain Guy9ace8f52011-07-07 20:50:11 -07001096 float x = rect.left;
1097 float y = rect.top;
Romain Guy3b753822013-03-05 10:27:35 -08001098 bool simpleTransform = currentTransform().isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001099 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001100 layer->getHeight() == (uint32_t) rect.getHeight();
1101
1102 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001103 // When we're swapping, the layer is already in screen coordinates
1104 if (!swap) {
Romain Guy3b753822013-03-05 10:27:35 -08001105 x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1106 y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001107 }
1108
Romain Guyd21b6e12011-11-30 20:21:23 -08001109 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001110 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001111 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001112 }
1113
Chris Craik16ecda52013-03-29 10:59:59 -07001114 float alpha = getLayerAlpha(layer);
Chris Craike83569c2013-03-20 16:57:09 -07001115 bool blend = layer->isBlend() || alpha < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001116 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Chris Craike83569c2013-03-20 16:57:09 -07001117 layer->getTexture(), alpha, layer->getMode(), blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001118 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001119 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001120
Romain Guyaa6c24c2011-04-28 18:40:04 -07001121 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1122 } else {
1123 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1124 drawTextureLayer(layer, rect);
1125 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1126 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001127}
1128
Chris Craik34416ea2013-04-15 16:08:28 -07001129/**
1130 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1131 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1132 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1133 * by saveLayer's restore
1134 */
1135#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1136 DRAW_COMMAND; \
1137 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1138 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1139 DRAW_COMMAND; \
1140 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1141 } \
1142 }
1143
1144#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1145
Romain Guy5b3b3522010-10-27 18:57:51 -07001146void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001147 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001148 layer->setRegionAsRect();
1149
Chris Craik34416ea2013-04-15 16:08:28 -07001150 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001151
Romain Guy5b3b3522010-10-27 18:57:51 -07001152 layer->region.clear();
1153 return;
1154 }
1155
Romain Guy211370f2012-02-01 16:10:55 -08001156 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001157 size_t count;
Chris Craik6c5b9be2013-02-27 14:03:19 -08001158 const android::Rect* rects;
1159 Region safeRegion;
1160 if (CC_LIKELY(hasRectToRectTransform())) {
1161 rects = layer->region.getArray(&count);
1162 } else {
1163 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1164 rects = safeRegion.getArray(&count);
1165 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001166
Chris Craik16ecda52013-03-29 10:59:59 -07001167 const float alpha = getLayerAlpha(layer);
Romain Guy9ace8f52011-07-07 20:50:11 -07001168 const float texX = 1.0f / float(layer->getWidth());
1169 const float texY = 1.0f / float(layer->getHeight());
Romain Guyf219da52011-01-16 12:54:25 -08001170 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001171
Romain Guy8ce00302013-01-15 18:51:42 -08001172 setupDraw();
1173
1174 // We must get (and therefore bind) the region mesh buffer
1175 // after we setup drawing in case we need to mess with the
1176 // stencil buffer in setupDraw()
Romain Guy5b3b3522010-10-27 18:57:51 -07001177 TextureVertex* mesh = mCaches.getRegionMesh();
Romain Guy03c00b52013-06-20 18:30:28 -07001178 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001179
Romain Guy7230a742011-01-10 22:26:16 -08001180 setupDrawWithTexture();
1181 setupDrawColor(alpha, alpha, alpha, alpha);
Romain Guyada830f2011-01-13 12:13:20 -08001182 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07001183 setupDrawBlending(layer->isBlend() || alpha < 1.0f, layer->getMode(), false);
Romain Guy7230a742011-01-10 22:26:16 -08001184 setupDrawProgram();
1185 setupDrawDirtyRegionsDisabled();
1186 setupDrawPureColorUniforms();
Romain Guyada830f2011-01-13 12:13:20 -08001187 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07001188 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08001189 if (currentTransform().isPureTranslate()) {
1190 const float x = (int) floorf(rect.left + currentTransform().getTranslateX() + 0.5f);
1191 const float y = (int) floorf(rect.top + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001192
Romain Guyd21b6e12011-11-30 20:21:23 -08001193 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08001194 setupDrawModelView(kModelViewMode_Translate, false,
1195 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001196 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001197 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08001198 setupDrawModelView(kModelViewMode_Translate, false,
1199 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -07001200 }
Romain Guy3380cfd2013-08-15 16:57:57 -07001201 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001202
1203 for (size_t i = 0; i < count; i++) {
1204 const android::Rect* r = &rects[i];
1205
1206 const float u1 = r->left * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001207 const float v1 = (height - r->top) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001208 const float u2 = r->right * texX;
Romain Guyf219da52011-01-16 12:54:25 -08001209 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001210
1211 // TODO: Reject quads outside of the clip
1212 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1213 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1214 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1215 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
1216
1217 numQuads++;
1218
Romain Guy31e08e92013-06-18 15:53:53 -07001219 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001220 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1221 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001222 numQuads = 0;
1223 mesh = mCaches.getRegionMesh();
1224 }
1225 }
1226
1227 if (numQuads > 0) {
Chris Craik34416ea2013-04-15 16:08:28 -07001228 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1229 GL_UNSIGNED_SHORT, NULL));
Romain Guy5b3b3522010-10-27 18:57:51 -07001230 }
1231
Romain Guy5b3b3522010-10-27 18:57:51 -07001232#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001233 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001234#endif
1235
1236 layer->region.clear();
1237 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001238}
1239
Romain Guy3a3133d2011-02-01 22:59:58 -08001240#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001241void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001242 size_t count;
1243 const android::Rect* rects = region.getArray(&count);
1244
1245 uint32_t colors[] = {
1246 0x7fff0000, 0x7f00ff00,
1247 0x7f0000ff, 0x7fff00ff,
1248 };
1249
1250 int offset = 0;
1251 int32_t top = rects[0].top;
1252
1253 for (size_t i = 0; i < count; i++) {
1254 if (top != rects[i].top) {
1255 offset ^= 0x2;
1256 top = rects[i].top;
1257 }
1258
1259 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
1260 drawColorRect(r.left, r.top, r.right, r.bottom, colors[offset + (i & 0x1)],
1261 SkXfermode::kSrcOver_Mode);
1262 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001263}
Chris Craike63f7c622013-10-17 10:30:55 -07001264#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001265
Romain Guy8ce00302013-01-15 18:51:42 -08001266void OpenGLRenderer::drawRegionRects(const SkRegion& region, int color,
1267 SkXfermode::Mode mode, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001268 Vector<float> rects;
1269
1270 SkRegion::Iterator it(region);
1271 while (!it.done()) {
1272 const SkIRect& r = it.rect();
1273 rects.push(r.fLeft);
1274 rects.push(r.fTop);
1275 rects.push(r.fRight);
1276 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001277 it.next();
1278 }
1279
Romain Guy448455f2013-07-22 13:57:50 -07001280 drawColorRects(rects.array(), rects.size(), color, mode, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001281}
1282
Romain Guy5b3b3522010-10-27 18:57:51 -07001283void OpenGLRenderer::dirtyLayer(const float left, const float top,
1284 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001285 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001286 Rect bounds(left, top, right, bottom);
1287 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001288 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001289 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001290}
1291
1292void OpenGLRenderer::dirtyLayer(const float left, const float top,
1293 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001294 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001295 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001296 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001297 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001298}
1299
1300void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craik14e51302013-12-30 15:32:54 -08001301 if (bounds.intersect(currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001302 bounds.snapToPixelBoundaries();
1303 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1304 if (!dirty.isEmpty()) {
1305 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001306 }
1307 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001308}
1309
Chris Craik4063a0e2013-11-15 16:06:56 -08001310void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001311 GLsizei elementsCount = quadsCount * 6;
1312 while (elementsCount > 0) {
1313 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1314
Romain Guy3380cfd2013-08-15 16:57:57 -07001315 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001316 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1317
1318 elementsCount -= drawCount;
1319 // Though there are 4 vertices in a quad, we use 6 indices per
1320 // quad to draw with GL_TRIANGLES
1321 mesh += (drawCount / 6) * 4;
1322 }
1323}
1324
Romain Guy54be1cd2011-06-13 19:04:27 -07001325void OpenGLRenderer::clearLayerRegions() {
1326 const size_t count = mLayers.size();
1327 if (count == 0) return;
1328
Chris Craik14e51302013-12-30 15:32:54 -08001329 if (!currentSnapshot().isIgnored()) {
Romain Guy54be1cd2011-06-13 19:04:27 -07001330 // Doing several glScissor/glClear here can negatively impact
1331 // GPUs with a tiler architecture, instead we draw quads with
1332 // the Clear blending mode
1333
1334 // The list contains bounds that have already been clipped
1335 // against their initial clip rect, and the current clip
1336 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001337 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001338
Romain Guy448455f2013-07-22 13:57:50 -07001339 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001340 Vertex* vertex = mesh;
1341
1342 for (uint32_t i = 0; i < count; i++) {
1343 Rect* bounds = mLayers.itemAt(i);
1344
Romain Guy54be1cd2011-06-13 19:04:27 -07001345 Vertex::set(vertex++, bounds->left, bounds->top);
1346 Vertex::set(vertex++, bounds->right, bounds->top);
1347 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001348 Vertex::set(vertex++, bounds->right, bounds->bottom);
1349
1350 delete bounds;
1351 }
Romain Guye67307c2013-02-11 18:01:20 -08001352 // We must clear the list of dirty rects before we
1353 // call setupDraw() to prevent stencil setup to do
1354 // the same thing again
1355 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001356
1357 setupDraw(false);
1358 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
1359 setupDrawBlending(true, SkXfermode::kClear_Mode);
1360 setupDrawProgram();
1361 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001362 setupDrawModelView(kModelViewMode_Translate, false,
1363 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001364
Chris Craik4063a0e2013-11-15 16:06:56 -08001365 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001366
1367 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001368 } else {
1369 for (uint32_t i = 0; i < count; i++) {
1370 delete mLayers.itemAt(i);
1371 }
Romain Guye67307c2013-02-11 18:01:20 -08001372 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001373 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001374}
1375
Romain Guybd6b79b2010-06-26 00:13:53 -07001376///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001377// State Deferral
1378///////////////////////////////////////////////////////////////////////////////
1379
Chris Craikff785832013-03-08 13:12:16 -08001380bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craik14e51302013-12-30 15:32:54 -08001381 const Rect& currentClip = currentClipRect();
1382 const mat4& currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001383
Chris Craikff785832013-03-08 13:12:16 -08001384 if (stateDeferFlags & kStateDeferFlag_Draw) {
1385 // state has bounds initialized in local coordinates
1386 if (!state.mBounds.isEmpty()) {
1387 currentMatrix.mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001388 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001389 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1390 // is used, it should more closely duplicate the quickReject logic (in how it uses
1391 // snapToPixelBoundaries)
1392
Chris Craik28ce94a2013-05-31 11:38:03 -07001393 if(!clippedBounds.intersect(currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001394 // quick rejected
1395 return true;
1396 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001397
Chris Craika02c4ed2013-06-14 13:43:58 -07001398 state.mClipSideFlags = kClipSide_None;
Chris Craik28ce94a2013-05-31 11:38:03 -07001399 if (!currentClip.contains(state.mBounds)) {
1400 int& flags = state.mClipSideFlags;
1401 // op partially clipped, so record which sides are clipped for clip-aware merging
1402 if (currentClip.left > state.mBounds.left) flags |= kClipSide_Left;
1403 if (currentClip.top > state.mBounds.top) flags |= kClipSide_Top;
1404 if (currentClip.right < state.mBounds.right) flags |= kClipSide_Right;
1405 if (currentClip.bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
1406 }
1407 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001408 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001409 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1410 // overdraw avoidance (since we don't know what it overlaps)
1411 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikff785832013-03-08 13:12:16 -08001412 state.mBounds.set(currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001413 }
1414 }
1415
Chris Craik527a3aa2013-03-04 10:19:31 -08001416 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1417 if (state.mClipValid) {
Chris Craikff785832013-03-08 13:12:16 -08001418 state.mClip.set(currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001419 }
1420
Chris Craik7273daa2013-03-28 11:25:24 -07001421 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1422 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikc3566d02013-02-04 16:16:33 -08001423 state.mMatrix.load(currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001424 state.mDrawModifiers = mDrawModifiers;
Chris Craik14e51302013-12-30 15:32:54 -08001425 state.mAlpha = currentSnapshot().alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001426 return false;
1427}
1428
Chris Craik527a3aa2013-03-04 10:19:31 -08001429void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001430 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001431 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001432 mDrawModifiers = state.mDrawModifiers;
Chris Craikff785832013-03-08 13:12:16 -08001433
Chris Craik527a3aa2013-03-04 10:19:31 -08001434 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001435 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1436 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001437 dirtyClip();
1438 }
Chris Craikc3566d02013-02-04 16:16:33 -08001439}
1440
Chris Craik28ce94a2013-05-31 11:38:03 -07001441/**
1442 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1443 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1444 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1445 *
1446 * This method should be called when restoreDisplayState() won't be restoring the clip
1447 */
1448void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1449 if (clipRect != NULL) {
1450 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1451 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001452 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001453 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001454 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001455 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001456}
1457
Chris Craikc3566d02013-02-04 16:16:33 -08001458///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001459// Clipping
1460///////////////////////////////////////////////////////////////////////////////
1461
Romain Guybb9524b2010-06-22 18:56:38 -07001462void OpenGLRenderer::setScissorFromClip() {
Chris Craik14e51302013-12-30 15:32:54 -08001463 Rect clip(currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001464 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001465
Chris Craik14e51302013-12-30 15:32:54 -08001466 if (mCaches.setScissor(clip.left, currentSnapshot().height - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001467 clip.getWidth(), clip.getHeight())) {
1468 mDirtyClip = false;
1469 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001470}
1471
Romain Guy8ce00302013-01-15 18:51:42 -08001472void OpenGLRenderer::ensureStencilBuffer() {
1473 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1474 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1475 // just hope we have one when hasLayer() returns false.
1476 if (hasLayer()) {
Chris Craik14e51302013-12-30 15:32:54 -08001477 attachStencilBufferToLayer(currentSnapshot().layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001478 }
1479}
1480
1481void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1482 // The layer's FBO is already bound when we reach this stage
1483 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001484 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1485 // is attached after we initiated tiling. We must turn it off,
1486 // attach the new render buffer then turn tiling back on
1487 endTiling();
1488
Romain Guy8d4aeb72013-02-12 16:08:55 -08001489 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001490 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001491 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001492
Romain Guyf735c8e2013-01-31 17:45:55 -08001493 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001494 }
1495}
1496
1497void OpenGLRenderer::setStencilFromClip() {
1498 if (!mCaches.debugOverdraw) {
Chris Craik14e51302013-12-30 15:32:54 -08001499 if (!currentSnapshot().clipRegion->isEmpty()) {
Romain Guy8ce00302013-01-15 18:51:42 -08001500 // NOTE: The order here is important, we must set dirtyClip to false
1501 // before any draw call to avoid calling back into this method
1502 mDirtyClip = false;
1503
1504 ensureStencilBuffer();
1505
1506 mCaches.stencil.enableWrite();
1507
1508 // Clear the stencil but first make sure we restrict drawing
1509 // to the region's bounds
1510 bool resetScissor = mCaches.enableScissor();
1511 if (resetScissor) {
1512 // The scissor was not set so we now need to update it
1513 setScissorFromClip();
1514 }
1515 mCaches.stencil.clear();
1516 if (resetScissor) mCaches.disableScissor();
1517
1518 // NOTE: We could use the region contour path to generate a smaller mesh
1519 // Since we are using the stencil we could use the red book path
1520 // drawing technique. It might increase bandwidth usage though.
1521
1522 // The last parameter is important: we are not drawing in the color buffer
1523 // so we don't want to dirty the current layer, if any
Chris Craik14e51302013-12-30 15:32:54 -08001524 drawRegionRects(*(currentSnapshot().clipRegion),
1525 0xff000000, SkXfermode::kSrc_Mode, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001526
1527 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001528
1529 // Draw the region used to generate the stencil if the appropriate debug
1530 // mode is enabled
1531 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Chris Craik14e51302013-12-30 15:32:54 -08001532 drawRegionRects(*(currentSnapshot().clipRegion),
1533 0x7f0000ff, SkXfermode::kSrcOver_Mode);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001534 }
Romain Guy8ce00302013-01-15 18:51:42 -08001535 } else {
1536 mCaches.stencil.disable();
1537 }
1538 }
1539}
1540
Chris Craikf0a59072013-11-19 18:00:46 -08001541/**
1542 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1543 *
1544 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1545 * style, and tessellated AA ramp
1546 */
1547bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
1548 SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001549 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001550 bool snapOut = paint && paint->isAntiAlias();
1551
1552 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1553 float outset = paint->getStrokeWidth() * 0.5f;
1554 left -= outset;
1555 top -= outset;
1556 right += outset;
1557 bottom += outset;
1558 }
1559
1560 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001561 return true;
1562 }
1563
Chris Craikb4589422013-12-26 15:13:13 -08001564 if (!isRecording()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001565 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001566 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001567 }
Chris Craik39a908c2013-06-13 14:39:01 -07001568 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001569}
1570
Romain Guy8ce00302013-01-15 18:51:42 -08001571void OpenGLRenderer::debugClip() {
1572#if DEBUG_CLIP_REGIONS
Chris Craik14e51302013-12-30 15:32:54 -08001573 if (!isRecording() && !currentSnapshot().clipRegion->isEmpty()) {
1574 drawRegionRects(*(currentSnapshot().clipRegion), 0x7f00ff00, SkXfermode::kSrcOver_Mode);
Romain Guy8ce00302013-01-15 18:51:42 -08001575 }
1576#endif
1577}
1578
Romain Guy079ba2c2010-07-16 14:12:24 -07001579bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
Romain Guy3b753822013-03-05 10:27:35 -08001580 if (CC_LIKELY(currentTransform().rectToRect())) {
Romain Guy8ce00302013-01-15 18:51:42 -08001581 bool clipped = mSnapshot->clip(left, top, right, bottom, op);
1582 if (clipped) {
1583 dirtyClip();
1584 }
1585 return !mSnapshot->clipRect->isEmpty();
1586 }
1587
1588 SkPath path;
1589 path.addRect(left, top, right, bottom);
1590
Romain Guyb7b93e02013-08-01 15:29:25 -07001591 return OpenGLRenderer::clipPath(&path, op);
Romain Guy8ce00302013-01-15 18:51:42 -08001592}
1593
1594bool OpenGLRenderer::clipPath(SkPath* path, SkRegion::Op op) {
1595 SkMatrix transform;
Romain Guy3b753822013-03-05 10:27:35 -08001596 currentTransform().copyTo(transform);
Romain Guy8ce00302013-01-15 18:51:42 -08001597
1598 SkPath transformed;
1599 path->transform(transform, &transformed);
1600
1601 SkRegion clip;
Romain Guyb7b93e02013-08-01 15:29:25 -07001602 if (!mSnapshot->previous->clipRegion->isEmpty()) {
1603 clip.setRegion(*mSnapshot->previous->clipRegion);
Romain Guy8ce00302013-01-15 18:51:42 -08001604 } else {
Romain Guyb7b93e02013-08-01 15:29:25 -07001605 if (mSnapshot->previous == mFirstSnapshot) {
Chris Craik14e51302013-12-30 15:32:54 -08001606 clip.setRect(0, 0, getWidth(), getHeight());
Romain Guyb7b93e02013-08-01 15:29:25 -07001607 } else {
1608 Rect* bounds = mSnapshot->previous->clipRect;
1609 clip.setRect(bounds->left, bounds->top, bounds->right, bounds->bottom);
1610 }
Romain Guy8ce00302013-01-15 18:51:42 -08001611 }
1612
1613 SkRegion region;
1614 region.setPath(transformed, clip);
1615
1616 bool clipped = mSnapshot->clipRegionTransformed(region, op);
Romain Guy7ae7ac42010-06-25 13:46:18 -07001617 if (clipped) {
Romain Guy746b7402010-10-26 16:27:31 -07001618 dirtyClip();
Romain Guy7ae7ac42010-06-25 13:46:18 -07001619 }
Romain Guy8aef54f2010-09-01 15:13:49 -07001620 return !mSnapshot->clipRect->isEmpty();
Romain Guye4d01122010-06-16 18:44:05 -07001621}
1622
Romain Guy735738c2012-12-03 12:34:51 -08001623bool OpenGLRenderer::clipRegion(SkRegion* region, SkRegion::Op op) {
Romain Guy8ce00302013-01-15 18:51:42 -08001624 bool clipped = mSnapshot->clipRegionTransformed(*region, op);
1625 if (clipped) {
1626 dirtyClip();
1627 }
1628 return !mSnapshot->clipRect->isEmpty();
Romain Guy735738c2012-12-03 12:34:51 -08001629}
1630
Romain Guyf6a11b82010-06-23 17:47:49 -07001631///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001632// Drawing commands
1633///////////////////////////////////////////////////////////////////////////////
1634
Romain Guy54be1cd2011-06-13 19:04:27 -07001635void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001636 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001637 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001638 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001639 // Make sure setScissor & setStencil happen at the beginning of
1640 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001641 if (mDirtyClip) {
1642 if (mCaches.scissorEnabled) {
1643 setScissorFromClip();
1644 }
Romain Guy8ce00302013-01-15 18:51:42 -08001645 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001646 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001647
Romain Guy70ca14e2010-12-13 18:24:33 -08001648 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001649
Romain Guy70ca14e2010-12-13 18:24:33 -08001650 mSetShaderColor = false;
1651 mColorSet = false;
1652 mColorA = mColorR = mColorG = mColorB = 0.0f;
1653 mTextureUnit = 0;
1654 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001655
1656 // Enable debug highlight when what we're about to draw is tested against
1657 // the stencil buffer and if stencil highlight debugging is on
1658 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1659 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1660 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001661
1662 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001663}
1664
1665void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1666 mDescription.hasTexture = true;
1667 mDescription.hasAlpha8Texture = isAlpha8;
1668}
1669
Romain Guyff316ec2013-02-13 18:39:43 -08001670void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1671 mDescription.hasTexture = true;
1672 mDescription.hasColors = true;
1673 mDescription.hasAlpha8Texture = isAlpha8;
1674}
1675
Romain Guyaa6c24c2011-04-28 18:40:04 -07001676void OpenGLRenderer::setupDrawWithExternalTexture() {
1677 mDescription.hasExternalTexture = true;
1678}
1679
Romain Guy15bc6432011-12-13 13:11:32 -08001680void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001681 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001682}
1683
Chris Craik710f46d2012-09-17 17:25:49 -07001684void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001685 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001686}
1687
Romain Guy8d0d4782010-12-14 20:13:35 -08001688void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1689 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001690 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1691 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1692 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001693 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001694 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001695}
1696
Romain Guy86568192010-12-14 15:55:39 -08001697void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1698 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001699 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1700 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1701 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001702 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001703 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001704}
1705
Romain Guy41210632012-07-16 17:04:24 -07001706void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1707 mCaches.fontRenderer->describe(mDescription, paint);
1708}
1709
Romain Guy70ca14e2010-12-13 18:24:33 -08001710void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1711 mColorA = a;
1712 mColorR = r;
1713 mColorG = g;
1714 mColorB = b;
1715 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001716 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001717}
1718
1719void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001720 if (mDrawModifiers.mShader) {
1721 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001722 }
1723}
1724
1725void OpenGLRenderer::setupDrawColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08001726 if (mDrawModifiers.mColorFilter) {
1727 mDrawModifiers.mColorFilter->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001728 }
1729}
1730
Romain Guyf09ef512011-05-27 11:43:46 -07001731void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1732 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1733 mColorA = 1.0f;
1734 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001735 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001736 }
1737}
1738
Romain Guy70ca14e2010-12-13 18:24:33 -08001739void OpenGLRenderer::setupDrawBlending(SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001740 // When the blending mode is kClear_Mode, we need to use a modulate color
1741 // argb=1,0,0,0
1742 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001743 bool blend = (mColorSet && mColorA < 1.0f) ||
1744 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend());
1745 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001746}
1747
1748void OpenGLRenderer::setupDrawBlending(bool blend, SkXfermode::Mode mode, bool swapSrcDst) {
Romain Guyf09ef512011-05-27 11:43:46 -07001749 // When the blending mode is kClear_Mode, we need to use a modulate color
1750 // argb=1,0,0,0
1751 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001752 blend |= (mColorSet && mColorA < 1.0f) ||
1753 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1754 (mDrawModifiers.mColorFilter && mDrawModifiers.mColorFilter->blend());
1755 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001756}
1757
1758void OpenGLRenderer::setupDrawProgram() {
1759 useProgram(mCaches.programCache.get(mDescription));
1760}
1761
1762void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1763 mTrackDirtyRegions = false;
1764}
1765
Chris Craik4063a0e2013-11-15 16:06:56 -08001766void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1767 float left, float top, float right, float bottom, bool ignoreTransform) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001768 mModelView.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001769 if (mode == kModelViewMode_TranslateAndScale) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001770 mModelView.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001771 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001772
Romain Guy86568192010-12-14 15:55:39 -08001773 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
1774 if (!ignoreTransform) {
Chris Craikf57776b2013-10-25 18:30:17 -07001775 mCaches.currentProgram->set(mViewProjMatrix, mModelView, currentTransform(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001776 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy86568192010-12-14 15:55:39 -08001777 } else {
Chris Craikf57776b2013-10-25 18:30:17 -07001778 mCaches.currentProgram->set(mViewProjMatrix, mModelView, mat4::identity(), offset);
Chris Craik4063a0e2013-11-15 16:06:56 -08001779 if (dirty && mTrackDirtyRegions) dirtyLayer(left, top, right, bottom);
Romain Guy86568192010-12-14 15:55:39 -08001780 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001781}
1782
1783void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001784 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001785 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1786 }
1787}
1788
Romain Guy86568192010-12-14 15:55:39 -08001789void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001790 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001791 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001792 }
1793}
1794
Romain Guy70ca14e2010-12-13 18:24:33 -08001795void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001796 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001797 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001798 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1799 // because it was built into modelView / the geometry, and the SkiaShader needs to
1800 // compensate.
1801 mat4 modelViewWithoutTransform;
1802 modelViewWithoutTransform.loadInverse(currentTransform());
1803 modelViewWithoutTransform.multiply(mModelView);
1804 mModelView.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001805 }
Chris Craikc3566d02013-02-04 16:16:33 -08001806 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
1807 mModelView, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001808 }
1809}
1810
1811void OpenGLRenderer::setupDrawColorFilterUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001812 if (mDrawModifiers.mColorFilter) {
1813 mDrawModifiers.mColorFilter->setupProgram(mCaches.currentProgram);
Romain Guy70ca14e2010-12-13 18:24:33 -08001814 }
1815}
1816
Romain Guy41210632012-07-16 17:04:24 -07001817void OpenGLRenderer::setupDrawTextGammaUniforms() {
1818 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1819}
1820
Romain Guy70ca14e2010-12-13 18:24:33 -08001821void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001822 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001823 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001824 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001825}
1826
1827void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001828 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001829 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001830 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001831}
1832
Romain Guyaa6c24c2011-04-28 18:40:04 -07001833void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1834 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001835 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001836 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001837}
1838
Romain Guy8f0095c2011-05-02 17:24:22 -07001839void OpenGLRenderer::setupDrawTextureTransform() {
1840 mDescription.hasTextureTransform = true;
1841}
1842
1843void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001844 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1845 GL_FALSE, &transform.data[0]);
1846}
1847
Romain Guy70ca14e2010-12-13 18:24:33 -08001848void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001849 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001850 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001851 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001852 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001853 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001854 }
Romain Guyd71dd362011-12-12 19:03:35 -08001855
Chris Craikcb4d6002012-09-25 12:00:29 -07001856 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001857 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001858 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001859 }
1860
1861 mCaches.unbindIndicesBuffer();
1862}
1863
Romain Guyff316ec2013-02-13 18:39:43 -08001864void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLvoid* colors) {
1865 bool force = mCaches.unbindMeshBuffer();
1866 GLsizei stride = sizeof(ColorTextureVertex);
1867
1868 mCaches.bindPositionVertexPointer(force, vertices, stride);
1869 if (mCaches.currentProgram->texCoords >= 0) {
1870 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1871 }
1872 int slot = mCaches.currentProgram->getAttrib("colors");
1873 if (slot >= 0) {
1874 glEnableVertexAttribArray(slot);
1875 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1876 }
1877
1878 mCaches.unbindIndicesBuffer();
1879}
1880
Romain Guy3b748a42013-04-17 18:54:38 -07001881void OpenGLRenderer::setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
1882 bool force = false;
1883 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1884 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1885 // use the default VBO found in Caches
1886 if (!vertices || vbo) {
1887 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1888 } else {
1889 force = mCaches.unbindMeshBuffer();
1890 }
1891 mCaches.bindIndicesBuffer();
1892
Chris Craikcb4d6002012-09-25 12:00:29 -07001893 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001894 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001895 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001896 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001897}
1898
Romain Guy448455f2013-07-22 13:57:50 -07001899void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001900 bool force = mCaches.unbindMeshBuffer();
Romain Guy448455f2013-07-22 13:57:50 -07001901 mCaches.bindIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001902 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001903}
1904
Romain Guy70ca14e2010-12-13 18:24:33 -08001905///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001906// Drawing
1907///////////////////////////////////////////////////////////////////////////////
1908
Chris Craikff785832013-03-08 13:12:16 -08001909status_t OpenGLRenderer::drawDisplayList(DisplayList* displayList, Rect& dirty,
1910 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001911 status_t status;
Chris Craikf57776b2013-10-25 18:30:17 -07001912
Chris Craikba9b6132013-12-15 17:10:19 -08001913 if (mCaches.propertyDirtyViewport) {
1914 // force recalc of view/proj matrices
Chris Craik14e51302013-12-30 15:32:54 -08001915 setViewport(getWidth(), getHeight());
Chris Craikba9b6132013-12-15 17:10:19 -08001916 mCaches.propertyDirtyViewport = false;
1917 }
1918
Romain Guy0fe478e2010-11-08 12:08:41 -08001919 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1920 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001921 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001922 // compute 3d ordering
1923 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001924 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001925 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001926 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
1927 displayList->replay(replayStruct, 0);
Chet Haase58d110a2013-04-10 07:43:29 -07001928 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001929 }
1930
Chris Craik28ce94a2013-05-31 11:38:03 -07001931 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
1932 DeferredDisplayList deferredList(*(mSnapshot->clipRect), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001933 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
1934 displayList->defer(deferStruct, 0);
Romain Guy96885eb2013-03-26 15:05:58 -07001935
1936 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001937 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001938
Chris Craikf57776b2013-10-25 18:30:17 -07001939 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001940 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001941
Romain Guy65549432012-03-26 16:45:05 -07001942 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001943}
1944
Romain Guya168d732011-03-18 16:50:13 -07001945void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, SkPaint* paint) {
1946 int alpha;
1947 SkXfermode::Mode mode;
1948 getAlphaAndMode(paint, &alpha, &mode);
1949
Romain Guy886b2752013-01-04 12:26:18 -08001950 int color = paint != NULL ? paint->getColor() : 0;
1951
Romain Guya168d732011-03-18 16:50:13 -07001952 float x = left;
1953 float y = top;
1954
Romain Guy886b2752013-01-04 12:26:18 -08001955 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1956
Romain Guya168d732011-03-18 16:50:13 -07001957 bool ignoreTransform = false;
Romain Guy3b753822013-03-05 10:27:35 -08001958 if (currentTransform().isPureTranslate()) {
1959 x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
1960 y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001961 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001962
1963 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001964 } else {
Romain Guy886b2752013-01-04 12:26:18 -08001965 texture->setFilter(FILTER(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001966 }
1967
Romain Guy3b748a42013-04-17 18:54:38 -07001968 // No need to check for a UV mapper on the texture object, only ARGB_8888
1969 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001970 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07001971 paint != NULL, color, alpha, mode, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
1972 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001973}
1974
Romain Guy03c00b52013-06-20 18:30:28 -07001975/**
1976 * Important note: this method is intended to draw batches of bitmaps and
1977 * will not set the scissor enable or dirty the current layer, if any.
1978 * The caller is responsible for properly dirtying the current layer.
1979 */
Romain Guy55b6f952013-06-27 15:27:09 -07001980status_t OpenGLRenderer::drawBitmaps(SkBitmap* bitmap, AssetAtlas::Entry* entry, int bitmapCount,
Chris Craik996fe652013-09-20 17:13:18 -07001981 TextureVertex* vertices, bool pureTranslate, const Rect& bounds, SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001982 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001983 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08001984 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07001985
Chris Craik527a3aa2013-03-04 10:19:31 -08001986 const AutoTexture autoCleanup(texture);
1987
1988 int alpha;
1989 SkXfermode::Mode mode;
1990 getAlphaAndMode(paint, &alpha, &mode);
1991
1992 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik996fe652013-09-20 17:13:18 -07001993 texture->setFilter(pureTranslate ? GL_NEAREST : FILTER(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001994
1995 const float x = (int) floorf(bounds.left + 0.5f);
1996 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05001997 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001998 int color = paint != NULL ? paint->getColor() : 0;
1999 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2000 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002001 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002002 GL_TRIANGLES, bitmapCount * 6, true,
2003 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002004 } else {
2005 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
2006 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002007 &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002008 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
2009 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08002010 }
2011
2012 return DrawGlInfo::kStatusDrew;
2013}
2014
Chet Haase48659092012-05-31 15:21:51 -07002015status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07002016 const float right = left + bitmap->width();
2017 const float bottom = top + bitmap->height();
2018
Chris Craikf0a59072013-11-19 18:00:46 -08002019 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002020 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002021 }
2022
Romain Guya1d3c912011-12-13 14:55:06 -08002023 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002024 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002025 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002026 const AutoTexture autoCleanup(texture);
2027
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002028 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07002029 drawAlphaBitmap(texture, left, top, paint);
2030 } else {
2031 drawTextureRect(left, top, right, bottom, texture, paint);
2032 }
Chet Haase48659092012-05-31 15:21:51 -07002033
2034 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07002035}
2036
Chet Haase48659092012-05-31 15:21:51 -07002037status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07002038 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
2039 const mat4 transform(*matrix);
2040 transform.mapRect(r);
2041
Chris Craikf0a59072013-11-19 18:00:46 -08002042 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002043 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002044 }
2045
Romain Guya1d3c912011-12-13 14:55:06 -08002046 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002047 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002048 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002049 const AutoTexture autoCleanup(texture);
2050
Romain Guy5b3b3522010-10-27 18:57:51 -07002051 // This could be done in a cheaper way, all we need is pass the matrix
2052 // to the vertex shader. The save/restore is a bit overkill.
2053 save(SkCanvas::kMatrix_SaveFlag);
2054 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002055 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002056 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2057 } else {
2058 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2059 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002060 restore();
Chet Haase48659092012-05-31 15:21:51 -07002061
2062 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002063}
2064
Chet Haase48659092012-05-31 15:21:51 -07002065status_t OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002066 const float right = left + bitmap->width();
2067 const float bottom = top + bitmap->height();
2068
Chris Craikf0a59072013-11-19 18:00:46 -08002069 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002070 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002071 }
2072
2073 mCaches.activeTexture(0);
2074 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2075 const AutoTexture autoCleanup(texture);
2076
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002077 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002078 drawAlphaBitmap(texture, left, top, paint);
2079 } else {
2080 drawTextureRect(left, top, right, bottom, texture, paint);
2081 }
Chet Haase48659092012-05-31 15:21:51 -07002082
2083 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002084}
2085
Chet Haase48659092012-05-31 15:21:51 -07002086status_t OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
Romain Guy5a7b4662011-01-20 19:09:30 -08002087 float* vertices, int* colors, SkPaint* paint) {
Romain Guy5a7b4662011-01-20 19:09:30 -08002088 if (!vertices || mSnapshot->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002089 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002090 }
2091
Chris Craik39a908c2013-06-13 14:39:01 -07002092 // TODO: use quickReject on bounds from vertices
2093 mCaches.enableScissor();
2094
Romain Guyb18d2d02011-02-10 15:52:54 -08002095 float left = FLT_MAX;
2096 float top = FLT_MAX;
2097 float right = FLT_MIN;
2098 float bottom = FLT_MIN;
2099
Romain Guya92bb4d2012-10-16 11:08:44 -07002100 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002101
Romain Guyff316ec2013-02-13 18:39:43 -08002102 ColorTextureVertex mesh[count];
2103 ColorTextureVertex* vertex = mesh;
2104
2105 bool cleanupColors = false;
2106 if (!colors) {
2107 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
2108 colors = new int[colorsCount];
2109 memset(colors, 0xff, colorsCount * sizeof(int));
2110 cleanupColors = true;
2111 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002112
Romain Guy3b748a42013-04-17 18:54:38 -07002113 mCaches.activeTexture(0);
2114 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2115 const UvMapper& mapper(getMapper(texture));
2116
Romain Guy5a7b4662011-01-20 19:09:30 -08002117 for (int32_t y = 0; y < meshHeight; y++) {
2118 for (int32_t x = 0; x < meshWidth; x++) {
2119 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2120
2121 float u1 = float(x) / meshWidth;
2122 float u2 = float(x + 1) / meshWidth;
2123 float v1 = float(y) / meshHeight;
2124 float v2 = float(y + 1) / meshHeight;
2125
Romain Guy3b748a42013-04-17 18:54:38 -07002126 mapper.map(u1, v1, u2, v2);
2127
Romain Guy5a7b4662011-01-20 19:09:30 -08002128 int ax = i + (meshWidth + 1) * 2;
2129 int ay = ax + 1;
2130 int bx = i;
2131 int by = bx + 1;
2132 int cx = i + 2;
2133 int cy = cx + 1;
2134 int dx = i + (meshWidth + 1) * 2 + 2;
2135 int dy = dx + 1;
2136
Romain Guyff316ec2013-02-13 18:39:43 -08002137 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2138 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2139 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002140
Romain Guyff316ec2013-02-13 18:39:43 -08002141 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2142 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2143 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002144
Romain Guya92bb4d2012-10-16 11:08:44 -07002145 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2146 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2147 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2148 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002149 }
2150 }
2151
Chris Craikf0a59072013-11-19 18:00:46 -08002152 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002153 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002154 return DrawGlInfo::kStatusDone;
2155 }
2156
Romain Guyff316ec2013-02-13 18:39:43 -08002157 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002158 texture = mCaches.textureCache.get(bitmap);
2159 if (!texture) {
2160 if (cleanupColors) delete[] colors;
2161 return DrawGlInfo::kStatusDone;
2162 }
Romain Guyff316ec2013-02-13 18:39:43 -08002163 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002164 const AutoTexture autoCleanup(texture);
2165
2166 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2167 texture->setFilter(FILTER(paint), true);
2168
2169 int alpha;
2170 SkXfermode::Mode mode;
2171 getAlphaAndMode(paint, &alpha, &mode);
2172
Romain Guyff316ec2013-02-13 18:39:43 -08002173 float a = alpha / 255.0f;
2174
Romain Guya92bb4d2012-10-16 11:08:44 -07002175 if (hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08002176 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002177 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002178
Romain Guyff316ec2013-02-13 18:39:43 -08002179 setupDraw();
2180 setupDrawWithTextureAndColor();
2181 setupDrawColor(a, a, a, a);
2182 setupDrawColorFilter();
2183 setupDrawBlending(true, mode, false);
2184 setupDrawProgram();
2185 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002186 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002187 setupDrawTexture(texture->id);
2188 setupDrawPureColorUniforms();
2189 setupDrawColorFilterUniforms();
Romain Guy3380cfd2013-08-15 16:57:57 -07002190 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002191
2192 glDrawArrays(GL_TRIANGLES, 0, count);
2193
Romain Guyff316ec2013-02-13 18:39:43 -08002194 int slot = mCaches.currentProgram->getAttrib("colors");
2195 if (slot >= 0) {
2196 glDisableVertexAttribArray(slot);
2197 }
2198
2199 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002200
2201 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002202}
2203
Chet Haase48659092012-05-31 15:21:51 -07002204status_t OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002205 float srcLeft, float srcTop, float srcRight, float srcBottom,
2206 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chet Haase5c13d892010-10-08 08:37:55 -07002207 SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002208 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002209 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002210 }
2211
Romain Guya1d3c912011-12-13 14:55:06 -08002212 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002213 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002214 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002215 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002216
Romain Guy8ba548f2010-06-30 19:21:21 -07002217 const float width = texture->width;
2218 const float height = texture->height;
2219
Romain Guy3b748a42013-04-17 18:54:38 -07002220 float u1 = fmax(0.0f, srcLeft / width);
2221 float v1 = fmax(0.0f, srcTop / height);
2222 float u2 = fmin(1.0f, srcRight / width);
2223 float v2 = fmin(1.0f, srcBottom / height);
2224
2225 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002226
Romain Guy03750a02010-10-18 14:06:08 -07002227 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002228 resetDrawTextureTexCoords(u1, v1, u2, v2);
2229
Romain Guy03750a02010-10-18 14:06:08 -07002230 int alpha;
2231 SkXfermode::Mode mode;
2232 getAlphaAndMode(paint, &alpha, &mode);
2233
Romain Guyd21b6e12011-11-30 20:21:23 -08002234 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2235
Romain Guy886b2752013-01-04 12:26:18 -08002236 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2237 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002238
Romain Guy886b2752013-01-04 12:26:18 -08002239 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2240 // Apply a scale transform on the canvas only when a shader is in use
2241 // Skia handles the ratio between the dst and src rects as a scale factor
2242 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002243 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002244 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002245
Romain Guy3b753822013-03-05 10:27:35 -08002246 if (CC_LIKELY(currentTransform().isPureTranslate() && !useScaleTransform)) {
2247 float x = (int) floorf(dstLeft + currentTransform().getTranslateX() + 0.5f);
2248 float y = (int) floorf(dstTop + currentTransform().getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002249
2250 dstRight = x + (dstRight - dstLeft);
2251 dstBottom = y + (dstBottom - dstTop);
2252
2253 dstLeft = x;
2254 dstTop = y;
2255
2256 texture->setFilter(scaled ? FILTER(paint) : GL_NEAREST, true);
2257 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002258 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002259 texture->setFilter(FILTER(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002260 }
2261
2262 if (CC_UNLIKELY(useScaleTransform)) {
2263 save(SkCanvas::kMatrix_SaveFlag);
2264 translate(dstLeft, dstTop);
2265 scale(scaleX, scaleY);
2266
2267 dstLeft = 0.0f;
2268 dstTop = 0.0f;
2269
2270 dstRight = srcRight - srcLeft;
2271 dstBottom = srcBottom - srcTop;
2272 }
2273
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002274 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002275 int color = paint ? paint->getColor() : 0;
2276 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2277 texture->id, paint != NULL, color, alpha, mode,
Romain Guy3380cfd2013-08-15 16:57:57 -07002278 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002279 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2280 } else {
2281 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
2282 texture->id, alpha / 255.0f, mode, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002283 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002284 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2285 }
2286
2287 if (CC_UNLIKELY(useScaleTransform)) {
2288 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002289 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002290
2291 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002292
2293 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002294}
2295
Romain Guy3b748a42013-04-17 18:54:38 -07002296status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
Chet Haase5c13d892010-10-08 08:37:55 -07002297 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002298 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002299 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002300 }
2301
Romain Guy4c2547f2013-06-11 16:19:24 -07002302 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002303 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2304 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002305
Romain Guy03c00b52013-06-20 18:30:28 -07002306 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002307}
2308
Romain Guy03c00b52013-06-20 18:30:28 -07002309status_t OpenGLRenderer::drawPatch(SkBitmap* bitmap, const Patch* mesh, AssetAtlas::Entry* entry,
2310 float left, float top, float right, float bottom, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002311 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002312 return DrawGlInfo::kStatusDone;
2313 }
2314
Romain Guy211370f2012-02-01 16:10:55 -08002315 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002316 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002317 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002318 if (!texture) return DrawGlInfo::kStatusDone;
2319 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002320
Romain Guya4adcf02013-02-28 12:15:35 -08002321 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2322 texture->setFilter(GL_LINEAR, true);
2323
Romain Guy03c00b52013-06-20 18:30:28 -07002324 int alpha;
2325 SkXfermode::Mode mode;
2326 getAlphaAndMode(paint, &alpha, &mode);
2327
Romain Guy3b753822013-03-05 10:27:35 -08002328 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002329 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002330 if (hasLayer() && mesh->hasEmptyQuads) {
Romain Guy3b753822013-03-05 10:27:35 -08002331 const float offsetX = left + currentTransform().getTranslateX();
2332 const float offsetY = top + currentTransform().getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002333 const size_t count = mesh->quads.size();
2334 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002335 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002336 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002337 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2338 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2339 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002340 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002341 dirtyLayer(left + bounds.left, top + bounds.top,
Romain Guy3b753822013-03-05 10:27:35 -08002342 left + bounds.right, top + bounds.bottom, currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002343 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002344 }
2345 }
2346
Chris Craik4063a0e2013-11-15 16:06:56 -08002347 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002348 if (CC_LIKELY(pureTranslate)) {
Romain Guy3b753822013-03-05 10:27:35 -08002349 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
2350 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002351
Romain Guy3b748a42013-04-17 18:54:38 -07002352 right = x + right - left;
2353 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002354 left = x;
2355 top = y;
2356 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002357 }
Chris Craik4063a0e2013-11-15 16:06:56 -08002358 drawIndexedTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f,
2359 mode, texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
2360 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2361 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002362 }
Chet Haase48659092012-05-31 15:21:51 -07002363
2364 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002365}
2366
Romain Guy03c00b52013-06-20 18:30:28 -07002367/**
2368 * Important note: this method is intended to draw batches of 9-patch objects and
2369 * will not set the scissor enable or dirty the current layer, if any.
2370 * The caller is responsible for properly dirtying the current layer.
2371 */
2372status_t OpenGLRenderer::drawPatches(SkBitmap* bitmap, AssetAtlas::Entry* entry,
2373 TextureVertex* vertices, uint32_t indexCount, SkPaint* paint) {
2374 mCaches.activeTexture(0);
2375 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2376 if (!texture) return DrawGlInfo::kStatusDone;
2377 const AutoTexture autoCleanup(texture);
2378
2379 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2380 texture->setFilter(GL_LINEAR, true);
2381
2382 int alpha;
2383 SkXfermode::Mode mode;
2384 getAlphaAndMode(paint, &alpha, &mode);
2385
2386 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, alpha / 255.0f,
Romain Guy3380cfd2013-08-15 16:57:57 -07002387 mode, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002388 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002389
2390 return DrawGlInfo::kStatusDrew;
2391}
2392
Chris Craik65cd6122012-12-10 17:56:27 -08002393status_t OpenGLRenderer::drawVertexBuffer(const VertexBuffer& vertexBuffer, SkPaint* paint,
2394 bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002395 // not missing call to quickReject/dirtyLayer, always done at a higher level
2396
Chris Craik6d29c8d2013-05-08 18:35:44 -07002397 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002398 // no vertices to draw
2399 return DrawGlInfo::kStatusDone;
2400 }
2401
Chris Craikcb4d6002012-09-25 12:00:29 -07002402 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002403 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
2404 bool isAA = paint->isAntiAlias();
2405
Chris Craik710f46d2012-09-17 17:25:49 -07002406 setupDraw();
2407 setupDrawNoTexture();
2408 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002409 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
2410 setupDrawColorFilter();
2411 setupDrawShader();
2412 setupDrawBlending(isAA, mode);
2413 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002414 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002415 setupDrawColorUniforms();
2416 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08002417 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002418
ztenghui55bfb4e2013-12-03 10:38:55 -08002419 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002420 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002421 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002422 mCaches.resetTexCoordsVertexPointer();
2423 mCaches.unbindIndicesBuffer();
Chet Haase858aa932011-05-12 09:06:00 -07002424
Chris Craik710f46d2012-09-17 17:25:49 -07002425 int alphaSlot = -1;
2426 if (isAA) {
2427 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2428 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik6ebdc112012-08-31 18:24:33 -07002429
Chris Craik710f46d2012-09-17 17:25:49 -07002430 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002431 glEnableVertexAttribArray(alphaSlot);
2432 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002433 }
Romain Guy04299382012-07-18 17:15:41 -07002434
Chris Craik6d29c8d2013-05-08 18:35:44 -07002435 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
Romain Guy04299382012-07-18 17:15:41 -07002436
Chris Craik710f46d2012-09-17 17:25:49 -07002437 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002438 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002439 }
Chris Craik65cd6122012-12-10 17:56:27 -08002440
2441 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002442}
2443
2444/**
Chris Craik65cd6122012-12-10 17:56:27 -08002445 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2446 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2447 * screen space in all directions. However, instead of using a fragment shader to compute the
2448 * translucency of the color from its position, we simply use a varying parameter to define how far
2449 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2450 *
2451 * Doesn't yet support joins, caps, or path effects.
2452 */
2453status_t OpenGLRenderer::drawConvexPath(const SkPath& path, SkPaint* paint) {
2454 VertexBuffer vertexBuffer;
2455 // TODO: try clipping large paths to viewport
2456 PathTessellator::tessellatePath(path, paint, mSnapshot->transform, vertexBuffer);
2457
Romain Guyc46d07a2013-03-15 19:06:39 -07002458 if (hasLayer()) {
2459 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002460 PathTessellator::expandBoundsForStroke(bounds, paint);
Romain Guyc46d07a2013-03-15 19:06:39 -07002461 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
2462 }
Chris Craik65cd6122012-12-10 17:56:27 -08002463
2464 return drawVertexBuffer(vertexBuffer, paint);
2465}
2466
2467/**
2468 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2469 * and additional geometry for defining an alpha slope perimeter.
2470 *
2471 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2472 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2473 * in-shader alpha region, but found it to be taxing on some GPUs.
2474 *
2475 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2476 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002477 */
Chet Haase48659092012-05-31 15:21:51 -07002478status_t OpenGLRenderer::drawLines(float* points, int count, SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002479 if (mSnapshot->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002480
Chris Craik65cd6122012-12-10 17:56:27 -08002481 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002482
Chris Craik65cd6122012-12-10 17:56:27 -08002483 VertexBuffer buffer;
2484 SkRect bounds;
2485 PathTessellator::tessellateLines(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002486
Chris Craikf0a59072013-11-19 18:00:46 -08002487 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2488 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002489 return DrawGlInfo::kStatusDone;
2490 }
2491
Romain Guy3b753822013-03-05 10:27:35 -08002492 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002493
Chris Craik65cd6122012-12-10 17:56:27 -08002494 bool useOffset = !paint->isAntiAlias();
2495 return drawVertexBuffer(buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002496}
2497
Chet Haase48659092012-05-31 15:21:51 -07002498status_t OpenGLRenderer::drawPoints(float* points, int count, SkPaint* paint) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002499 if (mSnapshot->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002500
Chris Craik6d29c8d2013-05-08 18:35:44 -07002501 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002502
Chris Craik6d29c8d2013-05-08 18:35:44 -07002503 VertexBuffer buffer;
2504 SkRect bounds;
2505 PathTessellator::tessellatePoints(points, count, paint, mSnapshot->transform, bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002506
Chris Craikf0a59072013-11-19 18:00:46 -08002507 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2508 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002509 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002510 }
2511
Chris Craik6d29c8d2013-05-08 18:35:44 -07002512 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002513
Chris Craik6d29c8d2013-05-08 18:35:44 -07002514 bool useOffset = !paint->isAntiAlias();
2515 return drawVertexBuffer(buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002516}
2517
Chet Haase48659092012-05-31 15:21:51 -07002518status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002519 // No need to check against the clip, we fill the clip region
Chet Haase48659092012-05-31 15:21:51 -07002520 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002521
Romain Guyae88e5e2010-10-22 17:49:18 -07002522 Rect& clip(*mSnapshot->clipRect);
2523 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002524
Romain Guy3d58c032010-07-14 16:34:53 -07002525 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode, true);
Chet Haase48659092012-05-31 15:21:51 -07002526
2527 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002528}
Romain Guy9d5316e2010-06-24 19:30:36 -07002529
Chet Haase48659092012-05-31 15:21:51 -07002530status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
2531 SkPaint* paint) {
2532 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002533 const AutoTexture autoCleanup(texture);
2534
2535 const float x = left + texture->left - texture->offset;
2536 const float y = top + texture->top - texture->offset;
2537
2538 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002539
2540 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002541}
2542
Chet Haase48659092012-05-31 15:21:51 -07002543status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002544 float rx, float ry, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002545 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002546 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002547 return DrawGlInfo::kStatusDone;
2548 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002549
Chris Craikcb4d6002012-09-25 12:00:29 -07002550 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002551 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002552 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002553 right - left, bottom - top, rx, ry, p);
2554 return drawShape(left, top, texture, p);
2555 }
2556
2557 SkPath path;
2558 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002559 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2560 float outset = p->getStrokeWidth() / 2;
2561 rect.outset(outset, outset);
2562 rx += outset;
2563 ry += outset;
2564 }
Chris Craik710f46d2012-09-17 17:25:49 -07002565 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002566 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002567}
2568
Chris Craik710f46d2012-09-17 17:25:49 -07002569status_t OpenGLRenderer::drawCircle(float x, float y, float radius, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002570 if (mSnapshot->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002571 x + radius, y + radius, p) ||
2572 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002573 return DrawGlInfo::kStatusDone;
2574 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002575 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002576 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002577 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002578 return drawShape(x - radius, y - radius, texture, p);
2579 }
2580
2581 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002582 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2583 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2584 } else {
2585 path.addCircle(x, y, radius);
2586 }
Chris Craik65cd6122012-12-10 17:56:27 -08002587 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002588}
Romain Guy01d58e42011-01-19 21:54:02 -08002589
Chet Haase48659092012-05-31 15:21:51 -07002590status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craik710f46d2012-09-17 17:25:49 -07002591 SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002592 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002593 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002594 return DrawGlInfo::kStatusDone;
2595 }
Romain Guy01d58e42011-01-19 21:54:02 -08002596
Chris Craikcb4d6002012-09-25 12:00:29 -07002597 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002598 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002599 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002600 return drawShape(left, top, texture, p);
2601 }
2602
2603 SkPath path;
2604 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002605 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2606 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2607 }
Chris Craik710f46d2012-09-17 17:25:49 -07002608 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002609 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002610}
2611
Chet Haase48659092012-05-31 15:21:51 -07002612status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craik780c1282012-10-04 14:10:49 -07002613 float startAngle, float sweepAngle, bool useCenter, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002614 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002615 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002616 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002617 }
2618
Chris Craik780c1282012-10-04 14:10:49 -07002619 if (fabs(sweepAngle) >= 360.0f) {
2620 return drawOval(left, top, right, bottom, p);
2621 }
2622
2623 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002624 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002625 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002626 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002627 startAngle, sweepAngle, useCenter, p);
2628 return drawShape(left, top, texture, p);
2629 }
2630
2631 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2632 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2633 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2634 }
2635
2636 SkPath path;
2637 if (useCenter) {
2638 path.moveTo(rect.centerX(), rect.centerY());
2639 }
2640 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2641 if (useCenter) {
2642 path.close();
2643 }
Chris Craik65cd6122012-12-10 17:56:27 -08002644 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002645}
2646
Romain Guycf8675e2012-10-02 12:32:25 -07002647// See SkPaintDefaults.h
2648#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2649
Chet Haase48659092012-05-31 15:21:51 -07002650status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom, SkPaint* p) {
Chris Craikf0a59072013-11-19 18:00:46 -08002651 if (mSnapshot->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002652 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002653 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002654 }
2655
Chris Craik710f46d2012-09-17 17:25:49 -07002656 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002657 // only fill style is supported by drawConvexPath, since others have to handle joins
2658 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2659 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2660 mCaches.activeTexture(0);
2661 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002662 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002663 return drawShape(left, top, texture, p);
2664 }
2665
2666 SkPath path;
2667 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2668 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2669 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2670 }
2671 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002672 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002673 }
2674
Romain Guy3b753822013-03-05 10:27:35 -08002675 if (p->isAntiAlias() && !currentTransform().isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002676 SkPath path;
2677 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002678 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002679 } else {
Chris Craik710f46d2012-09-17 17:25:49 -07002680 drawColorRect(left, top, right, bottom, p->getColor(), getXfermode(p->getXfermode()));
Chris Craik65cd6122012-12-10 17:56:27 -08002681 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002682 }
Romain Guyc7d53492010-06-25 13:41:57 -07002683}
Romain Guy9d5316e2010-06-24 19:30:36 -07002684
Raph Levien416a8472012-07-19 22:48:17 -07002685void OpenGLRenderer::drawTextShadow(SkPaint* paint, const char* text, int bytesCount, int count,
2686 const float* positions, FontRenderer& fontRenderer, int alpha, SkXfermode::Mode mode,
2687 float x, float y) {
2688 mCaches.activeTexture(0);
2689
2690 // NOTE: The drop shadow will not perform gamma correction
2691 // if shader-based correction is enabled
2692 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2693 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Chris Craikc3566d02013-02-04 16:16:33 -08002694 paint, text, bytesCount, count, mDrawModifiers.mShadowRadius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002695 // If the drop shadow exceeds the max texture size or couldn't be
2696 // allocated, skip drawing
2697 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002698 const AutoTexture autoCleanup(shadow);
2699
Chris Craikc3566d02013-02-04 16:16:33 -08002700 const float sx = x - shadow->left + mDrawModifiers.mShadowDx;
2701 const float sy = y - shadow->top + mDrawModifiers.mShadowDy;
Raph Levien416a8472012-07-19 22:48:17 -07002702
Chris Craikc3566d02013-02-04 16:16:33 -08002703 const int shadowAlpha = ((mDrawModifiers.mShadowColor >> 24) & 0xFF) * mSnapshot->alpha;
2704 int shadowColor = mDrawModifiers.mShadowColor;
2705 if (mDrawModifiers.mShader) {
Raph Levien416a8472012-07-19 22:48:17 -07002706 shadowColor = 0xffffffff;
2707 }
2708
2709 setupDraw();
2710 setupDrawWithTexture(true);
2711 setupDrawAlpha8Color(shadowColor, shadowAlpha < 255 ? shadowAlpha : alpha);
2712 setupDrawColorFilter();
2713 setupDrawShader();
2714 setupDrawBlending(true, mode);
2715 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002716 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2717 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002718 setupDrawTexture(shadow->id);
2719 setupDrawPureColorUniforms();
2720 setupDrawColorFilterUniforms();
2721 setupDrawShaderUniforms();
2722 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2723
2724 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2725}
2726
Romain Guy768bffc2013-02-27 13:50:45 -08002727bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
2728 float alpha = (mDrawModifiers.mHasShadow ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
2729 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2730}
2731
Chet Haase48659092012-05-31 15:21:51 -07002732status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Romain Guyeb9a5362012-01-17 17:39:26 -08002733 const float* positions, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002734 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002735 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002736 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002737
Romain Guy671d6cf2012-01-18 12:39:17 -08002738 // NOTE: Skia does not support perspective transform on drawPosText yet
Romain Guy3b753822013-03-05 10:27:35 -08002739 if (!currentTransform().isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002740 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002741 }
2742
Chris Craik39a908c2013-06-13 14:39:01 -07002743 mCaches.enableScissor();
2744
Romain Guy671d6cf2012-01-18 12:39:17 -08002745 float x = 0.0f;
2746 float y = 0.0f;
Romain Guy3b753822013-03-05 10:27:35 -08002747 const bool pureTranslate = currentTransform().isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002748 if (pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002749 x = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2750 y = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002751 }
2752
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002753 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002754 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002755
2756 int alpha;
2757 SkXfermode::Mode mode;
2758 getAlphaAndMode(paint, &alpha, &mode);
2759
Chris Craikc3566d02013-02-04 16:16:33 -08002760 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guye3a9b242013-01-08 11:15:30 -08002761 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2762 alpha, mode, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002763 }
2764
Romain Guy671d6cf2012-01-18 12:39:17 -08002765 // Pick the appropriate texture filtering
Romain Guy3b753822013-03-05 10:27:35 -08002766 bool linearFilter = currentTransform().changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002767 if (pureTranslate && !linearFilter) {
2768 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2769 }
Romain Guy257ae352013-03-20 16:31:12 -07002770 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002771
2772 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2773 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2774
Romain Guy211370f2012-02-01 16:10:55 -08002775 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002776
Victoria Lease1e546812013-06-25 14:25:17 -07002777 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002778 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002779 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002780 if (hasActiveLayer) {
2781 if (!pureTranslate) {
Romain Guy3b753822013-03-05 10:27:35 -08002782 currentTransform().mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002783 }
2784 dirtyLayerUnchecked(bounds, getRegion());
2785 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002786 }
Chet Haase48659092012-05-31 15:21:51 -07002787
2788 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002789}
2790
Romain Guy624234f2013-03-05 16:43:31 -08002791mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2792 mat4 fontTransform;
2793 if (CC_LIKELY(transform.isPureTranslate())) {
2794 fontTransform = mat4::identity();
2795 } else {
2796 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002797 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002798 } else {
2799 float sx, sy;
2800 currentTransform().decomposeScale(sx, sy);
2801 fontTransform.loadScale(sx, sy, 1.0f);
2802 }
2803 }
2804 return fontTransform;
2805}
2806
Chris Craik41541822013-05-03 16:35:54 -07002807status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
2808 const float* positions, SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002809 DrawOpMode drawOpMode) {
2810
Chris Craik527a3aa2013-03-04 10:19:31 -08002811 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002812 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2813 // drawing as ops from DeferredDisplayList are already filtered for these
2814 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002815 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002816 return DrawGlInfo::kStatusDone;
2817 }
Romain Guycac5fd32011-12-01 20:08:50 -08002818 }
2819
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002820 const float oldX = x;
2821 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002822
2823 const mat4& transform = currentTransform();
2824 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002825
Romain Guy211370f2012-02-01 16:10:55 -08002826 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002827 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2828 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002829 }
2830
Romain Guy86568192010-12-14 15:55:39 -08002831 int alpha;
2832 SkXfermode::Mode mode;
2833 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002834
Romain Guyc74f45a2013-02-26 19:10:14 -08002835 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2836
Chris Craikc3566d02013-02-04 16:16:33 -08002837 if (CC_UNLIKELY(mDrawModifiers.mHasShadow)) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002838 fontRenderer.setFont(paint, mat4::identity());
2839 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
2840 alpha, mode, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002841 }
2842
Romain Guy19d4dd82013-03-04 11:14:26 -08002843 const bool hasActiveLayer = hasLayer();
2844
Romain Guy624234f2013-03-05 16:43:31 -08002845 // We only pass a partial transform to the font renderer. That partial
2846 // matrix defines how glyphs are rasterized. Typically we want glyphs
2847 // to be rasterized at their final size on screen, which means the partial
2848 // matrix needs to take the scale factor into account.
2849 // When a partial matrix is used to transform glyphs during rasterization,
2850 // the mesh is generated with the inverse transform (in the case of scale,
2851 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2852 // apply the full transform matrix at draw time in the vertex shader.
2853 // Applying the full matrix in the shader is the easiest way to handle
2854 // rotation and perspective and allows us to always generated quads in the
2855 // font renderer which greatly simplifies the code, clipping in particular.
2856 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002857 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002858
Romain Guy6620c6d2010-12-06 18:07:02 -08002859 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002860 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002861 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002862
Romain Guy624234f2013-03-05 16:43:31 -08002863 // TODO: Implement better clipping for scaled/rotated text
2864 const Rect* clip = !pureTranslate ? NULL : mSnapshot->clipRect;
Chris Craik41541822013-05-03 16:35:54 -07002865 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 -07002866
Raph Levien996e57c2012-07-23 15:22:52 -07002867 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002868 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002869
2870 // don't call issuedrawcommand, do it at end of batch
2871 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002872 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002873 SkPaint paintCopy(*paint);
2874 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2875 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002876 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002877 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002878 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002879 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002880 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002881
Chris Craik527a3aa2013-03-04 10:19:31 -08002882 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002883 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002884 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002885 }
Chris Craik41541822013-05-03 16:35:54 -07002886 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002887 }
Romain Guy694b5192010-07-21 21:33:20 -07002888
Chris Craike63f7c622013-10-17 10:30:55 -07002889 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002890
2891 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002892}
2893
Chet Haase48659092012-05-31 15:21:51 -07002894status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count, SkPath* path,
Romain Guy325740f2012-02-24 16:48:34 -08002895 float hOffset, float vOffset, SkPaint* paint) {
Romain Guy768bffc2013-02-27 13:50:45 -08002896 if (text == NULL || count == 0 || mSnapshot->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002897 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002898 }
2899
Chris Craik39a908c2013-06-13 14:39:01 -07002900 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2901 mCaches.enableScissor();
2902
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002903 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002904 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002905 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002906
2907 int alpha;
2908 SkXfermode::Mode mode;
2909 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002910 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002911
Romain Guy97771732012-02-28 18:17:02 -08002912 const Rect* clip = &mSnapshot->getLocalClip();
2913 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 -08002914
Romain Guy97771732012-02-28 18:17:02 -08002915 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002916
2917 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002918 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002919 if (hasActiveLayer) {
Romain Guy3b753822013-03-05 10:27:35 -08002920 currentTransform().mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002921 dirtyLayerUnchecked(bounds, getRegion());
2922 }
Romain Guy97771732012-02-28 18:17:02 -08002923 }
Chet Haase48659092012-05-31 15:21:51 -07002924
2925 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002926}
2927
Chet Haase48659092012-05-31 15:21:51 -07002928status_t OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
2929 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002930
Romain Guya1d3c912011-12-13 14:55:06 -08002931 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002932
Romain Guyfb8b7632010-08-23 21:05:08 -07002933 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002934 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002935 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002936
Romain Guy8b55f372010-08-18 17:10:07 -07002937 const float x = texture->left - texture->offset;
2938 const float y = texture->top - texture->offset;
2939
Romain Guy01d58e42011-01-19 21:54:02 -08002940 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002941
2942 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002943}
2944
Chris Craika08f95c2013-03-15 17:24:33 -07002945status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002946 if (!layer) {
2947 return DrawGlInfo::kStatusDone;
2948 }
2949
Romain Guyb2e2f242012-10-17 18:18:35 -07002950 mat4* transform = NULL;
2951 if (layer->isTextureLayer()) {
2952 transform = &layer->getTransform();
2953 if (!transform->isIdentity()) {
2954 save(0);
Chris Craik14e51302013-12-30 15:32:54 -08002955 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002956 }
2957 }
2958
Chris Craik39a908c2013-06-13 14:39:01 -07002959 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002960 const bool rejected = calculateQuickRejectForScissor(x, y,
2961 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002962
2963 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002964 if (transform && !transform->isIdentity()) {
2965 restore();
2966 }
Chet Haase48659092012-05-31 15:21:51 -07002967 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002968 }
2969
Romain Guy5bb3c732012-11-29 17:52:58 -08002970 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002971
Chris Craik39a908c2013-06-13 14:39:01 -07002972 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002973 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002974
Romain Guy211370f2012-02-01 16:10:55 -08002975 if (CC_LIKELY(!layer->region.isEmpty())) {
Chris Craikc3566d02013-02-04 16:16:33 -08002976 SkiaColorFilter* oldFilter = mDrawModifiers.mColorFilter;
2977 mDrawModifiers.mColorFilter = layer->getColorFilter();
Romain Guye529ece2012-09-26 11:23:17 -07002978
Romain Guyc88e3572011-01-22 00:32:12 -08002979 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002980 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2981 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002982 } else if (layer->mesh) {
Chris Craik16ecda52013-03-29 10:59:59 -07002983 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002984 setupDraw();
2985 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002986 setupDrawColor(a, a, a, a);
Romain Guyc88e3572011-01-22 00:32:12 -08002987 setupDrawColorFilter();
Romain Guy9ace8f52011-07-07 20:50:11 -07002988 setupDrawBlending(layer->isBlend() || a < 1.0f, layer->getMode(), false);
Romain Guyc88e3572011-01-22 00:32:12 -08002989 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002990 setupDrawPureColorUniforms();
2991 setupDrawColorFilterUniforms();
Romain Guy9ace8f52011-07-07 20:50:11 -07002992 setupDrawTexture(layer->getTexture());
Romain Guy3b753822013-03-05 10:27:35 -08002993 if (CC_LIKELY(currentTransform().isPureTranslate())) {
2994 int tx = (int) floorf(x + currentTransform().getTranslateX() + 0.5f);
2995 int ty = (int) floorf(y + currentTransform().getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002996
Romain Guyd21b6e12011-11-30 20:21:23 -08002997 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002998 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002999 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07003000 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003001 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08003002 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07003003 x + layer->layer.getWidth(), y + layer->layer.getHeight());
3004 }
Romain Guyf219da52011-01-16 12:54:25 -08003005
Romain Guy448455f2013-07-22 13:57:50 -07003006 TextureVertex* mesh = &layer->mesh[0];
3007 GLsizei elementsCount = layer->meshElementCount;
3008
3009 while (elementsCount > 0) {
3010 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
3011
Romain Guy3380cfd2013-08-15 16:57:57 -07003012 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07003013 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
3014 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
3015
3016 elementsCount -= drawCount;
3017 // Though there are 4 vertices in a quad, we use 6 indices per
3018 // quad to draw with GL_TRIANGLES
3019 mesh += (drawCount / 6) * 4;
3020 }
Romain Guyf219da52011-01-16 12:54:25 -08003021
Romain Guy3a3133d2011-02-01 22:59:58 -08003022#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07003023 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08003024#endif
Romain Guyc88e3572011-01-22 00:32:12 -08003025 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07003026
Chris Craikc3566d02013-02-04 16:16:33 -08003027 mDrawModifiers.mColorFilter = oldFilter;
Romain Guye529ece2012-09-26 11:23:17 -07003028
Romain Guy5bb3c732012-11-29 17:52:58 -08003029 if (layer->debugDrawUpdate) {
3030 layer->debugDrawUpdate = false;
Romain Guy4ff0cf42012-08-06 14:51:10 -07003031 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(),
3032 0x7f00ff00, SkXfermode::kSrcOver_Mode);
3033 }
Romain Guyf219da52011-01-16 12:54:25 -08003034 }
Chris Craik34416ea2013-04-15 16:08:28 -07003035 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07003036
Romain Guyb2e2f242012-10-17 18:18:35 -07003037 if (transform && !transform->isIdentity()) {
3038 restore();
3039 }
3040
Chet Haase48659092012-05-31 15:21:51 -07003041 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08003042}
3043
Romain Guy6926c722010-07-12 20:20:03 -07003044///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07003045// Shaders
3046///////////////////////////////////////////////////////////////////////////////
3047
3048void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08003049 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003050}
3051
Romain Guy06f96e22010-07-30 19:18:16 -07003052void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003053 mDrawModifiers.mShader = shader;
3054 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003055 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003056 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003057}
3058
Romain Guyd27977d2010-07-14 19:18:51 -07003059///////////////////////////////////////////////////////////////////////////////
Romain Guydb1938e2010-08-02 18:50:22 -07003060// Color filters
3061///////////////////////////////////////////////////////////////////////////////
3062
3063void OpenGLRenderer::resetColorFilter() {
Chris Craikc3566d02013-02-04 16:16:33 -08003064 mDrawModifiers.mColorFilter = NULL;
Romain Guydb1938e2010-08-02 18:50:22 -07003065}
3066
3067void OpenGLRenderer::setupColorFilter(SkiaColorFilter* filter) {
Chris Craikc3566d02013-02-04 16:16:33 -08003068 mDrawModifiers.mColorFilter = filter;
Romain Guydb1938e2010-08-02 18:50:22 -07003069}
3070
3071///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -07003072// Drop shadow
3073///////////////////////////////////////////////////////////////////////////////
3074
3075void OpenGLRenderer::resetShadow() {
Chris Craikc3566d02013-02-04 16:16:33 -08003076 mDrawModifiers.mHasShadow = false;
Romain Guy1e45aae2010-08-13 19:39:53 -07003077}
3078
3079void OpenGLRenderer::setupShadow(float radius, float dx, float dy, int color) {
Chris Craikc3566d02013-02-04 16:16:33 -08003080 mDrawModifiers.mHasShadow = true;
3081 mDrawModifiers.mShadowRadius = radius;
3082 mDrawModifiers.mShadowDx = dx;
3083 mDrawModifiers.mShadowDy = dy;
3084 mDrawModifiers.mShadowColor = color;
Romain Guy1e45aae2010-08-13 19:39:53 -07003085}
3086
3087///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003088// Draw filters
3089///////////////////////////////////////////////////////////////////////////////
3090
3091void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003092 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3093 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003094 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003095 mDrawModifiers.mPaintFilterClearBits = 0;
3096 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003097}
3098
3099void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003100 mDrawModifiers.mHasDrawFilter = true;
3101 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3102 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003103}
3104
Chris Craika08f95c2013-03-15 17:24:33 -07003105SkPaint* OpenGLRenderer::filterPaint(SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003106 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003107 return paint;
3108 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003109
3110 uint32_t flags = paint->getFlags();
3111
3112 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003113 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3114 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003115
3116 return &mFilteredPaint;
3117}
3118
3119///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003120// Drawing implementation
3121///////////////////////////////////////////////////////////////////////////////
3122
Romain Guy3b748a42013-04-17 18:54:38 -07003123Texture* OpenGLRenderer::getTexture(SkBitmap* bitmap) {
3124 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3125 if (!texture) {
3126 return mCaches.textureCache.get(bitmap);
3127 }
3128 return texture;
3129}
3130
Romain Guy01d58e42011-01-19 21:54:02 -08003131void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
3132 float x, float y, SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003133 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003134 return;
3135 }
3136
3137 int alpha;
3138 SkXfermode::Mode mode;
3139 getAlphaAndMode(paint, &alpha, &mode);
3140
3141 setupDraw();
3142 setupDrawWithTexture(true);
3143 setupDrawAlpha8Color(paint->getColor(), alpha);
3144 setupDrawColorFilter();
3145 setupDrawShader();
3146 setupDrawBlending(true, mode);
3147 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003148 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3149 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003150 setupDrawTexture(texture->id);
3151 setupDrawPureColorUniforms();
3152 setupDrawColorFilterUniforms();
3153 setupDrawShaderUniforms();
3154 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3155
3156 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003157}
3158
Romain Guyf607bdc2010-09-10 19:20:06 -07003159// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003160#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3161#define kStdUnderline_Offset (1.0f / 9.0f)
3162#define kStdUnderline_Thickness (1.0f / 18.0f)
3163
Chris Craike63f7c622013-10-17 10:30:55 -07003164void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y, SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003165 // Handle underline and strike-through
3166 uint32_t flags = paint->getFlags();
3167 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003168 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003169
Romain Guy211370f2012-02-01 16:10:55 -08003170 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003171 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003172 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003173
Raph Levien8b4072d2012-07-30 15:50:00 -07003174 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003175 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003176
Romain Guyf6834472011-01-23 13:32:12 -08003177 int linesCount = 0;
3178 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3179 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3180
3181 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003182 float points[pointsCount];
3183 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003184
3185 if (flags & SkPaint::kUnderlineText_Flag) {
3186 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003187 points[currentPoint++] = left;
3188 points[currentPoint++] = top;
3189 points[currentPoint++] = left + underlineWidth;
3190 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003191 }
3192
3193 if (flags & SkPaint::kStrikeThruText_Flag) {
3194 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003195 points[currentPoint++] = left;
3196 points[currentPoint++] = top;
3197 points[currentPoint++] = left + underlineWidth;
3198 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003199 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003200
Romain Guy726aeba2011-06-01 14:52:00 -07003201 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003202
Romain Guy726aeba2011-06-01 14:52:00 -07003203 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003204 }
3205 }
3206}
3207
Romain Guy672433d2013-01-04 19:05:13 -08003208status_t OpenGLRenderer::drawRects(const float* rects, int count, SkPaint* paint) {
3209 if (mSnapshot->isIgnored()) {
3210 return DrawGlInfo::kStatusDone;
3211 }
3212
Romain Guy735738c2012-12-03 12:34:51 -08003213 int color = paint->getColor();
3214 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003215 if (mDrawModifiers.mShader) {
Romain Guy735738c2012-12-03 12:34:51 -08003216 color |= 0x00ffffff;
3217 }
3218 SkXfermode::Mode mode = getXfermode(paint->getXfermode());
3219
3220 return drawColorRects(rects, count, color, mode);
3221}
3222
Chris Craikf57776b2013-10-25 18:30:17 -07003223status_t OpenGLRenderer::drawShadow(const mat4& casterTransform, float casterAlpha,
3224 float width, float height) {
3225 if (mSnapshot->isIgnored()) return DrawGlInfo::kStatusDone;
3226
3227 // For now, always and scissor
3228 // TODO: use quickReject
3229 mCaches.enableScissor();
3230
3231 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003232 paint.setColor(mCaches.propertyShadowStrength << 24);
3233 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003234
ztenghui55bfb4e2013-12-03 10:38:55 -08003235 VertexBuffer shadowVertexBuffer;
3236 ShadowTessellator::tessellateAmbientShadow(width, height, casterTransform,
3237 shadowVertexBuffer);
3238 return drawVertexBuffer(shadowVertexBuffer, &paint);
Chris Craikf57776b2013-10-25 18:30:17 -07003239}
3240
Romain Guy735738c2012-12-03 12:34:51 -08003241status_t OpenGLRenderer::drawColorRects(const float* rects, int count, int color,
Romain Guy3bbacf22013-02-06 16:51:04 -08003242 SkXfermode::Mode mode, bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003243 if (count == 0) {
3244 return DrawGlInfo::kStatusDone;
3245 }
Romain Guy735738c2012-12-03 12:34:51 -08003246
Romain Guy672433d2013-01-04 19:05:13 -08003247 float left = FLT_MAX;
3248 float top = FLT_MAX;
3249 float right = FLT_MIN;
3250 float bottom = FLT_MIN;
3251
Romain Guy448455f2013-07-22 13:57:50 -07003252 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003253 Vertex* vertex = mesh;
3254
Chris Craik2af46352012-11-26 18:30:17 -08003255 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003256 float l = rects[index + 0];
3257 float t = rects[index + 1];
3258 float r = rects[index + 2];
3259 float b = rects[index + 3];
3260
Romain Guy3b753822013-03-05 10:27:35 -08003261 Vertex::set(vertex++, l, t);
3262 Vertex::set(vertex++, r, t);
3263 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003264 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003265
Romain Guy3b753822013-03-05 10:27:35 -08003266 left = fminf(left, l);
3267 top = fminf(top, t);
3268 right = fmaxf(right, r);
3269 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003270 }
3271
Chris Craikf0a59072013-11-19 18:00:46 -08003272 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003273 return DrawGlInfo::kStatusDone;
3274 }
Romain Guy672433d2013-01-04 19:05:13 -08003275
Romain Guy672433d2013-01-04 19:05:13 -08003276 setupDraw();
3277 setupDrawNoTexture();
3278 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
3279 setupDrawShader();
3280 setupDrawColorFilter();
3281 setupDrawBlending(mode);
3282 setupDrawProgram();
3283 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003284 setupDrawModelView(kModelViewMode_Translate, false,
3285 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003286 setupDrawColorUniforms();
3287 setupDrawShaderUniforms();
3288 setupDrawColorFilterUniforms();
Romain Guy672433d2013-01-04 19:05:13 -08003289
Romain Guy8ce00302013-01-15 18:51:42 -08003290 if (dirty && hasLayer()) {
Romain Guy3b753822013-03-05 10:27:35 -08003291 dirtyLayer(left, top, right, bottom, currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003292 }
3293
Chris Craik4063a0e2013-11-15 16:06:56 -08003294 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003295
3296 return DrawGlInfo::kStatusDrew;
3297}
3298
Romain Guy026c5e162010-06-28 17:12:22 -07003299void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Romain Guy1c740bc2010-09-13 18:00:09 -07003300 int color, SkXfermode::Mode mode, bool ignoreTransform) {
Romain Guyd27977d2010-07-14 19:18:51 -07003301 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003302 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003303 color |= 0x00ffffff;
3304 }
3305
Romain Guy70ca14e2010-12-13 18:24:33 -08003306 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003307 setupDrawNoTexture();
Romain Guy9c0b1882012-07-13 12:13:07 -07003308 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003309 setupDrawShader();
3310 setupDrawColorFilter();
3311 setupDrawBlending(mode);
3312 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003313 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3314 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003315 setupDrawColorUniforms();
3316 setupDrawShaderUniforms(ignoreTransform);
3317 setupDrawColorFilterUniforms();
3318 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003319
Romain Guyc95c8d62010-09-17 15:31:32 -07003320 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3321}
3322
Romain Guy82ba8142010-07-09 13:25:56 -07003323void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guy8164c2d2010-10-25 18:03:28 -07003324 Texture* texture, SkPaint* paint) {
Romain Guy82ba8142010-07-09 13:25:56 -07003325 int alpha;
3326 SkXfermode::Mode mode;
3327 getAlphaAndMode(paint, &alpha, &mode);
3328
Romain Guyd21b6e12011-11-30 20:21:23 -08003329 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003330
Romain Guy3b748a42013-04-17 18:54:38 -07003331 GLvoid* vertices = (GLvoid*) NULL;
3332 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3333
3334 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003335 vertices = &mMeshVertices[0].x;
3336 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003337
3338 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3339 texture->uvMapper->map(uvs);
3340
3341 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3342 }
3343
Romain Guy3b753822013-03-05 10:27:35 -08003344 if (CC_LIKELY(currentTransform().isPureTranslate())) {
3345 const float x = (int) floorf(left + currentTransform().getTranslateX() + 0.5f);
3346 const float y = (int) floorf(top + currentTransform().getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003347
Romain Guyd21b6e12011-11-30 20:21:23 -08003348 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003349 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Romain Guy3b748a42013-04-17 18:54:38 -07003350 alpha / 255.0f, mode, texture->blend, vertices, texCoords,
3351 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003352 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08003353 texture->setFilter(FILTER(paint), true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003354 drawTextureMesh(left, top, right, bottom, texture->id, alpha / 255.0f, mode,
Romain Guy3b748a42013-04-17 18:54:38 -07003355 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3356 }
3357
3358 if (texture->uvMapper) {
3359 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003360 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003361}
3362
Romain Guybd6b79b2010-06-26 00:13:53 -07003363void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003364 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend) {
3365 drawTextureMesh(left, top, right, bottom, texture, alpha, mode, blend,
Romain Guy03750a02010-10-18 14:06:08 -07003366 (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset, GL_TRIANGLE_STRIP, gMeshCount);
Romain Guyf7f93552010-07-08 19:17:03 -07003367}
3368
3369void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Romain Guya9794742010-07-13 11:37:54 -07003370 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003371 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003372 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3373 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003374
Romain Guy746b7402010-10-26 16:27:31 -07003375 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003376 setupDrawWithTexture();
3377 setupDrawColor(alpha, alpha, alpha, alpha);
3378 setupDrawColorFilter();
3379 setupDrawBlending(blend, mode, swapSrcDst);
3380 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003381 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003382 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003383 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003384 setupDrawPureColorUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003385 setupDrawColorFilterUniforms();
Romain Guy70ca14e2010-12-13 18:24:33 -08003386 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003387
Romain Guy6820ac82010-09-15 18:11:50 -07003388 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003389}
3390
Romain Guy3b748a42013-04-17 18:54:38 -07003391void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
3392 GLuint texture, float alpha, SkXfermode::Mode mode, bool blend,
3393 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003394 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3395 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003396
3397 setupDraw();
3398 setupDrawWithTexture();
3399 setupDrawColor(alpha, alpha, alpha, alpha);
3400 setupDrawColorFilter();
3401 setupDrawBlending(blend, mode, swapSrcDst);
3402 setupDrawProgram();
3403 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003404 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003405 setupDrawTexture(texture);
3406 setupDrawPureColorUniforms();
3407 setupDrawColorFilterUniforms();
3408 setupDrawMeshIndices(vertices, texCoords, vbo);
3409
3410 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003411}
3412
Romain Guy886b2752013-01-04 12:26:18 -08003413void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
3414 GLuint texture, bool hasColor, int color, int alpha, SkXfermode::Mode mode,
3415 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003416 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003417
3418 setupDraw();
3419 setupDrawWithTexture(true);
3420 if (hasColor) {
3421 setupDrawAlpha8Color(color, alpha);
3422 }
3423 setupDrawColorFilter();
3424 setupDrawShader();
3425 setupDrawBlending(true, mode);
3426 setupDrawProgram();
3427 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003428 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003429 setupDrawTexture(texture);
3430 setupDrawPureColorUniforms();
3431 setupDrawColorFilterUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08003432 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003433 setupDrawMesh(vertices, texCoords);
3434
3435 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003436}
3437
Romain Guya5aed0d2010-09-09 14:42:43 -07003438void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003439 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003440 if (mCountOverdraw) {
3441 if (!mCaches.blend) glEnable(GL_BLEND);
3442 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3443 glBlendFunc(GL_ONE, GL_ONE);
3444 }
3445
3446 mCaches.blend = true;
3447 mCaches.lastSrcMode = GL_ONE;
3448 mCaches.lastDstMode = GL_ONE;
3449
3450 return;
3451 }
3452
Romain Guy82ba8142010-07-09 13:25:56 -07003453 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003454
Romain Guy82ba8142010-07-09 13:25:56 -07003455 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003456 // These blend modes are not supported by OpenGL directly and have
3457 // to be implemented using shaders. Since the shader will perform
3458 // the blending, turn blending off here
3459 // If the blend mode cannot be implemented using shaders, fall
3460 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003461 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003462 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003463 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003464 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003465
Romain Guy82bc7a72012-01-03 14:13:39 -08003466 if (mCaches.blend) {
3467 glDisable(GL_BLEND);
3468 mCaches.blend = false;
3469 }
3470
3471 return;
3472 } else {
3473 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003474 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003475 }
3476
3477 if (!mCaches.blend) {
3478 glEnable(GL_BLEND);
3479 }
3480
3481 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3482 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3483
3484 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3485 glBlendFunc(sourceMode, destMode);
3486 mCaches.lastSrcMode = sourceMode;
3487 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003488 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003489 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003490 glDisable(GL_BLEND);
3491 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003492 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003493}
3494
Romain Guy889f8d12010-07-29 14:37:42 -07003495bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003496 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003497 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003498 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003499 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003500 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003501 }
Romain Guy6926c722010-07-12 20:20:03 -07003502 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003503}
3504
Romain Guy026c5e162010-06-28 17:12:22 -07003505void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003506 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003507 TextureVertex::setUV(v++, u1, v1);
3508 TextureVertex::setUV(v++, u2, v1);
3509 TextureVertex::setUV(v++, u1, v2);
3510 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003511}
3512
Chris Craik16ecda52013-03-29 10:59:59 -07003513void OpenGLRenderer::getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003514 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003515 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3516 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003517 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003518 }
Chet Haasedb8c9a62012-03-21 18:54:18 -07003519 *alpha *= mSnapshot->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003520}
3521
Chris Craik16ecda52013-03-29 10:59:59 -07003522float OpenGLRenderer::getLayerAlpha(Layer* layer) const {
3523 float alpha;
3524 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3525 alpha = mDrawModifiers.mOverrideLayerAlpha;
3526 } else {
3527 alpha = layer->getAlpha() / 255.0f;
3528 }
3529 return alpha * mSnapshot->alpha;
3530}
3531
Romain Guy9d5316e2010-06-24 19:30:36 -07003532}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003533}; // namespace android