blob: 091cdf42c85d1233477dabb56208dfa45b384a89 [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"
Chris Craike4aa95e2014-05-08 13:57:05 -070040#include "utils/GLUtils.h"
Romain Guya957eea2010-12-08 18:34:42 -080041#include "Vector.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080042#include "VertexBuffer.h"
Romain Guye4d01122010-06-16 18:44:05 -070043
44namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070045namespace uirenderer {
46
47///////////////////////////////////////////////////////////////////////////////
48// Defines
49///////////////////////////////////////////////////////////////////////////////
50
Romain Guy759ea802010-09-16 20:49:46 -070051#define RAD_TO_DEG (180.0f / 3.14159265f)
52#define MIN_ANGLE 0.001f
53
Romain Guyf8773082012-07-12 18:01:00 -070054#define ALPHA_THRESHOLD 0
Romain Guydbc26d22010-10-11 17:58:29 -070055
Chris Craik678625242014-02-28 12:26:34 -080056static GLenum getFilter(const SkPaint* paint) {
57 if (!paint || paint->getFilterLevel() != SkPaint::kNone_FilterLevel) {
58 return GL_LINEAR;
59 }
60 return GL_NEAREST;
61}
Romain Guyd21b6e12011-11-30 20:21:23 -080062
Romain Guy9d5316e2010-06-24 19:30:36 -070063///////////////////////////////////////////////////////////////////////////////
64// Globals
65///////////////////////////////////////////////////////////////////////////////
66
Romain Guy889f8d12010-07-29 14:37:42 -070067/**
68 * Structure mapping Skia xfermodes to OpenGL blending factors.
69 */
70struct Blender {
71 SkXfermode::Mode mode;
72 GLenum src;
73 GLenum dst;
74}; // struct Blender
75
Romain Guy026c5e162010-06-28 17:12:22 -070076// In this array, the index of each Blender equals the value of the first
77// entry. For instance, gBlends[1] == gBlends[SkXfermode::kSrc_Mode]
78static const Blender gBlends[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -070079 { SkXfermode::kClear_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
80 { SkXfermode::kSrc_Mode, GL_ONE, GL_ZERO },
81 { SkXfermode::kDst_Mode, GL_ZERO, GL_ONE },
82 { SkXfermode::kSrcOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
83 { SkXfermode::kDstOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
84 { SkXfermode::kSrcIn_Mode, GL_DST_ALPHA, GL_ZERO },
85 { SkXfermode::kDstIn_Mode, GL_ZERO, GL_SRC_ALPHA },
86 { SkXfermode::kSrcOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
87 { SkXfermode::kDstOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
88 { SkXfermode::kSrcATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
89 { SkXfermode::kDstATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
90 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
91 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -050092 { SkXfermode::kModulate_Mode, GL_ZERO, GL_SRC_COLOR },
Romain Guy2ffefd42011-09-08 15:33:03 -070093 { SkXfermode::kScreen_Mode, GL_ONE, GL_ONE_MINUS_SRC_COLOR }
Romain Guy026c5e162010-06-28 17:12:22 -070094};
Romain Guye4d01122010-06-16 18:44:05 -070095
Romain Guy87a76572010-09-13 18:11:21 -070096// This array contains the swapped version of each SkXfermode. For instance
97// this array's SrcOver blending mode is actually DstOver. You can refer to
98// createLayer() for more information on the purpose of this array.
Romain Guyf607bdc2010-09-10 19:20:06 -070099static const Blender gBlendsSwap[] = {
Romain Guy2ffefd42011-09-08 15:33:03 -0700100 { SkXfermode::kClear_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
101 { SkXfermode::kSrc_Mode, GL_ZERO, GL_ONE },
102 { SkXfermode::kDst_Mode, GL_ONE, GL_ZERO },
103 { SkXfermode::kSrcOver_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE },
104 { SkXfermode::kDstOver_Mode, GL_ONE, GL_ONE_MINUS_SRC_ALPHA },
105 { SkXfermode::kSrcIn_Mode, GL_ZERO, GL_SRC_ALPHA },
106 { SkXfermode::kDstIn_Mode, GL_DST_ALPHA, GL_ZERO },
107 { SkXfermode::kSrcOut_Mode, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA },
108 { SkXfermode::kDstOut_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ZERO },
109 { SkXfermode::kSrcATop_Mode, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA },
110 { SkXfermode::kDstATop_Mode, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
111 { SkXfermode::kXor_Mode, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA },
112 { SkXfermode::kPlus_Mode, GL_ONE, GL_ONE },
Derek Sollenbergerd81ec452013-02-04 15:42:26 -0500113 { SkXfermode::kModulate_Mode, GL_DST_COLOR, GL_ZERO },
Romain Guy2ffefd42011-09-08 15:33:03 -0700114 { SkXfermode::kScreen_Mode, GL_ONE_MINUS_DST_COLOR, GL_ONE }
Romain Guyf607bdc2010-09-10 19:20:06 -0700115};
116
Romain Guyf6a11b82010-06-23 17:47:49 -0700117///////////////////////////////////////////////////////////////////////////////
Romain Guy448455f2013-07-22 13:57:50 -0700118// Functions
119///////////////////////////////////////////////////////////////////////////////
120
121template<typename T>
122static inline T min(T a, T b) {
123 return a < b ? a : b;
124}
125
126///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700127// Constructors/destructor
128///////////////////////////////////////////////////////////////////////////////
129
Romain Guy3bbacf22013-02-06 16:51:04 -0800130OpenGLRenderer::OpenGLRenderer():
131 mCaches(Caches::getInstance()), mExtensions(Extensions::getInstance()) {
Chris Craik527a3aa2013-03-04 10:19:31 -0800132 // *set* draw modifiers to be 0
133 memset(&mDrawModifiers, 0, sizeof(mDrawModifiers));
Chris Craik16ecda52013-03-29 10:59:59 -0700134 mDrawModifiers.mOverrideLayerAlpha = 1.0f;
Romain Guy026c5e162010-06-28 17:12:22 -0700135
Romain Guyac670c02010-07-27 17:39:27 -0700136 memcpy(mMeshVertices, gMeshVertices, sizeof(gMeshVertices));
137
Romain Guy96885eb2013-03-26 15:05:58 -0700138 mFrameStarted = false;
Romain Guy78dd96d2013-05-03 14:24:16 -0700139 mCountOverdraw = false;
Romain Guy87e2f7572012-09-24 11:37:12 -0700140
141 mScissorOptimizationDisabled = false;
Romain Guye4d01122010-06-16 18:44:05 -0700142}
143
Romain Guy85bf02f2010-06-22 13:11:24 -0700144OpenGLRenderer::~OpenGLRenderer() {
Romain Guy29d89972010-09-22 16:10:57 -0700145 // The context has already been destroyed at this point, do not call
146 // GL APIs. All GL state should be kept in Caches.h
Romain Guye4d01122010-06-16 18:44:05 -0700147}
148
Romain Guy87e2f7572012-09-24 11:37:12 -0700149void OpenGLRenderer::initProperties() {
150 char property[PROPERTY_VALUE_MAX];
151 if (property_get(PROPERTY_DISABLE_SCISSOR_OPTIMIZATION, property, "false")) {
152 mScissorOptimizationDisabled = !strcasecmp(property, "true");
153 INIT_LOGD(" Scissor optimization %s",
154 mScissorOptimizationDisabled ? "disabled" : "enabled");
155 } else {
156 INIT_LOGD(" Scissor optimization enabled");
157 }
Romain Guy13631f32012-01-30 17:41:55 -0800158}
159
160///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700161// Setup
162///////////////////////////////////////////////////////////////////////////////
163
Romain Guy85bf02f2010-06-22 13:11:24 -0700164void OpenGLRenderer::setViewport(int width, int height) {
Romain Guy35643dd2012-09-18 15:40:58 -0700165 initViewport(width, height);
166
167 glDisable(GL_DITHER);
168 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
169
170 glEnableVertexAttribArray(Program::kBindingPosition);
171}
172
173void OpenGLRenderer::initViewport(int width, int height) {
Chris Craik0c0ec262014-05-09 16:11:14 -0700174 mSnapshot->orthoMatrix.loadOrtho(0, width, height, 0, -1, 1);
Romain Guybb9524b2010-06-22 18:56:38 -0700175
Chris Craik14e51302013-12-30 15:32:54 -0800176 initializeViewport(width, height);
Romain Guye4d01122010-06-16 18:44:05 -0700177}
178
Romain Guy96885eb2013-03-26 15:05:58 -0700179void OpenGLRenderer::setupFrameState(float left, float top,
Romain Guyc3fedaf2013-01-29 17:26:25 -0800180 float right, float bottom, bool opaque) {
Romain Guyfe48f652010-11-11 15:36:56 -0800181 mCaches.clearGarbage();
182
Chris Craik14e51302013-12-30 15:32:54 -0800183 initializeSaveStack(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700184 mOpaque = opaque;
Chris Craik5f803622013-03-21 14:39:04 -0700185 mTilingClip.set(left, top, right, bottom);
Romain Guy96885eb2013-03-26 15:05:58 -0700186}
187
188status_t OpenGLRenderer::startFrame() {
189 if (mFrameStarted) return DrawGlInfo::kStatusDone;
190 mFrameStarted = true;
191
Romain Guy41308e22012-10-22 20:02:43 -0700192 mDirtyClip = true;
Romain Guyddf74372012-05-22 14:07:07 -0700193
Romain Guy96885eb2013-03-26 15:05:58 -0700194 discardFramebuffer(mTilingClip.left, mTilingClip.top, mTilingClip.right, mTilingClip.bottom);
Romain Guy11cb6422012-09-21 00:39:43 -0700195
Chris Craik14e51302013-12-30 15:32:54 -0800196 glViewport(0, 0, getWidth(), getHeight());
Romain Guy7d7b5492011-01-24 16:33:45 -0800197
Romain Guy54c1a642012-09-27 17:55:46 -0700198 // Functors break the tiling extension in pretty spectacular ways
199 // This ensures we don't use tiling when a functor is going to be
200 // invoked during the frame
201 mSuppressTiling = mCaches.hasRegisteredFunctors();
202
Chris Craikd6b65f62014-01-01 14:45:21 -0800203 startTilingCurrentClip(true);
Romain Guy2b7028e2012-09-19 17:25:38 -0700204
Romain Guy7c450aa2012-09-21 19:15:00 -0700205 debugOverdraw(true, true);
206
Romain Guy96885eb2013-03-26 15:05:58 -0700207 return clear(mTilingClip.left, mTilingClip.top,
208 mTilingClip.right, mTilingClip.bottom, mOpaque);
209}
210
Romain Guy96885eb2013-03-26 15:05:58 -0700211status_t OpenGLRenderer::prepareDirty(float left, float top,
212 float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700213
Romain Guy96885eb2013-03-26 15:05:58 -0700214 setupFrameState(left, top, right, bottom, opaque);
215
216 // Layer renderers will start the frame immediately
217 // The framebuffer renderer will first defer the display list
218 // for each layer and wait until the first drawing command
219 // to start the frame
Chris Craikd6b65f62014-01-01 14:45:21 -0800220 if (currentSnapshot()->fbo == 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700221 syncState();
222 updateLayers();
223 } else {
224 return startFrame();
225 }
226
227 return DrawGlInfo::kStatusDone;
Romain Guy7c25aab2012-10-18 15:05:02 -0700228}
229
Romain Guydcfc8362013-01-03 13:08:57 -0800230void OpenGLRenderer::discardFramebuffer(float left, float top, float right, float bottom) {
231 // If we know that we are going to redraw the entire framebuffer,
232 // perform a discard to let the driver know we don't need to preserve
233 // the back buffer for this frame.
Romain Guy3bbacf22013-02-06 16:51:04 -0800234 if (mExtensions.hasDiscardFramebuffer() &&
Chris Craik14e51302013-12-30 15:32:54 -0800235 left <= 0.0f && top <= 0.0f && right >= getWidth() && bottom >= getHeight()) {
Romain Guyf1581982013-01-31 17:20:30 -0800236 const bool isFbo = getTargetFbo() == 0;
237 const GLenum attachments[] = {
238 isFbo ? (const GLenum) GL_COLOR_EXT : (const GLenum) GL_COLOR_ATTACHMENT0,
239 isFbo ? (const GLenum) GL_STENCIL_EXT : (const GLenum) GL_STENCIL_ATTACHMENT };
Romain Guydcfc8362013-01-03 13:08:57 -0800240 glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
241 }
242}
243
Romain Guy7c25aab2012-10-18 15:05:02 -0700244status_t OpenGLRenderer::clear(float left, float top, float right, float bottom, bool opaque) {
Romain Guy78dd96d2013-05-03 14:24:16 -0700245 if (!opaque || mCountOverdraw) {
Romain Guy586cae32012-07-13 15:28:31 -0700246 mCaches.enableScissor();
Chris Craikd6b65f62014-01-01 14:45:21 -0800247 mCaches.setScissor(left, currentSnapshot()->height - bottom, right - left, bottom - top);
Romain Guy6b7bd242010-10-06 19:49:23 -0700248 glClear(GL_COLOR_BUFFER_BIT);
Chet Haase44b2fe32012-06-06 19:03:58 -0700249 return DrawGlInfo::kStatusDrew;
Romain Guyddf74372012-05-22 14:07:07 -0700250 }
Chet Haase44b2fe32012-06-06 19:03:58 -0700251
Romain Guy7c25aab2012-10-18 15:05:02 -0700252 mCaches.resetScissor();
Chet Haase44b2fe32012-06-06 19:03:58 -0700253 return DrawGlInfo::kStatusDone;
Romain Guyddf74372012-05-22 14:07:07 -0700254}
255
256void OpenGLRenderer::syncState() {
Romain Guyddf74372012-05-22 14:07:07 -0700257 if (mCaches.blend) {
258 glEnable(GL_BLEND);
259 } else {
260 glDisable(GL_BLEND);
Romain Guy6b7bd242010-10-06 19:49:23 -0700261 }
Romain Guybb9524b2010-06-22 18:56:38 -0700262}
263
Chris Craikd6b65f62014-01-01 14:45:21 -0800264void OpenGLRenderer::startTilingCurrentClip(bool opaque) {
Romain Guy54c1a642012-09-27 17:55:46 -0700265 if (!mSuppressTiling) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800266 const Snapshot* snapshot = currentSnapshot();
267
Chris Craik14e51302013-12-30 15:32:54 -0800268 const Rect* clip = &mTilingClip;
Chris Craikd6b65f62014-01-01 14:45:21 -0800269 if (snapshot->flags & Snapshot::kFlagFboTarget) {
270 clip = &(snapshot->layer->clipRect);
Romain Guy54c1a642012-09-27 17:55:46 -0700271 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700272
Chris Craikd6b65f62014-01-01 14:45:21 -0800273 startTiling(*clip, snapshot->height, opaque);
Romain Guyc3fedaf2013-01-29 17:26:25 -0800274 }
275}
276
277void OpenGLRenderer::startTiling(const Rect& clip, int windowHeight, bool opaque) {
278 if (!mSuppressTiling) {
279 mCaches.startTiling(clip.left, windowHeight - clip.bottom,
Romain Guy52036b12013-02-14 18:03:37 -0800280 clip.right - clip.left, clip.bottom - clip.top, opaque);
Romain Guy54c1a642012-09-27 17:55:46 -0700281 }
Romain Guy2b7028e2012-09-19 17:25:38 -0700282}
283
284void OpenGLRenderer::endTiling() {
Romain Guy54c1a642012-09-27 17:55:46 -0700285 if (!mSuppressTiling) mCaches.endTiling();
Romain Guy2b7028e2012-09-19 17:25:38 -0700286}
287
Romain Guyb025b9c2010-09-16 14:16:48 -0700288void OpenGLRenderer::finish() {
Romain Guy7c450aa2012-09-21 19:15:00 -0700289 renderOverdraw();
Romain Guy2b7028e2012-09-19 17:25:38 -0700290 endTiling();
291
Romain Guyca89e2a2013-03-08 17:44:20 -0800292 // When finish() is invoked on FBO 0 we've reached the end
293 // of the current frame
294 if (getTargetFbo() == 0) {
295 mCaches.pathCache.trim();
296 }
297
Romain Guy11cb6422012-09-21 00:39:43 -0700298 if (!suppressErrorChecks()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700299#if DEBUG_OPENGL
Chris Craike4aa95e2014-05-08 13:57:05 -0700300 GLUtils::dumpGLErrors();
Romain Guyb025b9c2010-09-16 14:16:48 -0700301#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700302
Romain Guyc15008e2010-11-10 11:59:15 -0800303#if DEBUG_MEMORY_USAGE
Romain Guye190aa62010-11-10 19:01:29 -0800304 mCaches.dumpMemoryUsage();
Romain Guy11cb6422012-09-21 00:39:43 -0700305#else
306 if (mCaches.getDebugLevel() & kDebugMemory) {
307 mCaches.dumpMemoryUsage();
308 }
Romain Guyc15008e2010-11-10 11:59:15 -0800309#endif
Romain Guy11cb6422012-09-21 00:39:43 -0700310 }
Romain Guy96885eb2013-03-26 15:05:58 -0700311
Romain Guy78dd96d2013-05-03 14:24:16 -0700312 if (mCountOverdraw) {
313 countOverdraw();
314 }
315
Romain Guy96885eb2013-03-26 15:05:58 -0700316 mFrameStarted = false;
Romain Guyb025b9c2010-09-16 14:16:48 -0700317}
318
Romain Guy6c319ca2011-01-11 14:29:25 -0800319void OpenGLRenderer::interrupt() {
Romain Guyda8532c2010-08-31 11:50:35 -0700320 if (mCaches.currentProgram) {
321 if (mCaches.currentProgram->isInUse()) {
322 mCaches.currentProgram->remove();
323 mCaches.currentProgram = NULL;
324 }
325 }
Chris Craik9ab2d182013-07-22 16:16:06 -0700326 mCaches.resetActiveTexture();
Romain Guy50c0f092010-10-19 11:42:22 -0700327 mCaches.unbindMeshBuffer();
Romain Guy15bc6432011-12-13 13:11:32 -0800328 mCaches.unbindIndicesBuffer();
Romain Guyf3a910b42011-12-12 20:35:21 -0800329 mCaches.resetVertexPointers();
Romain Guyff316ec2013-02-13 18:39:43 -0800330 mCaches.disableTexCoordsVertexArray();
Romain Guy7c450aa2012-09-21 19:15:00 -0700331 debugOverdraw(false, false);
Romain Guyda8532c2010-08-31 11:50:35 -0700332}
333
Romain Guy6c319ca2011-01-11 14:29:25 -0800334void OpenGLRenderer::resume() {
Chris Craikd6b65f62014-01-01 14:45:21 -0800335 const Snapshot* snapshot = currentSnapshot();
336 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
337 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700338 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700339
Romain Guy3e263fa2011-12-12 16:47:48 -0800340 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
341
Chet Haase80250612012-08-15 13:46:54 -0700342 mCaches.scissorEnabled = glIsEnabled(GL_SCISSOR_TEST);
Romain Guy586cae32012-07-13 15:28:31 -0700343 mCaches.enableScissor();
Romain Guy82bc7a72012-01-03 14:13:39 -0800344 mCaches.resetScissor();
Romain Guy746b7402010-10-26 16:27:31 -0700345 dirtyClip();
Romain Guyda8532c2010-08-31 11:50:35 -0700346
Romain Guya1d3c912011-12-13 14:55:06 -0800347 mCaches.activeTexture(0);
Romain Guy8aa195d2013-06-04 18:00:09 -0700348 mCaches.resetBoundTextures();
Romain Guyf607bdc2010-09-10 19:20:06 -0700349
Romain Guy50c0f092010-10-19 11:42:22 -0700350 mCaches.blend = true;
351 glEnable(GL_BLEND);
352 glBlendFunc(mCaches.lastSrcMode, mCaches.lastDstMode);
353 glBlendEquation(GL_FUNC_ADD);
Romain Guyda8532c2010-08-31 11:50:35 -0700354}
355
Romain Guy35643dd2012-09-18 15:40:58 -0700356void OpenGLRenderer::resumeAfterLayer() {
Chris Craikd6b65f62014-01-01 14:45:21 -0800357 const Snapshot* snapshot = currentSnapshot();
358 glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());
359 glBindFramebuffer(GL_FRAMEBUFFER, snapshot->fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700360 debugOverdraw(true, false);
Romain Guy35643dd2012-09-18 15:40:58 -0700361
362 mCaches.resetScissor();
363 dirtyClip();
364}
365
Romain Guy8f3b8e32012-03-27 16:33:45 -0700366status_t OpenGLRenderer::callDrawGLFunction(Functor* functor, Rect& dirty) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800367 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craik408eb122013-03-26 18:55:15 -0700368
Chris Craikd6b65f62014-01-01 14:45:21 -0800369 Rect clip(*currentClipRect());
Romain Guy80911b82011-03-16 15:30:12 -0700370 clip.snapToPixelBoundaries();
371
Romain Guyd643bb52011-03-01 14:55:21 -0800372 // Since we don't know what the functor will draw, let's dirty
Chris Craikd6b65f62014-01-01 14:45:21 -0800373 // the entire clip region
Romain Guyd643bb52011-03-01 14:55:21 -0800374 if (hasLayer()) {
Romain Guyd643bb52011-03-01 14:55:21 -0800375 dirtyLayerUnchecked(clip, getRegion());
376 }
Romain Guyd643bb52011-03-01 14:55:21 -0800377
Romain Guy08aa2cb2011-03-17 11:06:57 -0700378 DrawGlInfo info;
379 info.clipLeft = clip.left;
380 info.clipTop = clip.top;
381 info.clipRight = clip.right;
382 info.clipBottom = clip.bottom;
383 info.isLayer = hasLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800384 info.width = currentSnapshot()->viewport.getWidth();
385 info.height = currentSnapshot()->height;
386 currentTransform()->copyTo(&info.transform[0]);
Romain Guy80911b82011-03-16 15:30:12 -0700387
John Reck25d2f7b2013-09-10 13:12:09 -0700388 bool dirtyClip = mDirtyClip;
Chris Craik54f574a2013-08-26 11:23:46 -0700389 // setup GL state for functor
390 if (mDirtyClip) {
Chris Craik54f574a2013-08-26 11:23:46 -0700391 setStencilFromClip(); // can issue draws, so must precede enableScissor()/interrupt()
392 }
John Reck25d2f7b2013-09-10 13:12:09 -0700393 if (mCaches.enableScissor() || dirtyClip) {
394 setScissorFromClip();
395 }
Chris Craik54f574a2013-08-26 11:23:46 -0700396 interrupt();
397
398 // call functor immediately after GL state setup
John Reck750ca6d2014-03-28 16:33:18 -0700399 (*functor)(DrawGlInfo::kModeDraw, &info);
Romain Guycabfcc12011-03-07 18:06:46 -0800400
Chet Haasedaf98e92011-01-10 14:10:36 -0800401 resume();
John Reck750ca6d2014-03-28 16:33:18 -0700402 return DrawGlInfo::kStatusDrew;
Chet Haasedaf98e92011-01-10 14:10:36 -0800403}
404
Romain Guyf6a11b82010-06-23 17:47:49 -0700405///////////////////////////////////////////////////////////////////////////////
Romain Guy87e2f7572012-09-24 11:37:12 -0700406// Debug
407///////////////////////////////////////////////////////////////////////////////
408
Romain Guy0f6675332013-03-01 14:31:04 -0800409void OpenGLRenderer::eventMark(const char* name) const {
410 mCaches.eventMark(0, name);
411}
412
Romain Guy87e2f7572012-09-24 11:37:12 -0700413void OpenGLRenderer::startMark(const char* name) const {
414 mCaches.startMark(0, name);
415}
416
417void OpenGLRenderer::endMark() const {
418 mCaches.endMark();
419}
420
421void OpenGLRenderer::debugOverdraw(bool enable, bool clear) {
422 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
423 if (clear) {
424 mCaches.disableScissor();
425 mCaches.stencil.clear();
426 }
427 if (enable) {
428 mCaches.stencil.enableDebugWrite();
429 } else {
430 mCaches.stencil.disable();
431 }
432 }
433}
434
435void OpenGLRenderer::renderOverdraw() {
436 if (mCaches.debugOverdraw && getTargetFbo() == 0) {
Chris Craik5f803622013-03-21 14:39:04 -0700437 const Rect* clip = &mTilingClip;
Romain Guy87e2f7572012-09-24 11:37:12 -0700438
439 mCaches.enableScissor();
Chris Craikd6b65f62014-01-01 14:45:21 -0800440 mCaches.setScissor(clip->left, firstSnapshot()->height - clip->bottom,
Romain Guy87e2f7572012-09-24 11:37:12 -0700441 clip->right - clip->left, clip->bottom - clip->top);
442
Romain Guy627c6fd2013-08-21 11:53:18 -0700443 // 1x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700444 mCaches.stencil.enableDebugTest(2);
Romain Guy627c6fd2013-08-21 11:53:18 -0700445 drawColor(mCaches.getOverdrawColor(1), SkXfermode::kSrcOver_Mode);
446
447 // 2x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700448 mCaches.stencil.enableDebugTest(3);
Romain Guy627c6fd2013-08-21 11:53:18 -0700449 drawColor(mCaches.getOverdrawColor(2), SkXfermode::kSrcOver_Mode);
450
451 // 3x overdraw
Romain Guy87e2f7572012-09-24 11:37:12 -0700452 mCaches.stencil.enableDebugTest(4);
Romain Guy627c6fd2013-08-21 11:53:18 -0700453 drawColor(mCaches.getOverdrawColor(3), SkXfermode::kSrcOver_Mode);
454
455 // 4x overdraw and higher
Romain Guy87e2f7572012-09-24 11:37:12 -0700456 mCaches.stencil.enableDebugTest(4, true);
Romain Guy627c6fd2013-08-21 11:53:18 -0700457 drawColor(mCaches.getOverdrawColor(4), SkXfermode::kSrcOver_Mode);
458
Romain Guy87e2f7572012-09-24 11:37:12 -0700459 mCaches.stencil.disable();
460 }
461}
462
Romain Guy78dd96d2013-05-03 14:24:16 -0700463void OpenGLRenderer::countOverdraw() {
Chris Craik14e51302013-12-30 15:32:54 -0800464 size_t count = getWidth() * getHeight();
Romain Guy78dd96d2013-05-03 14:24:16 -0700465 uint32_t* buffer = new uint32_t[count];
Chris Craik14e51302013-12-30 15:32:54 -0800466 glReadPixels(0, 0, getWidth(), getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &buffer[0]);
Romain Guy78dd96d2013-05-03 14:24:16 -0700467
468 size_t total = 0;
469 for (size_t i = 0; i < count; i++) {
470 total += buffer[i] & 0xff;
471 }
472
473 mOverdraw = total / float(count);
474
475 delete[] buffer;
476}
477
Romain Guy87e2f7572012-09-24 11:37:12 -0700478///////////////////////////////////////////////////////////////////////////////
Romain Guy11cb6422012-09-21 00:39:43 -0700479// Layers
480///////////////////////////////////////////////////////////////////////////////
481
482bool OpenGLRenderer::updateLayer(Layer* layer, bool inFrame) {
Romain Guy96885eb2013-03-26 15:05:58 -0700483 if (layer->deferredUpdateScheduled && layer->renderer &&
John Reck087bc0c2014-04-04 16:20:08 -0700484 layer->displayList.get() && layer->displayList->isRenderable()) {
Romain Guy40543602013-06-12 15:31:28 -0700485 ATRACE_CALL();
486
Romain Guy11cb6422012-09-21 00:39:43 -0700487 Rect& dirty = layer->dirtyRect;
488
Romain Guy7c450aa2012-09-21 19:15:00 -0700489 if (inFrame) {
490 endTiling();
491 debugOverdraw(false, false);
492 }
Romain Guy11cb6422012-09-21 00:39:43 -0700493
Romain Guy96885eb2013-03-26 15:05:58 -0700494 if (CC_UNLIKELY(inFrame || mCaches.drawDeferDisabled)) {
Romain Guy02b49b72013-03-29 12:37:16 -0700495 layer->render();
Romain Guy96885eb2013-03-26 15:05:58 -0700496 } else {
497 layer->defer();
498 }
Romain Guy11cb6422012-09-21 00:39:43 -0700499
500 if (inFrame) {
501 resumeAfterLayer();
Chris Craikd6b65f62014-01-01 14:45:21 -0800502 startTilingCurrentClip();
Romain Guy11cb6422012-09-21 00:39:43 -0700503 }
504
Romain Guy5bb3c732012-11-29 17:52:58 -0800505 layer->debugDrawUpdate = mCaches.debugLayersUpdates;
Chris Craik34416ea2013-04-15 16:08:28 -0700506 layer->hasDrawnSinceUpdate = false;
Romain Guy11cb6422012-09-21 00:39:43 -0700507
508 return true;
509 }
510
511 return false;
512}
513
514void OpenGLRenderer::updateLayers() {
Romain Guy96885eb2013-03-26 15:05:58 -0700515 // If draw deferring is enabled this method will simply defer
516 // the display list of each individual layer. The layers remain
517 // in the layer updates list which will be cleared by flushLayers().
Romain Guy11cb6422012-09-21 00:39:43 -0700518 int count = mLayerUpdates.size();
519 if (count > 0) {
Romain Guy96885eb2013-03-26 15:05:58 -0700520 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
521 startMark("Layer Updates");
522 } else {
523 startMark("Defer Layer Updates");
524 }
Romain Guy11cb6422012-09-21 00:39:43 -0700525
Chris Craik1206b9b2013-04-04 14:46:24 -0700526 // Note: it is very important to update the layers in order
527 for (int i = 0; i < count; i++) {
Romain Guy11cb6422012-09-21 00:39:43 -0700528 Layer* layer = mLayerUpdates.itemAt(i);
529 updateLayer(layer, false);
Romain Guy96885eb2013-03-26 15:05:58 -0700530 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
531 mCaches.resourceCache.decrementRefcount(layer);
532 }
Romain Guy11cb6422012-09-21 00:39:43 -0700533 }
Romain Guy11cb6422012-09-21 00:39:43 -0700534
Romain Guy96885eb2013-03-26 15:05:58 -0700535 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
536 mLayerUpdates.clear();
537 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
538 }
539 endMark();
540 }
541}
542
543void OpenGLRenderer::flushLayers() {
544 int count = mLayerUpdates.size();
545 if (count > 0) {
546 startMark("Apply Layer Updates");
547 char layerName[12];
548
Chris Craik1206b9b2013-04-04 14:46:24 -0700549 // Note: it is very important to update the layers in order
550 for (int i = 0; i < count; i++) {
Romain Guy96885eb2013-03-26 15:05:58 -0700551 sprintf(layerName, "Layer #%d", i);
Romain Guy02b49b72013-03-29 12:37:16 -0700552 startMark(layerName);
553
Romain Guy40543602013-06-12 15:31:28 -0700554 ATRACE_BEGIN("flushLayer");
Romain Guy02b49b72013-03-29 12:37:16 -0700555 Layer* layer = mLayerUpdates.itemAt(i);
556 layer->flush();
Romain Guy40543602013-06-12 15:31:28 -0700557 ATRACE_END();
558
Romain Guy02b49b72013-03-29 12:37:16 -0700559 mCaches.resourceCache.decrementRefcount(layer);
560
Romain Guy96885eb2013-03-26 15:05:58 -0700561 endMark();
562 }
563
564 mLayerUpdates.clear();
Romain Guy11cb6422012-09-21 00:39:43 -0700565 glBindFramebuffer(GL_FRAMEBUFFER, getTargetFbo());
Romain Guy96885eb2013-03-26 15:05:58 -0700566
Romain Guy11cb6422012-09-21 00:39:43 -0700567 endMark();
568 }
569}
570
571void OpenGLRenderer::pushLayerUpdate(Layer* layer) {
572 if (layer) {
Romain Guy02b49b72013-03-29 12:37:16 -0700573 // Make sure we don't introduce duplicates.
574 // SortedVector would do this automatically but we need to respect
575 // the insertion order. The linear search is not an issue since
576 // this list is usually very short (typically one item, at most a few)
577 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
578 if (mLayerUpdates.itemAt(i) == layer) {
579 return;
580 }
581 }
Romain Guy11cb6422012-09-21 00:39:43 -0700582 mLayerUpdates.push_back(layer);
583 mCaches.resourceCache.incrementRefcount(layer);
584 }
585}
586
Romain Guye93482f2013-06-17 13:14:51 -0700587void OpenGLRenderer::cancelLayerUpdate(Layer* layer) {
588 if (layer) {
589 for (int i = mLayerUpdates.size() - 1; i >= 0; i--) {
590 if (mLayerUpdates.itemAt(i) == layer) {
591 mLayerUpdates.removeAt(i);
592 mCaches.resourceCache.decrementRefcount(layer);
593 break;
594 }
595 }
596 }
597}
598
Romain Guy11cb6422012-09-21 00:39:43 -0700599void OpenGLRenderer::clearLayerUpdates() {
600 size_t count = mLayerUpdates.size();
601 if (count > 0) {
602 mCaches.resourceCache.lock();
603 for (size_t i = 0; i < count; i++) {
604 mCaches.resourceCache.decrementRefcountLocked(mLayerUpdates.itemAt(i));
605 }
606 mCaches.resourceCache.unlock();
607 mLayerUpdates.clear();
608 }
609}
610
Romain Guy40543602013-06-12 15:31:28 -0700611void OpenGLRenderer::flushLayerUpdates() {
612 syncState();
613 updateLayers();
614 flushLayers();
615 // Wait for all the layer updates to be executed
616 AutoFence fence;
617}
618
Romain Guy11cb6422012-09-21 00:39:43 -0700619///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -0700620// State management
621///////////////////////////////////////////////////////////////////////////////
622
Chris Craik14e51302013-12-30 15:32:54 -0800623void OpenGLRenderer::onSnapshotRestored(const Snapshot& removed, const Snapshot& restored) {
Chris Craik0c0ec262014-05-09 16:11:14 -0700624 bool restoreViewport = removed.flags & Snapshot::kFlagIsFboLayer;
Chris Craik14e51302013-12-30 15:32:54 -0800625 bool restoreClip = removed.flags & Snapshot::kFlagClipSet;
626 bool restoreLayer = removed.flags & Snapshot::kFlagIsLayer;
Romain Guybd6b79b2010-06-26 00:13:53 -0700627
Chris Craik0c0ec262014-05-09 16:11:14 -0700628 if (restoreViewport) {
Chris Craik14e51302013-12-30 15:32:54 -0800629 const Rect& r = restored.viewport;
Romain Guyeb993562010-10-05 18:14:38 -0700630 glViewport(r.left, r.top, r.right, r.bottom);
Romain Guyeb993562010-10-05 18:14:38 -0700631 }
632
Romain Guy2542d192010-08-18 11:47:12 -0700633 if (restoreClip) {
Romain Guy746b7402010-10-26 16:27:31 -0700634 dirtyClip();
Romain Guy8fb95422010-08-17 18:38:51 -0700635 }
Romain Guy2542d192010-08-18 11:47:12 -0700636
Romain Guy5ec99242010-11-03 16:19:08 -0700637 if (restoreLayer) {
Chris Craik7273daa2013-03-28 11:25:24 -0700638 endMark(); // Savelayer
639 startMark("ComposeLayer");
Chris Craik14e51302013-12-30 15:32:54 -0800640 composeLayer(removed, restored);
Chris Craik7273daa2013-03-28 11:25:24 -0700641 endMark();
Romain Guy5ec99242010-11-03 16:19:08 -0700642 }
Romain Guybb9524b2010-06-22 18:56:38 -0700643}
644
Romain Guyf6a11b82010-06-23 17:47:49 -0700645///////////////////////////////////////////////////////////////////////////////
Romain Guybd6b79b2010-06-26 00:13:53 -0700646// Layers
647///////////////////////////////////////////////////////////////////////////////
648
649int OpenGLRenderer::saveLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700650 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700651 const int count = saveSnapshot(flags);
Romain Guyd55a8612010-06-28 17:42:46 -0700652
Chris Craikd6b65f62014-01-01 14:45:21 -0800653 if (!currentSnapshot()->isIgnored()) {
Chris Craik3f0854292014-04-15 16:18:08 -0700654 createLayer(left, top, right, bottom, paint, flags, convexMask);
Romain Guydbc26d22010-10-11 17:58:29 -0700655 }
Romain Guyd55a8612010-06-28 17:42:46 -0700656
657 return count;
Romain Guybd6b79b2010-06-26 00:13:53 -0700658}
659
Chris Craikd90144d2013-03-19 15:03:48 -0700660void OpenGLRenderer::calculateLayerBoundsAndClip(Rect& bounds, Rect& clip, bool fboLayer) {
661 const Rect untransformedBounds(bounds);
662
Chris Craikd6b65f62014-01-01 14:45:21 -0800663 currentTransform()->mapRect(bounds);
Chris Craikd90144d2013-03-19 15:03:48 -0700664
665 // Layers only make sense if they are in the framebuffer's bounds
Chris Craikd6b65f62014-01-01 14:45:21 -0800666 if (bounds.intersect(*currentClipRect())) {
Chris Craikd90144d2013-03-19 15:03:48 -0700667 // We cannot work with sub-pixels in this case
668 bounds.snapToPixelBoundaries();
669
670 // When the layer is not an FBO, we may use glCopyTexImage so we
671 // need to make sure the layer does not extend outside the bounds
672 // of the framebuffer
Chris Craikd6b65f62014-01-01 14:45:21 -0800673 if (!bounds.intersect(currentSnapshot()->previous->viewport)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700674 bounds.setEmpty();
675 } else if (fboLayer) {
676 clip.set(bounds);
677 mat4 inverse;
Chris Craikd6b65f62014-01-01 14:45:21 -0800678 inverse.loadInverse(*currentTransform());
Chris Craikd90144d2013-03-19 15:03:48 -0700679 inverse.mapRect(clip);
680 clip.snapToPixelBoundaries();
681 if (clip.intersect(untransformedBounds)) {
682 clip.translate(-untransformedBounds.left, -untransformedBounds.top);
683 bounds.set(untransformedBounds);
684 } else {
685 clip.setEmpty();
686 }
687 }
688 } else {
689 bounds.setEmpty();
690 }
691}
692
Chris Craik408eb122013-03-26 18:55:15 -0700693void OpenGLRenderer::updateSnapshotIgnoreForLayer(const Rect& bounds, const Rect& clip,
694 bool fboLayer, int alpha) {
695 if (bounds.isEmpty() || bounds.getWidth() > mCaches.maxTextureSize ||
696 bounds.getHeight() > mCaches.maxTextureSize ||
697 (fboLayer && clip.isEmpty())) {
698 mSnapshot->empty = fboLayer;
699 } else {
700 mSnapshot->invisible = mSnapshot->invisible || (alpha <= ALPHA_THRESHOLD && fboLayer);
701 }
702}
703
Chris Craikd90144d2013-03-19 15:03:48 -0700704int OpenGLRenderer::saveLayerDeferred(float left, float top, float right, float bottom,
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500705 const SkPaint* paint, int flags) {
Chris Craikd90144d2013-03-19 15:03:48 -0700706 const int count = saveSnapshot(flags);
707
Chris Craikd6b65f62014-01-01 14:45:21 -0800708 if (!currentSnapshot()->isIgnored() && (flags & SkCanvas::kClipToLayer_SaveFlag)) {
Chris Craikd90144d2013-03-19 15:03:48 -0700709 // initialize the snapshot as though it almost represents an FBO layer so deferred draw
710 // operations will be able to store and restore the current clip and transform info, and
711 // quick rejection will be correct (for display lists)
712
713 Rect bounds(left, top, right, bottom);
714 Rect clip;
715 calculateLayerBoundsAndClip(bounds, clip, true);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500716 updateSnapshotIgnoreForLayer(bounds, clip, true, getAlphaDirect(paint));
Chris Craikd90144d2013-03-19 15:03:48 -0700717
Chris Craikd6b65f62014-01-01 14:45:21 -0800718 if (!currentSnapshot()->isIgnored()) {
Chris Craikd90144d2013-03-19 15:03:48 -0700719 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
720 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
Chris Craik0e87f002013-06-19 16:54:59 -0700721 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
Chris Craikd90144d2013-03-19 15:03:48 -0700722 }
723 }
724
725 return count;
726}
727
Romain Guy1c740bc2010-09-13 18:00:09 -0700728/**
729 * Layers are viewed by Skia are slightly different than layers in image editing
730 * programs (for instance.) When a layer is created, previously created layers
731 * and the frame buffer still receive every drawing command. For instance, if a
732 * layer is created and a shape intersecting the bounds of the layers and the
733 * framebuffer is draw, the shape will be drawn on both (unless the layer was
734 * created with the SkCanvas::kClipToLayer_SaveFlag flag.)
735 *
736 * A way to implement layers is to create an FBO for each layer, backed by an RGBA
737 * texture. Unfortunately, this is inefficient as it requires every primitive to
738 * be drawn n + 1 times, where n is the number of active layers. In practice this
739 * means, for every primitive:
740 * - Switch active frame buffer
741 * - Change viewport, clip and projection matrix
742 * - Issue the drawing
743 *
744 * Switching rendering target n + 1 times per drawn primitive is extremely costly.
Romain Guy6b7bd242010-10-06 19:49:23 -0700745 * To avoid this, layers are implemented in a different way here, at least in the
746 * general case. FBOs are used, as an optimization, when the "clip to layer" flag
747 * is set. When this flag is set we can redirect all drawing operations into a
748 * single FBO.
Romain Guy1c740bc2010-09-13 18:00:09 -0700749 *
750 * This implementation relies on the frame buffer being at least RGBA 8888. When
751 * a layer is created, only a texture is created, not an FBO. The content of the
752 * frame buffer contained within the layer's bounds is copied into this texture
Romain Guy87a76572010-09-13 18:11:21 -0700753 * using glCopyTexImage2D(). The layer's region is then cleared(1) in the frame
Romain Guy1c740bc2010-09-13 18:00:09 -0700754 * buffer and drawing continues as normal. This technique therefore treats the
755 * frame buffer as a scratch buffer for the layers.
756 *
757 * To compose the layers back onto the frame buffer, each layer texture
758 * (containing the original frame buffer data) is drawn as a simple quad over
759 * the frame buffer. The trick is that the quad is set as the composition
760 * destination in the blending equation, and the frame buffer becomes the source
761 * of the composition.
762 *
763 * Drawing layers with an alpha value requires an extra step before composition.
764 * An empty quad is drawn over the layer's region in the frame buffer. This quad
765 * is drawn with the rgba color (0,0,0,alpha). The alpha value offered by the
766 * quad is used to multiply the colors in the frame buffer. This is achieved by
767 * changing the GL blend functions for the GL_FUNC_ADD blend equation to
768 * GL_ZERO, GL_SRC_ALPHA.
769 *
770 * Because glCopyTexImage2D() can be slow, an alternative implementation might
771 * be use to draw a single clipped layer. The implementation described above
772 * is correct in every case.
Romain Guy87a76572010-09-13 18:11:21 -0700773 *
774 * (1) The frame buffer is actually not cleared right away. To allow the GPU
775 * to potentially optimize series of calls to glCopyTexImage2D, the frame
776 * buffer is left untouched until the first drawing operation. Only when
777 * something actually gets drawn are the layers regions cleared.
Romain Guy1c740bc2010-09-13 18:00:09 -0700778 */
Chet Haased48885a2012-08-28 17:43:28 -0700779bool OpenGLRenderer::createLayer(float left, float top, float right, float bottom,
Chris Craik3f0854292014-04-15 16:18:08 -0700780 const SkPaint* paint, int flags, const SkPath* convexMask) {
Romain Guyeb993562010-10-05 18:14:38 -0700781 LAYER_LOGD("Requesting layer %.2fx%.2f", right - left, bottom - top);
Romain Guyfb8b7632010-08-23 21:05:08 -0700782 LAYER_LOGD("Layer cache size = %d", mCaches.layerCache.getSize());
Romain Guy8ba548f2010-06-30 19:21:21 -0700783
Romain Guyeb993562010-10-05 18:14:38 -0700784 const bool fboLayer = flags & SkCanvas::kClipToLayer_SaveFlag;
785
Romain Guyf607bdc2010-09-10 19:20:06 -0700786 // Window coordinates of the layer
Chet Haased48885a2012-08-28 17:43:28 -0700787 Rect clip;
Romain Guy8aef54f2010-09-01 15:13:49 -0700788 Rect bounds(left, top, right, bottom);
Chris Craikd90144d2013-03-19 15:03:48 -0700789 calculateLayerBoundsAndClip(bounds, clip, fboLayer);
Derek Sollenberger674554f2014-02-19 16:47:32 +0000790 updateSnapshotIgnoreForLayer(bounds, clip, fboLayer, getAlphaDirect(paint));
Romain Guydbc26d22010-10-11 17:58:29 -0700791
792 // Bail out if we won't draw in this snapshot
Chris Craikd6b65f62014-01-01 14:45:21 -0800793 if (currentSnapshot()->isIgnored()) {
Romain Guyb025b9c2010-09-16 14:16:48 -0700794 return false;
795 }
Romain Guyf18fd992010-07-08 11:45:51 -0700796
Romain Guya1d3c912011-12-13 14:55:06 -0800797 mCaches.activeTexture(0);
Romain Guy8550c4c2010-10-08 15:49:53 -0700798 Layer* layer = mCaches.layerCache.get(bounds.getWidth(), bounds.getHeight());
Romain Guydda570202010-07-06 11:39:32 -0700799 if (!layer) {
Romain Guyf18fd992010-07-08 11:45:51 -0700800 return false;
Romain Guybd6b79b2010-06-26 00:13:53 -0700801 }
802
Derek Sollenberger674554f2014-02-19 16:47:32 +0000803 layer->setPaint(paint);
Romain Guy8aef54f2010-09-01 15:13:49 -0700804 layer->layer.set(bounds);
Romain Guy9ace8f52011-07-07 20:50:11 -0700805 layer->texCoords.set(0.0f, bounds.getHeight() / float(layer->getHeight()),
806 bounds.getWidth() / float(layer->getWidth()), 0.0f);
Derek Sollenbergerd44fbe52014-02-05 16:47:00 -0500807
Chet Haasea23eed82012-04-12 15:19:04 -0700808 layer->setBlend(true);
Romain Guy7c25aab2012-10-18 15:05:02 -0700809 layer->setDirty(false);
Chris Craik3f0854292014-04-15 16:18:08 -0700810 layer->setConvexMask(convexMask); // note: the mask must be cleared before returning to the cache
Romain Guydda570202010-07-06 11:39:32 -0700811
Romain Guy8fb95422010-08-17 18:38:51 -0700812 // Save the layer in the snapshot
Chet Haased48885a2012-08-28 17:43:28 -0700813 mSnapshot->flags |= Snapshot::kFlagIsLayer;
814 mSnapshot->layer = layer;
Romain Guy1d83e192010-08-17 11:37:00 -0700815
Chris Craik7273daa2013-03-28 11:25:24 -0700816 startMark("SaveLayer");
Romain Guyeb993562010-10-05 18:14:38 -0700817 if (fboLayer) {
Chris Craike63f7c622013-10-17 10:30:55 -0700818 return createFboLayer(layer, bounds, clip);
Romain Guyeb993562010-10-05 18:14:38 -0700819 } else {
820 // Copy the framebuffer into the layer
Romain Guy9ace8f52011-07-07 20:50:11 -0700821 layer->bindTexture();
Romain Guy514fb182011-01-19 14:38:29 -0800822 if (!bounds.isEmpty()) {
Romain Guy9ace8f52011-07-07 20:50:11 -0700823 if (layer->isEmpty()) {
Romain Guyb254c242013-06-27 17:15:24 -0700824 // Workaround for some GL drivers. When reading pixels lying outside
825 // of the window we should get undefined values for those pixels.
826 // Unfortunately some drivers will turn the entire target texture black
827 // when reading outside of the window.
828 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, layer->getWidth(), layer->getHeight(),
829 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
Romain Guy9ace8f52011-07-07 20:50:11 -0700830 layer->setEmpty(false);
Romain Guy514fb182011-01-19 14:38:29 -0800831 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -0700832
Romain Guyb254c242013-06-27 17:15:24 -0700833 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, bounds.left,
834 mSnapshot->height - bounds.bottom, bounds.getWidth(), bounds.getHeight());
835
Romain Guy54be1cd2011-06-13 19:04:27 -0700836 // Enqueue the buffer coordinates to clear the corresponding region later
837 mLayers.push(new Rect(bounds));
Romain Guyae88e5e2010-10-22 17:49:18 -0700838 }
Romain Guyeb993562010-10-05 18:14:38 -0700839 }
Romain Guyf86ef572010-07-01 11:05:42 -0700840
Romain Guyd55a8612010-06-28 17:42:46 -0700841 return true;
Romain Guybd6b79b2010-06-26 00:13:53 -0700842}
843
Chris Craike63f7c622013-10-17 10:30:55 -0700844bool OpenGLRenderer::createFboLayer(Layer* layer, Rect& bounds, Rect& clip) {
Romain Guyc3fedaf2013-01-29 17:26:25 -0800845 layer->clipRect.set(clip);
Romain Guy9ace8f52011-07-07 20:50:11 -0700846 layer->setFbo(mCaches.fboCache.get());
Romain Guy5b3b3522010-10-27 18:57:51 -0700847
Chet Haased48885a2012-08-28 17:43:28 -0700848 mSnapshot->region = &mSnapshot->layer->region;
Chris Craik0c0ec262014-05-09 16:11:14 -0700849 mSnapshot->flags |= Snapshot::kFlagFboTarget | Snapshot::kFlagIsFboLayer;
Chet Haased48885a2012-08-28 17:43:28 -0700850 mSnapshot->fbo = layer->getFbo();
851 mSnapshot->resetTransform(-bounds.left, -bounds.top, 0.0f);
852 mSnapshot->resetClip(clip.left, clip.top, clip.right, clip.bottom);
853 mSnapshot->viewport.set(0.0f, 0.0f, bounds.getWidth(), bounds.getHeight());
854 mSnapshot->height = bounds.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -0700855
Romain Guy2b7028e2012-09-19 17:25:38 -0700856 endTiling();
Romain Guy7c450aa2012-09-21 19:15:00 -0700857 debugOverdraw(false, false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700858 // Bind texture to FBO
Romain Guy9ace8f52011-07-07 20:50:11 -0700859 glBindFramebuffer(GL_FRAMEBUFFER, layer->getFbo());
860 layer->bindTexture();
Romain Guy5b3b3522010-10-27 18:57:51 -0700861
862 // Initialize the texture if needed
Romain Guy9ace8f52011-07-07 20:50:11 -0700863 if (layer->isEmpty()) {
Romain Guy09087642013-04-04 12:27:54 -0700864 layer->allocateTexture();
Romain Guy9ace8f52011-07-07 20:50:11 -0700865 layer->setEmpty(false);
Romain Guy5b3b3522010-10-27 18:57:51 -0700866 }
867
868 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Romain Guy9ace8f52011-07-07 20:50:11 -0700869 layer->getTexture(), 0);
Romain Guy5b3b3522010-10-27 18:57:51 -0700870
Chris Craikd6b65f62014-01-01 14:45:21 -0800871 startTilingCurrentClip(true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700872
873 // Clear the FBO, expand the clear region by 1 to get nice bilinear filtering
Romain Guy586cae32012-07-13 15:28:31 -0700874 mCaches.enableScissor();
Romain Guy8f85e802011-12-14 19:23:32 -0800875 mCaches.setScissor(clip.left - 1.0f, bounds.getHeight() - clip.bottom - 1.0f,
Romain Guy5b3b3522010-10-27 18:57:51 -0700876 clip.getWidth() + 2.0f, clip.getHeight() + 2.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700877 glClear(GL_COLOR_BUFFER_BIT);
878
879 dirtyClip();
880
881 // Change the ortho projection
882 glViewport(0, 0, bounds.getWidth(), bounds.getHeight());
Chris Craik0c0ec262014-05-09 16:11:14 -0700883 mSnapshot->orthoMatrix.loadOrtho(0.0f, bounds.getWidth(), bounds.getHeight(), 0.0f, -1.0f, 1.0f);
Romain Guy5b3b3522010-10-27 18:57:51 -0700884
885 return true;
886}
887
Romain Guy1c740bc2010-09-13 18:00:09 -0700888/**
889 * Read the documentation of createLayer() before doing anything in this method.
890 */
Chris Craik14e51302013-12-30 15:32:54 -0800891void OpenGLRenderer::composeLayer(const Snapshot& removed, const Snapshot& restored) {
892 if (!removed.layer) {
Steve Block3762c312012-01-06 19:20:56 +0000893 ALOGE("Attempting to compose a layer that does not exist");
Romain Guy1d83e192010-08-17 11:37:00 -0700894 return;
895 }
896
Chris Craik14e51302013-12-30 15:32:54 -0800897 Layer* layer = removed.layer;
Romain Guy8ce00302013-01-15 18:51:42 -0800898 const Rect& rect = layer->layer;
Chris Craik14e51302013-12-30 15:32:54 -0800899 const bool fboLayer = removed.flags & Snapshot::kFlagIsFboLayer;
Romain Guyeb993562010-10-05 18:14:38 -0700900
Chris Craik39a908c2013-06-13 14:39:01 -0700901 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -0800902 calculateQuickRejectForScissor(rect.left, rect.top, rect.right, rect.bottom,
903 &clipRequired, false); // safely ignore return, should never be rejected
Chris Craik39a908c2013-06-13 14:39:01 -0700904 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
905
Romain Guyeb993562010-10-05 18:14:38 -0700906 if (fboLayer) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700907 endTiling();
908
Romain Guye0aa84b2012-04-03 19:30:26 -0700909 // Detach the texture from the FBO
910 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
Romain Guy8ce00302013-01-15 18:51:42 -0800911
912 layer->removeFbo(false);
913
Romain Guyeb993562010-10-05 18:14:38 -0700914 // Unbind current FBO and restore previous one
Chris Craik14e51302013-12-30 15:32:54 -0800915 glBindFramebuffer(GL_FRAMEBUFFER, restored.fbo);
Romain Guy7c450aa2012-09-21 19:15:00 -0700916 debugOverdraw(true, false);
Romain Guy2b7028e2012-09-19 17:25:38 -0700917
Chris Craikd6b65f62014-01-01 14:45:21 -0800918 startTilingCurrentClip();
Romain Guyeb993562010-10-05 18:14:38 -0700919 }
920
Romain Guy9ace8f52011-07-07 20:50:11 -0700921 if (!fboLayer && layer->getAlpha() < 255) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500922 SkPaint layerPaint;
923 layerPaint.setAlpha(layer->getAlpha());
924 layerPaint.setXfermodeMode(SkXfermode::kDstIn_Mode);
925 layerPaint.setColorFilter(layer->getColorFilter());
926
927 drawColorRect(rect.left, rect.top, rect.right, rect.bottom, &layerPaint, true);
Romain Guy5b3b3522010-10-27 18:57:51 -0700928 // Required below, composeLayerRect() will divide by 255
Romain Guy9ace8f52011-07-07 20:50:11 -0700929 layer->setAlpha(255);
Romain Guyf607bdc2010-09-10 19:20:06 -0700930 }
931
Romain Guy03750a02010-10-18 14:06:08 -0700932 mCaches.unbindMeshBuffer();
Romain Guy8b55f372010-08-18 17:10:07 -0700933
Romain Guya1d3c912011-12-13 14:55:06 -0800934 mCaches.activeTexture(0);
Romain Guy1d83e192010-08-17 11:37:00 -0700935
Romain Guy5b3b3522010-10-27 18:57:51 -0700936 // When the layer is stored in an FBO, we can save a bit of fillrate by
937 // drawing only the dirty region
938 if (fboLayer) {
Chris Craik14e51302013-12-30 15:32:54 -0800939 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom, *restored.transform);
Romain Guy5b3b3522010-10-27 18:57:51 -0700940 composeLayerRegion(layer, rect);
Romain Guy9ace8f52011-07-07 20:50:11 -0700941 } else if (!rect.isEmpty()) {
942 dirtyLayer(rect.left, rect.top, rect.right, rect.bottom);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530943
944 save(0);
945 // the layer contains screen buffer content that shouldn't be alpha modulated
946 // (and any necessary alpha modulation was handled drawing into the layer)
947 mSnapshot->alpha = 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -0700948 composeLayerRect(layer, rect, true);
Digish Pandyaa5ff7392013-11-04 06:30:25 +0530949 restore();
Romain Guy5b3b3522010-10-27 18:57:51 -0700950 }
Romain Guy8b55f372010-08-18 17:10:07 -0700951
Romain Guy746b7402010-10-26 16:27:31 -0700952 dirtyClip();
953
Romain Guyeb993562010-10-05 18:14:38 -0700954 // Failing to add the layer to the cache should happen only if the layer is too large
Chris Craik3f0854292014-04-15 16:18:08 -0700955 layer->setConvexMask(NULL);
Romain Guy8550c4c2010-10-08 15:49:53 -0700956 if (!mCaches.layerCache.put(layer)) {
Romain Guy1d83e192010-08-17 11:37:00 -0700957 LAYER_LOGD("Deleting layer");
Chet Haase603f6de2012-09-14 15:31:25 -0700958 Caches::getInstance().resourceCache.decrementRefcount(layer);
Romain Guy1d83e192010-08-17 11:37:00 -0700959 }
960}
961
Romain Guyaa6c24c2011-04-28 18:40:04 -0700962void OpenGLRenderer::drawTextureLayer(Layer* layer, const Rect& rect) {
Romain Guy24589392013-06-19 12:17:01 -0700963 float alpha = getLayerAlpha(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700964
965 setupDraw();
Romain Guy9ace8f52011-07-07 20:50:11 -0700966 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
Romain Guy8f0095c2011-05-02 17:24:22 -0700967 setupDrawWithTexture();
968 } else {
969 setupDrawWithExternalTexture();
970 }
971 setupDrawTextureTransform();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700972 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500973 setupDrawColorFilter(layer->getColorFilter());
974 setupDrawBlending(layer);
Romain Guyaa6c24c2011-04-28 18:40:04 -0700975 setupDrawProgram();
Romain Guyaa6c24c2011-04-28 18:40:04 -0700976 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -0500977 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -0700978 if (layer->getRenderTarget() == GL_TEXTURE_2D) {
979 setupDrawTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700980 } else {
Romain Guy9ace8f52011-07-07 20:50:11 -0700981 setupDrawExternalTexture(layer->getTexture());
Romain Guy8f0095c2011-05-02 17:24:22 -0700982 }
Chris Craikd6b65f62014-01-01 14:45:21 -0800983 if (currentTransform()->isPureTranslate() &&
Chris Craik9757ac02014-02-25 18:50:17 -0800984 !layer->getForceFilter() &&
Romain Guyec19b4a2011-07-07 21:05:04 -0700985 layer->getWidth() == (uint32_t) rect.getWidth() &&
986 layer->getHeight() == (uint32_t) rect.getHeight()) {
Chris Craikd6b65f62014-01-01 14:45:21 -0800987 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
988 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -0700989
Romain Guyd21b6e12011-11-30 20:21:23 -0800990 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -0800991 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
992 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -0700993 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -0800994 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -0800995 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
996 rect.left, rect.top, rect.right, rect.bottom);
Romain Guy9ace8f52011-07-07 20:50:11 -0700997 }
998 setupDrawTextureTransformUniforms(layer->getTexTransform());
Romain Guy3380cfd2013-08-15 16:57:57 -0700999 setupDrawMesh(&mMeshVertices[0].x, &mMeshVertices[0].u);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001000
1001 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guyaa6c24c2011-04-28 18:40:04 -07001002}
1003
Romain Guy5b3b3522010-10-27 18:57:51 -07001004void OpenGLRenderer::composeLayerRect(Layer* layer, const Rect& rect, bool swap) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001005 if (!layer->isTextureLayer()) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001006 const Rect& texCoords = layer->texCoords;
1007 resetDrawTextureTexCoords(texCoords.left, texCoords.top,
1008 texCoords.right, texCoords.bottom);
Romain Guy5b3b3522010-10-27 18:57:51 -07001009
Romain Guy9ace8f52011-07-07 20:50:11 -07001010 float x = rect.left;
1011 float y = rect.top;
Chris Craikd6b65f62014-01-01 14:45:21 -08001012 bool simpleTransform = currentTransform()->isPureTranslate() &&
Romain Guyec19b4a2011-07-07 21:05:04 -07001013 layer->getWidth() == (uint32_t) rect.getWidth() &&
Romain Guyb2479152011-07-08 11:57:29 -07001014 layer->getHeight() == (uint32_t) rect.getHeight();
1015
1016 if (simpleTransform) {
Romain Guy9ace8f52011-07-07 20:50:11 -07001017 // When we're swapping, the layer is already in screen coordinates
1018 if (!swap) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001019 x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1020 y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001021 }
1022
Romain Guyd21b6e12011-11-30 20:21:23 -08001023 layer->setFilter(GL_NEAREST, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001024 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08001025 layer->setFilter(GL_LINEAR, true);
Romain Guy9ace8f52011-07-07 20:50:11 -07001026 }
1027
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001028 SkPaint layerPaint;
1029 layerPaint.setAlpha(getLayerAlpha(layer) * 255);
1030 layerPaint.setXfermodeMode(layer->getMode());
1031 layerPaint.setColorFilter(layer->getColorFilter());
1032
1033 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f;
Romain Guy9ace8f52011-07-07 20:50:11 -07001034 drawTextureMesh(x, y, x + rect.getWidth(), y + rect.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001035 layer->getTexture(), &layerPaint, blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07001036 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy9ace8f52011-07-07 20:50:11 -07001037 GL_TRIANGLE_STRIP, gMeshCount, swap, swap || simpleTransform);
Romain Guy5b3b3522010-10-27 18:57:51 -07001038
Romain Guyaa6c24c2011-04-28 18:40:04 -07001039 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1040 } else {
1041 resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
1042 drawTextureLayer(layer, rect);
1043 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
1044 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001045}
1046
Chris Craik34416ea2013-04-15 16:08:28 -07001047/**
1048 * Issues the command X, and if we're composing a save layer to the fbo or drawing a newly updated
1049 * hardware layer with overdraw debug on, draws again to the stencil only, so that these draw
1050 * operations are correctly counted twice for overdraw. NOTE: assumes composeLayerRegion only used
1051 * by saveLayer's restore
1052 */
1053#define DRAW_DOUBLE_STENCIL_IF(COND, DRAW_COMMAND) { \
1054 DRAW_COMMAND; \
1055 if (CC_UNLIKELY(mCaches.debugOverdraw && getTargetFbo() == 0 && COND)) { \
1056 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); \
1057 DRAW_COMMAND; \
1058 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); \
1059 } \
1060 }
1061
1062#define DRAW_DOUBLE_STENCIL(DRAW_COMMAND) DRAW_DOUBLE_STENCIL_IF(true, DRAW_COMMAND)
1063
Romain Guy5b3b3522010-10-27 18:57:51 -07001064void OpenGLRenderer::composeLayerRegion(Layer* layer, const Rect& rect) {
Chris Craik3f0854292014-04-15 16:18:08 -07001065 if (CC_UNLIKELY(layer->region.isEmpty())) return; // nothing to draw
1066
1067 if (layer->getConvexMask()) {
1068 save(SkCanvas::kClip_SaveFlag | SkCanvas::kMatrix_SaveFlag);
1069
1070 // clip to the area of the layer the mask can be larger
1071 clipRect(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kIntersect_Op);
1072
1073 SkPaint paint;
1074 paint.setAntiAlias(true);
1075 paint.setColor(SkColorSetARGB(int(getLayerAlpha(layer) * 255), 0, 0, 0));
1076
1077 SkiaShader* oldShader = mDrawModifiers.mShader;
1078
1079 // create LayerShader to map SaveLayer content into subsequent draw
1080 SkMatrix shaderMatrix;
1081 shaderMatrix.setTranslate(rect.left, rect.bottom);
1082 shaderMatrix.preScale(1, -1);
1083 SkiaLayerShader layerShader(layer, &shaderMatrix);
1084 mDrawModifiers.mShader = &layerShader;
1085
1086 // Since the drawing primitive is defined in local drawing space,
1087 // we don't need to modify the draw matrix
1088 const SkPath* maskPath = layer->getConvexMask();
1089 DRAW_DOUBLE_STENCIL(drawConvexPath(*maskPath, &paint));
1090
1091 mDrawModifiers.mShader = oldShader;
1092 restore();
1093
1094 return;
1095 }
1096
Romain Guy5b3b3522010-10-27 18:57:51 -07001097 if (layer->region.isRect()) {
Romain Guy9fc27812011-04-27 14:21:41 -07001098 layer->setRegionAsRect();
1099
Chris Craik34416ea2013-04-15 16:08:28 -07001100 DRAW_DOUBLE_STENCIL(composeLayerRect(layer, layer->regionRect));
Romain Guy9fc27812011-04-27 14:21:41 -07001101
Romain Guy5b3b3522010-10-27 18:57:51 -07001102 layer->region.clear();
1103 return;
1104 }
1105
Chris Craik3f0854292014-04-15 16:18:08 -07001106 // standard Region based draw
1107 size_t count;
1108 const android::Rect* rects;
1109 Region safeRegion;
1110 if (CC_LIKELY(hasRectToRectTransform())) {
1111 rects = layer->region.getArray(&count);
1112 } else {
1113 safeRegion = Region::createTJunctionFreeRegion(layer->region);
1114 rects = safeRegion.getArray(&count);
1115 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001116
Chris Craik3f0854292014-04-15 16:18:08 -07001117 const float alpha = getLayerAlpha(layer);
1118 const float texX = 1.0f / float(layer->getWidth());
1119 const float texY = 1.0f / float(layer->getHeight());
1120 const float height = rect.getHeight();
Romain Guy5b3b3522010-10-27 18:57:51 -07001121
Chris Craik3f0854292014-04-15 16:18:08 -07001122 setupDraw();
Romain Guy8ce00302013-01-15 18:51:42 -08001123
Chris Craik3f0854292014-04-15 16:18:08 -07001124 // We must get (and therefore bind) the region mesh buffer
1125 // after we setup drawing in case we need to mess with the
1126 // stencil buffer in setupDraw()
1127 TextureVertex* mesh = mCaches.getRegionMesh();
1128 uint32_t numQuads = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -07001129
Chris Craik3f0854292014-04-15 16:18:08 -07001130 setupDrawWithTexture();
1131 setupDrawColor(alpha, alpha, alpha, alpha);
1132 setupDrawColorFilter(layer->getColorFilter());
1133 setupDrawBlending(layer);
1134 setupDrawProgram();
1135 setupDrawDirtyRegionsDisabled();
1136 setupDrawPureColorUniforms();
1137 setupDrawColorFilterUniforms(layer->getColorFilter());
1138 setupDrawTexture(layer->getTexture());
1139 if (currentTransform()->isPureTranslate()) {
1140 const float x = (int) floorf(rect.left + currentTransform()->getTranslateX() + 0.5f);
1141 const float y = (int) floorf(rect.top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07001142
Chris Craik3f0854292014-04-15 16:18:08 -07001143 layer->setFilter(GL_NEAREST);
1144 setupDrawModelView(kModelViewMode_Translate, false,
1145 x, y, x + rect.getWidth(), y + rect.getHeight(), true);
1146 } else {
1147 layer->setFilter(GL_LINEAR);
1148 setupDrawModelView(kModelViewMode_Translate, false,
1149 rect.left, rect.top, rect.right, rect.bottom);
1150 }
1151 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy5b3b3522010-10-27 18:57:51 -07001152
Chris Craik3f0854292014-04-15 16:18:08 -07001153 for (size_t i = 0; i < count; i++) {
1154 const android::Rect* r = &rects[i];
Romain Guy5b3b3522010-10-27 18:57:51 -07001155
Chris Craik3f0854292014-04-15 16:18:08 -07001156 const float u1 = r->left * texX;
1157 const float v1 = (height - r->top) * texY;
1158 const float u2 = r->right * texX;
1159 const float v2 = (height - r->bottom) * texY;
Romain Guy5b3b3522010-10-27 18:57:51 -07001160
Chris Craik3f0854292014-04-15 16:18:08 -07001161 // TODO: Reject quads outside of the clip
1162 TextureVertex::set(mesh++, r->left, r->top, u1, v1);
1163 TextureVertex::set(mesh++, r->right, r->top, u2, v1);
1164 TextureVertex::set(mesh++, r->left, r->bottom, u1, v2);
1165 TextureVertex::set(mesh++, r->right, r->bottom, u2, v2);
Romain Guy5b3b3522010-10-27 18:57:51 -07001166
Chris Craik3f0854292014-04-15 16:18:08 -07001167 numQuads++;
Romain Guy5b3b3522010-10-27 18:57:51 -07001168
Chris Craik3f0854292014-04-15 16:18:08 -07001169 if (numQuads >= gMaxNumberOfQuads) {
Chris Craik34416ea2013-04-15 16:08:28 -07001170 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1171 GL_UNSIGNED_SHORT, NULL));
Chris Craik3f0854292014-04-15 16:18:08 -07001172 numQuads = 0;
1173 mesh = mCaches.getRegionMesh();
Romain Guy5b3b3522010-10-27 18:57:51 -07001174 }
Chris Craik3f0854292014-04-15 16:18:08 -07001175 }
1176
1177 if (numQuads > 0) {
1178 DRAW_DOUBLE_STENCIL(glDrawElements(GL_TRIANGLES, numQuads * 6,
1179 GL_UNSIGNED_SHORT, NULL));
1180 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001181
Romain Guy5b3b3522010-10-27 18:57:51 -07001182#if DEBUG_LAYERS_AS_REGIONS
Chris Craik3f0854292014-04-15 16:18:08 -07001183 drawRegionRectsDebug(layer->region);
Romain Guy5b3b3522010-10-27 18:57:51 -07001184#endif
1185
Chris Craik3f0854292014-04-15 16:18:08 -07001186 layer->region.clear();
Romain Guy5b3b3522010-10-27 18:57:51 -07001187}
1188
Romain Guy3a3133d2011-02-01 22:59:58 -08001189#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07001190void OpenGLRenderer::drawRegionRectsDebug(const Region& region) {
Romain Guy3a3133d2011-02-01 22:59:58 -08001191 size_t count;
1192 const android::Rect* rects = region.getArray(&count);
1193
1194 uint32_t colors[] = {
1195 0x7fff0000, 0x7f00ff00,
1196 0x7f0000ff, 0x7fff00ff,
1197 };
1198
1199 int offset = 0;
1200 int32_t top = rects[0].top;
1201
1202 for (size_t i = 0; i < count; i++) {
1203 if (top != rects[i].top) {
1204 offset ^= 0x2;
1205 top = rects[i].top;
1206 }
1207
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001208 SkPaint paint;
1209 paint.setColor(colors[offset + (i & 0x1)]);
Romain Guy3a3133d2011-02-01 22:59:58 -08001210 Rect r(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001211 drawColorRect(r.left, r.top, r.right, r.bottom, paint);
Romain Guy3a3133d2011-02-01 22:59:58 -08001212 }
Romain Guy3a3133d2011-02-01 22:59:58 -08001213}
Chris Craike63f7c622013-10-17 10:30:55 -07001214#endif
Romain Guy3a3133d2011-02-01 22:59:58 -08001215
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001216void OpenGLRenderer::drawRegionRects(const SkRegion& region, const SkPaint& paint, bool dirty) {
Romain Guy8ce00302013-01-15 18:51:42 -08001217 Vector<float> rects;
1218
1219 SkRegion::Iterator it(region);
1220 while (!it.done()) {
1221 const SkIRect& r = it.rect();
1222 rects.push(r.fLeft);
1223 rects.push(r.fTop);
1224 rects.push(r.fRight);
1225 rects.push(r.fBottom);
Romain Guy8ce00302013-01-15 18:51:42 -08001226 it.next();
1227 }
1228
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001229 drawColorRects(rects.array(), rects.size(), &paint, true, dirty, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001230}
1231
Romain Guy5b3b3522010-10-27 18:57:51 -07001232void OpenGLRenderer::dirtyLayer(const float left, const float top,
1233 const float right, const float bottom, const mat4 transform) {
Romain Guyf219da52011-01-16 12:54:25 -08001234 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001235 Rect bounds(left, top, right, bottom);
1236 transform.mapRect(bounds);
Romain Guyf219da52011-01-16 12:54:25 -08001237 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07001238 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001239}
1240
1241void OpenGLRenderer::dirtyLayer(const float left, const float top,
1242 const float right, const float bottom) {
Romain Guyf219da52011-01-16 12:54:25 -08001243 if (hasLayer()) {
Romain Guy5b3b3522010-10-27 18:57:51 -07001244 Rect bounds(left, top, right, bottom);
Romain Guyf219da52011-01-16 12:54:25 -08001245 dirtyLayerUnchecked(bounds, getRegion());
Romain Guy1bd1bad2011-01-14 20:07:20 -08001246 }
Romain Guy1bd1bad2011-01-14 20:07:20 -08001247}
1248
1249void OpenGLRenderer::dirtyLayerUnchecked(Rect& bounds, Region* region) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001250 if (bounds.intersect(*currentClipRect())) {
Romain Guy1bd1bad2011-01-14 20:07:20 -08001251 bounds.snapToPixelBoundaries();
1252 android::Rect dirty(bounds.left, bounds.top, bounds.right, bounds.bottom);
1253 if (!dirty.isEmpty()) {
1254 region->orSelf(dirty);
Romain Guy5b3b3522010-10-27 18:57:51 -07001255 }
1256 }
Romain Guy5b3b3522010-10-27 18:57:51 -07001257}
1258
Chris Craik4063a0e2013-11-15 16:06:56 -08001259void OpenGLRenderer::issueIndexedQuadDraw(Vertex* mesh, GLsizei quadsCount) {
Romain Guy448455f2013-07-22 13:57:50 -07001260 GLsizei elementsCount = quadsCount * 6;
1261 while (elementsCount > 0) {
1262 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
1263
Romain Guy3380cfd2013-08-15 16:57:57 -07001264 setupDrawIndexedVertices(&mesh[0].x);
Romain Guy448455f2013-07-22 13:57:50 -07001265 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL);
1266
1267 elementsCount -= drawCount;
1268 // Though there are 4 vertices in a quad, we use 6 indices per
1269 // quad to draw with GL_TRIANGLES
1270 mesh += (drawCount / 6) * 4;
1271 }
1272}
1273
Romain Guy54be1cd2011-06-13 19:04:27 -07001274void OpenGLRenderer::clearLayerRegions() {
1275 const size_t count = mLayers.size();
1276 if (count == 0) return;
1277
Chris Craikd6b65f62014-01-01 14:45:21 -08001278 if (!currentSnapshot()->isIgnored()) {
Romain Guy54be1cd2011-06-13 19:04:27 -07001279 // Doing several glScissor/glClear here can negatively impact
1280 // GPUs with a tiler architecture, instead we draw quads with
1281 // the Clear blending mode
1282
1283 // The list contains bounds that have already been clipped
1284 // against their initial clip rect, and the current clip
1285 // is likely different so we need to disable clipping here
Romain Guy8a4ac612012-07-17 17:32:48 -07001286 bool scissorChanged = mCaches.disableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001287
Romain Guy448455f2013-07-22 13:57:50 -07001288 Vertex mesh[count * 4];
Romain Guy54be1cd2011-06-13 19:04:27 -07001289 Vertex* vertex = mesh;
1290
1291 for (uint32_t i = 0; i < count; i++) {
1292 Rect* bounds = mLayers.itemAt(i);
1293
Romain Guy54be1cd2011-06-13 19:04:27 -07001294 Vertex::set(vertex++, bounds->left, bounds->top);
1295 Vertex::set(vertex++, bounds->right, bounds->top);
1296 Vertex::set(vertex++, bounds->left, bounds->bottom);
Romain Guy54be1cd2011-06-13 19:04:27 -07001297 Vertex::set(vertex++, bounds->right, bounds->bottom);
1298
1299 delete bounds;
1300 }
Romain Guye67307c2013-02-11 18:01:20 -08001301 // We must clear the list of dirty rects before we
1302 // call setupDraw() to prevent stencil setup to do
1303 // the same thing again
1304 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001305
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001306 SkPaint clearPaint;
1307 clearPaint.setXfermodeMode(SkXfermode::kClear_Mode);
1308
Romain Guy54be1cd2011-06-13 19:04:27 -07001309 setupDraw(false);
1310 setupDrawColor(0.0f, 0.0f, 0.0f, 1.0f);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001311 setupDrawBlending(&clearPaint, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001312 setupDrawProgram();
1313 setupDrawPureColorUniforms();
Chris Craik4063a0e2013-11-15 16:06:56 -08001314 setupDrawModelView(kModelViewMode_Translate, false,
1315 0.0f, 0.0f, 0.0f, 0.0f, true);
Romain Guy54be1cd2011-06-13 19:04:27 -07001316
Chris Craik4063a0e2013-11-15 16:06:56 -08001317 issueIndexedQuadDraw(&mesh[0], count);
Romain Guy8a4ac612012-07-17 17:32:48 -07001318
1319 if (scissorChanged) mCaches.enableScissor();
Romain Guy54be1cd2011-06-13 19:04:27 -07001320 } else {
1321 for (uint32_t i = 0; i < count; i++) {
1322 delete mLayers.itemAt(i);
1323 }
Romain Guye67307c2013-02-11 18:01:20 -08001324 mLayers.clear();
Romain Guy54be1cd2011-06-13 19:04:27 -07001325 }
Romain Guy54be1cd2011-06-13 19:04:27 -07001326}
1327
Romain Guybd6b79b2010-06-26 00:13:53 -07001328///////////////////////////////////////////////////////////////////////////////
Chris Craikc3566d02013-02-04 16:16:33 -08001329// State Deferral
1330///////////////////////////////////////////////////////////////////////////////
1331
Chris Craikff785832013-03-08 13:12:16 -08001332bool OpenGLRenderer::storeDisplayState(DeferredDisplayState& state, int stateDeferFlags) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001333 const Rect* currentClip = currentClipRect();
1334 const mat4* currentMatrix = currentTransform();
Chris Craikc3566d02013-02-04 16:16:33 -08001335
Chris Craikff785832013-03-08 13:12:16 -08001336 if (stateDeferFlags & kStateDeferFlag_Draw) {
1337 // state has bounds initialized in local coordinates
1338 if (!state.mBounds.isEmpty()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001339 currentMatrix->mapRect(state.mBounds);
Chris Craik28ce94a2013-05-31 11:38:03 -07001340 Rect clippedBounds(state.mBounds);
Chris Craik5e49b302013-07-30 19:05:20 -07001341 // NOTE: if we ever want to use this clipping info to drive whether the scissor
1342 // is used, it should more closely duplicate the quickReject logic (in how it uses
1343 // snapToPixelBoundaries)
1344
Chris Craikd6b65f62014-01-01 14:45:21 -08001345 if(!clippedBounds.intersect(*currentClip)) {
Chris Craikff785832013-03-08 13:12:16 -08001346 // quick rejected
1347 return true;
1348 }
Chris Craik28ce94a2013-05-31 11:38:03 -07001349
Chris Craika02c4ed2013-06-14 13:43:58 -07001350 state.mClipSideFlags = kClipSide_None;
Chris Craikd6b65f62014-01-01 14:45:21 -08001351 if (!currentClip->contains(state.mBounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001352 int& flags = state.mClipSideFlags;
1353 // op partially clipped, so record which sides are clipped for clip-aware merging
Chris Craikd6b65f62014-01-01 14:45:21 -08001354 if (currentClip->left > state.mBounds.left) flags |= kClipSide_Left;
1355 if (currentClip->top > state.mBounds.top) flags |= kClipSide_Top;
1356 if (currentClip->right < state.mBounds.right) flags |= kClipSide_Right;
1357 if (currentClip->bottom < state.mBounds.bottom) flags |= kClipSide_Bottom;
Chris Craik28ce94a2013-05-31 11:38:03 -07001358 }
1359 state.mBounds.set(clippedBounds);
Chris Craikff785832013-03-08 13:12:16 -08001360 } else {
Chris Craikd72b73c2013-06-17 13:52:06 -07001361 // Empty bounds implies size unknown. Label op as conservatively clipped to disable
1362 // overdraw avoidance (since we don't know what it overlaps)
1363 state.mClipSideFlags = kClipSide_ConservativeFull;
Chris Craikd6b65f62014-01-01 14:45:21 -08001364 state.mBounds.set(*currentClip);
Chris Craikc3566d02013-02-04 16:16:33 -08001365 }
1366 }
1367
Chris Craik527a3aa2013-03-04 10:19:31 -08001368 state.mClipValid = (stateDeferFlags & kStateDeferFlag_Clip);
1369 if (state.mClipValid) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001370 state.mClip.set(*currentClip);
Chris Craikff785832013-03-08 13:12:16 -08001371 }
1372
Chris Craik7273daa2013-03-28 11:25:24 -07001373 // Transform, drawModifiers, and alpha always deferred, since they are used by state operations
1374 // (Note: saveLayer/restore use colorFilter and alpha, so we just save restore everything)
Chris Craikd6b65f62014-01-01 14:45:21 -08001375 state.mMatrix.load(*currentMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001376 state.mDrawModifiers = mDrawModifiers;
Chris Craikd6b65f62014-01-01 14:45:21 -08001377 state.mAlpha = currentSnapshot()->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08001378 return false;
1379}
1380
Chris Craik527a3aa2013-03-04 10:19:31 -08001381void OpenGLRenderer::restoreDisplayState(const DeferredDisplayState& state, bool skipClipRestore) {
Chris Craik14e51302013-12-30 15:32:54 -08001382 setMatrix(state.mMatrix);
Chris Craik7273daa2013-03-28 11:25:24 -07001383 mSnapshot->alpha = state.mAlpha;
Chris Craik14e51302013-12-30 15:32:54 -08001384 mDrawModifiers = state.mDrawModifiers;
Chris Craikff785832013-03-08 13:12:16 -08001385
Chris Craik527a3aa2013-03-04 10:19:31 -08001386 if (state.mClipValid && !skipClipRestore) {
Chris Craik28ce94a2013-05-31 11:38:03 -07001387 mSnapshot->setClip(state.mClip.left, state.mClip.top,
1388 state.mClip.right, state.mClip.bottom);
Chris Craikff785832013-03-08 13:12:16 -08001389 dirtyClip();
1390 }
Chris Craikc3566d02013-02-04 16:16:33 -08001391}
1392
Chris Craik28ce94a2013-05-31 11:38:03 -07001393/**
1394 * Merged multidraw (such as in drawText and drawBitmaps rely on the fact that no clipping is done
1395 * in the draw path. Instead, clipping is done ahead of time - either as a single clip rect (when at
1396 * least one op is clipped), or disabled entirely (because no merged op is clipped)
1397 *
1398 * This method should be called when restoreDisplayState() won't be restoring the clip
1399 */
1400void OpenGLRenderer::setupMergedMultiDraw(const Rect* clipRect) {
1401 if (clipRect != NULL) {
1402 mSnapshot->setClip(clipRect->left, clipRect->top, clipRect->right, clipRect->bottom);
1403 } else {
Chris Craik14e51302013-12-30 15:32:54 -08001404 mSnapshot->setClip(0, 0, getWidth(), getHeight());
Chris Craik28ce94a2013-05-31 11:38:03 -07001405 }
Chris Craik527a3aa2013-03-04 10:19:31 -08001406 dirtyClip();
Chris Craik28ce94a2013-05-31 11:38:03 -07001407 mCaches.setScissorEnabled(clipRect != NULL || mScissorOptimizationDisabled);
Chris Craik527a3aa2013-03-04 10:19:31 -08001408}
1409
Chris Craikc3566d02013-02-04 16:16:33 -08001410///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001411// Clipping
1412///////////////////////////////////////////////////////////////////////////////
1413
Romain Guybb9524b2010-06-22 18:56:38 -07001414void OpenGLRenderer::setScissorFromClip() {
Chris Craikd6b65f62014-01-01 14:45:21 -08001415 Rect clip(*currentClipRect());
Romain Guye5ebcb02010-10-15 13:57:28 -07001416 clip.snapToPixelBoundaries();
Romain Guy8f85e802011-12-14 19:23:32 -08001417
Chris Craikd6b65f62014-01-01 14:45:21 -08001418 if (mCaches.setScissor(clip.left, currentSnapshot()->height - clip.bottom,
Romain Guy8a4ac612012-07-17 17:32:48 -07001419 clip.getWidth(), clip.getHeight())) {
1420 mDirtyClip = false;
1421 }
Romain Guy9d5316e2010-06-24 19:30:36 -07001422}
1423
Romain Guy8ce00302013-01-15 18:51:42 -08001424void OpenGLRenderer::ensureStencilBuffer() {
1425 // Thanks to the mismatch between EGL and OpenGL ES FBO we
1426 // cannot attach a stencil buffer to fbo0 dynamically. Let's
1427 // just hope we have one when hasLayer() returns false.
1428 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001429 attachStencilBufferToLayer(currentSnapshot()->layer);
Romain Guy8ce00302013-01-15 18:51:42 -08001430 }
1431}
1432
1433void OpenGLRenderer::attachStencilBufferToLayer(Layer* layer) {
1434 // The layer's FBO is already bound when we reach this stage
1435 if (!layer->getStencilRenderBuffer()) {
Romain Guyc3fedaf2013-01-29 17:26:25 -08001436 // GL_QCOM_tiled_rendering doesn't like it if a renderbuffer
1437 // is attached after we initiated tiling. We must turn it off,
1438 // attach the new render buffer then turn tiling back on
1439 endTiling();
1440
Romain Guy8d4aeb72013-02-12 16:08:55 -08001441 RenderBuffer* buffer = mCaches.renderBufferCache.get(
Romain Guy3bbacf22013-02-06 16:51:04 -08001442 Stencil::getSmallestStencilFormat(), layer->getWidth(), layer->getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001443 layer->setStencilRenderBuffer(buffer);
Romain Guyc3fedaf2013-01-29 17:26:25 -08001444
Romain Guyf735c8e2013-01-31 17:45:55 -08001445 startTiling(layer->clipRect, layer->layer.getHeight());
Romain Guy8ce00302013-01-15 18:51:42 -08001446 }
1447}
1448
1449void OpenGLRenderer::setStencilFromClip() {
1450 if (!mCaches.debugOverdraw) {
Chris Craikd6b65f62014-01-01 14:45:21 -08001451 if (!currentSnapshot()->clipRegion->isEmpty()) {
Romain Guy8ce00302013-01-15 18:51:42 -08001452 // NOTE: The order here is important, we must set dirtyClip to false
1453 // before any draw call to avoid calling back into this method
1454 mDirtyClip = false;
1455
1456 ensureStencilBuffer();
1457
1458 mCaches.stencil.enableWrite();
1459
1460 // Clear the stencil but first make sure we restrict drawing
1461 // to the region's bounds
1462 bool resetScissor = mCaches.enableScissor();
1463 if (resetScissor) {
1464 // The scissor was not set so we now need to update it
1465 setScissorFromClip();
1466 }
1467 mCaches.stencil.clear();
1468 if (resetScissor) mCaches.disableScissor();
1469
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001470 SkPaint paint;
1471 paint.setColor(0xff000000);
1472 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
1473
Romain Guy8ce00302013-01-15 18:51:42 -08001474 // NOTE: We could use the region contour path to generate a smaller mesh
1475 // Since we are using the stencil we could use the red book path
1476 // drawing technique. It might increase bandwidth usage though.
1477
1478 // The last parameter is important: we are not drawing in the color buffer
1479 // so we don't want to dirty the current layer, if any
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001480 drawRegionRects(*(currentSnapshot()->clipRegion), paint, false);
Romain Guy8ce00302013-01-15 18:51:42 -08001481
1482 mCaches.stencil.enableTest();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001483
1484 // Draw the region used to generate the stencil if the appropriate debug
1485 // mode is enabled
1486 if (mCaches.debugStencilClip == Caches::kStencilShowRegion) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001487 paint.setColor(0x7f0000ff);
1488 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
1489 drawRegionRects(*(currentSnapshot()->clipRegion), paint);
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001490 }
Romain Guy8ce00302013-01-15 18:51:42 -08001491 } else {
1492 mCaches.stencil.disable();
1493 }
1494 }
1495}
1496
Chris Craikf0a59072013-11-19 18:00:46 -08001497/**
1498 * Returns false and sets scissor enable based upon bounds if drawing won't be clipped out.
1499 *
1500 * @param paint if not null, the bounds will be expanded to account for stroke depending on paint
1501 * style, and tessellated AA ramp
1502 */
1503bool OpenGLRenderer::quickRejectSetupScissor(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08001504 const SkPaint* paint) {
Chris Craik39a908c2013-06-13 14:39:01 -07001505 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08001506 bool snapOut = paint && paint->isAntiAlias();
1507
1508 if (paint && paint->getStyle() != SkPaint::kFill_Style) {
1509 float outset = paint->getStrokeWidth() * 0.5f;
1510 left -= outset;
1511 top -= outset;
1512 right += outset;
1513 bottom += outset;
1514 }
1515
1516 if (calculateQuickRejectForScissor(left, top, right, bottom, &clipRequired, snapOut)) {
Romain Guydbc26d22010-10-11 17:58:29 -07001517 return true;
1518 }
1519
Chris Craikb4589422013-12-26 15:13:13 -08001520 if (!isRecording()) {
Chris Craikf0a59072013-11-19 18:00:46 -08001521 // not quick rejected, so enable the scissor if clipRequired
Chris Craik39a908c2013-06-13 14:39:01 -07001522 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guy586cae32012-07-13 15:28:31 -07001523 }
Chris Craik39a908c2013-06-13 14:39:01 -07001524 return false;
Romain Guyc7d53492010-06-25 13:41:57 -07001525}
1526
Romain Guy8ce00302013-01-15 18:51:42 -08001527void OpenGLRenderer::debugClip() {
1528#if DEBUG_CLIP_REGIONS
Chris Craikd6b65f62014-01-01 14:45:21 -08001529 if (!isRecording() && !currentSnapshot()->clipRegion->isEmpty()) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001530 SkPaint paint;
1531 paint.setColor(0x7f00ff00);
1532 drawRegionRects(*(currentSnapshot()->clipRegion, paint);
1533
Romain Guy8ce00302013-01-15 18:51:42 -08001534 }
1535#endif
1536}
1537
Romain Guyf6a11b82010-06-23 17:47:49 -07001538///////////////////////////////////////////////////////////////////////////////
Romain Guy70ca14e2010-12-13 18:24:33 -08001539// Drawing commands
1540///////////////////////////////////////////////////////////////////////////////
1541
Romain Guy54be1cd2011-06-13 19:04:27 -07001542void OpenGLRenderer::setupDraw(bool clear) {
Chris Craikf0a59072013-11-19 18:00:46 -08001543 // TODO: It would be best if we could do this before quickRejectSetupScissor()
Romain Guy8a4ac612012-07-17 17:32:48 -07001544 // changes the scissor test state
Romain Guy54be1cd2011-06-13 19:04:27 -07001545 if (clear) clearLayerRegions();
Romain Guy8ce00302013-01-15 18:51:42 -08001546 // Make sure setScissor & setStencil happen at the beginning of
1547 // this method
Chris Craikb98a0162013-02-21 11:30:22 -08001548 if (mDirtyClip) {
1549 if (mCaches.scissorEnabled) {
1550 setScissorFromClip();
1551 }
Romain Guy8ce00302013-01-15 18:51:42 -08001552 setStencilFromClip();
Romain Guy70ca14e2010-12-13 18:24:33 -08001553 }
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001554
Romain Guy70ca14e2010-12-13 18:24:33 -08001555 mDescription.reset();
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001556
Romain Guy70ca14e2010-12-13 18:24:33 -08001557 mSetShaderColor = false;
1558 mColorSet = false;
1559 mColorA = mColorR = mColorG = mColorB = 0.0f;
1560 mTextureUnit = 0;
1561 mTrackDirtyRegions = true;
Romain Guy3ff0bfd2013-02-25 14:15:37 -08001562
1563 // Enable debug highlight when what we're about to draw is tested against
1564 // the stencil buffer and if stencil highlight debugging is on
1565 mDescription.hasDebugHighlight = !mCaches.debugOverdraw &&
1566 mCaches.debugStencilClip == Caches::kStencilShowHighlight &&
1567 mCaches.stencil.isTestEnabled();
Romain Guy78dd96d2013-05-03 14:24:16 -07001568
1569 mDescription.emulateStencil = mCountOverdraw;
Romain Guy70ca14e2010-12-13 18:24:33 -08001570}
1571
1572void OpenGLRenderer::setupDrawWithTexture(bool isAlpha8) {
1573 mDescription.hasTexture = true;
1574 mDescription.hasAlpha8Texture = isAlpha8;
1575}
1576
Romain Guyff316ec2013-02-13 18:39:43 -08001577void OpenGLRenderer::setupDrawWithTextureAndColor(bool isAlpha8) {
1578 mDescription.hasTexture = true;
1579 mDescription.hasColors = true;
1580 mDescription.hasAlpha8Texture = isAlpha8;
1581}
1582
Romain Guyaa6c24c2011-04-28 18:40:04 -07001583void OpenGLRenderer::setupDrawWithExternalTexture() {
1584 mDescription.hasExternalTexture = true;
1585}
1586
Romain Guy15bc6432011-12-13 13:11:32 -08001587void OpenGLRenderer::setupDrawNoTexture() {
Romain Guyff316ec2013-02-13 18:39:43 -08001588 mCaches.disableTexCoordsVertexArray();
Romain Guy15bc6432011-12-13 13:11:32 -08001589}
1590
Chris Craik710f46d2012-09-17 17:25:49 -07001591void OpenGLRenderer::setupDrawAA() {
Chet Haase99585ad2011-05-02 15:00:16 -07001592 mDescription.isAA = true;
Chet Haase5b0200b2011-04-13 17:58:08 -07001593}
1594
Romain Guy8d0d4782010-12-14 20:13:35 -08001595void OpenGLRenderer::setupDrawColor(int color, int alpha) {
1596 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001597 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1598 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1599 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy70ca14e2010-12-13 18:24:33 -08001600 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001601 mSetShaderColor = mDescription.setColorModulate(mColorA);
Romain Guy70ca14e2010-12-13 18:24:33 -08001602}
1603
Romain Guy86568192010-12-14 15:55:39 -08001604void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
1605 mColorA = alpha / 255.0f;
Romain Guy886b2752013-01-04 12:26:18 -08001606 mColorR = mColorA * ((color >> 16) & 0xFF) / 255.0f;
1607 mColorG = mColorA * ((color >> 8) & 0xFF) / 255.0f;
1608 mColorB = mColorA * ((color ) & 0xFF) / 255.0f;
Romain Guy86568192010-12-14 15:55:39 -08001609 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001610 mSetShaderColor = mDescription.setAlpha8ColorModulate(mColorR, mColorG, mColorB, mColorA);
Romain Guy86568192010-12-14 15:55:39 -08001611}
1612
Romain Guy41210632012-07-16 17:04:24 -07001613void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
1614 mCaches.fontRenderer->describe(mDescription, paint);
1615}
1616
Romain Guy70ca14e2010-12-13 18:24:33 -08001617void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
1618 mColorA = a;
1619 mColorR = r;
1620 mColorG = g;
1621 mColorB = b;
1622 mColorSet = true;
Chris Craike63f7c622013-10-17 10:30:55 -07001623 mSetShaderColor = mDescription.setColorModulate(a);
Romain Guy70ca14e2010-12-13 18:24:33 -08001624}
1625
1626void OpenGLRenderer::setupDrawShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08001627 if (mDrawModifiers.mShader) {
1628 mDrawModifiers.mShader->describe(mDescription, mExtensions);
Romain Guy70ca14e2010-12-13 18:24:33 -08001629 }
1630}
1631
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001632void OpenGLRenderer::setupDrawColorFilter(const SkColorFilter* filter) {
1633 if (filter == NULL) {
1634 return;
1635 }
1636
1637 SkXfermode::Mode mode;
1638 if (filter->asColorMode(NULL, &mode)) {
1639 mDescription.colorOp = ProgramDescription::kColorBlend;
1640 mDescription.colorMode = mode;
1641 } else if (filter->asColorMatrix(NULL)) {
1642 mDescription.colorOp = ProgramDescription::kColorMatrix;
Romain Guy70ca14e2010-12-13 18:24:33 -08001643 }
1644}
1645
Romain Guyf09ef512011-05-27 11:43:46 -07001646void OpenGLRenderer::accountForClear(SkXfermode::Mode mode) {
1647 if (mColorSet && mode == SkXfermode::kClear_Mode) {
1648 mColorA = 1.0f;
1649 mColorR = mColorG = mColorB = 0.0f;
Romain Guy54be1cd2011-06-13 19:04:27 -07001650 mSetShaderColor = mDescription.modulate = true;
Romain Guyf09ef512011-05-27 11:43:46 -07001651 }
1652}
1653
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001654void OpenGLRenderer::setupDrawBlending(const Layer* layer, bool swapSrcDst) {
1655 SkXfermode::Mode mode = layer->getMode();
Romain Guyf09ef512011-05-27 11:43:46 -07001656 // When the blending mode is kClear_Mode, we need to use a modulate color
1657 // argb=1,0,0,0
1658 accountForClear(mode);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001659 bool blend = layer->isBlend() || getLayerAlpha(layer) < 1.0f ||
1660 (mColorSet && mColorA < 1.0f) ||
1661 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
1662 layer->getColorFilter();
Chris Craikc3566d02013-02-04 16:16:33 -08001663 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001664}
1665
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001666void OpenGLRenderer::setupDrawBlending(const SkPaint* paint, bool blend, bool swapSrcDst) {
1667 SkXfermode::Mode mode = getXfermodeDirect(paint);
Romain Guyf09ef512011-05-27 11:43:46 -07001668 // When the blending mode is kClear_Mode, we need to use a modulate color
1669 // argb=1,0,0,0
1670 accountForClear(mode);
Chris Craikc3566d02013-02-04 16:16:33 -08001671 blend |= (mColorSet && mColorA < 1.0f) ||
1672 (mDrawModifiers.mShader && mDrawModifiers.mShader->blend()) ||
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001673 (paint && paint->getColorFilter());
Chris Craikc3566d02013-02-04 16:16:33 -08001674 chooseBlending(blend, mode, mDescription, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08001675}
1676
1677void OpenGLRenderer::setupDrawProgram() {
1678 useProgram(mCaches.programCache.get(mDescription));
1679}
1680
1681void OpenGLRenderer::setupDrawDirtyRegionsDisabled() {
1682 mTrackDirtyRegions = false;
1683}
1684
Chris Craik4063a0e2013-11-15 16:06:56 -08001685void OpenGLRenderer::setupDrawModelView(ModelViewMode mode, bool offset,
1686 float left, float top, float right, float bottom, bool ignoreTransform) {
Chris Craike10e8272014-05-08 14:28:26 -07001687 mModelViewMatrix.loadTranslate(left, top, 0.0f);
Chris Craik4063a0e2013-11-15 16:06:56 -08001688 if (mode == kModelViewMode_TranslateAndScale) {
Chris Craike10e8272014-05-08 14:28:26 -07001689 mModelViewMatrix.scale(right - left, bottom - top, 1.0f);
Romain Guy70ca14e2010-12-13 18:24:33 -08001690 }
Chris Craik4063a0e2013-11-15 16:06:56 -08001691
Romain Guy86568192010-12-14 15:55:39 -08001692 bool dirty = right - left > 0.0f && bottom - top > 0.0f;
Chris Craik0c0ec262014-05-09 16:11:14 -07001693 const Matrix4& transformMatrix = ignoreTransform ? Matrix4::identity() : *currentTransform();
1694 mCaches.currentProgram->set(mSnapshot->orthoMatrix, mModelViewMatrix, transformMatrix, offset);
1695 if (dirty && mTrackDirtyRegions) {
1696 if (!ignoreTransform) {
1697 dirtyLayer(left, top, right, bottom, *currentTransform());
1698 } else {
1699 dirtyLayer(left, top, right, bottom);
1700 }
Romain Guy86568192010-12-14 15:55:39 -08001701 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001702}
1703
1704void OpenGLRenderer::setupDrawColorUniforms() {
Chris Craikc3566d02013-02-04 16:16:33 -08001705 if ((mColorSet && !mDrawModifiers.mShader) || (mDrawModifiers.mShader && mSetShaderColor)) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001706 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
1707 }
1708}
1709
Romain Guy86568192010-12-14 15:55:39 -08001710void OpenGLRenderer::setupDrawPureColorUniforms() {
Romain Guy55368412010-12-14 10:59:41 -08001711 if (mSetShaderColor) {
Romain Guy86568192010-12-14 15:55:39 -08001712 mCaches.currentProgram->setColor(mColorR, mColorG, mColorB, mColorA);
Romain Guy55368412010-12-14 10:59:41 -08001713 }
1714}
1715
Romain Guy70ca14e2010-12-13 18:24:33 -08001716void OpenGLRenderer::setupDrawShaderUniforms(bool ignoreTransform) {
Chris Craikc3566d02013-02-04 16:16:33 -08001717 if (mDrawModifiers.mShader) {
Romain Guy70ca14e2010-12-13 18:24:33 -08001718 if (ignoreTransform) {
Chris Craik4063a0e2013-11-15 16:06:56 -08001719 // if ignoreTransform=true was passed to setupDrawModelView, undo currentTransform()
1720 // because it was built into modelView / the geometry, and the SkiaShader needs to
1721 // compensate.
1722 mat4 modelViewWithoutTransform;
Chris Craikd6b65f62014-01-01 14:45:21 -08001723 modelViewWithoutTransform.loadInverse(*currentTransform());
Chris Craike10e8272014-05-08 14:28:26 -07001724 modelViewWithoutTransform.multiply(mModelViewMatrix);
1725 mModelViewMatrix.load(modelViewWithoutTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08001726 }
Chris Craikc3566d02013-02-04 16:16:33 -08001727 mDrawModifiers.mShader->setupProgram(mCaches.currentProgram,
Chris Craike10e8272014-05-08 14:28:26 -07001728 mModelViewMatrix, *mSnapshot, &mTextureUnit);
Romain Guy70ca14e2010-12-13 18:24:33 -08001729 }
1730}
1731
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001732void OpenGLRenderer::setupDrawColorFilterUniforms(const SkColorFilter* filter) {
1733 if (NULL == filter) {
1734 return;
Romain Guy70ca14e2010-12-13 18:24:33 -08001735 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001736
1737 SkColor color;
1738 SkXfermode::Mode mode;
1739 if (filter->asColorMode(&color, &mode)) {
1740 const int alpha = SkColorGetA(color);
1741 const GLfloat a = alpha / 255.0f;
1742 const GLfloat r = a * SkColorGetR(color) / 255.0f;
1743 const GLfloat g = a * SkColorGetG(color) / 255.0f;
1744 const GLfloat b = a * SkColorGetB(color) / 255.0f;
1745 glUniform4f(mCaches.currentProgram->getUniform("colorBlend"), r, g, b, a);
1746 return;
1747 }
1748
1749 SkScalar srcColorMatrix[20];
1750 if (filter->asColorMatrix(srcColorMatrix)) {
1751
1752 float colorMatrix[16];
1753 memcpy(colorMatrix, srcColorMatrix, 4 * sizeof(float));
1754 memcpy(&colorMatrix[4], &srcColorMatrix[5], 4 * sizeof(float));
1755 memcpy(&colorMatrix[8], &srcColorMatrix[10], 4 * sizeof(float));
1756 memcpy(&colorMatrix[12], &srcColorMatrix[15], 4 * sizeof(float));
1757
1758 // Skia uses the range [0..255] for the addition vector, but we need
1759 // the [0..1] range to apply the vector in GLSL
1760 float colorVector[4];
1761 colorVector[0] = srcColorMatrix[4] / 255.0f;
1762 colorVector[1] = srcColorMatrix[9] / 255.0f;
1763 colorVector[2] = srcColorMatrix[14] / 255.0f;
1764 colorVector[3] = srcColorMatrix[19] / 255.0f;
1765
1766 glUniformMatrix4fv(mCaches.currentProgram->getUniform("colorMatrix"), 1,
1767 GL_FALSE, colorMatrix);
1768 glUniform4fv(mCaches.currentProgram->getUniform("colorMatrixVector"), 1, colorVector);
1769 return;
1770 }
1771
1772 // it is an error if we ever get here
Romain Guy70ca14e2010-12-13 18:24:33 -08001773}
1774
Romain Guy41210632012-07-16 17:04:24 -07001775void OpenGLRenderer::setupDrawTextGammaUniforms() {
1776 mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
1777}
1778
Romain Guy70ca14e2010-12-13 18:24:33 -08001779void OpenGLRenderer::setupDrawSimpleMesh() {
Romain Guyf3a910b42011-12-12 20:35:21 -08001780 bool force = mCaches.bindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001781 mCaches.bindPositionVertexPointer(force, 0);
Romain Guy15bc6432011-12-13 13:11:32 -08001782 mCaches.unbindIndicesBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001783}
1784
1785void OpenGLRenderer::setupDrawTexture(GLuint texture) {
Romain Guy257ae352013-03-20 16:31:12 -07001786 if (texture) bindTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001787 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001788 mCaches.enableTexCoordsVertexArray();
Romain Guy70ca14e2010-12-13 18:24:33 -08001789}
1790
Romain Guyaa6c24c2011-04-28 18:40:04 -07001791void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
1792 bindExternalTexture(texture);
Romain Guy2d4fd362011-12-13 22:00:19 -08001793 mTextureUnit++;
Romain Guy15bc6432011-12-13 13:11:32 -08001794 mCaches.enableTexCoordsVertexArray();
Romain Guyaa6c24c2011-04-28 18:40:04 -07001795}
1796
Romain Guy8f0095c2011-05-02 17:24:22 -07001797void OpenGLRenderer::setupDrawTextureTransform() {
1798 mDescription.hasTextureTransform = true;
1799}
1800
1801void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
Romain Guyaa6c24c2011-04-28 18:40:04 -07001802 glUniformMatrix4fv(mCaches.currentProgram->getUniform("mainTextureTransform"), 1,
1803 GL_FALSE, &transform.data[0]);
1804}
1805
Chris Craik564acf72014-01-02 16:46:18 -08001806void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1807 const GLvoid* texCoords, GLuint vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001808 bool force = false;
Romain Guy3b748a42013-04-17 18:54:38 -07001809 if (!vertices || vbo) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001810 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
Romain Guy70ca14e2010-12-13 18:24:33 -08001811 } else {
Romain Guyf3a910b42011-12-12 20:35:21 -08001812 force = mCaches.unbindMeshBuffer();
Romain Guy70ca14e2010-12-13 18:24:33 -08001813 }
Romain Guyd71dd362011-12-12 19:03:35 -08001814
Chris Craikcb4d6002012-09-25 12:00:29 -07001815 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001816 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001817 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy15bc6432011-12-13 13:11:32 -08001818 }
1819
1820 mCaches.unbindIndicesBuffer();
1821}
1822
Chris Craik564acf72014-01-02 16:46:18 -08001823void OpenGLRenderer::setupDrawMesh(const GLvoid* vertices,
1824 const GLvoid* texCoords, const GLvoid* colors) {
Romain Guyff316ec2013-02-13 18:39:43 -08001825 bool force = mCaches.unbindMeshBuffer();
1826 GLsizei stride = sizeof(ColorTextureVertex);
1827
1828 mCaches.bindPositionVertexPointer(force, vertices, stride);
1829 if (mCaches.currentProgram->texCoords >= 0) {
1830 mCaches.bindTexCoordsVertexPointer(force, texCoords, stride);
1831 }
1832 int slot = mCaches.currentProgram->getAttrib("colors");
1833 if (slot >= 0) {
1834 glEnableVertexAttribArray(slot);
1835 glVertexAttribPointer(slot, 4, GL_FLOAT, GL_FALSE, stride, colors);
1836 }
1837
1838 mCaches.unbindIndicesBuffer();
1839}
1840
Chris Craik564acf72014-01-02 16:46:18 -08001841void OpenGLRenderer::setupDrawMeshIndices(const GLvoid* vertices,
1842 const GLvoid* texCoords, GLuint vbo) {
Romain Guy3b748a42013-04-17 18:54:38 -07001843 bool force = false;
1844 // If vbo is != 0 we want to treat the vertices parameter as an offset inside
1845 // a VBO. However, if vertices is set to NULL and vbo == 0 then we want to
1846 // use the default VBO found in Caches
1847 if (!vertices || vbo) {
1848 force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
1849 } else {
1850 force = mCaches.unbindMeshBuffer();
1851 }
ztenghui63d41ab2014-02-14 13:13:41 -08001852 mCaches.bindQuadIndicesBuffer();
Romain Guy3b748a42013-04-17 18:54:38 -07001853
Chris Craikcb4d6002012-09-25 12:00:29 -07001854 mCaches.bindPositionVertexPointer(force, vertices);
Romain Guy15bc6432011-12-13 13:11:32 -08001855 if (mCaches.currentProgram->texCoords >= 0) {
Chris Craikcb4d6002012-09-25 12:00:29 -07001856 mCaches.bindTexCoordsVertexPointer(force, texCoords);
Romain Guy8d0d4782010-12-14 20:13:35 -08001857 }
Romain Guy70ca14e2010-12-13 18:24:33 -08001858}
1859
Romain Guy448455f2013-07-22 13:57:50 -07001860void OpenGLRenderer::setupDrawIndexedVertices(GLvoid* vertices) {
Romain Guyf3a910b42011-12-12 20:35:21 -08001861 bool force = mCaches.unbindMeshBuffer();
ztenghui63d41ab2014-02-14 13:13:41 -08001862 mCaches.bindQuadIndicesBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07001863 mCaches.bindPositionVertexPointer(force, vertices, gVertexStride);
Chet Haase5b0200b2011-04-13 17:58:08 -07001864}
1865
Romain Guy70ca14e2010-12-13 18:24:33 -08001866///////////////////////////////////////////////////////////////////////////////
Romain Guyf6a11b82010-06-23 17:47:49 -07001867// Drawing
1868///////////////////////////////////////////////////////////////////////////////
1869
John Recke18264b2014-03-12 13:56:30 -07001870status_t OpenGLRenderer::drawDisplayList(RenderNode* displayList, Rect& dirty,
Chris Craikff785832013-03-08 13:12:16 -08001871 int32_t replayFlags) {
Chet Haase58d110a2013-04-10 07:43:29 -07001872 status_t status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001873 // All the usual checks and setup operations (quickReject, setupDraw, etc.)
1874 // will be performed by the display list itself
Romain Guy04c9d8c2011-08-25 14:01:48 -07001875 if (displayList && displayList->isRenderable()) {
Chris Craikf57776b2013-10-25 18:30:17 -07001876 // compute 3d ordering
1877 displayList->computeOrdering();
Chris Craikd90144d2013-03-19 15:03:48 -07001878 if (CC_UNLIKELY(mCaches.drawDeferDisabled)) {
Chet Haase58d110a2013-04-10 07:43:29 -07001879 status = startFrame();
Chris Craikff785832013-03-08 13:12:16 -08001880 ReplayStateStruct replayStruct(*this, dirty, replayFlags);
Chris Craikb265e2c2014-03-27 15:50:09 -07001881 displayList->replayNodeTree(replayStruct);
Chet Haase58d110a2013-04-10 07:43:29 -07001882 return status | replayStruct.mDrawGlStatus;
Chris Craikc3566d02013-02-04 16:16:33 -08001883 }
1884
Chris Craik28ce94a2013-05-31 11:38:03 -07001885 bool avoidOverdraw = !mCaches.debugOverdraw && !mCountOverdraw; // shh, don't tell devs!
Chris Craikd6b65f62014-01-01 14:45:21 -08001886 DeferredDisplayList deferredList(*currentClipRect(), avoidOverdraw);
Chris Craikff785832013-03-08 13:12:16 -08001887 DeferStateStruct deferStruct(deferredList, *this, replayFlags);
Chris Craikb265e2c2014-03-27 15:50:09 -07001888 displayList->deferNodeTree(deferStruct);
Romain Guy96885eb2013-03-26 15:05:58 -07001889
1890 flushLayers();
Chet Haase58d110a2013-04-10 07:43:29 -07001891 status = startFrame();
Romain Guy96885eb2013-03-26 15:05:58 -07001892
Chris Craikf57776b2013-10-25 18:30:17 -07001893 return deferredList.flush(*this, dirty) | status;
Romain Guy0fe478e2010-11-08 12:08:41 -08001894 }
Romain Guy7b5b6ab2011-03-14 18:05:08 -07001895
Romain Guy65549432012-03-26 16:45:05 -07001896 return DrawGlInfo::kStatusDone;
Romain Guy0fe478e2010-11-08 12:08:41 -08001897}
1898
Chris Craikd218a922014-01-02 17:13:34 -08001899void OpenGLRenderer::drawAlphaBitmap(Texture* texture, float left, float top, const SkPaint* paint) {
Romain Guy886b2752013-01-04 12:26:18 -08001900 int color = paint != NULL ? paint->getColor() : 0;
1901
Romain Guya168d732011-03-18 16:50:13 -07001902 float x = left;
1903 float y = top;
1904
Romain Guy886b2752013-01-04 12:26:18 -08001905 texture->setWrap(GL_CLAMP_TO_EDGE, true);
1906
Romain Guya168d732011-03-18 16:50:13 -07001907 bool ignoreTransform = false;
Chris Craikd6b65f62014-01-01 14:45:21 -08001908 if (currentTransform()->isPureTranslate()) {
1909 x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
1910 y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guya168d732011-03-18 16:50:13 -07001911 ignoreTransform = true;
Romain Guy886b2752013-01-04 12:26:18 -08001912
1913 texture->setFilter(GL_NEAREST, true);
Romain Guyd21b6e12011-11-30 20:21:23 -08001914 } else {
Chris Craik678625242014-02-28 12:26:34 -08001915 texture->setFilter(getFilter(paint), true);
Romain Guya168d732011-03-18 16:50:13 -07001916 }
1917
Romain Guy3b748a42013-04-17 18:54:38 -07001918 // No need to check for a UV mapper on the texture object, only ARGB_8888
1919 // bitmaps get packed in the atlas
Romain Guy886b2752013-01-04 12:26:18 -08001920 drawAlpha8TextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001921 paint, (GLvoid*) NULL, (GLvoid*) gMeshTextureOffset,
Romain Guy3b748a42013-04-17 18:54:38 -07001922 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
Romain Guya168d732011-03-18 16:50:13 -07001923}
1924
Romain Guy03c00b52013-06-20 18:30:28 -07001925/**
1926 * Important note: this method is intended to draw batches of bitmaps and
1927 * will not set the scissor enable or dirty the current layer, if any.
1928 * The caller is responsible for properly dirtying the current layer.
1929 */
Chris Craikd218a922014-01-02 17:13:34 -08001930status_t OpenGLRenderer::drawBitmaps(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
1931 int bitmapCount, TextureVertex* vertices, bool pureTranslate,
1932 const Rect& bounds, const SkPaint* paint) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001933 mCaches.activeTexture(0);
Romain Guy55b6f952013-06-27 15:27:09 -07001934 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Chris Craik527a3aa2013-03-04 10:19:31 -08001935 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy3b748a42013-04-17 18:54:38 -07001936
Chris Craik527a3aa2013-03-04 10:19:31 -08001937 const AutoTexture autoCleanup(texture);
1938
Chris Craik527a3aa2013-03-04 10:19:31 -08001939 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08001940 texture->setFilter(pureTranslate ? GL_NEAREST : getFilter(paint), true);
Chris Craik527a3aa2013-03-04 10:19:31 -08001941
1942 const float x = (int) floorf(bounds.left + 0.5f);
1943 const float y = (int) floorf(bounds.top + 0.5f);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05001944 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Chris Craik527a3aa2013-03-04 10:19:31 -08001945 drawAlpha8TextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001946 texture->id, paint, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001947 GL_TRIANGLES, bitmapCount * 6, true,
1948 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001949 } else {
1950 drawTextureMesh(x, y, x + bounds.getWidth(), y + bounds.getHeight(),
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05001951 texture->id, paint, texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08001952 GL_TRIANGLES, bitmapCount * 6, false, true, 0,
1953 kModelViewMode_Translate, false);
Chris Craik527a3aa2013-03-04 10:19:31 -08001954 }
1955
1956 return DrawGlInfo::kStatusDrew;
1957}
1958
Chris Craikd218a922014-01-02 17:13:34 -08001959status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, float left, float top,
1960 const SkPaint* paint) {
Romain Guy6926c722010-07-12 20:20:03 -07001961 const float right = left + bitmap->width();
1962 const float bottom = top + bitmap->height();
1963
Chris Craikf0a59072013-11-19 18:00:46 -08001964 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001965 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001966 }
1967
Romain Guya1d3c912011-12-13 14:55:06 -08001968 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001969 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001970 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001971 const AutoTexture autoCleanup(texture);
1972
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05001973 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guya168d732011-03-18 16:50:13 -07001974 drawAlphaBitmap(texture, left, top, paint);
1975 } else {
1976 drawTextureRect(left, top, right, bottom, texture, paint);
1977 }
Chet Haase48659092012-05-31 15:21:51 -07001978
1979 return DrawGlInfo::kStatusDrew;
Romain Guyce0537b2010-06-29 21:05:21 -07001980}
1981
Chris Craikd218a922014-01-02 17:13:34 -08001982status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap, const SkMatrix* matrix,
1983 const SkPaint* paint) {
Romain Guyf86ef572010-07-01 11:05:42 -07001984 Rect r(0.0f, 0.0f, bitmap->width(), bitmap->height());
1985 const mat4 transform(*matrix);
1986 transform.mapRect(r);
1987
Chris Craikf0a59072013-11-19 18:00:46 -08001988 if (quickRejectSetupScissor(r.left, r.top, r.right, r.bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07001989 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07001990 }
1991
Romain Guya1d3c912011-12-13 14:55:06 -08001992 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07001993 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07001994 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07001995 const AutoTexture autoCleanup(texture);
1996
Romain Guy5b3b3522010-10-27 18:57:51 -07001997 // This could be done in a cheaper way, all we need is pass the matrix
1998 // to the vertex shader. The save/restore is a bit overkill.
1999 save(SkCanvas::kMatrix_SaveFlag);
2000 concatMatrix(matrix);
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002001 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002002 drawAlphaBitmap(texture, 0.0f, 0.0f, paint);
2003 } else {
2004 drawTextureRect(0.0f, 0.0f, bitmap->width(), bitmap->height(), texture, paint);
2005 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002006 restore();
Chet Haase48659092012-05-31 15:21:51 -07002007
2008 return DrawGlInfo::kStatusDrew;
Romain Guyf86ef572010-07-01 11:05:42 -07002009}
2010
Chris Craikd218a922014-01-02 17:13:34 -08002011status_t OpenGLRenderer::drawBitmapData(const SkBitmap* bitmap, float left, float top,
2012 const SkPaint* paint) {
Romain Guye651cc62012-05-14 19:44:40 -07002013 const float right = left + bitmap->width();
2014 const float bottom = top + bitmap->height();
2015
Chris Craikf0a59072013-11-19 18:00:46 -08002016 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002017 return DrawGlInfo::kStatusDone;
Romain Guye651cc62012-05-14 19:44:40 -07002018 }
2019
2020 mCaches.activeTexture(0);
2021 Texture* texture = mCaches.textureCache.getTransient(bitmap);
2022 const AutoTexture autoCleanup(texture);
2023
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002024 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002025 drawAlphaBitmap(texture, left, top, paint);
2026 } else {
2027 drawTextureRect(left, top, right, bottom, texture, paint);
2028 }
Chet Haase48659092012-05-31 15:21:51 -07002029
2030 return DrawGlInfo::kStatusDrew;
Romain Guye651cc62012-05-14 19:44:40 -07002031}
2032
Chris Craikd218a922014-01-02 17:13:34 -08002033status_t OpenGLRenderer::drawBitmapMesh(const SkBitmap* bitmap, int meshWidth, int meshHeight,
2034 const float* vertices, const int* colors, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002035 if (!vertices || currentSnapshot()->isIgnored()) {
Chet Haase48659092012-05-31 15:21:51 -07002036 return DrawGlInfo::kStatusDone;
Romain Guy5a7b4662011-01-20 19:09:30 -08002037 }
2038
Chris Craik39a908c2013-06-13 14:39:01 -07002039 // TODO: use quickReject on bounds from vertices
2040 mCaches.enableScissor();
2041
Romain Guyb18d2d02011-02-10 15:52:54 -08002042 float left = FLT_MAX;
2043 float top = FLT_MAX;
2044 float right = FLT_MIN;
2045 float bottom = FLT_MIN;
2046
Romain Guya92bb4d2012-10-16 11:08:44 -07002047 const uint32_t count = meshWidth * meshHeight * 6;
Romain Guyb18d2d02011-02-10 15:52:54 -08002048
Chris Craik564acf72014-01-02 16:46:18 -08002049 Vector<ColorTextureVertex> mesh; // TODO: use C++11 unique_ptr
2050 mesh.setCapacity(count);
2051 ColorTextureVertex* vertex = mesh.editArray();
Romain Guyff316ec2013-02-13 18:39:43 -08002052
2053 bool cleanupColors = false;
2054 if (!colors) {
2055 uint32_t colorsCount = (meshWidth + 1) * (meshHeight + 1);
Chris Craikd218a922014-01-02 17:13:34 -08002056 int* newColors = new int[colorsCount];
2057 memset(newColors, 0xff, colorsCount * sizeof(int));
2058 colors = newColors;
Romain Guyff316ec2013-02-13 18:39:43 -08002059 cleanupColors = true;
2060 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002061
Romain Guy3b748a42013-04-17 18:54:38 -07002062 mCaches.activeTexture(0);
2063 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
2064 const UvMapper& mapper(getMapper(texture));
2065
Romain Guy5a7b4662011-01-20 19:09:30 -08002066 for (int32_t y = 0; y < meshHeight; y++) {
2067 for (int32_t x = 0; x < meshWidth; x++) {
2068 uint32_t i = (y * (meshWidth + 1) + x) * 2;
2069
2070 float u1 = float(x) / meshWidth;
2071 float u2 = float(x + 1) / meshWidth;
2072 float v1 = float(y) / meshHeight;
2073 float v2 = float(y + 1) / meshHeight;
2074
Romain Guy3b748a42013-04-17 18:54:38 -07002075 mapper.map(u1, v1, u2, v2);
2076
Romain Guy5a7b4662011-01-20 19:09:30 -08002077 int ax = i + (meshWidth + 1) * 2;
2078 int ay = ax + 1;
2079 int bx = i;
2080 int by = bx + 1;
2081 int cx = i + 2;
2082 int cy = cx + 1;
2083 int dx = i + (meshWidth + 1) * 2 + 2;
2084 int dy = dx + 1;
2085
Romain Guyff316ec2013-02-13 18:39:43 -08002086 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2087 ColorTextureVertex::set(vertex++, vertices[ax], vertices[ay], u1, v2, colors[ax / 2]);
2088 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
Romain Guy5a7b4662011-01-20 19:09:30 -08002089
Romain Guyff316ec2013-02-13 18:39:43 -08002090 ColorTextureVertex::set(vertex++, vertices[dx], vertices[dy], u2, v2, colors[dx / 2]);
2091 ColorTextureVertex::set(vertex++, vertices[bx], vertices[by], u1, v1, colors[bx / 2]);
2092 ColorTextureVertex::set(vertex++, vertices[cx], vertices[cy], u2, v1, colors[cx / 2]);
Romain Guyb18d2d02011-02-10 15:52:54 -08002093
Romain Guya92bb4d2012-10-16 11:08:44 -07002094 left = fminf(left, fminf(vertices[ax], fminf(vertices[bx], vertices[cx])));
2095 top = fminf(top, fminf(vertices[ay], fminf(vertices[by], vertices[cy])));
2096 right = fmaxf(right, fmaxf(vertices[ax], fmaxf(vertices[bx], vertices[cx])));
2097 bottom = fmaxf(bottom, fmaxf(vertices[ay], fmaxf(vertices[by], vertices[cy])));
Romain Guy5a7b4662011-01-20 19:09:30 -08002098 }
2099 }
2100
Chris Craikf0a59072013-11-19 18:00:46 -08002101 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guyff316ec2013-02-13 18:39:43 -08002102 if (cleanupColors) delete[] colors;
Romain Guya92bb4d2012-10-16 11:08:44 -07002103 return DrawGlInfo::kStatusDone;
2104 }
2105
Romain Guyff316ec2013-02-13 18:39:43 -08002106 if (!texture) {
Romain Guy3b748a42013-04-17 18:54:38 -07002107 texture = mCaches.textureCache.get(bitmap);
2108 if (!texture) {
2109 if (cleanupColors) delete[] colors;
2110 return DrawGlInfo::kStatusDone;
2111 }
Romain Guyff316ec2013-02-13 18:39:43 -08002112 }
Romain Guya92bb4d2012-10-16 11:08:44 -07002113 const AutoTexture autoCleanup(texture);
2114
2115 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Chris Craik678625242014-02-28 12:26:34 -08002116 texture->setFilter(getFilter(paint), true);
Romain Guya92bb4d2012-10-16 11:08:44 -07002117
2118 int alpha;
2119 SkXfermode::Mode mode;
2120 getAlphaAndMode(paint, &alpha, &mode);
2121
Romain Guyff316ec2013-02-13 18:39:43 -08002122 float a = alpha / 255.0f;
2123
Romain Guya92bb4d2012-10-16 11:08:44 -07002124 if (hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002125 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guyb18d2d02011-02-10 15:52:54 -08002126 }
Romain Guyb18d2d02011-02-10 15:52:54 -08002127
Romain Guyff316ec2013-02-13 18:39:43 -08002128 setupDraw();
2129 setupDrawWithTextureAndColor();
2130 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002131 setupDrawColorFilter(getColorFilter(paint));
2132 setupDrawBlending(paint, true);
Romain Guyff316ec2013-02-13 18:39:43 -08002133 setupDrawProgram();
2134 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08002135 setupDrawModelView(kModelViewMode_TranslateAndScale, false, 0.0f, 0.0f, 1.0f, 1.0f);
Romain Guyff316ec2013-02-13 18:39:43 -08002136 setupDrawTexture(texture->id);
2137 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002138 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3380cfd2013-08-15 16:57:57 -07002139 setupDrawMesh(&mesh[0].x, &mesh[0].u, &mesh[0].r);
Romain Guyff316ec2013-02-13 18:39:43 -08002140
2141 glDrawArrays(GL_TRIANGLES, 0, count);
2142
Romain Guyff316ec2013-02-13 18:39:43 -08002143 int slot = mCaches.currentProgram->getAttrib("colors");
2144 if (slot >= 0) {
2145 glDisableVertexAttribArray(slot);
2146 }
2147
2148 if (cleanupColors) delete[] colors;
Chet Haase48659092012-05-31 15:21:51 -07002149
2150 return DrawGlInfo::kStatusDrew;
Romain Guy5a7b4662011-01-20 19:09:30 -08002151}
2152
Chris Craikd218a922014-01-02 17:13:34 -08002153status_t OpenGLRenderer::drawBitmap(const SkBitmap* bitmap,
Romain Guy8ba548f2010-06-30 19:21:21 -07002154 float srcLeft, float srcTop, float srcRight, float srcBottom,
2155 float dstLeft, float dstTop, float dstRight, float dstBottom,
Chris Craikd218a922014-01-02 17:13:34 -08002156 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002157 if (quickRejectSetupScissor(dstLeft, dstTop, dstRight, dstBottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002158 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002159 }
2160
Romain Guya1d3c912011-12-13 14:55:06 -08002161 mCaches.activeTexture(0);
Romain Guy3b748a42013-04-17 18:54:38 -07002162 Texture* texture = getTexture(bitmap);
Chet Haase48659092012-05-31 15:21:51 -07002163 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002164 const AutoTexture autoCleanup(texture);
Romain Guy8ba548f2010-06-30 19:21:21 -07002165
Romain Guy8ba548f2010-06-30 19:21:21 -07002166 const float width = texture->width;
2167 const float height = texture->height;
2168
Romain Guy3b748a42013-04-17 18:54:38 -07002169 float u1 = fmax(0.0f, srcLeft / width);
2170 float v1 = fmax(0.0f, srcTop / height);
2171 float u2 = fmin(1.0f, srcRight / width);
2172 float v2 = fmin(1.0f, srcBottom / height);
2173
2174 getMapper(texture).map(u1, v1, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07002175
Romain Guy03750a02010-10-18 14:06:08 -07002176 mCaches.unbindMeshBuffer();
Romain Guy8ba548f2010-06-30 19:21:21 -07002177 resetDrawTextureTexCoords(u1, v1, u2, v2);
2178
Romain Guyd21b6e12011-11-30 20:21:23 -08002179 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2180
Romain Guy886b2752013-01-04 12:26:18 -08002181 float scaleX = (dstRight - dstLeft) / (srcRight - srcLeft);
2182 float scaleY = (dstBottom - dstTop) / (srcBottom - srcTop);
Romain Guy6620c6d2010-12-06 18:07:02 -08002183
Romain Guy886b2752013-01-04 12:26:18 -08002184 bool scaled = scaleX != 1.0f || scaleY != 1.0f;
2185 // Apply a scale transform on the canvas only when a shader is in use
2186 // Skia handles the ratio between the dst and src rects as a scale factor
2187 // when a shader is set
Chris Craikc3566d02013-02-04 16:16:33 -08002188 bool useScaleTransform = mDrawModifiers.mShader && scaled;
Romain Guy886b2752013-01-04 12:26:18 -08002189 bool ignoreTransform = false;
Romain Guyb5014982011-07-28 15:39:12 -07002190
Chris Craikd6b65f62014-01-01 14:45:21 -08002191 if (CC_LIKELY(currentTransform()->isPureTranslate() && !useScaleTransform)) {
2192 float x = (int) floorf(dstLeft + currentTransform()->getTranslateX() + 0.5f);
2193 float y = (int) floorf(dstTop + currentTransform()->getTranslateY() + 0.5f);
Romain Guy886b2752013-01-04 12:26:18 -08002194
2195 dstRight = x + (dstRight - dstLeft);
2196 dstBottom = y + (dstBottom - dstTop);
2197
2198 dstLeft = x;
2199 dstTop = y;
2200
Chris Craik678625242014-02-28 12:26:34 -08002201 texture->setFilter(scaled ? getFilter(paint) : GL_NEAREST, true);
Romain Guy886b2752013-01-04 12:26:18 -08002202 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002203 } else {
Chris Craik678625242014-02-28 12:26:34 -08002204 texture->setFilter(getFilter(paint), true);
Romain Guy886b2752013-01-04 12:26:18 -08002205 }
2206
2207 if (CC_UNLIKELY(useScaleTransform)) {
2208 save(SkCanvas::kMatrix_SaveFlag);
2209 translate(dstLeft, dstTop);
2210 scale(scaleX, scaleY);
2211
2212 dstLeft = 0.0f;
2213 dstTop = 0.0f;
2214
2215 dstRight = srcRight - srcLeft;
2216 dstBottom = srcBottom - srcTop;
2217 }
2218
Leon Scroggins III4b9a19a2013-12-03 15:06:30 -05002219 if (CC_UNLIKELY(bitmap->config() == SkBitmap::kA8_Config)) {
Romain Guy886b2752013-01-04 12:26:18 -08002220 drawAlpha8TextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002221 texture->id, paint,
Romain Guy3380cfd2013-08-15 16:57:57 -07002222 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002223 GL_TRIANGLE_STRIP, gMeshCount, ignoreTransform);
2224 } else {
2225 drawTextureMesh(dstLeft, dstTop, dstRight, dstBottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002226 texture->id, paint, texture->blend,
Romain Guy3380cfd2013-08-15 16:57:57 -07002227 &mMeshVertices[0].x, &mMeshVertices[0].u,
Romain Guy886b2752013-01-04 12:26:18 -08002228 GL_TRIANGLE_STRIP, gMeshCount, false, ignoreTransform);
2229 }
2230
2231 if (CC_UNLIKELY(useScaleTransform)) {
2232 restore();
Romain Guy6620c6d2010-12-06 18:07:02 -08002233 }
Romain Guy8ba548f2010-06-30 19:21:21 -07002234
2235 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Chet Haase48659092012-05-31 15:21:51 -07002236
2237 return DrawGlInfo::kStatusDrew;
Romain Guy8ba548f2010-06-30 19:21:21 -07002238}
2239
Chris Craikd218a922014-01-02 17:13:34 -08002240status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Res_png_9patch* patch,
2241 float left, float top, float right, float bottom, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002242 if (quickRejectSetupScissor(left, top, right, bottom)) {
Chet Haase48659092012-05-31 15:21:51 -07002243 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002244 }
2245
Romain Guy4c2547f2013-06-11 16:19:24 -07002246 AssetAtlas::Entry* entry = mCaches.assetAtlas.getEntry(bitmap);
Romain Guy3b748a42013-04-17 18:54:38 -07002247 const Patch* mesh = mCaches.patchCache.get(entry, bitmap->width(), bitmap->height(),
2248 right - left, bottom - top, patch);
Romain Guyf7f93552010-07-08 19:17:03 -07002249
Romain Guy03c00b52013-06-20 18:30:28 -07002250 return drawPatch(bitmap, mesh, entry, left, top, right, bottom, paint);
Romain Guy4c2547f2013-06-11 16:19:24 -07002251}
2252
Chris Craikd218a922014-01-02 17:13:34 -08002253status_t OpenGLRenderer::drawPatch(const SkBitmap* bitmap, const Patch* mesh,
2254 AssetAtlas::Entry* entry, float left, float top, float right, float bottom,
2255 const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08002256 if (quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guy4c2547f2013-06-11 16:19:24 -07002257 return DrawGlInfo::kStatusDone;
2258 }
2259
Romain Guy211370f2012-02-01 16:10:55 -08002260 if (CC_LIKELY(mesh && mesh->verticesCount > 0)) {
Romain Guya4adcf02013-02-28 12:15:35 -08002261 mCaches.activeTexture(0);
Romain Guya404e162013-05-24 16:19:19 -07002262 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
Romain Guya4adcf02013-02-28 12:15:35 -08002263 if (!texture) return DrawGlInfo::kStatusDone;
2264 const AutoTexture autoCleanup(texture);
Romain Guy3b748a42013-04-17 18:54:38 -07002265
Romain Guya4adcf02013-02-28 12:15:35 -08002266 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2267 texture->setFilter(GL_LINEAR, true);
2268
Chris Craikd6b65f62014-01-01 14:45:21 -08002269 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy5b3b3522010-10-27 18:57:51 -07002270 // Mark the current layer dirty where we are going to draw the patch
Romain Guy81683962011-01-24 20:40:18 -08002271 if (hasLayer() && mesh->hasEmptyQuads) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002272 const float offsetX = left + currentTransform()->getTranslateX();
2273 const float offsetY = top + currentTransform()->getTranslateY();
Romain Guy5b3b3522010-10-27 18:57:51 -07002274 const size_t count = mesh->quads.size();
2275 for (size_t i = 0; i < count; i++) {
Romain Guy8ab40792010-12-07 13:30:10 -08002276 const Rect& bounds = mesh->quads.itemAt(i);
Romain Guy211370f2012-02-01 16:10:55 -08002277 if (CC_LIKELY(pureTranslate)) {
Romain Guyc78b5d52011-02-04 14:00:42 -08002278 const float x = (int) floorf(bounds.left + offsetX + 0.5f);
2279 const float y = (int) floorf(bounds.top + offsetY + 0.5f);
2280 dirtyLayer(x, y, x + bounds.getWidth(), y + bounds.getHeight());
Romain Guy6620c6d2010-12-06 18:07:02 -08002281 } else {
Romain Guyc78b5d52011-02-04 14:00:42 -08002282 dirtyLayer(left + bounds.left, top + bounds.top,
Chris Craikd6b65f62014-01-01 14:45:21 -08002283 left + bounds.right, top + bounds.bottom, *currentTransform());
Romain Guy6620c6d2010-12-06 18:07:02 -08002284 }
Romain Guy5b3b3522010-10-27 18:57:51 -07002285 }
2286 }
2287
Chris Craik4063a0e2013-11-15 16:06:56 -08002288 bool ignoreTransform = false;
Romain Guy211370f2012-02-01 16:10:55 -08002289 if (CC_LIKELY(pureTranslate)) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002290 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
2291 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002292
Romain Guy3b748a42013-04-17 18:54:38 -07002293 right = x + right - left;
2294 bottom = y + bottom - top;
Chris Craik4063a0e2013-11-15 16:06:56 -08002295 left = x;
2296 top = y;
2297 ignoreTransform = true;
Romain Guy6620c6d2010-12-06 18:07:02 -08002298 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002299 drawIndexedTextureMesh(left, top, right, bottom, texture->id, paint,
2300 texture->blend, (GLvoid*) mesh->offset, (GLvoid*) mesh->textureOffset,
Chris Craik4063a0e2013-11-15 16:06:56 -08002301 GL_TRIANGLES, mesh->indexCount, false, ignoreTransform,
2302 mCaches.patchCache.getMeshBuffer(), kModelViewMode_Translate, !mesh->hasEmptyQuads);
Romain Guy054dc182010-10-15 17:55:25 -07002303 }
Chet Haase48659092012-05-31 15:21:51 -07002304
2305 return DrawGlInfo::kStatusDrew;
Romain Guyf7f93552010-07-08 19:17:03 -07002306}
2307
Romain Guy03c00b52013-06-20 18:30:28 -07002308/**
2309 * Important note: this method is intended to draw batches of 9-patch objects and
2310 * will not set the scissor enable or dirty the current layer, if any.
2311 * The caller is responsible for properly dirtying the current layer.
2312 */
Chris Craikd218a922014-01-02 17:13:34 -08002313status_t OpenGLRenderer::drawPatches(const SkBitmap* bitmap, AssetAtlas::Entry* entry,
2314 TextureVertex* vertices, uint32_t indexCount, const SkPaint* paint) {
Romain Guy03c00b52013-06-20 18:30:28 -07002315 mCaches.activeTexture(0);
2316 Texture* texture = entry ? entry->texture : mCaches.textureCache.get(bitmap);
2317 if (!texture) return DrawGlInfo::kStatusDone;
2318 const AutoTexture autoCleanup(texture);
2319
2320 texture->setWrap(GL_CLAMP_TO_EDGE, true);
2321 texture->setFilter(GL_LINEAR, true);
2322
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002323 drawIndexedTextureMesh(0.0f, 0.0f, 1.0f, 1.0f, texture->id, paint,
2324 texture->blend, &vertices[0].x, &vertices[0].u,
Chris Craik4063a0e2013-11-15 16:06:56 -08002325 GL_TRIANGLES, indexCount, false, true, 0, kModelViewMode_Translate, false);
Romain Guy03c00b52013-06-20 18:30:28 -07002326
2327 return DrawGlInfo::kStatusDrew;
2328}
2329
ztenghui63d41ab2014-02-14 13:13:41 -08002330status_t OpenGLRenderer::drawVertexBuffer(VertexBufferMode mode,
2331 const VertexBuffer& vertexBuffer, const SkPaint* paint, bool useOffset) {
Chris Craik4063a0e2013-11-15 16:06:56 -08002332 // not missing call to quickReject/dirtyLayer, always done at a higher level
Chris Craik6d29c8d2013-05-08 18:35:44 -07002333 if (!vertexBuffer.getVertexCount()) {
Chris Craik65cd6122012-12-10 17:56:27 -08002334 // no vertices to draw
2335 return DrawGlInfo::kStatusDone;
2336 }
2337
Chris Craikcb4d6002012-09-25 12:00:29 -07002338 int color = paint->getColor();
Chris Craikcb4d6002012-09-25 12:00:29 -07002339 bool isAA = paint->isAntiAlias();
2340
Chris Craik710f46d2012-09-17 17:25:49 -07002341 setupDraw();
2342 setupDrawNoTexture();
2343 if (isAA) setupDrawAA();
Chris Craik710f46d2012-09-17 17:25:49 -07002344 setupDrawColor(color, ((color >> 24) & 0xFF) * mSnapshot->alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002345 setupDrawColorFilter(getColorFilter(paint));
Chris Craik710f46d2012-09-17 17:25:49 -07002346 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002347 setupDrawBlending(paint, isAA);
Chris Craik710f46d2012-09-17 17:25:49 -07002348 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002349 setupDrawModelView(kModelViewMode_Translate, useOffset, 0, 0, 0, 0);
Chris Craik710f46d2012-09-17 17:25:49 -07002350 setupDrawColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002351 setupDrawColorFilterUniforms(getColorFilter(paint));
Chris Craik4063a0e2013-11-15 16:06:56 -08002352 setupDrawShaderUniforms();
Chet Haase858aa932011-05-12 09:06:00 -07002353
ztenghui55bfb4e2013-12-03 10:38:55 -08002354 const void* vertices = vertexBuffer.getBuffer();
Chris Craik710f46d2012-09-17 17:25:49 -07002355 bool force = mCaches.unbindMeshBuffer();
Chris Craikcb4d6002012-09-25 12:00:29 -07002356 mCaches.bindPositionVertexPointer(true, vertices, isAA ? gAlphaVertexStride : gVertexStride);
Chris Craik710f46d2012-09-17 17:25:49 -07002357 mCaches.resetTexCoordsVertexPointer();
ztenghui63d41ab2014-02-14 13:13:41 -08002358
Chet Haase858aa932011-05-12 09:06:00 -07002359
Chris Craik710f46d2012-09-17 17:25:49 -07002360 int alphaSlot = -1;
2361 if (isAA) {
2362 void* alphaCoords = ((GLbyte*) vertices) + gVertexAlphaOffset;
2363 alphaSlot = mCaches.currentProgram->getAttrib("vtxAlpha");
Chris Craik710f46d2012-09-17 17:25:49 -07002364 // TODO: avoid enable/disable in back to back uses of the alpha attribute
Chris Craik6ebdc112012-08-31 18:24:33 -07002365 glEnableVertexAttribArray(alphaSlot);
2366 glVertexAttribPointer(alphaSlot, 1, GL_FLOAT, GL_FALSE, gAlphaVertexStride, alphaCoords);
Chris Craik710f46d2012-09-17 17:25:49 -07002367 }
Romain Guy04299382012-07-18 17:15:41 -07002368
ztenghui63d41ab2014-02-14 13:13:41 -08002369 if (mode == kVertexBufferMode_Standard) {
2370 mCaches.unbindIndicesBuffer();
2371 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexBuffer.getVertexCount());
ztenghui50ecf842014-03-11 16:52:30 -07002372 } else if (mode == kVertexBufferMode_OnePolyRingShadow) {
ztenghui63d41ab2014-02-14 13:13:41 -08002373 mCaches.bindShadowIndicesBuffer();
ztenghui50ecf842014-03-11 16:52:30 -07002374 glDrawElements(GL_TRIANGLE_STRIP, ONE_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
2375 } else if (mode == kVertexBufferMode_TwoPolyRingShadow) {
2376 mCaches.bindShadowIndicesBuffer();
2377 glDrawElements(GL_TRIANGLE_STRIP, TWO_POLY_RING_SHADOW_INDEX_COUNT, GL_UNSIGNED_SHORT, 0);
ztenghui63d41ab2014-02-14 13:13:41 -08002378 }
Romain Guy04299382012-07-18 17:15:41 -07002379
Chris Craik710f46d2012-09-17 17:25:49 -07002380 if (isAA) {
Chris Craik6ebdc112012-08-31 18:24:33 -07002381 glDisableVertexAttribArray(alphaSlot);
Romain Guy04299382012-07-18 17:15:41 -07002382 }
Chris Craik65cd6122012-12-10 17:56:27 -08002383
2384 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002385}
2386
2387/**
Chris Craik65cd6122012-12-10 17:56:27 -08002388 * Renders a convex path via tessellation. For AA paths, this function uses a similar approach to
2389 * that of AA lines in the drawLines() function. We expand the convex path by a half pixel in
2390 * screen space in all directions. However, instead of using a fragment shader to compute the
2391 * translucency of the color from its position, we simply use a varying parameter to define how far
2392 * a given pixel is from the edge. For non-AA paths, the expansion and alpha varying are not used.
2393 *
2394 * Doesn't yet support joins, caps, or path effects.
2395 */
Chris Craikd218a922014-01-02 17:13:34 -08002396status_t OpenGLRenderer::drawConvexPath(const SkPath& path, const SkPaint* paint) {
Chris Craik65cd6122012-12-10 17:56:27 -08002397 VertexBuffer vertexBuffer;
2398 // TODO: try clipping large paths to viewport
Chris Craikd6b65f62014-01-01 14:45:21 -08002399 PathTessellator::tessellatePath(path, paint, *currentTransform(), vertexBuffer);
Chris Craik65cd6122012-12-10 17:56:27 -08002400
Romain Guyc46d07a2013-03-15 19:06:39 -07002401 if (hasLayer()) {
2402 SkRect bounds = path.getBounds();
Chris Craikf0a59072013-11-19 18:00:46 -08002403 PathTessellator::expandBoundsForStroke(bounds, paint);
Chris Craikd6b65f62014-01-01 14:45:21 -08002404 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Romain Guyc46d07a2013-03-15 19:06:39 -07002405 }
Chris Craik65cd6122012-12-10 17:56:27 -08002406
ztenghui63d41ab2014-02-14 13:13:41 -08002407 return drawVertexBuffer(kVertexBufferMode_Standard, vertexBuffer, paint);
Chris Craik65cd6122012-12-10 17:56:27 -08002408}
2409
2410/**
2411 * We create tristrips for the lines much like shape stroke tessellation, using a per-vertex alpha
2412 * and additional geometry for defining an alpha slope perimeter.
2413 *
2414 * Using GL_LINES can be difficult because the rasterization rules for those lines produces some
2415 * unexpected results, and may vary between hardware devices. Previously we used a varying-base
2416 * in-shader alpha region, but found it to be taxing on some GPUs.
2417 *
2418 * TODO: try using a fixed input buffer for non-capped lines as in text rendering. this may reduce
2419 * memory transfer by removing need for degenerate vertices.
Chet Haase99ecdc42011-05-06 12:06:34 -07002420 */
Chris Craikd218a922014-01-02 17:13:34 -08002421status_t OpenGLRenderer::drawLines(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002422 if (currentSnapshot()->isIgnored() || count < 4) return DrawGlInfo::kStatusDone;
Chet Haase8a5cc922011-04-26 07:28:09 -07002423
Chris Craik65cd6122012-12-10 17:56:27 -08002424 count &= ~0x3; // round down to nearest four
Romain Guy7b631422012-04-04 11:38:54 -07002425
Chris Craik65cd6122012-12-10 17:56:27 -08002426 VertexBuffer buffer;
2427 SkRect bounds;
Chris Craikd6b65f62014-01-01 14:45:21 -08002428 PathTessellator::tessellateLines(points, count, paint, *currentTransform(), bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002429
Chris Craikf0a59072013-11-19 18:00:46 -08002430 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2431 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Romain Guyd71ff91d2013-02-08 13:46:40 -08002432 return DrawGlInfo::kStatusDone;
2433 }
2434
Chris Craikd6b65f62014-01-01 14:45:21 -08002435 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Romain Guy7b631422012-04-04 11:38:54 -07002436
Chris Craik65cd6122012-12-10 17:56:27 -08002437 bool useOffset = !paint->isAntiAlias();
ztenghui63d41ab2014-02-14 13:13:41 -08002438 return drawVertexBuffer(kVertexBufferMode_Standard, buffer, paint, useOffset);
Chet Haase5b0200b2011-04-13 17:58:08 -07002439}
2440
Chris Craikd218a922014-01-02 17:13:34 -08002441status_t OpenGLRenderer::drawPoints(const float* points, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002442 if (currentSnapshot()->isIgnored() || count < 2) return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002443
Chris Craik6d29c8d2013-05-08 18:35:44 -07002444 count &= ~0x1; // round down to nearest two
Romain Guyed6fcb02011-03-21 13:11:28 -07002445
Chris Craik6d29c8d2013-05-08 18:35:44 -07002446 VertexBuffer buffer;
2447 SkRect bounds;
Chris Craikd6b65f62014-01-01 14:45:21 -08002448 PathTessellator::tessellatePoints(points, count, paint, *currentTransform(), bounds, buffer);
Romain Guyd71ff91d2013-02-08 13:46:40 -08002449
Chris Craikf0a59072013-11-19 18:00:46 -08002450 // can't pass paint, since style would be checked for outset. outset done by tessellation.
2451 if (quickRejectSetupScissor(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom)) {
Chris Craik6d29c8d2013-05-08 18:35:44 -07002452 return DrawGlInfo::kStatusDone;
Romain Guyed6fcb02011-03-21 13:11:28 -07002453 }
2454
Chris Craikd6b65f62014-01-01 14:45:21 -08002455 dirtyLayer(bounds.fLeft, bounds.fTop, bounds.fRight, bounds.fBottom, *currentTransform());
Chet Haase48659092012-05-31 15:21:51 -07002456
Chris Craik6d29c8d2013-05-08 18:35:44 -07002457 bool useOffset = !paint->isAntiAlias();
ztenghui63d41ab2014-02-14 13:13:41 -08002458 return drawVertexBuffer(kVertexBufferMode_Standard, buffer, paint, useOffset);
Romain Guyed6fcb02011-03-21 13:11:28 -07002459}
2460
Chet Haase48659092012-05-31 15:21:51 -07002461status_t OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
Romain Guye45362c2010-11-03 19:58:32 -07002462 // No need to check against the clip, we fill the clip region
Chris Craikd6b65f62014-01-01 14:45:21 -08002463 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guye45362c2010-11-03 19:58:32 -07002464
Chris Craikd6b65f62014-01-01 14:45:21 -08002465 Rect clip(*currentClipRect());
Romain Guyae88e5e2010-10-22 17:49:18 -07002466 clip.snapToPixelBoundaries();
Romain Guy70ca14e2010-12-13 18:24:33 -08002467
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002468 SkPaint paint;
2469 paint.setColor(color);
2470 paint.setXfermodeMode(mode);
2471
2472 drawColorRect(clip.left, clip.top, clip.right, clip.bottom, &paint, true);
Chet Haase48659092012-05-31 15:21:51 -07002473
2474 return DrawGlInfo::kStatusDrew;
Romain Guyc7d53492010-06-25 13:41:57 -07002475}
Romain Guy9d5316e2010-06-24 19:30:36 -07002476
Chet Haase48659092012-05-31 15:21:51 -07002477status_t OpenGLRenderer::drawShape(float left, float top, const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08002478 const SkPaint* paint) {
Chet Haase48659092012-05-31 15:21:51 -07002479 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy01d58e42011-01-19 21:54:02 -08002480 const AutoTexture autoCleanup(texture);
2481
2482 const float x = left + texture->left - texture->offset;
2483 const float y = top + texture->top - texture->offset;
2484
2485 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002486
2487 return DrawGlInfo::kStatusDrew;
Romain Guy01d58e42011-01-19 21:54:02 -08002488}
2489
Chet Haase48659092012-05-31 15:21:51 -07002490status_t OpenGLRenderer::drawRoundRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002491 float rx, float ry, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002492 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002493 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002494 return DrawGlInfo::kStatusDone;
2495 }
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002496
Chris Craikcb4d6002012-09-25 12:00:29 -07002497 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002498 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002499 const PathTexture* texture = mCaches.pathCache.getRoundRect(
Chris Craik710f46d2012-09-17 17:25:49 -07002500 right - left, bottom - top, rx, ry, p);
2501 return drawShape(left, top, texture, p);
2502 }
2503
2504 SkPath path;
2505 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002506 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2507 float outset = p->getStrokeWidth() / 2;
2508 rect.outset(outset, outset);
2509 rx += outset;
2510 ry += outset;
2511 }
Chris Craik710f46d2012-09-17 17:25:49 -07002512 path.addRoundRect(rect, rx, ry);
Chris Craik65cd6122012-12-10 17:56:27 -08002513 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002514}
2515
Chris Craikd218a922014-01-02 17:13:34 -08002516status_t OpenGLRenderer::drawCircle(float x, float y, float radius, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002517 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(x - radius, y - radius,
Romain Guy257ae352013-03-20 16:31:12 -07002518 x + radius, y + radius, p) ||
2519 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002520 return DrawGlInfo::kStatusDone;
2521 }
Chris Craikcb4d6002012-09-25 12:00:29 -07002522 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002523 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002524 const PathTexture* texture = mCaches.pathCache.getCircle(radius, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002525 return drawShape(x - radius, y - radius, texture, p);
2526 }
2527
2528 SkPath path;
Chris Craikcb4d6002012-09-25 12:00:29 -07002529 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2530 path.addCircle(x, y, radius + p->getStrokeWidth() / 2);
2531 } else {
2532 path.addCircle(x, y, radius);
2533 }
Chris Craik65cd6122012-12-10 17:56:27 -08002534 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002535}
Romain Guy01d58e42011-01-19 21:54:02 -08002536
Chet Haase48659092012-05-31 15:21:51 -07002537status_t OpenGLRenderer::drawOval(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002538 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002539 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002540 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik710f46d2012-09-17 17:25:49 -07002541 return DrawGlInfo::kStatusDone;
2542 }
Romain Guy01d58e42011-01-19 21:54:02 -08002543
Chris Craikcb4d6002012-09-25 12:00:29 -07002544 if (p->getPathEffect() != 0) {
Chris Craik710f46d2012-09-17 17:25:49 -07002545 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002546 const PathTexture* texture = mCaches.pathCache.getOval(right - left, bottom - top, p);
Chris Craik710f46d2012-09-17 17:25:49 -07002547 return drawShape(left, top, texture, p);
2548 }
2549
2550 SkPath path;
2551 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Chris Craikcb4d6002012-09-25 12:00:29 -07002552 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2553 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2554 }
Chris Craik710f46d2012-09-17 17:25:49 -07002555 path.addOval(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002556 return drawConvexPath(path, p);
Romain Guyc1cd9ba32011-01-23 14:18:41 -08002557}
2558
Chet Haase48659092012-05-31 15:21:51 -07002559status_t OpenGLRenderer::drawArc(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08002560 float startAngle, float sweepAngle, bool useCenter, const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002561 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002562 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chris Craik780c1282012-10-04 14:10:49 -07002563 return DrawGlInfo::kStatusDone;
Romain Guy8b2f5262011-01-23 16:15:02 -08002564 }
2565
Chris Craik780c1282012-10-04 14:10:49 -07002566 if (fabs(sweepAngle) >= 360.0f) {
2567 return drawOval(left, top, right, bottom, p);
2568 }
2569
2570 // TODO: support fills (accounting for concavity if useCenter && sweepAngle > 180)
Chris Craik65cd6122012-12-10 17:56:27 -08002571 if (p->getStyle() != SkPaint::kStroke_Style || p->getPathEffect() != 0 || useCenter) {
Chris Craik780c1282012-10-04 14:10:49 -07002572 mCaches.activeTexture(0);
Romain Guyc46d07a2013-03-15 19:06:39 -07002573 const PathTexture* texture = mCaches.pathCache.getArc(right - left, bottom - top,
Chris Craik780c1282012-10-04 14:10:49 -07002574 startAngle, sweepAngle, useCenter, p);
2575 return drawShape(left, top, texture, p);
2576 }
2577
2578 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2579 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2580 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2581 }
2582
2583 SkPath path;
2584 if (useCenter) {
2585 path.moveTo(rect.centerX(), rect.centerY());
2586 }
2587 path.arcTo(rect, startAngle, sweepAngle, !useCenter);
2588 if (useCenter) {
2589 path.close();
2590 }
Chris Craik65cd6122012-12-10 17:56:27 -08002591 return drawConvexPath(path, p);
Romain Guy8b2f5262011-01-23 16:15:02 -08002592}
2593
Romain Guycf8675e2012-10-02 12:32:25 -07002594// See SkPaintDefaults.h
2595#define SkPaintDefaults_MiterLimit SkIntToScalar(4)
2596
Chris Craikd218a922014-01-02 17:13:34 -08002597status_t OpenGLRenderer::drawRect(float left, float top, float right, float bottom,
2598 const SkPaint* p) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002599 if (currentSnapshot()->isIgnored() || quickRejectSetupScissor(left, top, right, bottom, p) ||
Romain Guy257ae352013-03-20 16:31:12 -07002600 (p->getAlpha() == 0 && getXfermode(p->getXfermode()) != SkXfermode::kClear_Mode)) {
Chet Haase48659092012-05-31 15:21:51 -07002601 return DrawGlInfo::kStatusDone;
Romain Guy6926c722010-07-12 20:20:03 -07002602 }
2603
Chris Craik710f46d2012-09-17 17:25:49 -07002604 if (p->getStyle() != SkPaint::kFill_Style) {
Romain Guycf8675e2012-10-02 12:32:25 -07002605 // only fill style is supported by drawConvexPath, since others have to handle joins
2606 if (p->getPathEffect() != 0 || p->getStrokeJoin() != SkPaint::kMiter_Join ||
2607 p->getStrokeMiter() != SkPaintDefaults_MiterLimit) {
2608 mCaches.activeTexture(0);
2609 const PathTexture* texture =
Romain Guyc46d07a2013-03-15 19:06:39 -07002610 mCaches.pathCache.getRect(right - left, bottom - top, p);
Romain Guycf8675e2012-10-02 12:32:25 -07002611 return drawShape(left, top, texture, p);
2612 }
2613
2614 SkPath path;
2615 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
2616 if (p->getStyle() == SkPaint::kStrokeAndFill_Style) {
2617 rect.outset(p->getStrokeWidth() / 2, p->getStrokeWidth() / 2);
2618 }
2619 path.addRect(rect);
Chris Craik65cd6122012-12-10 17:56:27 -08002620 return drawConvexPath(path, p);
Romain Guy026c5e162010-06-28 17:12:22 -07002621 }
2622
Chris Craikd6b65f62014-01-01 14:45:21 -08002623 if (p->isAntiAlias() && !currentTransform()->isSimple()) {
Chris Craik710f46d2012-09-17 17:25:49 -07002624 SkPath path;
2625 path.addRect(left, top, right, bottom);
Chris Craik65cd6122012-12-10 17:56:27 -08002626 return drawConvexPath(path, p);
Chet Haase858aa932011-05-12 09:06:00 -07002627 } else {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002628 drawColorRect(left, top, right, bottom, p);
Chris Craik65cd6122012-12-10 17:56:27 -08002629 return DrawGlInfo::kStatusDrew;
Chet Haase858aa932011-05-12 09:06:00 -07002630 }
Romain Guyc7d53492010-06-25 13:41:57 -07002631}
Romain Guy9d5316e2010-06-24 19:30:36 -07002632
Chris Craikd218a922014-01-02 17:13:34 -08002633void OpenGLRenderer::drawTextShadow(const SkPaint* paint, const char* text,
2634 int bytesCount, int count, const float* positions,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002635 FontRenderer& fontRenderer, int alpha, float x, float y) {
Raph Levien416a8472012-07-19 22:48:17 -07002636 mCaches.activeTexture(0);
2637
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002638 TextShadow textShadow;
2639 if (!getTextShadow(paint, &textShadow)) {
2640 LOG_ALWAYS_FATAL("failed to query shadow attributes");
2641 }
2642
Raph Levien416a8472012-07-19 22:48:17 -07002643 // NOTE: The drop shadow will not perform gamma correction
2644 // if shader-based correction is enabled
2645 mCaches.dropShadowCache.setFontRenderer(fontRenderer);
2646 const ShadowTexture* shadow = mCaches.dropShadowCache.get(
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002647 paint, text, bytesCount, count, textShadow.radius, positions);
Romain Guycf51a412013-04-08 19:40:31 -07002648 // If the drop shadow exceeds the max texture size or couldn't be
2649 // allocated, skip drawing
2650 if (!shadow) return;
Raph Levien416a8472012-07-19 22:48:17 -07002651 const AutoTexture autoCleanup(shadow);
2652
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002653 const float sx = x - shadow->left + textShadow.dx;
2654 const float sy = y - shadow->top + textShadow.dy;
Raph Levien416a8472012-07-19 22:48:17 -07002655
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002656 const int shadowAlpha = ((textShadow.color >> 24) & 0xFF) * mSnapshot->alpha;
Chris Craikc3566d02013-02-04 16:16:33 -08002657 if (mDrawModifiers.mShader) {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002658 textShadow.color = SK_ColorWHITE;
Raph Levien416a8472012-07-19 22:48:17 -07002659 }
2660
2661 setupDraw();
2662 setupDrawWithTexture(true);
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002663 setupDrawAlpha8Color(textShadow.color, shadowAlpha < 255 ? shadowAlpha : alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002664 setupDrawColorFilter(getColorFilter(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002665 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002666 setupDrawBlending(paint, true);
Raph Levien416a8472012-07-19 22:48:17 -07002667 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08002668 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
2669 sx, sy, sx + shadow->width, sy + shadow->height);
Raph Levien416a8472012-07-19 22:48:17 -07002670 setupDrawTexture(shadow->id);
2671 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002672 setupDrawColorFilterUniforms(getColorFilter(paint));
Raph Levien416a8472012-07-19 22:48:17 -07002673 setupDrawShaderUniforms();
2674 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
2675
2676 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
2677}
2678
Romain Guy768bffc2013-02-27 13:50:45 -08002679bool OpenGLRenderer::canSkipText(const SkPaint* paint) const {
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002680 float alpha = (hasTextShadow(paint) ? 1.0f : paint->getAlpha()) * mSnapshot->alpha;
Romain Guy768bffc2013-02-27 13:50:45 -08002681 return alpha == 0.0f && getXfermode(paint->getXfermode()) == SkXfermode::kSrcOver_Mode;
2682}
2683
Chet Haase48659092012-05-31 15:21:51 -07002684status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count,
Chris Craikd218a922014-01-02 17:13:34 -08002685 const float* positions, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002686 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002687 return DrawGlInfo::kStatusDone;
Romain Guyeb9a5362012-01-17 17:39:26 -08002688 }
Romain Guyeb9a5362012-01-17 17:39:26 -08002689
Romain Guy671d6cf2012-01-18 12:39:17 -08002690 // NOTE: Skia does not support perspective transform on drawPosText yet
Chris Craikd6b65f62014-01-01 14:45:21 -08002691 if (!currentTransform()->isSimple()) {
Chet Haase48659092012-05-31 15:21:51 -07002692 return DrawGlInfo::kStatusDone;
Romain Guy671d6cf2012-01-18 12:39:17 -08002693 }
2694
Chris Craik39a908c2013-06-13 14:39:01 -07002695 mCaches.enableScissor();
2696
Romain Guy671d6cf2012-01-18 12:39:17 -08002697 float x = 0.0f;
2698 float y = 0.0f;
Chris Craikd6b65f62014-01-01 14:45:21 -08002699 const bool pureTranslate = currentTransform()->isPureTranslate();
Romain Guy671d6cf2012-01-18 12:39:17 -08002700 if (pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002701 x = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2702 y = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy671d6cf2012-01-18 12:39:17 -08002703 }
2704
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002705 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002706 fontRenderer.setFont(paint, mat4::identity());
Romain Guy671d6cf2012-01-18 12:39:17 -08002707
2708 int alpha;
2709 SkXfermode::Mode mode;
2710 getAlphaAndMode(paint, &alpha, &mode);
2711
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002712 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guye3a9b242013-01-08 11:15:30 -08002713 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002714 alpha, 0.0f, 0.0f);
Raph Levien416a8472012-07-19 22:48:17 -07002715 }
2716
Romain Guy671d6cf2012-01-18 12:39:17 -08002717 // Pick the appropriate texture filtering
Chris Craikd6b65f62014-01-01 14:45:21 -08002718 bool linearFilter = currentTransform()->changesBounds();
Romain Guy671d6cf2012-01-18 12:39:17 -08002719 if (pureTranslate && !linearFilter) {
2720 linearFilter = fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
2721 }
Romain Guy257ae352013-03-20 16:31:12 -07002722 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy671d6cf2012-01-18 12:39:17 -08002723
2724 const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
2725 Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
2726
Romain Guy211370f2012-02-01 16:10:55 -08002727 const bool hasActiveLayer = hasLayer();
Romain Guy671d6cf2012-01-18 12:39:17 -08002728
Victoria Lease1e546812013-06-25 14:25:17 -07002729 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Romain Guy671d6cf2012-01-18 12:39:17 -08002730 if (fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Romain Guy257ae352013-03-20 16:31:12 -07002731 positions, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy671d6cf2012-01-18 12:39:17 -08002732 if (hasActiveLayer) {
2733 if (!pureTranslate) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002734 currentTransform()->mapRect(bounds);
Romain Guy671d6cf2012-01-18 12:39:17 -08002735 }
2736 dirtyLayerUnchecked(bounds, getRegion());
2737 }
Romain Guy671d6cf2012-01-18 12:39:17 -08002738 }
Chet Haase48659092012-05-31 15:21:51 -07002739
2740 return DrawGlInfo::kStatusDrew;
Romain Guyeb9a5362012-01-17 17:39:26 -08002741}
2742
Romain Guy624234f2013-03-05 16:43:31 -08002743mat4 OpenGLRenderer::findBestFontTransform(const mat4& transform) const {
2744 mat4 fontTransform;
2745 if (CC_LIKELY(transform.isPureTranslate())) {
2746 fontTransform = mat4::identity();
2747 } else {
2748 if (CC_UNLIKELY(transform.isPerspective())) {
Romain Guyb09f1472013-03-07 17:01:05 -08002749 fontTransform = mat4::identity();
Romain Guy624234f2013-03-05 16:43:31 -08002750 } else {
2751 float sx, sy;
Chris Craikd6b65f62014-01-01 14:45:21 -08002752 currentTransform()->decomposeScale(sx, sy);
Romain Guy624234f2013-03-05 16:43:31 -08002753 fontTransform.loadScale(sx, sy, 1.0f);
2754 }
2755 }
2756 return fontTransform;
2757}
2758
Chris Craik41541822013-05-03 16:35:54 -07002759status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count, float x, float y,
Chris Craikd218a922014-01-02 17:13:34 -08002760 const float* positions, const SkPaint* paint, float totalAdvance, const Rect& bounds,
Chris Craik527a3aa2013-03-04 10:19:31 -08002761 DrawOpMode drawOpMode) {
2762
Chris Craik527a3aa2013-03-04 10:19:31 -08002763 if (drawOpMode == kDrawOpMode_Immediate) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002764 // The checks for corner-case ignorable text and quick rejection is only done for immediate
2765 // drawing as ops from DeferredDisplayList are already filtered for these
Chris Craikd6b65f62014-01-01 14:45:21 -08002766 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint) ||
Chris Craikf0a59072013-11-19 18:00:46 -08002767 quickRejectSetupScissor(bounds)) {
Chris Craik28ce94a2013-05-31 11:38:03 -07002768 return DrawGlInfo::kStatusDone;
2769 }
Romain Guycac5fd32011-12-01 20:08:50 -08002770 }
2771
Romain Guy3a3fa1b2010-12-06 18:47:50 -08002772 const float oldX = x;
2773 const float oldY = y;
Romain Guy624234f2013-03-05 16:43:31 -08002774
Chris Craikd6b65f62014-01-01 14:45:21 -08002775 const mat4& transform = *currentTransform();
Romain Guy624234f2013-03-05 16:43:31 -08002776 const bool pureTranslate = transform.isPureTranslate();
Romain Guyc74f45a2013-02-26 19:10:14 -08002777
Romain Guy211370f2012-02-01 16:10:55 -08002778 if (CC_LIKELY(pureTranslate)) {
Romain Guy624234f2013-03-05 16:43:31 -08002779 x = (int) floorf(x + transform.getTranslateX() + 0.5f);
2780 y = (int) floorf(y + transform.getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08002781 }
2782
Romain Guy86568192010-12-14 15:55:39 -08002783 int alpha;
2784 SkXfermode::Mode mode;
2785 getAlphaAndMode(paint, &alpha, &mode);
Romain Guy9d13fe252010-10-15 16:06:03 -07002786
Romain Guyc74f45a2013-02-26 19:10:14 -08002787 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
2788
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -04002789 if (CC_UNLIKELY(hasTextShadow(paint))) {
Romain Guyc74f45a2013-02-26 19:10:14 -08002790 fontRenderer.setFont(paint, mat4::identity());
2791 drawTextShadow(paint, text, bytesCount, count, positions, fontRenderer,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002792 alpha, oldX, oldY);
Romain Guy1e45aae2010-08-13 19:39:53 -07002793 }
2794
Romain Guy19d4dd82013-03-04 11:14:26 -08002795 const bool hasActiveLayer = hasLayer();
2796
Romain Guy624234f2013-03-05 16:43:31 -08002797 // We only pass a partial transform to the font renderer. That partial
2798 // matrix defines how glyphs are rasterized. Typically we want glyphs
2799 // to be rasterized at their final size on screen, which means the partial
2800 // matrix needs to take the scale factor into account.
2801 // When a partial matrix is used to transform glyphs during rasterization,
2802 // the mesh is generated with the inverse transform (in the case of scale,
2803 // the mesh is generated at 1.0 / scale for instance.) This allows us to
2804 // apply the full transform matrix at draw time in the vertex shader.
2805 // Applying the full matrix in the shader is the easiest way to handle
2806 // rotation and perspective and allows us to always generated quads in the
2807 // font renderer which greatly simplifies the code, clipping in particular.
2808 mat4 fontTransform = findBestFontTransform(transform);
Romain Guy3b753822013-03-05 10:27:35 -08002809 fontRenderer.setFont(paint, fontTransform);
Romain Guyc74f45a2013-02-26 19:10:14 -08002810
Romain Guy6620c6d2010-12-06 18:07:02 -08002811 // Pick the appropriate texture filtering
Romain Guya4adcf02013-02-28 12:15:35 -08002812 bool linearFilter = !pureTranslate || fabs(y - (int) y) > 0.0f || fabs(x - (int) x) > 0.0f;
Romain Guy257ae352013-03-20 16:31:12 -07002813 fontRenderer.setTextureFiltering(linearFilter);
Romain Guy06f96e22010-07-30 19:18:16 -07002814
Romain Guy624234f2013-03-05 16:43:31 -08002815 // TODO: Implement better clipping for scaled/rotated text
Chris Craikd6b65f62014-01-01 14:45:21 -08002816 const Rect* clip = !pureTranslate ? NULL : currentClipRect();
Chris Craik41541822013-05-03 16:35:54 -07002817 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 -07002818
Raph Levien996e57c2012-07-23 15:22:52 -07002819 bool status;
Victoria Lease1e546812013-06-25 14:25:17 -07002820 TextSetupFunctor functor(this, x, y, pureTranslate, alpha, mode, paint);
Chris Craik527a3aa2013-03-04 10:19:31 -08002821
2822 // don't call issuedrawcommand, do it at end of batch
2823 bool forceFinish = (drawOpMode != kDrawOpMode_Defer);
Romain Guya3dc55f2012-09-28 13:55:44 -07002824 if (CC_UNLIKELY(paint->getTextAlign() != SkPaint::kLeft_Align)) {
Raph Levien8b4072d2012-07-30 15:50:00 -07002825 SkPaint paintCopy(*paint);
2826 paintCopy.setTextAlign(SkPaint::kLeft_Align);
2827 status = fontRenderer.renderPosText(&paintCopy, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002828 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002829 } else {
Raph Levien8b4072d2012-07-30 15:50:00 -07002830 status = fontRenderer.renderPosText(paint, clip, text, 0, bytesCount, count, x, y,
Chris Craik41541822013-05-03 16:35:54 -07002831 positions, hasActiveLayer ? &layerBounds : NULL, &functor, forceFinish);
Raph Levien996e57c2012-07-23 15:22:52 -07002832 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002833
Chris Craik527a3aa2013-03-04 10:19:31 -08002834 if ((status || drawOpMode != kDrawOpMode_Immediate) && hasActiveLayer) {
Romain Guy624234f2013-03-05 16:43:31 -08002835 if (!pureTranslate) {
Chris Craik41541822013-05-03 16:35:54 -07002836 transform.mapRect(layerBounds);
Romain Guya4adcf02013-02-28 12:15:35 -08002837 }
Chris Craik41541822013-05-03 16:35:54 -07002838 dirtyLayerUnchecked(layerBounds, getRegion());
Romain Guy5b3b3522010-10-27 18:57:51 -07002839 }
Romain Guy694b5192010-07-21 21:33:20 -07002840
Chris Craike63f7c622013-10-17 10:30:55 -07002841 drawTextDecorations(totalAdvance, oldX, oldY, paint);
Chet Haase48659092012-05-31 15:21:51 -07002842
2843 return DrawGlInfo::kStatusDrew;
Romain Guy694b5192010-07-21 21:33:20 -07002844}
2845
Chris Craikd218a922014-01-02 17:13:34 -08002846status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int count,
2847 const SkPath* path, float hOffset, float vOffset, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002848 if (text == NULL || count == 0 || currentSnapshot()->isIgnored() || canSkipText(paint)) {
Chet Haase48659092012-05-31 15:21:51 -07002849 return DrawGlInfo::kStatusDone;
Romain Guy03d58522012-02-24 17:54:07 -08002850 }
2851
Chris Craik39a908c2013-06-13 14:39:01 -07002852 // TODO: avoid scissor by calculating maximum bounds using path bounds + font metrics
2853 mCaches.enableScissor();
2854
Romain Guyb1d0a4e2012-07-13 18:25:35 -07002855 FontRenderer& fontRenderer = mCaches.fontRenderer->getFontRenderer(paint);
Romain Guyc74f45a2013-02-26 19:10:14 -08002856 fontRenderer.setFont(paint, mat4::identity());
Romain Guy257ae352013-03-20 16:31:12 -07002857 fontRenderer.setTextureFiltering(true);
Romain Guy03d58522012-02-24 17:54:07 -08002858
2859 int alpha;
2860 SkXfermode::Mode mode;
2861 getAlphaAndMode(paint, &alpha, &mode);
Victoria Lease1e546812013-06-25 14:25:17 -07002862 TextSetupFunctor functor(this, 0.0f, 0.0f, false, alpha, mode, paint);
Romain Guy03d58522012-02-24 17:54:07 -08002863
Romain Guy97771732012-02-28 18:17:02 -08002864 const Rect* clip = &mSnapshot->getLocalClip();
2865 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 -08002866
Romain Guy97771732012-02-28 18:17:02 -08002867 const bool hasActiveLayer = hasLayer();
Romain Guy97771732012-02-28 18:17:02 -08002868
2869 if (fontRenderer.renderTextOnPath(paint, clip, text, 0, bytesCount, count, path,
Victoria Lease1e546812013-06-25 14:25:17 -07002870 hOffset, vOffset, hasActiveLayer ? &bounds : NULL, &functor)) {
Romain Guy97771732012-02-28 18:17:02 -08002871 if (hasActiveLayer) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002872 currentTransform()->mapRect(bounds);
Romain Guy97771732012-02-28 18:17:02 -08002873 dirtyLayerUnchecked(bounds, getRegion());
2874 }
Romain Guy97771732012-02-28 18:17:02 -08002875 }
Chet Haase48659092012-05-31 15:21:51 -07002876
2877 return DrawGlInfo::kStatusDrew;
Romain Guy325740f2012-02-24 16:48:34 -08002878}
2879
Chris Craikd218a922014-01-02 17:13:34 -08002880status_t OpenGLRenderer::drawPath(const SkPath* path, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08002881 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Romain Guydbc26d22010-10-11 17:58:29 -07002882
Romain Guya1d3c912011-12-13 14:55:06 -08002883 mCaches.activeTexture(0);
Romain Guy7fbcc042010-08-04 15:40:07 -07002884
Romain Guyfb8b7632010-08-23 21:05:08 -07002885 const PathTexture* texture = mCaches.pathCache.get(path, paint);
Chet Haase48659092012-05-31 15:21:51 -07002886 if (!texture) return DrawGlInfo::kStatusDone;
Romain Guy22158e12010-08-06 11:18:34 -07002887 const AutoTexture autoCleanup(texture);
Romain Guy7fbcc042010-08-04 15:40:07 -07002888
Romain Guy8b55f372010-08-18 17:10:07 -07002889 const float x = texture->left - texture->offset;
2890 const float y = texture->top - texture->offset;
2891
Romain Guy01d58e42011-01-19 21:54:02 -08002892 drawPathTexture(texture, x, y, paint);
Chet Haase48659092012-05-31 15:21:51 -07002893
2894 return DrawGlInfo::kStatusDrew;
Romain Guy7fbcc042010-08-04 15:40:07 -07002895}
2896
Chris Craika08f95c2013-03-15 17:24:33 -07002897status_t OpenGLRenderer::drawLayer(Layer* layer, float x, float y) {
Romain Guy35643dd2012-09-18 15:40:58 -07002898 if (!layer) {
2899 return DrawGlInfo::kStatusDone;
2900 }
2901
Romain Guyb2e2f242012-10-17 18:18:35 -07002902 mat4* transform = NULL;
2903 if (layer->isTextureLayer()) {
2904 transform = &layer->getTransform();
2905 if (!transform->isIdentity()) {
Chris Craik3f0854292014-04-15 16:18:08 -07002906 save(SkCanvas::kMatrix_SaveFlag);
Chris Craik14e51302013-12-30 15:32:54 -08002907 concatMatrix(*transform);
Romain Guyb2e2f242012-10-17 18:18:35 -07002908 }
2909 }
2910
Chris Craik39a908c2013-06-13 14:39:01 -07002911 bool clipRequired = false;
Chris Craikf0a59072013-11-19 18:00:46 -08002912 const bool rejected = calculateQuickRejectForScissor(x, y,
2913 x + layer->layer.getWidth(), y + layer->layer.getHeight(), &clipRequired, false);
Romain Guy35643dd2012-09-18 15:40:58 -07002914
2915 if (rejected) {
Romain Guyb2e2f242012-10-17 18:18:35 -07002916 if (transform && !transform->isIdentity()) {
2917 restore();
2918 }
Chet Haase48659092012-05-31 15:21:51 -07002919 return DrawGlInfo::kStatusDone;
Romain Guy6c319ca2011-01-11 14:29:25 -08002920 }
2921
Romain Guy5bb3c732012-11-29 17:52:58 -08002922 updateLayer(layer, true);
Romain Guy2bf68f02012-03-02 13:37:47 -08002923
Chris Craik39a908c2013-06-13 14:39:01 -07002924 mCaches.setScissorEnabled(mScissorOptimizationDisabled || clipRequired);
Romain Guya1d3c912011-12-13 14:55:06 -08002925 mCaches.activeTexture(0);
Romain Guy6c319ca2011-01-11 14:29:25 -08002926
Romain Guy211370f2012-02-01 16:10:55 -08002927 if (CC_LIKELY(!layer->region.isEmpty())) {
Romain Guyc88e3572011-01-22 00:32:12 -08002928 if (layer->region.isRect()) {
Chris Craik34416ea2013-04-15 16:08:28 -07002929 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2930 composeLayerRect(layer, layer->regionRect));
Romain Guyc88e3572011-01-22 00:32:12 -08002931 } else if (layer->mesh) {
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002932
Chris Craik16ecda52013-03-29 10:59:59 -07002933 const float a = getLayerAlpha(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002934 setupDraw();
2935 setupDrawWithTexture();
Romain Guy81683962011-01-24 20:40:18 -08002936 setupDrawColor(a, a, a, a);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002937 setupDrawColorFilter(layer->getColorFilter());
2938 setupDrawBlending(layer);
Romain Guyc88e3572011-01-22 00:32:12 -08002939 setupDrawProgram();
Romain Guyc88e3572011-01-22 00:32:12 -08002940 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002941 setupDrawColorFilterUniforms(layer->getColorFilter());
Romain Guy9ace8f52011-07-07 20:50:11 -07002942 setupDrawTexture(layer->getTexture());
Chris Craikd6b65f62014-01-01 14:45:21 -08002943 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
2944 int tx = (int) floorf(x + currentTransform()->getTranslateX() + 0.5f);
2945 int ty = (int) floorf(y + currentTransform()->getTranslateY() + 0.5f);
Romain Guy9ace8f52011-07-07 20:50:11 -07002946
Romain Guyd21b6e12011-11-30 20:21:23 -08002947 layer->setFilter(GL_NEAREST);
Chris Craik4063a0e2013-11-15 16:06:56 -08002948 setupDrawModelView(kModelViewMode_Translate, false, tx, ty,
Romain Guy4ff0cf42012-08-06 14:51:10 -07002949 tx + layer->layer.getWidth(), ty + layer->layer.getHeight(), true);
Romain Guy9ace8f52011-07-07 20:50:11 -07002950 } else {
Romain Guyd21b6e12011-11-30 20:21:23 -08002951 layer->setFilter(GL_LINEAR);
Chris Craik4063a0e2013-11-15 16:06:56 -08002952 setupDrawModelView(kModelViewMode_Translate, false, x, y,
Romain Guy9ace8f52011-07-07 20:50:11 -07002953 x + layer->layer.getWidth(), y + layer->layer.getHeight());
2954 }
Romain Guyf219da52011-01-16 12:54:25 -08002955
Romain Guy448455f2013-07-22 13:57:50 -07002956 TextureVertex* mesh = &layer->mesh[0];
2957 GLsizei elementsCount = layer->meshElementCount;
2958
2959 while (elementsCount > 0) {
2960 GLsizei drawCount = min(elementsCount, (GLsizei) gMaxNumberOfQuads * 6);
2961
Romain Guy3380cfd2013-08-15 16:57:57 -07002962 setupDrawMeshIndices(&mesh[0].x, &mesh[0].u);
Romain Guy448455f2013-07-22 13:57:50 -07002963 DRAW_DOUBLE_STENCIL_IF(!layer->hasDrawnSinceUpdate,
2964 glDrawElements(GL_TRIANGLES, drawCount, GL_UNSIGNED_SHORT, NULL));
2965
2966 elementsCount -= drawCount;
2967 // Though there are 4 vertices in a quad, we use 6 indices per
2968 // quad to draw with GL_TRIANGLES
2969 mesh += (drawCount / 6) * 4;
2970 }
Romain Guyf219da52011-01-16 12:54:25 -08002971
Romain Guy3a3133d2011-02-01 22:59:58 -08002972#if DEBUG_LAYERS_AS_REGIONS
Chris Craike63f7c622013-10-17 10:30:55 -07002973 drawRegionRectsDebug(layer->region);
Romain Guy3a3133d2011-02-01 22:59:58 -08002974#endif
Romain Guyc88e3572011-01-22 00:32:12 -08002975 }
Romain Guy4ff0cf42012-08-06 14:51:10 -07002976
Romain Guy5bb3c732012-11-29 17:52:58 -08002977 if (layer->debugDrawUpdate) {
2978 layer->debugDrawUpdate = false;
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05002979
2980 SkPaint paint;
2981 paint.setColor(0x7f00ff00);
2982 drawColorRect(x, y, x + layer->layer.getWidth(), y + layer->layer.getHeight(), &paint);
Romain Guy4ff0cf42012-08-06 14:51:10 -07002983 }
Romain Guyf219da52011-01-16 12:54:25 -08002984 }
Chris Craik34416ea2013-04-15 16:08:28 -07002985 layer->hasDrawnSinceUpdate = true;
Chet Haase48659092012-05-31 15:21:51 -07002986
Romain Guyb2e2f242012-10-17 18:18:35 -07002987 if (transform && !transform->isIdentity()) {
2988 restore();
2989 }
2990
Chet Haase48659092012-05-31 15:21:51 -07002991 return DrawGlInfo::kStatusDrew;
Romain Guy6c319ca2011-01-11 14:29:25 -08002992}
2993
Romain Guy6926c722010-07-12 20:20:03 -07002994///////////////////////////////////////////////////////////////////////////////
Romain Guyd27977d2010-07-14 19:18:51 -07002995// Shaders
2996///////////////////////////////////////////////////////////////////////////////
2997
2998void OpenGLRenderer::resetShader() {
Chris Craikc3566d02013-02-04 16:16:33 -08002999 mDrawModifiers.mShader = NULL;
Romain Guyd27977d2010-07-14 19:18:51 -07003000}
3001
Romain Guy06f96e22010-07-30 19:18:16 -07003002void OpenGLRenderer::setupShader(SkiaShader* shader) {
Chris Craikc3566d02013-02-04 16:16:33 -08003003 mDrawModifiers.mShader = shader;
3004 if (mDrawModifiers.mShader) {
Romain Guy8aa195d2013-06-04 18:00:09 -07003005 mDrawModifiers.mShader->setCaches(mCaches);
Romain Guy06f96e22010-07-30 19:18:16 -07003006 }
Romain Guy7fac2e12010-07-16 17:10:13 -07003007}
3008
Romain Guyd27977d2010-07-14 19:18:51 -07003009///////////////////////////////////////////////////////////////////////////////
Romain Guy5ff9df62012-01-23 17:09:05 -08003010// Draw filters
3011///////////////////////////////////////////////////////////////////////////////
3012
3013void OpenGLRenderer::resetPaintFilter() {
Chris Craik527a3aa2013-03-04 10:19:31 -08003014 // when clearing the PaintFilter, the masks should also be cleared for simple DrawModifier
3015 // comparison, see MergingDrawBatch::canMergeWith
Chris Craikc3566d02013-02-04 16:16:33 -08003016 mDrawModifiers.mHasDrawFilter = false;
Chris Craik527a3aa2013-03-04 10:19:31 -08003017 mDrawModifiers.mPaintFilterClearBits = 0;
3018 mDrawModifiers.mPaintFilterSetBits = 0;
Romain Guy5ff9df62012-01-23 17:09:05 -08003019}
3020
3021void OpenGLRenderer::setupPaintFilter(int clearBits, int setBits) {
Chris Craikc3566d02013-02-04 16:16:33 -08003022 mDrawModifiers.mHasDrawFilter = true;
3023 mDrawModifiers.mPaintFilterClearBits = clearBits & SkPaint::kAllFlags;
3024 mDrawModifiers.mPaintFilterSetBits = setBits & SkPaint::kAllFlags;
Romain Guy5ff9df62012-01-23 17:09:05 -08003025}
3026
Chris Craikd218a922014-01-02 17:13:34 -08003027const SkPaint* OpenGLRenderer::filterPaint(const SkPaint* paint) {
Romain Guy758724f2013-02-27 11:53:12 -08003028 if (CC_LIKELY(!mDrawModifiers.mHasDrawFilter || !paint)) {
Romain Guy758724f2013-02-27 11:53:12 -08003029 return paint;
3030 }
Romain Guy5ff9df62012-01-23 17:09:05 -08003031
3032 uint32_t flags = paint->getFlags();
3033
3034 mFilteredPaint = *paint;
Chris Craikc3566d02013-02-04 16:16:33 -08003035 mFilteredPaint.setFlags((flags & ~mDrawModifiers.mPaintFilterClearBits) |
3036 mDrawModifiers.mPaintFilterSetBits);
Romain Guy5ff9df62012-01-23 17:09:05 -08003037
3038 return &mFilteredPaint;
3039}
3040
3041///////////////////////////////////////////////////////////////////////////////
Romain Guy6926c722010-07-12 20:20:03 -07003042// Drawing implementation
3043///////////////////////////////////////////////////////////////////////////////
3044
Chris Craikd218a922014-01-02 17:13:34 -08003045Texture* OpenGLRenderer::getTexture(const SkBitmap* bitmap) {
Romain Guy3b748a42013-04-17 18:54:38 -07003046 Texture* texture = mCaches.assetAtlas.getEntryTexture(bitmap);
3047 if (!texture) {
3048 return mCaches.textureCache.get(bitmap);
3049 }
3050 return texture;
3051}
3052
Romain Guy01d58e42011-01-19 21:54:02 -08003053void OpenGLRenderer::drawPathTexture(const PathTexture* texture,
Chris Craikd218a922014-01-02 17:13:34 -08003054 float x, float y, const SkPaint* paint) {
Chris Craikf0a59072013-11-19 18:00:46 -08003055 if (quickRejectSetupScissor(x, y, x + texture->width, y + texture->height)) {
Romain Guy01d58e42011-01-19 21:54:02 -08003056 return;
3057 }
3058
3059 int alpha;
3060 SkXfermode::Mode mode;
3061 getAlphaAndMode(paint, &alpha, &mode);
3062
3063 setupDraw();
3064 setupDrawWithTexture(true);
3065 setupDrawAlpha8Color(paint->getColor(), alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003066 setupDrawColorFilter(getColorFilter(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003067 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003068 setupDrawBlending(paint, true);
Romain Guy01d58e42011-01-19 21:54:02 -08003069 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003070 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3071 x, y, x + texture->width, y + texture->height);
Romain Guy01d58e42011-01-19 21:54:02 -08003072 setupDrawTexture(texture->id);
3073 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003074 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy01d58e42011-01-19 21:54:02 -08003075 setupDrawShaderUniforms();
3076 setupDrawMesh(NULL, (GLvoid*) gMeshTextureOffset);
3077
3078 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
Romain Guy01d58e42011-01-19 21:54:02 -08003079}
3080
Romain Guyf607bdc2010-09-10 19:20:06 -07003081// Same values used by Skia
Romain Guy0a417492010-08-16 20:26:20 -07003082#define kStdStrikeThru_Offset (-6.0f / 21.0f)
3083#define kStdUnderline_Offset (1.0f / 9.0f)
3084#define kStdUnderline_Thickness (1.0f / 18.0f)
3085
Chris Craikd218a922014-01-02 17:13:34 -08003086void OpenGLRenderer::drawTextDecorations(float underlineWidth, float x, float y,
3087 const SkPaint* paint) {
Romain Guy0a417492010-08-16 20:26:20 -07003088 // Handle underline and strike-through
3089 uint32_t flags = paint->getFlags();
3090 if (flags & (SkPaint::kUnderlineText_Flag | SkPaint::kStrikeThruText_Flag)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003091 SkPaint paintCopy(*paint);
Romain Guy0a417492010-08-16 20:26:20 -07003092
Romain Guy211370f2012-02-01 16:10:55 -08003093 if (CC_LIKELY(underlineWidth > 0.0f)) {
Romain Guy726aeba2011-06-01 14:52:00 -07003094 const float textSize = paintCopy.getTextSize();
Romain Guyf6834472011-01-23 13:32:12 -08003095 const float strokeWidth = fmax(textSize * kStdUnderline_Thickness, 1.0f);
Romain Guy0a417492010-08-16 20:26:20 -07003096
Raph Levien8b4072d2012-07-30 15:50:00 -07003097 const float left = x;
Romain Guy0a417492010-08-16 20:26:20 -07003098 float top = 0.0f;
Romain Guye20ecbd2010-09-22 19:49:04 -07003099
Romain Guyf6834472011-01-23 13:32:12 -08003100 int linesCount = 0;
3101 if (flags & SkPaint::kUnderlineText_Flag) linesCount++;
3102 if (flags & SkPaint::kStrikeThruText_Flag) linesCount++;
3103
3104 const int pointsCount = 4 * linesCount;
Romain Guye20ecbd2010-09-22 19:49:04 -07003105 float points[pointsCount];
3106 int currentPoint = 0;
Romain Guy0a417492010-08-16 20:26:20 -07003107
3108 if (flags & SkPaint::kUnderlineText_Flag) {
3109 top = y + textSize * kStdUnderline_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003110 points[currentPoint++] = left;
3111 points[currentPoint++] = top;
3112 points[currentPoint++] = left + underlineWidth;
3113 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003114 }
3115
3116 if (flags & SkPaint::kStrikeThruText_Flag) {
3117 top = y + textSize * kStdStrikeThru_Offset;
Romain Guye20ecbd2010-09-22 19:49:04 -07003118 points[currentPoint++] = left;
3119 points[currentPoint++] = top;
3120 points[currentPoint++] = left + underlineWidth;
3121 points[currentPoint++] = top;
Romain Guy0a417492010-08-16 20:26:20 -07003122 }
Romain Guye20ecbd2010-09-22 19:49:04 -07003123
Romain Guy726aeba2011-06-01 14:52:00 -07003124 paintCopy.setStrokeWidth(strokeWidth);
Romain Guye20ecbd2010-09-22 19:49:04 -07003125
Romain Guy726aeba2011-06-01 14:52:00 -07003126 drawLines(&points[0], pointsCount, &paintCopy);
Romain Guy0a417492010-08-16 20:26:20 -07003127 }
3128 }
3129}
3130
Chris Craikd218a922014-01-02 17:13:34 -08003131status_t OpenGLRenderer::drawRects(const float* rects, int count, const SkPaint* paint) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003132 if (currentSnapshot()->isIgnored()) {
Romain Guy672433d2013-01-04 19:05:13 -08003133 return DrawGlInfo::kStatusDone;
3134 }
3135
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003136 return drawColorRects(rects, count, paint, false, true, true);
Romain Guy735738c2012-12-03 12:34:51 -08003137}
3138
Chris Craikb79a3e32014-03-11 12:20:17 -07003139static void mapPointFakeZ(Vector3& point, const mat4& transformXY, const mat4& transformZ) {
3140 // map z coordinate with true 3d matrix
3141 point.z = transformZ.mapZ(point);
3142
3143 // map x,y coordinates with draw/Skia matrix
3144 transformXY.mapPoint(point.x, point.y);
3145}
3146
3147status_t OpenGLRenderer::drawShadow(const mat4& casterTransformXY, const mat4& casterTransformZ,
Chris Craik024433f2014-03-26 13:19:14 -07003148 float casterAlpha, bool casterUnclipped, const SkPath* casterPerimeter) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003149 if (currentSnapshot()->isIgnored()) return DrawGlInfo::kStatusDone;
Chris Craikf57776b2013-10-25 18:30:17 -07003150
Chris Craik15a07a22014-01-26 13:43:53 -08003151 // TODO: use quickRejectWithScissor. For now, always force enable scissor.
Chris Craikf57776b2013-10-25 18:30:17 -07003152 mCaches.enableScissor();
3153
3154 SkPaint paint;
Chris Craikba9b6132013-12-15 17:10:19 -08003155 paint.setAntiAlias(true); // want to use AlphaVertex
Chris Craikf57776b2013-10-25 18:30:17 -07003156
Chris Craik15a07a22014-01-26 13:43:53 -08003157 // tessellate caster outline into a 2d polygon
3158 Vector<Vertex> casterVertices2d;
3159 const float casterRefinementThresholdSquared = 20.0f; // TODO: experiment with this value
Chris Craik8c271ca2014-03-25 10:33:01 -07003160 PathTessellator::approximatePathOutlineVertices(*casterPerimeter,
Chris Craik15a07a22014-01-26 13:43:53 -08003161 casterRefinementThresholdSquared, casterVertices2d);
ztenghui2e023f32014-04-28 16:43:13 -07003162 if (!ShadowTessellator::isClockwisePath(*casterPerimeter)) {
3163 ShadowTessellator::reverseVertexArray(casterVertices2d.editArray(),
3164 casterVertices2d.size());
3165 }
Chris Craik15a07a22014-01-26 13:43:53 -08003166
Chris Craika2fe7af2014-01-28 17:25:06 -08003167 if (casterVertices2d.size() == 0) {
3168 // empty caster polygon computed from path
3169 return DrawGlInfo::kStatusDone;
3170 }
3171
Chris Craik15a07a22014-01-26 13:43:53 -08003172 // map 2d caster poly into 3d
3173 const int casterVertexCount = casterVertices2d.size();
3174 Vector3 casterPolygon[casterVertexCount];
Chris Craikb98f2112014-03-11 17:42:29 -07003175 float minZ = FLT_MAX;
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003176 float maxZ = -FLT_MAX;
Chris Craik15a07a22014-01-26 13:43:53 -08003177 for (int i = 0; i < casterVertexCount; i++) {
3178 const Vertex& point2d = casterVertices2d[i];
3179 casterPolygon[i] = Vector3(point2d.x, point2d.y, 0);
Chris Craikb79a3e32014-03-11 12:20:17 -07003180 mapPointFakeZ(casterPolygon[i], casterTransformXY, casterTransformZ);
Chris Craikb98f2112014-03-11 17:42:29 -07003181 minZ = fmin(minZ, casterPolygon[i].z);
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003182 maxZ = fmax(maxZ, casterPolygon[i].z);
Chris Craik15a07a22014-01-26 13:43:53 -08003183 }
3184
ztenghui63d41ab2014-02-14 13:13:41 -08003185 // map the centroid of the caster into 3d
3186 Vector2 centroid = ShadowTessellator::centroid2d(
3187 reinterpret_cast<const Vector2*>(casterVertices2d.array()),
3188 casterVertexCount);
3189 Vector3 centroid3d(centroid.x, centroid.y, 0);
Chris Craikb79a3e32014-03-11 12:20:17 -07003190 mapPointFakeZ(centroid3d, casterTransformXY, casterTransformZ);
ztenghui63d41ab2014-02-14 13:13:41 -08003191
Chris Craikb98f2112014-03-11 17:42:29 -07003192 // if the caster intersects the z=0 plane, lift it in Z so it doesn't
3193 if (minZ < SHADOW_MIN_CASTER_Z) {
3194 float casterLift = SHADOW_MIN_CASTER_Z - minZ;
3195 for (int i = 0; i < casterVertexCount; i++) {
3196 casterPolygon[i].z += casterLift;
3197 }
3198 centroid3d.z += casterLift;
3199 }
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003200
3201 // Check whether we want to draw the shadow at all by checking the caster's
3202 // bounds against clip.
3203 // We only have ortho projection, so we can just ignore the Z in caster for
3204 // simple rejection calculation.
3205 Rect localClip = mSnapshot->getLocalClip();
Chris Craik8c271ca2014-03-25 10:33:01 -07003206 Rect casterBounds(casterPerimeter->getBounds());
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003207 casterTransformXY.mapRect(casterBounds);
3208
Chris Craik024433f2014-03-26 13:19:14 -07003209 bool isCasterOpaque = (casterAlpha == 1.0f) && casterUnclipped;
Chris Craik15a07a22014-01-26 13:43:53 -08003210 // draw caster's shadows
ztenghuief94c6f2014-02-13 17:09:45 -08003211 if (mCaches.propertyAmbientShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003212 paint.setARGB(casterAlpha * mCaches.propertyAmbientShadowStrength, 0, 0, 0);
ztenghuief94c6f2014-02-13 17:09:45 -08003213 VertexBuffer ambientShadowVertexBuffer;
ztenghui50ecf842014-03-11 16:52:30 -07003214 VertexBufferMode vertexBufferMode = ShadowTessellator::tessellateAmbientShadow(
3215 isCasterOpaque, casterPolygon, casterVertexCount, centroid3d,
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003216 casterBounds, localClip, maxZ, ambientShadowVertexBuffer);
ztenghui50ecf842014-03-11 16:52:30 -07003217 drawVertexBuffer(vertexBufferMode, ambientShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003218 }
ztenghui7b4516e2014-01-07 10:42:55 -08003219
ztenghuief94c6f2014-02-13 17:09:45 -08003220 if (mCaches.propertySpotShadowStrength > 0) {
Chris Craik919e95c2014-02-21 17:13:45 -08003221 paint.setARGB(casterAlpha * mCaches.propertySpotShadowStrength, 0, 0, 0);
ztenghuief94c6f2014-02-13 17:09:45 -08003222 VertexBuffer spotShadowVertexBuffer;
ztenghui50ecf842014-03-11 16:52:30 -07003223 VertexBufferMode vertexBufferMode = ShadowTessellator::tessellateSpotShadow(
Chris Craikf5be3ca2014-04-30 18:20:03 -07003224 isCasterOpaque, casterPolygon, casterVertexCount,
ztenghuiaf6f7ed2014-03-18 17:25:49 -07003225 *currentTransform(), getWidth(), getHeight(), casterBounds, localClip,
3226 spotShadowVertexBuffer);
ztenghui50ecf842014-03-11 16:52:30 -07003227 drawVertexBuffer(vertexBufferMode, spotShadowVertexBuffer, &paint);
ztenghuief94c6f2014-02-13 17:09:45 -08003228 }
ztenghui7b4516e2014-01-07 10:42:55 -08003229
3230 return DrawGlInfo::kStatusDrew;
Chris Craikf57776b2013-10-25 18:30:17 -07003231}
3232
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003233status_t OpenGLRenderer::drawColorRects(const float* rects, int count, const SkPaint* paint,
3234 bool ignoreTransform, bool dirty, bool clip) {
Romain Guy3b753822013-03-05 10:27:35 -08003235 if (count == 0) {
3236 return DrawGlInfo::kStatusDone;
3237 }
Romain Guy735738c2012-12-03 12:34:51 -08003238
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003239 int color = paint->getColor();
3240 // If a shader is set, preserve only the alpha
3241 if (mDrawModifiers.mShader) {
3242 color |= 0x00ffffff;
3243 }
3244
Romain Guy672433d2013-01-04 19:05:13 -08003245 float left = FLT_MAX;
3246 float top = FLT_MAX;
3247 float right = FLT_MIN;
3248 float bottom = FLT_MIN;
3249
Romain Guy448455f2013-07-22 13:57:50 -07003250 Vertex mesh[count];
Romain Guy672433d2013-01-04 19:05:13 -08003251 Vertex* vertex = mesh;
3252
Chris Craik2af46352012-11-26 18:30:17 -08003253 for (int index = 0; index < count; index += 4) {
Romain Guy672433d2013-01-04 19:05:13 -08003254 float l = rects[index + 0];
3255 float t = rects[index + 1];
3256 float r = rects[index + 2];
3257 float b = rects[index + 3];
3258
Romain Guy3b753822013-03-05 10:27:35 -08003259 Vertex::set(vertex++, l, t);
3260 Vertex::set(vertex++, r, t);
3261 Vertex::set(vertex++, l, b);
Romain Guy3b753822013-03-05 10:27:35 -08003262 Vertex::set(vertex++, r, b);
Romain Guy672433d2013-01-04 19:05:13 -08003263
Romain Guy3b753822013-03-05 10:27:35 -08003264 left = fminf(left, l);
3265 top = fminf(top, t);
3266 right = fmaxf(right, r);
3267 bottom = fmaxf(bottom, b);
Romain Guy672433d2013-01-04 19:05:13 -08003268 }
3269
Chris Craikf0a59072013-11-19 18:00:46 -08003270 if (clip && quickRejectSetupScissor(left, top, right, bottom)) {
Romain Guya362c692013-02-04 13:50:16 -08003271 return DrawGlInfo::kStatusDone;
3272 }
Romain Guy672433d2013-01-04 19:05:13 -08003273
Romain Guy672433d2013-01-04 19:05:13 -08003274 setupDraw();
3275 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003276 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Romain Guy672433d2013-01-04 19:05:13 -08003277 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003278 setupDrawColorFilter(getColorFilter(paint));
3279 setupDrawBlending(paint);
Romain Guy672433d2013-01-04 19:05:13 -08003280 setupDrawProgram();
3281 setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003282 setupDrawModelView(kModelViewMode_Translate, false,
3283 0.0f, 0.0f, 0.0f, 0.0f, ignoreTransform);
Romain Guy672433d2013-01-04 19:05:13 -08003284 setupDrawColorUniforms();
3285 setupDrawShaderUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003286 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy672433d2013-01-04 19:05:13 -08003287
Romain Guy8ce00302013-01-15 18:51:42 -08003288 if (dirty && hasLayer()) {
Chris Craikd6b65f62014-01-01 14:45:21 -08003289 dirtyLayer(left, top, right, bottom, *currentTransform());
Romain Guy672433d2013-01-04 19:05:13 -08003290 }
3291
Chris Craik4063a0e2013-11-15 16:06:56 -08003292 issueIndexedQuadDraw(&mesh[0], count / 4);
Romain Guy672433d2013-01-04 19:05:13 -08003293
3294 return DrawGlInfo::kStatusDrew;
3295}
3296
Romain Guy026c5e162010-06-28 17:12:22 -07003297void OpenGLRenderer::drawColorRect(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003298 const SkPaint* paint, bool ignoreTransform) {
3299 int color = paint->getColor();
Romain Guyd27977d2010-07-14 19:18:51 -07003300 // If a shader is set, preserve only the alpha
Chris Craikc3566d02013-02-04 16:16:33 -08003301 if (mDrawModifiers.mShader) {
Romain Guyd27977d2010-07-14 19:18:51 -07003302 color |= 0x00ffffff;
3303 }
3304
Romain Guy70ca14e2010-12-13 18:24:33 -08003305 setupDraw();
Romain Guy15bc6432011-12-13 13:11:32 -08003306 setupDrawNoTexture();
Chris Craikd6b65f62014-01-01 14:45:21 -08003307 setupDrawColor(color, ((color >> 24) & 0xFF) * currentSnapshot()->alpha);
Romain Guy70ca14e2010-12-13 18:24:33 -08003308 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003309 setupDrawColorFilter(getColorFilter(paint));
3310 setupDrawBlending(paint);
Romain Guy70ca14e2010-12-13 18:24:33 -08003311 setupDrawProgram();
Chris Craik4063a0e2013-11-15 16:06:56 -08003312 setupDrawModelView(kModelViewMode_TranslateAndScale, false,
3313 left, top, right, bottom, ignoreTransform);
Romain Guy70ca14e2010-12-13 18:24:33 -08003314 setupDrawColorUniforms();
3315 setupDrawShaderUniforms(ignoreTransform);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003316 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003317 setupDrawSimpleMesh();
Romain Guyc0ac1932010-07-19 18:43:02 -07003318
Romain Guyc95c8d62010-09-17 15:31:32 -07003319 glDrawArrays(GL_TRIANGLE_STRIP, 0, gMeshCount);
3320}
3321
Romain Guy82ba8142010-07-09 13:25:56 -07003322void OpenGLRenderer::drawTextureRect(float left, float top, float right, float bottom,
Chris Craikd218a922014-01-02 17:13:34 -08003323 Texture* texture, const SkPaint* paint) {
Romain Guyd21b6e12011-11-30 20:21:23 -08003324 texture->setWrap(GL_CLAMP_TO_EDGE, true);
Romain Guy8164c2d2010-10-25 18:03:28 -07003325
Romain Guy3b748a42013-04-17 18:54:38 -07003326 GLvoid* vertices = (GLvoid*) NULL;
3327 GLvoid* texCoords = (GLvoid*) gMeshTextureOffset;
3328
3329 if (texture->uvMapper) {
Romain Guy3380cfd2013-08-15 16:57:57 -07003330 vertices = &mMeshVertices[0].x;
3331 texCoords = &mMeshVertices[0].u;
Romain Guy3b748a42013-04-17 18:54:38 -07003332
3333 Rect uvs(0.0f, 0.0f, 1.0f, 1.0f);
3334 texture->uvMapper->map(uvs);
3335
3336 resetDrawTextureTexCoords(uvs.left, uvs.top, uvs.right, uvs.bottom);
3337 }
3338
Chris Craikd6b65f62014-01-01 14:45:21 -08003339 if (CC_LIKELY(currentTransform()->isPureTranslate())) {
3340 const float x = (int) floorf(left + currentTransform()->getTranslateX() + 0.5f);
3341 const float y = (int) floorf(top + currentTransform()->getTranslateY() + 0.5f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003342
Romain Guyd21b6e12011-11-30 20:21:23 -08003343 texture->setFilter(GL_NEAREST, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003344 drawTextureMesh(x, y, x + texture->width, y + texture->height, texture->id,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003345 paint, texture->blend, vertices, texCoords,
Romain Guy3b748a42013-04-17 18:54:38 -07003346 GL_TRIANGLE_STRIP, gMeshCount, false, true);
Romain Guy6620c6d2010-12-06 18:07:02 -08003347 } else {
Chris Craik678625242014-02-28 12:26:34 -08003348 texture->setFilter(getFilter(paint), true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003349 drawTextureMesh(left, top, right, bottom, texture->id, paint,
Romain Guy3b748a42013-04-17 18:54:38 -07003350 texture->blend, vertices, texCoords, GL_TRIANGLE_STRIP, gMeshCount);
3351 }
3352
3353 if (texture->uvMapper) {
3354 resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
Romain Guy6620c6d2010-12-06 18:07:02 -08003355 }
Romain Guy85bf02f2010-06-22 13:11:24 -07003356}
3357
Romain Guyf7f93552010-07-08 19:17:03 -07003358void OpenGLRenderer::drawTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003359 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy6820ac82010-09-15 18:11:50 -07003360 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003361 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3362 ModelViewMode modelViewMode, bool dirty) {
Romain Guy70ca14e2010-12-13 18:24:33 -08003363
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003364 int a;
3365 SkXfermode::Mode mode;
3366 getAlphaAndMode(paint, &a, &mode);
3367 const float alpha = a / 255.0f;
3368
Romain Guy746b7402010-10-26 16:27:31 -07003369 setupDraw();
Romain Guy70ca14e2010-12-13 18:24:33 -08003370 setupDrawWithTexture();
3371 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003372 setupDrawColorFilter(getColorFilter(paint));
3373 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy70ca14e2010-12-13 18:24:33 -08003374 setupDrawProgram();
Romain Guy886b2752013-01-04 12:26:18 -08003375 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003376 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003377 setupDrawTexture(texture);
Romain Guy86568192010-12-14 15:55:39 -08003378 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003379 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy70ca14e2010-12-13 18:24:33 -08003380 setupDrawMesh(vertices, texCoords, vbo);
Romain Guydb1938e2010-08-02 18:50:22 -07003381
Romain Guy6820ac82010-09-15 18:11:50 -07003382 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy82ba8142010-07-09 13:25:56 -07003383}
3384
Romain Guy3b748a42013-04-17 18:54:38 -07003385void OpenGLRenderer::drawIndexedTextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003386 GLuint texture, const SkPaint* paint, bool blend,
Romain Guy3b748a42013-04-17 18:54:38 -07003387 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003388 bool swapSrcDst, bool ignoreTransform, GLuint vbo,
3389 ModelViewMode modelViewMode, bool dirty) {
Romain Guy3b748a42013-04-17 18:54:38 -07003390
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003391 int a;
3392 SkXfermode::Mode mode;
3393 getAlphaAndMode(paint, &a, &mode);
3394 const float alpha = a / 255.0f;
3395
Romain Guy3b748a42013-04-17 18:54:38 -07003396 setupDraw();
3397 setupDrawWithTexture();
3398 setupDrawColor(alpha, alpha, alpha, alpha);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003399 setupDrawColorFilter(getColorFilter(paint));
3400 setupDrawBlending(paint, blend, swapSrcDst);
Romain Guy3b748a42013-04-17 18:54:38 -07003401 setupDrawProgram();
3402 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003403 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy3b748a42013-04-17 18:54:38 -07003404 setupDrawTexture(texture);
3405 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003406 setupDrawColorFilterUniforms(getColorFilter(paint));
Romain Guy3b748a42013-04-17 18:54:38 -07003407 setupDrawMeshIndices(vertices, texCoords, vbo);
3408
3409 glDrawElements(drawMode, elementsCount, GL_UNSIGNED_SHORT, NULL);
Romain Guy3b748a42013-04-17 18:54:38 -07003410}
3411
Romain Guy886b2752013-01-04 12:26:18 -08003412void OpenGLRenderer::drawAlpha8TextureMesh(float left, float top, float right, float bottom,
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003413 GLuint texture, const SkPaint* paint,
Romain Guy886b2752013-01-04 12:26:18 -08003414 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount,
Chris Craik4063a0e2013-11-15 16:06:56 -08003415 bool ignoreTransform, ModelViewMode modelViewMode, bool dirty) {
Romain Guy886b2752013-01-04 12:26:18 -08003416
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003417 int color = paint != NULL ? paint->getColor() : 0;
3418 int alpha;
3419 SkXfermode::Mode mode;
3420 getAlphaAndMode(paint, &alpha, &mode);
3421
Romain Guy886b2752013-01-04 12:26:18 -08003422 setupDraw();
3423 setupDrawWithTexture(true);
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003424 if (paint != NULL) {
Romain Guy886b2752013-01-04 12:26:18 -08003425 setupDrawAlpha8Color(color, alpha);
3426 }
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003427 setupDrawColorFilter(getColorFilter(paint));
Romain Guy886b2752013-01-04 12:26:18 -08003428 setupDrawShader();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003429 setupDrawBlending(paint, true);
Romain Guy886b2752013-01-04 12:26:18 -08003430 setupDrawProgram();
3431 if (!dirty) setupDrawDirtyRegionsDisabled();
Chris Craik4063a0e2013-11-15 16:06:56 -08003432 setupDrawModelView(modelViewMode, false, left, top, right, bottom, ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003433 setupDrawTexture(texture);
3434 setupDrawPureColorUniforms();
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003435 setupDrawColorFilterUniforms(getColorFilter(paint));
Chris Craik4063a0e2013-11-15 16:06:56 -08003436 setupDrawShaderUniforms(ignoreTransform);
Romain Guy886b2752013-01-04 12:26:18 -08003437 setupDrawMesh(vertices, texCoords);
3438
3439 glDrawArrays(drawMode, 0, elementsCount);
Romain Guy886b2752013-01-04 12:26:18 -08003440}
3441
Romain Guya5aed0d2010-09-09 14:42:43 -07003442void OpenGLRenderer::chooseBlending(bool blend, SkXfermode::Mode mode,
Romain Guyf607bdc2010-09-10 19:20:06 -07003443 ProgramDescription& description, bool swapSrcDst) {
Romain Guy78dd96d2013-05-03 14:24:16 -07003444 if (mCountOverdraw) {
3445 if (!mCaches.blend) glEnable(GL_BLEND);
3446 if (mCaches.lastSrcMode != GL_ONE || mCaches.lastDstMode != GL_ONE) {
3447 glBlendFunc(GL_ONE, GL_ONE);
3448 }
3449
3450 mCaches.blend = true;
3451 mCaches.lastSrcMode = GL_ONE;
3452 mCaches.lastDstMode = GL_ONE;
3453
3454 return;
3455 }
3456
Romain Guy82ba8142010-07-09 13:25:56 -07003457 blend = blend || mode != SkXfermode::kSrcOver_Mode;
Romain Guyc189ef52012-04-25 20:02:53 -07003458
Romain Guy82ba8142010-07-09 13:25:56 -07003459 if (blend) {
Romain Guy82bc7a72012-01-03 14:13:39 -08003460 // These blend modes are not supported by OpenGL directly and have
3461 // to be implemented using shaders. Since the shader will perform
3462 // the blending, turn blending off here
3463 // If the blend mode cannot be implemented using shaders, fall
3464 // back to the default SrcOver blend mode instead
Romain Guy33fa1f72012-08-07 19:09:57 -07003465 if (CC_UNLIKELY(mode > SkXfermode::kScreen_Mode)) {
Romain Guy3bbacf22013-02-06 16:51:04 -08003466 if (CC_UNLIKELY(mExtensions.hasFramebufferFetch())) {
Romain Guya5aed0d2010-09-09 14:42:43 -07003467 description.framebufferMode = mode;
Romain Guyf607bdc2010-09-10 19:20:06 -07003468 description.swapSrcDst = swapSrcDst;
Romain Guya5aed0d2010-09-09 14:42:43 -07003469
Romain Guy82bc7a72012-01-03 14:13:39 -08003470 if (mCaches.blend) {
3471 glDisable(GL_BLEND);
3472 mCaches.blend = false;
3473 }
3474
3475 return;
3476 } else {
3477 mode = SkXfermode::kSrcOver_Mode;
Romain Guya5aed0d2010-09-09 14:42:43 -07003478 }
Romain Guy82bc7a72012-01-03 14:13:39 -08003479 }
3480
3481 if (!mCaches.blend) {
3482 glEnable(GL_BLEND);
3483 }
3484
3485 GLenum sourceMode = swapSrcDst ? gBlendsSwap[mode].src : gBlends[mode].src;
3486 GLenum destMode = swapSrcDst ? gBlendsSwap[mode].dst : gBlends[mode].dst;
3487
3488 if (sourceMode != mCaches.lastSrcMode || destMode != mCaches.lastDstMode) {
3489 glBlendFunc(sourceMode, destMode);
3490 mCaches.lastSrcMode = sourceMode;
3491 mCaches.lastDstMode = destMode;
Romain Guy82ba8142010-07-09 13:25:56 -07003492 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003493 } else if (mCaches.blend) {
Romain Guy82ba8142010-07-09 13:25:56 -07003494 glDisable(GL_BLEND);
3495 }
Romain Guyfb8b7632010-08-23 21:05:08 -07003496 mCaches.blend = blend;
Romain Guybd6b79b2010-06-26 00:13:53 -07003497}
3498
Romain Guy889f8d12010-07-29 14:37:42 -07003499bool OpenGLRenderer::useProgram(Program* program) {
Romain Guyd27977d2010-07-14 19:18:51 -07003500 if (!program->isInUse()) {
Romain Guyfb8b7632010-08-23 21:05:08 -07003501 if (mCaches.currentProgram != NULL) mCaches.currentProgram->remove();
Romain Guyd27977d2010-07-14 19:18:51 -07003502 program->use();
Romain Guyfb8b7632010-08-23 21:05:08 -07003503 mCaches.currentProgram = program;
Romain Guy6926c722010-07-12 20:20:03 -07003504 return false;
Romain Guy260e1022010-07-12 14:41:06 -07003505 }
Romain Guy6926c722010-07-12 20:20:03 -07003506 return true;
Romain Guy260e1022010-07-12 14:41:06 -07003507}
3508
Romain Guy026c5e162010-06-28 17:12:22 -07003509void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
Romain Guyac670c02010-07-27 17:39:27 -07003510 TextureVertex* v = &mMeshVertices[0];
Romain Guy82ba8142010-07-09 13:25:56 -07003511 TextureVertex::setUV(v++, u1, v1);
3512 TextureVertex::setUV(v++, u2, v1);
3513 TextureVertex::setUV(v++, u1, v2);
3514 TextureVertex::setUV(v++, u2, v2);
Romain Guy8ba548f2010-06-30 19:21:21 -07003515}
3516
Chris Craikd218a922014-01-02 17:13:34 -08003517void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) const {
Romain Guybe6f9dc2012-07-16 12:41:17 -07003518 getAlphaAndModeDirect(paint, alpha, mode);
Chris Craik16ecda52013-03-29 10:59:59 -07003519 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3520 // if drawing a layer, ignore the paint's alpha
Romain Guy87b515c2013-05-03 17:42:27 -07003521 *alpha = mDrawModifiers.mOverrideLayerAlpha * 255;
Chris Craik16ecda52013-03-29 10:59:59 -07003522 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003523 *alpha *= currentSnapshot()->alpha;
Romain Guy026c5e162010-06-28 17:12:22 -07003524}
3525
Derek Sollenberger76d3a1b2013-12-10 12:28:58 -05003526float OpenGLRenderer::getLayerAlpha(const Layer* layer) const {
Chris Craik16ecda52013-03-29 10:59:59 -07003527 float alpha;
3528 if (mDrawModifiers.mOverrideLayerAlpha < 1.0f) {
3529 alpha = mDrawModifiers.mOverrideLayerAlpha;
3530 } else {
3531 alpha = layer->getAlpha() / 255.0f;
3532 }
Chris Craikd6b65f62014-01-01 14:45:21 -08003533 return alpha * currentSnapshot()->alpha;
Chris Craik16ecda52013-03-29 10:59:59 -07003534}
3535
Romain Guy9d5316e2010-06-24 19:30:36 -07003536}; // namespace uirenderer
Romain Guye4d01122010-06-16 18:44:05 -07003537}; // namespace android